active-record-query-count 0.1.3 → 0.1.5

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: c74868de3cd6aa45193be102108bb86b55dc7ec196e1a604c66ffc750ac30ee1
4
- data.tar.gz: 5852828907c5f1704ea19721fe285edcfe98202861c9f81f759fc73474462d4d
3
+ metadata.gz: 3e9f34b85f506639383a20f5abe438e1f52938a77cafd40189c5c60f9d380f3c
4
+ data.tar.gz: 1f70e660ded4a8414ab1f3cc4b921a68837a83c9100f3fc73f257c63834b280b
5
5
  SHA512:
6
- metadata.gz: df7c9406bd9ffb4eafb799aa24637a068f77f7b4258c157ad30ca2f87e79485df494084922b990edf54e53593a1dccbabae634f3cb14822484de6bfcd7fee37e
7
- data.tar.gz: 380c18c78e63cd3f899e42f3c07e993f40db1aa17e27b74f13f6bbd599cbdc28430599bd6e6dc4519c981b8e2afb0f36dd86088be6c3960ea107ac27328c6a48
6
+ metadata.gz: 3eb642147b61276500c618f26fc91b86463f5fbec7f9d03a7dba6ac504c2dc7419aff6943dd539be414b68da1cc16ae60344248e758cf2960ca08f3029e2bba9
7
+ data.tar.gz: cec9bbf99b2ba114ddf172717ca4282a168cb75462588006a7e6d8b20b211bef3cf0868d5e7ce137748db688da700ed4fecf0bea89f28de2b8fa209ec9751cd9
data/assets/style.css CHANGED
@@ -4,10 +4,9 @@
4
4
  width: 100%;
5
5
  border-collapse: collapse;
6
6
  margin-top: 20px;
7
-
8
7
  height: 100%;
9
- overflow:auto;
10
- display: block; /* Ensure the table behaves as a block element */
8
+ overflow: auto;
9
+ display: block;
11
10
  box-sizing: border-box;
12
11
  }
13
12
  th, td {
@@ -17,11 +16,12 @@
17
16
  th {
18
17
  background-color: #f2f2f2;
19
18
  }
20
- tr:nth-child(even) {
21
- background-color: #f9f9f9;
19
+ tr.main-row.even, tr.sub-row.even {
20
+ background-color: #f2f2f2;
22
21
  }
23
- .sub-row {
24
- background-color: #f9f9f9;
22
+
23
+ tr.main-row.odd, tr.sub-row.odd {
24
+ background-color: #ffffff;
25
25
  }
26
26
 
27
27
  .center-container {
@@ -39,4 +39,4 @@
39
39
  #queryTable {
40
40
  display: table;
41
41
  }
42
- }
42
+ }
@@ -3,9 +3,10 @@
3
3
  </style>
4
4
  <div id='query_counter_report_gem'>
5
5
  <h2>Query Count Report</h2>
6
- <p>Total query count on process: <%= total_query_count %></p>
7
- <p>The table will show only tables with more than <%= ::ActiveRecordQueryCount::Configuration.ignore_table_count %> queries</p>
8
- <p>Only the top <%= ::ActiveRecordQueryCount::Configuration.max_locations_per_table %> locations with the most occurrences will be shown.</p>
6
+ <p>Total number of queries in this process: <%= total_query_count %></p>
7
+ <p>Total time spent on queries in this process (ms): <%= total_duration_time %></p>
8
+ <p>Only tables with <%= ::ActiveRecordQueryCount::Configuration.ignore_table_count %> or more queries will be displayed.</p>
9
+ <p>The top <%= ::ActiveRecordQueryCount::Configuration.max_locations_per_table %> locations with the highest occurrences will be shown for each table.</p>
9
10
  <button id="toggleButton" onclick="toggleView()">Show Chart View</button>
10
11
  <button id="toggleColumnButton" onclick="toggleColumnContent()">Show SQL</button>
11
12
 
@@ -19,14 +20,15 @@
19
20
  <th id="columnHeader">File Path</th>
20
21
  <th>Method</th>
21
22
  <th>Location Count</th>
23
+ <th>Total Duration(ms)</th>
22
24
  </tr>
23
- <% filter_data(data).each do |table, info| %>
24
- <tr>
25
+ <% filter_data(data).each_with_index do |(table, info), index| %>
26
+ <tr class="main-row <%= index.even? ? 'even' : 'odd' %>">
25
27
  <td rowspan="<%= info[:location].size + 1 %>"><%= table %></td>
26
28
  <td rowspan="<%= info[:location].size + 1 %>"><%= info[:count] %></td>
27
29
  </tr>
28
30
  <% info[:location].each do |loc, detail| %>
29
- <tr class="sub-row">
31
+ <tr class="sub-row <%= index.even? ? 'even' : 'odd' %>">
30
32
  <% match = loc&.match(/^(?<file_path>.*):in.*`(?<method>.*)'$/) %>
31
33
  <% file_path = match ? match[:file_path] : loc %>
32
34
  <% method = match ? match[:method] : nil %>
@@ -36,6 +38,7 @@
36
38
  </td>
37
39
  <td class="method-column"><%= method %></td>
38
40
  <td><%= detail[:count] %></td>
41
+ <td><%= detail[:duration] %></td>
39
42
  </tr>
40
43
  <% end %>
41
44
  <% end %>
@@ -21,7 +21,7 @@ module ActiveRecordQueryCount
21
21
  raise 'Exactly two code blocks are required' if @scripts_loaded != 2
22
22
 
23
23
  ActiveRecordQueryCount::Printer::HtmlCompare.new(data_1: results.slice(results.keys[0]),
24
- data_2: results.slice(results.keys[1])).print
24
+ data_2: results.slice(results.keys[1])).print
25
25
  end
26
26
  end
27
27
 
@@ -11,6 +11,11 @@ module ActiveRecordQueryCount
11
11
  INJECT_TEMPLATE_PATH = File.join(parent_dir, 'assets', 'template_for_inject.html.erb')
12
12
 
13
13
  def filter_data data
14
+ data.each_value do |info|
15
+ info[:location].each_value do |detail|
16
+ detail[:duration] = detail[:duration].truncate(2)
17
+ end
18
+ end
14
19
  data = data.select { |_, v| v[:count] >= Configuration.ignore_table_count }
15
20
  data = data.sort_by { |_, v| -v[:count] }.each do |_category, info|
16
21
  info[:location] = info[:location].sort_by do |_, detail|
@@ -21,6 +21,7 @@ module ActiveRecordQueryCount
21
21
  info[:location].each do |loc, details|
22
22
  puts " - File location: #{loc}"
23
23
  puts " Query count: #{details[:count].to_s.colorize(:blue)}"
24
+ puts " Total Duration(ms): #{details[:duration]}"
24
25
  end
25
26
  puts
26
27
  end
@@ -14,13 +14,21 @@ module ActiveRecordQueryCount
14
14
  end
15
15
 
16
16
  def chart_data
17
- @chart_data ||= generate_chart_data(data)
17
+ @chart_data ||= generate_chart_js_data(data)
18
18
  end
19
19
 
20
20
  def total_query_count
21
21
  @total_query_count ||= data.values.sum { |v| v[:count] }
22
22
  end
23
23
 
24
+ def total_duration_time
25
+ @total_duration_time ||= data.sum do |_, info|
26
+ info[:location].sum do |_, detail|
27
+ detail[:duration]
28
+ end
29
+ end.truncate(2)
30
+ end
31
+
24
32
  def inject_in_html
25
33
  ERB.new(inject_template_content).result(binding)
26
34
  end
@@ -36,13 +44,13 @@ module ActiveRecordQueryCount
36
44
 
37
45
  private
38
46
 
39
- def generate_chart_data(data)
47
+ def generate_chart_js_data(data)
40
48
  chart_data = { labels: [], data: [], locations: {} }
41
49
  data.each do |table, info|
42
50
  chart_data[:labels] << table
43
51
  chart_data[:data] << info[:count]
44
52
  chart_data[:locations][table] = info[:location].map do |loc, detail|
45
- { location: loc, count: detail[:count] }
53
+ { location: loc, count: detail[:count], duration: detail[:duration] }
46
54
  end
47
55
  end
48
56
  chart_data
@@ -25,7 +25,7 @@ module ActiveRecordQueryCount
25
25
  tables = data_1.keys | data_2.keys
26
26
  total_query_count_1 = data_1.values.sum { |v| v[:count] }
27
27
  total_query_count_2 = data_2.values.sum { |v| v[:count] }
28
- chart_data = generate_chart_data_compare(data_1, data_2)
28
+ chart_data = generate_chart_js_data_compare(data_1, data_2)
29
29
  # end
30
30
  html_dest = generate_html(binding)
31
31
  open_file(html_dest)
@@ -33,7 +33,7 @@ module ActiveRecordQueryCount
33
33
 
34
34
  private
35
35
 
36
- def generate_chart_data_compare(data_1, data_2)
36
+ def generate_chart_js_data_compare(data_1, data_2)
37
37
  labels = (data_1.keys | data_2.keys).sort
38
38
  chart_data = { labels: [], data_1: {}, data_2: {}, locations: {} }
39
39
  chart_data[:data_1][:name] = @script_1_name
@@ -19,13 +19,15 @@ module ActiveRecordQueryCount
19
19
  def subscribe
20
20
  return unless subscription.nil?
21
21
 
22
- @subscription = ActiveSupport::Notifications.subscribe('sql.active_record') do |_a, _b, _c, _d, payload|
22
+ @subscription = ActiveSupport::Notifications.subscribe('sql.active_record') do |_a, start, finish, _d, payload|
23
23
  caller_from_sql = caller
24
24
  sql = payload[:sql]
25
25
  match = sql.match(REGEX_TABLE_SQL)
26
26
  if match.present? && match[:table]
27
27
  actual_location = Rails.backtrace_cleaner.clean(caller_from_sql).first
28
28
  active_record_query_tracker[match[:table]][:count] += 1
29
+ active_record_query_tracker[match[:table]][:location][actual_location][:duration] ||= 0
30
+ active_record_query_tracker[match[:table]][:location][actual_location][:duration] += (finish - start) * 1000
29
31
  active_record_query_tracker[match[:table]][:location][actual_location][:count] += 1
30
32
  active_record_query_tracker[match[:table]][:location][actual_location][:sql] = sql
31
33
  end
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module ActiveRecordQueryCount
4
- VERSION = '0.1.3'
4
+ VERSION = '0.1.5'
5
5
  end
metadata CHANGED
@@ -1,29 +1,35 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: active-record-query-count
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.3
4
+ version: 0.1.5
5
5
  platform: ruby
6
6
  authors:
7
7
  - Jose Lara
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2024-07-15 00:00:00.000000000 Z
11
+ date: 2024-10-17 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: activesupport
15
15
  requirement: !ruby/object:Gem::Requirement
16
16
  requirements:
17
- - - "~>"
17
+ - - ">="
18
18
  - !ruby/object:Gem::Version
19
- version: '6.0'
19
+ version: '5.0'
20
+ - - "<"
21
+ - !ruby/object:Gem::Version
22
+ version: '8.0'
20
23
  type: :runtime
21
24
  prerelease: false
22
25
  version_requirements: !ruby/object:Gem::Requirement
23
26
  requirements:
24
- - - "~>"
27
+ - - ">="
25
28
  - !ruby/object:Gem::Version
26
- version: '6.0'
29
+ version: '5.0'
30
+ - - "<"
31
+ - !ruby/object:Gem::Version
32
+ version: '8.0'
27
33
  - !ruby/object:Gem::Dependency
28
34
  name: colorize
29
35
  requirement: !ruby/object:Gem::Requirement
@@ -70,16 +76,22 @@ dependencies:
70
76
  name: activerecord
71
77
  requirement: !ruby/object:Gem::Requirement
72
78
  requirements:
73
- - - "~>"
79
+ - - ">="
74
80
  - !ruby/object:Gem::Version
75
- version: '6.0'
81
+ version: '5.0'
82
+ - - "<"
83
+ - !ruby/object:Gem::Version
84
+ version: '8.0'
76
85
  type: :development
77
86
  prerelease: false
78
87
  version_requirements: !ruby/object:Gem::Requirement
79
88
  requirements:
80
- - - "~>"
89
+ - - ">="
81
90
  - !ruby/object:Gem::Version
82
- version: '6.0'
91
+ version: '5.0'
92
+ - - "<"
93
+ - !ruby/object:Gem::Version
94
+ version: '8.0'
83
95
  - !ruby/object:Gem::Dependency
84
96
  name: minitest
85
97
  requirement: !ruby/object:Gem::Requirement
@@ -168,16 +180,16 @@ dependencies:
168
180
  name: shoulda-context
169
181
  requirement: !ruby/object:Gem::Requirement
170
182
  requirements:
171
- - - ">="
183
+ - - "~>"
172
184
  - !ruby/object:Gem::Version
173
- version: '0'
185
+ version: 3.0.0.rc1
174
186
  type: :development
175
187
  prerelease: false
176
188
  version_requirements: !ruby/object:Gem::Requirement
177
189
  requirements:
178
- - - ">="
190
+ - - "~>"
179
191
  - !ruby/object:Gem::Version
180
- version: '0'
192
+ version: 3.0.0.rc1
181
193
  - !ruby/object:Gem::Dependency
182
194
  name: shoulda-matchers
183
195
  requirement: !ruby/object:Gem::Requirement
@@ -253,14 +265,17 @@ required_ruby_version: !ruby/object:Gem::Requirement
253
265
  requirements:
254
266
  - - ">="
255
267
  - !ruby/object:Gem::Version
256
- version: 3.2.2
268
+ version: '2.7'
269
+ - - "<"
270
+ - !ruby/object:Gem::Version
271
+ version: '4.0'
257
272
  required_rubygems_version: !ruby/object:Gem::Requirement
258
273
  requirements:
259
274
  - - ">="
260
275
  - !ruby/object:Gem::Version
261
276
  version: '0'
262
277
  requirements: []
263
- rubygems_version: 3.5.15
278
+ rubygems_version: 3.4.10
264
279
  signing_key:
265
280
  specification_version: 4
266
281
  summary: Display an overview of the quantity of queries being made and their origins