Class: Shiftzilla::Source

Inherits:
Object
  • Object
show all
Defined in:
lib/shiftzilla/source.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(qid, qinfo) ⇒ Source

Returns a new instance of Source.



6
7
8
9
10
11
12
13
14
# File 'lib/shiftzilla/source.rb', line 6

def initialize(qid,qinfo)
  @id           = qid.to_sym
  @search       = qinfo['search']
  @sharer       = qinfo['sharer']
  @table        = qinfo['table']
  @external_sub = qinfo['external_sub']
  @fields       = qinfo['fields'].map{ |f| f.to_sym }
  @external_bugs_idx = @fields.index(:external_bugs)
end

Instance Attribute Details

#idObject (readonly)

Returns the value of attribute id.



5
6
7
# File 'lib/shiftzilla/source.rb', line 5

def id
  @id
end

#tableObject (readonly)

Returns the value of attribute table.



5
6
7
# File 'lib/shiftzilla/source.rb', line 5

def table
  @table
end

Instance Method Details

#has_records_for_today?Boolean

Returns:

  • (Boolean)


16
17
18
19
20
21
22
# File 'lib/shiftzilla/source.rb', line 16

def has_records_for_today?
  count = 0
  dbh.execute("SELECT count(*) FROM #{@table} WHERE Snapshot = date('now')") do |row|
    count = row[0].to_i
  end
  return count > 0
end

#load_records(options) ⇒ Object



24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
# File 'lib/shiftzilla/source.rb', line 24

def load_records(options)
  output_format = @fields.map{ |fld| "%{#{fld.to_s}}" }.join("\x1F")
  table_fields  = @fields.map{ |fld| "\"#{field_map[fld]}\"" }.join(',')
  insert_frame  = @fields.map{ |fld| '?' }.join(', ')
  bz_command    = "bugzilla query --savedsearch #{@search} --savedsearch-sharer-id=#{@sharer} --outputformat='#{output_format}'"
  bz_csv        = `#{bz_command}`
  retrieved     = []
  bz_csv.split("\n").each do |row|
    values = row.split("\x1F")
    if not @external_bugs_idx.nil?
      if not @external_sub.nil? and values[@external_bugs_idx].include?(@external_sub)
        values[@external_bugs_idx] = 1
      else
        values[@external_bugs_idx] = 0
      end
    end
    retrieved << values
  end
  puts "Retrieved #{retrieved.length} rows"
  if retrieved.length > 0
    if options[:purge]
      # We know we have new data, so it is okay to nuke the old data
      puts "Purging old records"
      purge_records
    end
    puts "Loading new records"
    dbh.transaction
    retrieved.each do |values|
      dbh.execute("INSERT INTO #{@table} (#{table_fields}) VALUES (#{insert_frame})", values)
    end
    dbh.execute("UPDATE #{@table} SET Snapshot = date('now') WHERE Snapshot ISNULL")
    dbh.commit
  end
  return retrieved.length
end

#purge_recordsObject



60
61
62
# File 'lib/shiftzilla/source.rb', line 60

def purge_records
  dbh.execute("DELETE FROM #{@table} WHERE Snapshot == date('now') OR Snapshot ISNULL")
end