Module: Shiftzilla::Helpers

Defined in:
lib/shiftzilla/helpers.rb

Constant Summary collapse

BZ_URL =
'https://bugzilla.redhat.com/show_bug.cgi?id='
DB_FNAME =
'shiftzilla.sqlite'
DEFAULT_DIR =
File.join(ENV['HOME'],'.shiftzilla')
THIS_PATH =
File.symlink?(__FILE__) ? File.readlink(__FILE__) : __FILE__
HAML_TMPL =
File.expand_path(File.join(File.dirname(THIS_PATH), '../../template.haml'))
CFG_TMPL =
File.expand_path(File.join(File.dirname(THIS_PATH), '../../shiftzilla_cfg.yml.tmpl'))
SQL_TMPL =
File.expand_path(File.join(File.dirname(THIS_PATH), '../../shiftzilla.sql.tmpl'))
VENDOR_DIR =
File.expand_path(File.join(File.dirname(THIS_PATH), '../../vendor'))
GRAPH_DIMENSIONS =
'800x400'
GRAPH_THEME =
{
  :colors => [
    '#268bd2', # Blue
    '#cb4b16', # Orange
    '#859900', # Green
    '#2aa198', # Cyan
    '#d33682', # Magenta
    '#6c71c4', # Violet
    '#b58900', # Yellow
    '#dc322f', # Red
  ],
  :marker_color      => '#93a1a1', # Base1
  :font_color        => '#586e75', # Base01
  :background_colors => '#fdf6e3', # Base3
  :background_image  => nil,
}

Instance Method Summary collapse

Instance Method Details

#all_bugs_query(snapshot) ⇒ Object



124
125
126
# File 'lib/shiftzilla/helpers.rb', line 124

def all_bugs_query(snapshot)
  return "SELECT AB.'Bug ID', AB.'Component', AB.'Target Release', AB.'Assignee', AB.'Status', AB.'Summary', AB.'Keywords', AB.'PM Score', AB.'External Bugs' FROM ALL_BUGS AB WHERE AB.'Snapshot' = date('#{snapshot}')"
end

#all_snapshotsObject



140
141
142
143
144
145
146
147
148
# File 'lib/shiftzilla/helpers.rb', line 140

def all_snapshots
  @all_snapshots ||= begin
    all_snapshots = []
    dbh.execute('SELECT DISTINCT Snapshot FROM ALL_BUGS ORDER BY Snapshot ASC') do |row|
      all_snapshots << row[0]
    end
    all_snapshots
  end
end

#backup_dbObject



76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
# File 'lib/shiftzilla/helpers.rb', line 76

def backup_db
  unless db_backed_up
    today = Date.today.strftime('%Y-%m-%d')
    tpath = File.join(Shiftzilla::ARCH_DIR,today)
    unless Dir.exists?(tpath)
      Dir.mkdir(tpath)
    end
    apath    = ''
    copy_idx = 0
    loop do
      copynum = "%02d" % copy_idx
      apath   = File.join(tpath,"#{copynum}-#{DB_FNAME}")
      break unless File.exists?(apath)
      copy_idx += 1
    end
    FileUtils.cp Shiftzilla::DB_FPATH, apath
    puts "Backed up the database."
    @db_backed_up = true
  end
end

#bug_url(bug_id) ⇒ Object



101
102
103
# File 'lib/shiftzilla/helpers.rb', line 101

def bug_url(bug_id)
  return "#{BZ_URL}#{bug_id}"
end

#cfg_fileObject



54
55
56
# File 'lib/shiftzilla/helpers.rb', line 54

def cfg_file
  @cfg_file ||= validated_config_file(YAML.load_file(Shiftzilla::CFG_FILE))
end

#component_bugs_count(components, snapshot) ⇒ Object



134
135
136
137
138
# File 'lib/shiftzilla/helpers.rb', line 134

def component_bugs_count(components,snapshot)
  rclause = list_clause(components)
  rfilter = rclause == '' ? '' : " AND AB.'Component' #{rclause}"
  return "SELECT count(*) FROM ALL_BUGS AB WHERE AB.'Snapshot' = date('#{snapshot}')#{rfilter}"
end

#component_bugs_query(components, snapshot) ⇒ Object



128
129
130
131
132
# File 'lib/shiftzilla/helpers.rb', line 128

def component_bugs_query(components,snapshot)
  rclause = list_clause(components)
  rfilter = rclause == '' ? '' : " AND AB.'Component' #{rclause}"
  return "SELECT AB.'Bug ID', AB.'Component', AB.'Target Release', AB.'Assignee', AB.'Status', AB.'Summary', AB.'Keywords', AB.'PM Score', AB.'External Bugs' FROM ALL_BUGS AB WHERE AB.'Snapshot' = date('#{snapshot}')#{rfilter} ORDER BY AB.'Target Release' DESC"
end

#dbhObject



58
59
60
# File 'lib/shiftzilla/helpers.rb', line 58

def dbh
  @dbh ||= SQLite3::Database.new(Shiftzilla::DB_FPATH)
end

#field_mapObject



154
155
156
157
158
159
160
161
162
163
164
165
166
# File 'lib/shiftzilla/helpers.rb', line 154

def field_map
  {
    :assigned_to      => 'Assignee',
    :component        => 'Component',
    :id               => 'Bug ID',
    :keywords         => 'Keywords',
    :cf_pm_score      => 'PM Score',
    :status           => 'Status',
    :summary          => 'Summary',
    :target_release   => 'Target Release',
    :external_bugs    => 'External Bugs',
  }
end

#haml_engineObject



62
63
64
# File 'lib/shiftzilla/helpers.rb', line 62

def haml_engine
  @haml_engine ||= Haml::Engine.new(File.read(HAML_TMPL))
end

#latest_snapshotObject



150
151
152
# File 'lib/shiftzilla/helpers.rb', line 150

def latest_snapshot
  all_snapshots[-1]
end

#new_graph(labels, max_y) ⇒ Object



66
67
68
69
70
71
72
73
74
# File 'lib/shiftzilla/helpers.rb', line 66

def new_graph(labels,max_y)
  g = Gruff::Line.new(GRAPH_DIMENSIONS)
  g.theme            = GRAPH_THEME
  g.line_width       = 2
  g.labels           = labels
  g.y_axis_increment = set_axis_increment(max_y)
  g.hide_dots        = true
  return g
end

#set_axis_increment(initial_value) ⇒ Object



105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
# File 'lib/shiftzilla/helpers.rb', line 105

def set_axis_increment(initial_value)
  case
  when initial_value < 10
    return 1
  when initial_value < 20
    return 2
  when initial_value < 50
    return 5
  when initial_value < 100
    return 10
  when initial_value < 200
    return 20
  when initial_value < 400
    return 25
  else
    return 100
  end
end

#set_sza_arch(sza_arch) ⇒ Object



46
47
48
# File 'lib/shiftzilla/helpers.rb', line 46

def set_sza_arch(sza_arch)
  Shiftzilla.const_set('ARCH_DIR',sza_arch)
end

#set_sza_cfg(sza_cfg) ⇒ Object



38
39
40
# File 'lib/shiftzilla/helpers.rb', line 38

def set_sza_cfg(sza_cfg)
  Shiftzilla.const_set('CFG_FILE',sza_cfg)
end

#set_sza_db(sza_db) ⇒ Object



42
43
44
# File 'lib/shiftzilla/helpers.rb', line 42

def set_sza_db(sza_db)
  Shiftzilla.const_set('DB_FPATH',sza_db)
end

#timestampObject



97
98
99
# File 'lib/shiftzilla/helpers.rb', line 97

def timestamp
  DateTime.now.to_s
end

#tmp_dirObject



50
51
52
# File 'lib/shiftzilla/helpers.rb', line 50

def tmp_dir
  @tmp_dir ||= Dir.mktmpdir('shiftzilla-reports-')
end