Module: Bicho::Export

Defined in:
lib/bicho/export.rb

Overview

Utility methods for exporting bugs to other systems

Class Method Summary collapse

Class Method Details

.to_json(bug) ⇒ Object

Exports full data of a bug to json, including some extended calculated attributes.



11
12
13
14
15
16
# File 'lib/bicho/export.rb', line 11

def self.to_json(bug)
  bug_h = bug.to_h
  bug_h['history'] = bug.history.changesets.map(&:to_h)
  bug_h['resolution_time'] = Bicho::Reports.resolution_time(bug)
  JSON.generate(bug_h)
end

.to_prometheus_push_gateway(query) ⇒ Object

Export a query for usage as a metric in prometheus See github.com/prometheus/pushgateway

The metric name will be ‘bugs_total’ See prometheus.io/docs/practices/naming/)

And every attributed specified in the query will be used as a label



26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
# File 'lib/bicho/export.rb', line 26

def self.to_prometheus_push_gateway(query)
  buf = StringIO.new
  dimensions = [:product, :status, :priority, :severity, :resolution, :component]
  grouped = query.to_a.group_by do |i|
    puts i
    dimensions.map { |d| [d, i[d]] }.to_h
  end

  buf.write("# TYPE bugs_total gauge\n")
  grouped.each do |attrs, set|
    labels = attrs
             .map { |e| "#{e[0]}=\"#{e[1]}\"" }
             .join(',')
    buf.write("bugs_total{#{labels}} #{set.size}\n")
  end
  buf.write("\n")
  buf.close
  buf.string
end