class SqlTracker::Report

Attributes

raw_data[RW]
terminal_width[RW]

Public Class Methods

new(data) click to toggle source
# File lib/sql_tracker/report.rb, line 6
def initialize(data)
  self.raw_data = data
end

Public Instance Methods

+(other) click to toggle source
# File lib/sql_tracker/report.rb, line 77
def +(other)
  unless self.class == other.class
    raise ArgumentError, "cannot combine #{other.class}"
  end
  unless version == other.version
    raise ArgumentError, "cannot combine v#{version} with v#{other.version}"
  end

  r1 = data
  r2 = other.data

  merged = (r1.keys + r2.keys).uniq.each_with_object({}) do |id, memo|
    if !r1.key?(id)
      memo[id] = r2[id]
    elsif r2.key?(id)
      memo[id] = r1[id]
      memo[id]['count'] += r2[id]['count']
      memo[id]['duration'] += r2[id]['duration']
      memo[id]['source'] += r2[id]['source']
    else
      memo[id] = r1[id]
    end
  end
  merged_data = { 'data' => merged, 'format_version' => version }

  self.class.new(merged_data)
end
data() click to toggle source
# File lib/sql_tracker/report.rb, line 25
def data
  raw_data['data']
end
print_text(options) click to toggle source
row_format() click to toggle source
# File lib/sql_tracker/report.rb, line 63
def row_format
  "%-#{count_width}s | %-#{duration_width}s | %-#{sql_width}s | %-#{sql_width}s\n"
end
sort_data(data, sort_by) click to toggle source
# File lib/sql_tracker/report.rb, line 67
def sort_data(data, sort_by)
  data.sort_by do |d|
    if sort_by == 'duration'
      -d['duration'].to_f / d['count']
    else
      -d['count']
    end
  end
end
valid?() click to toggle source
# File lib/sql_tracker/report.rb, line 10
def valid?
  return false unless raw_data.key?('format_version')
  return false unless raw_data.key?('data')
  return false if raw_data['data'].nil? || raw_data['data'].empty?
  sample = raw_data['data'].values.first
  %w(sql count source).each do |key|
    return false unless sample.key?(key)
  end
  true
end
version() click to toggle source
# File lib/sql_tracker/report.rb, line 21
def version
  raw_data['format_version']
end

Private Instance Methods

count_width() click to toggle source
# File lib/sql_tracker/report.rb, line 123
def count_width
  5
end
duration_width() click to toggle source
# File lib/sql_tracker/report.rb, line 127
def duration_width
  15
end
sql_width() click to toggle source
# File lib/sql_tracker/report.rb, line 119
def sql_width
  @sql_width ||= (terminal_width - count_width - duration_width) / 2
end
wrap_list(list, width) click to toggle source

an array of text

# File lib/sql_tracker/report.rb, line 113
def wrap_list(list, width)
  list.map do |text|
    wrap_text(text, width)
  end.flatten
end
wrap_text(text, width) click to toggle source
# File lib/sql_tracker/report.rb, line 107
def wrap_text(text, width)
  return [text] if text.length <= width
  text.scan(/.{1,#{width}}/)
end