Class: SqlTracker::Report

Inherits:
Object
  • Object
show all
Defined in:
lib/sql_tracker/report.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(data) ⇒ Report

Returns a new instance of Report.



5
6
7
# File 'lib/sql_tracker/report.rb', line 5

def initialize(data)
  self.raw_data = data
end

Instance Attribute Details

#raw_dataObject

Returns the value of attribute raw_data.



3
4
5
# File 'lib/sql_tracker/report.rb', line 3

def raw_data
  @raw_data
end

Instance Method Details

#+(other) ⇒ Object



75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
# File 'lib/sql_tracker/report.rb', line 75

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

#dataObject



24
25
26
# File 'lib/sql_tracker/report.rb', line 24

def data
  raw_data['data']
end


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
59
# File 'lib/sql_tracker/report.rb', line 28

def print_text(options)
  f = STDOUT
  f.puts '=================================='
  f.puts "Total Unique SQL Queries: #{data.keys.size}"
  f.puts '=================================='
  f.printf(
    "%-#{count_width}s | %-#{duration_width}s | %-#{sql_width}s | Source\n",
    'Count', 'Avg Time (ms)', 'SQL Query'
  )
  f.puts '-' * terminal_width

  sorted_data = sort_data(data.values, options[:sort_by])
  sorted_data.each do |row|
    chopped_sql = wrap_text(row['sql'], sql_width)
    source_list = wrap_list(row['source'].uniq, sql_width - 10)
    avg_duration = row['duration'].to_f / row['count']
    total_lines = if chopped_sql.length >= source_list.length
                    chopped_sql.length
                  else
                    source_list.length
                  end

    (0...total_lines).each do |line|
      count = line == 0 ? row['count'].to_s : ''
      duration = line == 0 ? avg_duration.round(2) : ''
      source = source_list.length > line ? source_list[line] : ''
      query = row['sql'].length > line ? chopped_sql[line] : ''
      f.printf(row_format, count, duration, query, source)
    end
    f.puts '-' * terminal_width
  end
end

#row_formatObject



61
62
63
# File 'lib/sql_tracker/report.rb', line 61

def row_format
  "%-#{count_width}s | %-#{duration_width}s | %-#{sql_width}s | %-#{sql_width}s\n"
end

#sort_data(data, sort_by) ⇒ Object



65
66
67
68
69
70
71
72
73
# File 'lib/sql_tracker/report.rb', line 65

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?Boolean

Returns:

  • (Boolean)


9
10
11
12
13
14
15
16
17
18
# File 'lib/sql_tracker/report.rb', line 9

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

#versionObject



20
21
22
# File 'lib/sql_tracker/report.rb', line 20

def version
  raw_data['format_version']
end