Class: SqlTracker::Report
- Inherits:
-
Object
- Object
- SqlTracker::Report
- Defined in:
- lib/sql_tracker/report.rb
Instance Attribute Summary collapse
-
#raw_data ⇒ Object
Returns the value of attribute raw_data.
-
#terminal_width ⇒ Object
Returns the value of attribute terminal_width.
Instance Method Summary collapse
- #+(other) ⇒ Object
- #data ⇒ Object
-
#initialize(data) ⇒ Report
constructor
A new instance of Report.
- #print_text(options) ⇒ Object
- #row_format ⇒ Object
- #sort_data(data, sort_by) ⇒ Object
- #valid? ⇒ Boolean
- #version ⇒ Object
Constructor Details
#initialize(data) ⇒ Report
Returns a new instance of Report.
6 7 8 |
# File 'lib/sql_tracker/report.rb', line 6 def initialize(data) self.raw_data = data end |
Instance Attribute Details
#raw_data ⇒ Object
Returns the value of attribute raw_data.
3 4 5 |
# File 'lib/sql_tracker/report.rb', line 3 def raw_data @raw_data end |
#terminal_width ⇒ Object
Returns the value of attribute terminal_width.
4 5 6 |
# File 'lib/sql_tracker/report.rb', line 4 def terminal_width @terminal_width end |
Instance Method Details
#+(other) ⇒ Object
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 102 103 |
# 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 ⇒ Object
25 26 27 |
# File 'lib/sql_tracker/report.rb', line 25 def data raw_data['data'] end |
#print_text(options) ⇒ Object
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 60 61 |
# File 'lib/sql_tracker/report.rb', line 29 def print_text() self.terminal_width = .fetch(:terminal_width) 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, [: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_format ⇒ Object
63 64 65 |
# 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) ⇒ Object
67 68 69 70 71 72 73 74 75 |
# 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? ⇒ Boolean
10 11 12 13 14 15 16 17 18 19 |
# 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 ⇒ Object
21 22 23 |
# File 'lib/sql_tracker/report.rb', line 21 def version raw_data['format_version'] end |