query_count 1.0.0

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA256:
3
+ metadata.gz: d6bf8e6307491ffbb5fb4cb411765d6641b84b97f4af8db094abdc5e9f6fd91d
4
+ data.tar.gz: 8a70f7fddb6d3200e01f1fe6c7be8b7699bd260d3df0acba109174c3c9cf1cf1
5
+ SHA512:
6
+ metadata.gz: fe202188d69375b0124215792c42ef186edff113b1616dd069c207bd2f313165e8baef1184c95eec2852cab62415d2f0d110a983d4e1d38596037bc41bbdc8b8
7
+ data.tar.gz: 9771031807f5599d968154f5771d330d1c3a19c9ab221e0839a0bef241d23c7ec341989015f538be4820045562edfcd21c7d855b2fb8c29e5b6f125f9a903a32
@@ -0,0 +1,13 @@
1
+ /.bundle/
2
+ /.yardoc
3
+ /_yardoc/
4
+ /coverage/
5
+ /doc/
6
+ /Gemfile.lock
7
+ /pkg/
8
+ /spec/reports/
9
+ /spec/support/dummy_rails_app/log/
10
+ /spec/support/dummy_rails_app/tmp/
11
+ /spec/support/dummy_rails_app/db/*.db
12
+ /tmp/
13
+ .rspec_status
data/.rspec ADDED
@@ -0,0 +1,3 @@
1
+ --format documentation
2
+ --color
3
+ --require spec_helper
@@ -0,0 +1,18 @@
1
+ AllCops:
2
+ NewCops: enable
3
+ TargetRubyVersion: 2.6
4
+
5
+ Layout/IndentationConsistency:
6
+ EnforcedStyle: indented_internal_methods
7
+
8
+ Layout/LineLength:
9
+ Max: 100
10
+
11
+ Metrics/BlockLength:
12
+ ExcludedMethods: [RSpec.describe, context]
13
+
14
+ Style/Documentation:
15
+ Enabled: false
16
+
17
+ Style/FrozenStringLiteralComment:
18
+ EnforcedStyle: never
@@ -0,0 +1,11 @@
1
+ language: ruby
2
+ cache: bundler
3
+ rvm:
4
+ - 2.5.8
5
+ - 2.6.6
6
+ - 2.7.1
7
+ env:
8
+ - RAILS_VERSION=5.0
9
+ - RAILS_VERSION=5.1
10
+ - RAILS_VERSION=5.2
11
+ - RAILS_VERSION=6.0
@@ -0,0 +1,3 @@
1
+ # 1.0.0 (2020-05-23)
2
+
3
+ Initial release.
data/Gemfile ADDED
@@ -0,0 +1,8 @@
1
+ source 'https://rubygems.org'
2
+
3
+ gemspec
4
+
5
+ if ENV['CI']
6
+ rails_version = ENV['RAILS_VERSION'] || '6.0'
7
+ eval_gemfile File.expand_path("../gemfiles/#{rails_version}.gemfile", __FILE__)
8
+ end
@@ -0,0 +1,21 @@
1
+ The MIT License (MIT)
2
+
3
+ Copyright (c) 2020 Dmitriy Tarasov
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in
13
+ all copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
21
+ THE SOFTWARE.
@@ -0,0 +1,27 @@
1
+ # Query Count
2
+
3
+ Counts the number of SQL queries performed by the ActiveRecord.
4
+
5
+ ## Installation
6
+
7
+ Add this line to your application's Gemfile:
8
+
9
+ ```ruby
10
+ gem 'query_count'
11
+ ```
12
+
13
+ Run `$ bundle install`
14
+
15
+ ## Usage
16
+
17
+ The gem will automatically include the number of SQL queries to the Rails log.
18
+
19
+ ```
20
+ ActiveRecord: 34.0ms | SQL Queries: 8 (1 cached)
21
+ ```
22
+
23
+ This log example shows that the total number of queries was 8 and 1 of those queries was cached. Meaning that the database was hit 7 times.
24
+
25
+ ## License
26
+
27
+ `query_count` © Dmitriy Tarasov. Released under the [MIT](LICENSE.txt) license.
@@ -0,0 +1,6 @@
1
+ require 'bundler/gem_tasks'
2
+ require 'rspec/core/rake_task'
3
+
4
+ RSpec::Core::RakeTask.new(:spec)
5
+
6
+ task default: :spec
@@ -0,0 +1,14 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ require 'bundler/setup'
4
+ require 'query_count'
5
+
6
+ # You can add fixtures and/or initialization code here to make experimenting
7
+ # with your gem easier. You can also use a different console, if you like.
8
+
9
+ # (If you use this, don't forget to add pry to your Gemfile!)
10
+ # require 'pry'
11
+ # Pry.start
12
+
13
+ require 'irb'
14
+ IRB.start(__FILE__)
@@ -0,0 +1,8 @@
1
+ #!/usr/bin/env bash
2
+ set -euo pipefail
3
+ IFS=$'\n\t'
4
+ set -vx
5
+
6
+ bundle install
7
+
8
+ # Do any other automated setup that you need to do here
@@ -0,0 +1,2 @@
1
+ gem 'rails', '~> 5.0.0'
2
+ gem 'sqlite3', '~> 1.3.0'
@@ -0,0 +1 @@
1
+ gem 'rails', '~> 5.1.0'
@@ -0,0 +1 @@
1
+ gem 'rails', '~> 5.2.0'
@@ -0,0 +1 @@
1
+ gem 'rails', '~> 6.0.0'
@@ -0,0 +1,4 @@
1
+ require 'query_count/counter'
2
+ require 'query_count/controller_runtime'
3
+ require 'query_count/railtie'
4
+ require 'query_count/version'
@@ -0,0 +1,37 @@
1
+ # https://github.com/rails/rails/blob/master/activerecord/lib/active_record/railties/controller_runtime.rb
2
+ module QueryCount
3
+ module ControllerRuntime
4
+ extend ActiveSupport::Concern
5
+
6
+ module ClassMethods
7
+ def log_process_action(payload)
8
+ messages = super
9
+ count = payload[:query_count]
10
+ count_cache = payload[:query_count_cache]
11
+
12
+ messages.push(
13
+ "SQL Queries: #{count + count_cache}"\
14
+ " (#{count_cache} cached)"
15
+ )
16
+
17
+ messages
18
+ end
19
+ end
20
+
21
+ private
22
+
23
+ def process_action(action, *args)
24
+ Counter.reset_counter
25
+ Counter.reset_counter_cache
26
+ super
27
+ end
28
+
29
+ def append_info_to_payload(payload)
30
+ super
31
+ return unless ActiveRecord::Base.connected?
32
+
33
+ payload[:query_count] = Counter.reset_counter
34
+ payload[:query_count_cache] = Counter.reset_counter_cache
35
+ end
36
+ end
37
+ end
@@ -0,0 +1,50 @@
1
+ # https://api.rubyonrails.org/classes/ActiveRecord/LogSubscriber.html
2
+ require 'active_support'
3
+
4
+ module QueryCount
5
+ class Counter < ActiveSupport::LogSubscriber
6
+ IGNORE_PAYLOAD_NAMES = %w[SCHEMA EXPLAIN].freeze
7
+
8
+ def self.counter=(value)
9
+ Thread.current['query_count'] = value
10
+ end
11
+
12
+ def self.counter
13
+ Thread.current['query_count'] ||= 0
14
+ end
15
+
16
+ def self.counter_cache=(value)
17
+ Thread.current['query_count_cache'] = value
18
+ end
19
+
20
+ def self.counter_cache
21
+ Thread.current['query_count_cache'] ||= 0
22
+ end
23
+
24
+ def self.reset_counter
25
+ rc = counter
26
+ self.counter = 0
27
+
28
+ rc
29
+ end
30
+
31
+ def self.reset_counter_cache
32
+ rcc = counter_cache
33
+ self.counter_cache = 0
34
+
35
+ rcc
36
+ end
37
+
38
+ def sql(event)
39
+ payload = event.payload
40
+
41
+ return if IGNORE_PAYLOAD_NAMES.include?(payload[:name])
42
+
43
+ if payload[:cached]
44
+ self.class.counter_cache += 1
45
+ else
46
+ self.class.counter += 1
47
+ end
48
+ end
49
+ end
50
+ end
@@ -0,0 +1,13 @@
1
+ # https://api.rubyonrails.org/classes/Rails/Railtie.html
2
+ require 'rails/railtie'
3
+
4
+ module QueryCount
5
+ class Railtie < Rails::Railtie
6
+ initializer 'query_count.initialize' do
7
+ QueryCount::Counter.attach_to :active_record
8
+ ActiveSupport.on_load(:action_controller) do
9
+ include QueryCount::ControllerRuntime
10
+ end
11
+ end
12
+ end
13
+ end
@@ -0,0 +1,3 @@
1
+ module QueryCount
2
+ VERSION = '1.0.0'.freeze
3
+ end
@@ -0,0 +1,32 @@
1
+ require_relative 'lib/query_count/version'
2
+
3
+ Gem::Specification.new do |spec|
4
+ spec.name = 'query_count'
5
+ spec.version = QueryCount::VERSION
6
+ spec.authors = ['Dmitriy Tarasov']
7
+ spec.email = ['info@rubysamurai.com']
8
+
9
+ spec.summary = 'SQL queries counter for Rails apps'
10
+ spec.description = spec.summary
11
+ spec.homepage = 'https://github.com/rubysamurai/query_count'
12
+ spec.license = 'MIT'
13
+ spec.required_ruby_version = Gem::Requirement.new('>= 2.3.0')
14
+
15
+ spec.metadata['homepage_uri'] = spec.homepage
16
+ spec.metadata['source_code_uri'] = spec.homepage
17
+ spec.metadata['bug_tracker_uri'] = "#{spec.homepage}/issues"
18
+ spec.metadata['changelog_uri'] = "#{spec.homepage}/blob/master/CHANGELOG.md"
19
+
20
+ spec.files = Dir.chdir(File.expand_path(__dir__)) do
21
+ `git ls-files -z`.split("\x0").reject { |f| f.match(%r{^(test|spec|features)/}) }
22
+ end
23
+ spec.bindir = 'exe'
24
+ spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
25
+ spec.require_paths = ['lib']
26
+
27
+ spec.add_runtime_dependency 'rails', '>= 5.0'
28
+
29
+ spec.add_development_dependency 'rake', '>= 13.0'
30
+ spec.add_development_dependency 'rspec', '>= 3.9'
31
+ spec.add_development_dependency 'sqlite3', '>= 1.3'
32
+ end
metadata ADDED
@@ -0,0 +1,124 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: query_count
3
+ version: !ruby/object:Gem::Version
4
+ version: 1.0.0
5
+ platform: ruby
6
+ authors:
7
+ - Dmitriy Tarasov
8
+ autorequire:
9
+ bindir: exe
10
+ cert_chain: []
11
+ date: 2020-05-23 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: rails
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - ">="
18
+ - !ruby/object:Gem::Version
19
+ version: '5.0'
20
+ type: :runtime
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - ">="
25
+ - !ruby/object:Gem::Version
26
+ version: '5.0'
27
+ - !ruby/object:Gem::Dependency
28
+ name: rake
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - ">="
32
+ - !ruby/object:Gem::Version
33
+ version: '13.0'
34
+ type: :development
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - ">="
39
+ - !ruby/object:Gem::Version
40
+ version: '13.0'
41
+ - !ruby/object:Gem::Dependency
42
+ name: rspec
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - ">="
46
+ - !ruby/object:Gem::Version
47
+ version: '3.9'
48
+ type: :development
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - ">="
53
+ - !ruby/object:Gem::Version
54
+ version: '3.9'
55
+ - !ruby/object:Gem::Dependency
56
+ name: sqlite3
57
+ requirement: !ruby/object:Gem::Requirement
58
+ requirements:
59
+ - - ">="
60
+ - !ruby/object:Gem::Version
61
+ version: '1.3'
62
+ type: :development
63
+ prerelease: false
64
+ version_requirements: !ruby/object:Gem::Requirement
65
+ requirements:
66
+ - - ">="
67
+ - !ruby/object:Gem::Version
68
+ version: '1.3'
69
+ description: SQL queries counter for Rails apps
70
+ email:
71
+ - info@rubysamurai.com
72
+ executables: []
73
+ extensions: []
74
+ extra_rdoc_files: []
75
+ files:
76
+ - ".gitignore"
77
+ - ".rspec"
78
+ - ".rubocop.yml"
79
+ - ".travis.yml"
80
+ - CHANGELOG.md
81
+ - Gemfile
82
+ - LICENSE.txt
83
+ - README.md
84
+ - Rakefile
85
+ - bin/console
86
+ - bin/setup
87
+ - gemfiles/5.0.gemfile
88
+ - gemfiles/5.1.gemfile
89
+ - gemfiles/5.2.gemfile
90
+ - gemfiles/6.0.gemfile
91
+ - lib/query_count.rb
92
+ - lib/query_count/controller_runtime.rb
93
+ - lib/query_count/counter.rb
94
+ - lib/query_count/railtie.rb
95
+ - lib/query_count/version.rb
96
+ - query_count.gemspec
97
+ homepage: https://github.com/rubysamurai/query_count
98
+ licenses:
99
+ - MIT
100
+ metadata:
101
+ homepage_uri: https://github.com/rubysamurai/query_count
102
+ source_code_uri: https://github.com/rubysamurai/query_count
103
+ bug_tracker_uri: https://github.com/rubysamurai/query_count/issues
104
+ changelog_uri: https://github.com/rubysamurai/query_count/blob/master/CHANGELOG.md
105
+ post_install_message:
106
+ rdoc_options: []
107
+ require_paths:
108
+ - lib
109
+ required_ruby_version: !ruby/object:Gem::Requirement
110
+ requirements:
111
+ - - ">="
112
+ - !ruby/object:Gem::Version
113
+ version: 2.3.0
114
+ required_rubygems_version: !ruby/object:Gem::Requirement
115
+ requirements:
116
+ - - ">="
117
+ - !ruby/object:Gem::Version
118
+ version: '0'
119
+ requirements: []
120
+ rubygems_version: 3.0.8
121
+ signing_key:
122
+ specification_version: 4
123
+ summary: SQL queries counter for Rails apps
124
+ test_files: []