sql-query-stats 0.1.1

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 ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: 2f34e686dad58a5034ac7bd019b30ff9e027f059
4
+ data.tar.gz: 66544a00fce3bc8135305f6ee1a5efa9a95b1c06
5
+ SHA512:
6
+ metadata.gz: 97ed4062d6795a9a258e415d3cd803ba1bbb446a3ea0087ecaee8f5ff06c9a923840342fde5ef400b8edf45ad7c598888b0718bb56433ddae7cc7bed7e4a4a0d
7
+ data.tar.gz: cef95d9dccfa2d765c6ad4da0067de440689e42adc84927c981109c6918b513288e56816b8f6630b850dfff42cc9715b902001f9bee09c415074542badc400d4
data/LICENSE ADDED
@@ -0,0 +1,20 @@
1
+ Copyright 2018 Ben Barber
2
+
3
+ Permission is hereby granted, free of charge, to any person obtaining
4
+ a copy of this software and associated documentation files (the
5
+ "Software"), to deal in the Software without restriction, including
6
+ without limitation the rights to use, copy, modify, merge, publish,
7
+ distribute, sublicense, and/or sell copies of the Software, and to
8
+ permit persons to whom the Software is furnished to do so, subject to
9
+ the following conditions:
10
+
11
+ The above copyright notice and this permission notice shall be
12
+ included in all copies or substantial portions of the Software.
13
+
14
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
15
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
16
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
17
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
18
+ LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
19
+ OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
20
+ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
data/README.md ADDED
@@ -0,0 +1,29 @@
1
+ # SqlQueryStats
2
+
3
+ Short description and motivation.
4
+
5
+ ## Usage
6
+ How to use my plugin.
7
+
8
+ ## Installation
9
+ Add this line to your application's Gemfile:
10
+
11
+ ```ruby
12
+ gem 'sql-query-stats'
13
+ ```
14
+
15
+ And then execute:
16
+ ```bash
17
+ $ bundle
18
+ ```
19
+
20
+ Or install it yourself as:
21
+ ```bash
22
+ $ gem install sql_query_stats
23
+ ```
24
+
25
+ ## Contributing
26
+ Contribution directions go here.
27
+
28
+ ## License
29
+ The gem is available as open source under the terms of the [MIT License](http://opensource.org/licenses/MIT).
data/Rakefile ADDED
@@ -0,0 +1,27 @@
1
+ begin
2
+ require 'bundler/setup'
3
+ rescue LoadError
4
+ puts 'You must `gem install bundler` and `bundle install` to run rake tasks'
5
+ end
6
+
7
+ require 'rdoc/task'
8
+
9
+ RDoc::Task.new(:rdoc) do |rdoc|
10
+ rdoc.rdoc_dir = 'rdoc'
11
+ rdoc.title = 'SqlQueryStats'
12
+ rdoc.options << '--line-numbers'
13
+ rdoc.rdoc_files.include('README.md')
14
+ rdoc.rdoc_files.include('lib/**/*.rb')
15
+ end
16
+
17
+ require 'bundler/gem_tasks'
18
+
19
+ require 'rake/testtask'
20
+
21
+ Rake::TestTask.new(:test) do |t|
22
+ t.libs << 'test'
23
+ t.pattern = 'test/**/*_test.rb'
24
+ t.verbose = false
25
+ end
26
+
27
+ task default: :test
@@ -0,0 +1,4 @@
1
+ require 'sql_query_stats/version'
2
+ require 'sql_query_stats/reporter'
3
+ require 'sql_query_stats/controller_runtime'
4
+ require 'sql_query_stats/initializer'
@@ -0,0 +1,40 @@
1
+ module SqlQueryStats
2
+ # SqlQueryStats Controler Runtime
3
+ module ControllerRuntime
4
+ extend ActiveSupport::Concern
5
+
6
+ protected
7
+
8
+ def process_action(action, *args)
9
+ Reporter.reset
10
+ super
11
+ end
12
+
13
+ def append_info_to_payload(payload)
14
+ super
15
+ payload[:sql_query_stats] = Reporter.reset if ActiveRecord::Base.connected?
16
+ end
17
+
18
+ # Add QueryStats to the Rails Process Action log
19
+ module ClassMethods
20
+ def log_process_action(payload)
21
+ messages = super
22
+ exclusions = %i[total_duration]
23
+ stats = payload[:sql_query_stats].with_indifferent_access
24
+
25
+ stats.except(*exclusions).each do |stat, value|
26
+ messages << formatted_message(stat, value)
27
+ end
28
+
29
+ messages
30
+ end
31
+
32
+ private
33
+
34
+ def formatted_message(stat, value)
35
+ value = "#{value}ms" if stat.end_with? 'duration'
36
+ "#{stat.to_s.camelize}: #{value}"
37
+ end
38
+ end
39
+ end
40
+ end
@@ -0,0 +1,14 @@
1
+ module SqlQueryStats
2
+ # QueryStats Initializer
3
+ class Initializer < Rails::Railtie
4
+ initializer 'sql_query_stats.initialize' do
5
+ # Attach to the reporter ActiveRecord Events
6
+ SqlQueryStats::Reporter.attach_to :active_record
7
+
8
+ # Load the controller runtime
9
+ ActiveSupport.on_load(:action_controller) do
10
+ include SqlQueryStats::ControllerRuntime
11
+ end
12
+ end
13
+ end
14
+ end
@@ -0,0 +1,62 @@
1
+ module SqlQueryStats
2
+ # SqlQueryStats Reporter
3
+ class Reporter < ActiveSupport::LogSubscriber
4
+ attr_accessor :stats
5
+
6
+ DEFAULTS = {
7
+ total_queries: 0,
8
+ total_duration: 0,
9
+ query_cache_used: 0,
10
+ slowest_query: '',
11
+ slowest_query_duration: 0
12
+ }.freeze
13
+
14
+ def self.reset
15
+ stats = Thread.current['sql_query_stats']
16
+ Thread.current['sql_query_stats'] = DEFAULTS.dup
17
+ stats
18
+ end
19
+
20
+ def initialize
21
+ super
22
+ end
23
+
24
+ def log(key, value)
25
+ Thread.current['sql_query_stats'][key] = value
26
+ end
27
+
28
+ def stats
29
+ Thread.current['sql_query_stats'] ||= DEFAULTS.dup
30
+ end
31
+
32
+ def sql(event)
33
+ log(:total_queries, stats[:total_queries] + 1)
34
+ log(:total_duration, (stats[:total_duration] + event.duration).round(1))
35
+
36
+ check_cache_used(event)
37
+ check_slowest_query(event)
38
+ end
39
+
40
+ private
41
+
42
+ def check_cache_used(event)
43
+ return unless event.payload[:cached]
44
+ log(:query_cache_used, stats[:query_cache_used] + 1)
45
+ end
46
+
47
+ def check_slowest_query(event)
48
+ reportable_queries = %w[INSERT SELECT UPDATE]
49
+ return unless event.duration > stats[:slowest_query_duration]
50
+ return unless event.payload[:sql].start_with?(*reportable_queries)
51
+
52
+ report_slowest_query(event)
53
+ end
54
+
55
+ def report_slowest_query(event)
56
+ duration = stats[:slowest_query_duration] + event.duration
57
+
58
+ log(:slowest_query, event.payload[:sql])
59
+ log(:slowest_query_duration, duration.round(1))
60
+ end
61
+ end
62
+ end
@@ -0,0 +1,3 @@
1
+ module SqlQueryStats
2
+ VERSION = '0.1.1'.freeze
3
+ end
@@ -0,0 +1,4 @@
1
+ # desc "Explaining what the task does"
2
+ # task :sql_query_stats do
3
+ # # Task goes here
4
+ # end
metadata ADDED
@@ -0,0 +1,93 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: sql-query-stats
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.1
5
+ platform: ruby
6
+ authors:
7
+ - Ben Barber
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2018-02-26 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: '3.0'
20
+ - - ">"
21
+ - !ruby/object:Gem::Version
22
+ version: '3.0'
23
+ type: :runtime
24
+ prerelease: false
25
+ version_requirements: !ruby/object:Gem::Requirement
26
+ requirements:
27
+ - - "~>"
28
+ - !ruby/object:Gem::Version
29
+ version: '3.0'
30
+ - - ">"
31
+ - !ruby/object:Gem::Version
32
+ version: '3.0'
33
+ - !ruby/object:Gem::Dependency
34
+ name: sqlite3
35
+ requirement: !ruby/object:Gem::Requirement
36
+ requirements:
37
+ - - "~>"
38
+ - !ruby/object:Gem::Version
39
+ version: '1.2'
40
+ - - ">"
41
+ - !ruby/object:Gem::Version
42
+ version: '1.2'
43
+ type: :development
44
+ prerelease: false
45
+ version_requirements: !ruby/object:Gem::Requirement
46
+ requirements:
47
+ - - "~>"
48
+ - !ruby/object:Gem::Version
49
+ version: '1.2'
50
+ - - ">"
51
+ - !ruby/object:Gem::Version
52
+ version: '1.2'
53
+ description: SqlQueryStats
54
+ email:
55
+ - contact@benbarber.co.uk
56
+ executables: []
57
+ extensions: []
58
+ extra_rdoc_files: []
59
+ files:
60
+ - LICENSE
61
+ - README.md
62
+ - Rakefile
63
+ - lib/sql_query_stats.rb
64
+ - lib/sql_query_stats/controller_runtime.rb
65
+ - lib/sql_query_stats/initializer.rb
66
+ - lib/sql_query_stats/reporter.rb
67
+ - lib/sql_query_stats/version.rb
68
+ - lib/tasks/sql_query_stats_tasks.rake
69
+ homepage: https://github.com/benbarber/sql-query-stats
70
+ licenses:
71
+ - MIT
72
+ metadata: {}
73
+ post_install_message:
74
+ rdoc_options: []
75
+ require_paths:
76
+ - lib
77
+ required_ruby_version: !ruby/object:Gem::Requirement
78
+ requirements:
79
+ - - ">="
80
+ - !ruby/object:Gem::Version
81
+ version: '0'
82
+ required_rubygems_version: !ruby/object:Gem::Requirement
83
+ requirements:
84
+ - - ">="
85
+ - !ruby/object:Gem::Version
86
+ version: '0'
87
+ requirements: []
88
+ rubyforge_project:
89
+ rubygems_version: 2.6.11
90
+ signing_key:
91
+ specification_version: 4
92
+ summary: Get more information from ActiveRecord
93
+ test_files: []