sql_event_analyzer 0.1.0

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: 45ac7d368d025a8cd7154de382c68bf224dace83
4
+ data.tar.gz: 42693e5aa757fd57011981b9fd9952fbf8a203a3
5
+ SHA512:
6
+ metadata.gz: eb16c5c8c7b0910d0ea2efa5339f68db70cd5e4f9d1aa57824eec036eafe8b0b1cb1c03776bc4b5ff13632441c2154d3468bb8ad068d43c0f3806992e02c0a3a
7
+ data.tar.gz: 579efba8137ab510de74af37106d6ae01fa88d6ada22c37c7b1ac31737f29cf1be243ddecc1e7b8020cb816bae9e65ab75ce5c5e69c9e484a8701e751d1e9f34
data/.gitignore ADDED
@@ -0,0 +1,9 @@
1
+ /.bundle/
2
+ /.yardoc
3
+ /Gemfile.lock
4
+ /_yardoc/
5
+ /coverage/
6
+ /doc/
7
+ /pkg/
8
+ /spec/reports/
9
+ /tmp/
data/.rspec ADDED
@@ -0,0 +1,2 @@
1
+ --format documentation
2
+ --color
data/.rubocop.yml ADDED
@@ -0,0 +1,2 @@
1
+ AllCops:
2
+ TargetRubyVersion: 2.2
data/.travis.yml ADDED
@@ -0,0 +1,5 @@
1
+ sudo: false
2
+ language: ruby
3
+ rvm:
4
+ - 2.2.3
5
+ before_install: gem install bundler -v 1.12.5
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source 'https://rubygems.org'
2
+
3
+ # Specify your gem's dependencies in sql_event_analyzer.gemspec
4
+ gemspec
data/LICENSE.txt ADDED
@@ -0,0 +1,21 @@
1
+ The MIT License (MIT)
2
+
3
+ Copyright (c) 2017 Scott Pierce
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.
data/README.md ADDED
@@ -0,0 +1,70 @@
1
+ # Overview
2
+
3
+ Consume Rails SQL events and generates an HTML overview for the SQL utilization.
4
+
5
+ ## Usage
6
+
7
+ ### Around any calls
8
+ ```ruby
9
+ SqlEventAnalyzer.start(name: 'report1') do
10
+ # Some code that does a lot of SQL
11
+ end
12
+ ```
13
+
14
+ ### View the Results
15
+
16
+ A directory will be created at the root of the project directry from which Rails
17
+ is running. All results are contained there as HTML files. Open them in your
18
+ web browser.
19
+
20
+ ```sh
21
+ find tmp/sql_event_analyzer/
22
+ open tmp/sql_event_analyzer/report1.html
23
+ ```
24
+
25
+ ### Around a Rails Controller
26
+ ```ruby
27
+ class ApplicationController < ActionController::Base
28
+
29
+ # @see http://guides.rubyonrails.org/action_controller_overview.html#after-filters-and-around-filters
30
+ around_action :capture_sql_filter
31
+
32
+ def capture_sql_filter(&block)
33
+ stats_file_name = "#{params[:controller]}-#{params[:action]}"
34
+ SqlEventAnalyzer.start(name: stats_file_name, &block)
35
+ end
36
+ end
37
+ ```
38
+
39
+ ## Installation
40
+
41
+ Add this line to your application's Gemfile:
42
+
43
+ ```ruby
44
+ gem 'sql_event_analyzer'
45
+ ```
46
+
47
+ And then execute:
48
+
49
+ $ bundle
50
+
51
+ Or install it yourself as:
52
+
53
+ $ gem install sql_event_analyzer
54
+
55
+
56
+ ## Development
57
+
58
+ After checking out the repo, run `bin/setup` to install dependencies. Then, run `rake spec` to run the tests. You can also run `bin/console` for an interactive prompt that will allow you to experiment.
59
+
60
+ To install this gem onto your local machine, run `bundle exec rake install`. To release a new version, update the version number in `version.rb`, and then run `bundle exec rake release`, which will create a git tag for the version, push git commits and tags, and push the `.gem` file to [rubygems.org](https://rubygems.org).
61
+
62
+ ## Contributing
63
+
64
+ Bug reports and pull requests are welcome on GitHub at https://github.com/ddrscott/sql_event_analyzer.
65
+
66
+
67
+ ## License
68
+
69
+ The gem is available as open source under the terms of the [MIT License](http://opensource.org/licenses/MIT).
70
+
data/Rakefile ADDED
@@ -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
data/bin/console ADDED
@@ -0,0 +1,14 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ require "bundler/setup"
4
+ require "sql_event_analyzer"
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
data/bin/setup ADDED
@@ -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,3 @@
1
+ class SqlEventAnalyzer
2
+ VERSION = '0.1.0'.freeze
3
+ end
@@ -0,0 +1,55 @@
1
+ <html>
2
+ <head>
3
+ <link href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css" rel="stylesheet" integrity="sha384-BVYiiSIFeK1dGmJRAkycuHAHRg32OmUcww7on3RYdg4Va+PmSTsz/K68vbdEjh4u" crossorigin="anonymous">
4
+ <script src="https://cdnjs.cloudflare.com/ajax/libs/ace/1.2.6/ace.js" integrity="sha256-xrr4HH5eSY+cFz4SH7ja/LaAi9qcEdjMpeMP49/iOLs=" crossorigin="anonymous"></script>
5
+ <style>
6
+ textarea {
7
+ width: 70em;
8
+ font-family: monospace;
9
+ font-size: .8em;
10
+ height: 15em;
11
+ overflow-x: hidden;
12
+ }
13
+ </style>
14
+ </head>
15
+ <body>
16
+ <h2>Overall</h2>
17
+ <div class="row">
18
+ <div class="col-sm-6">
19
+ <table class="table table-condensed">
20
+ <tr>
21
+ <th>uniq callers</th>
22
+ <th>total calls</th>
23
+ <th>total millis</th>
24
+ </tr>
25
+ <tr>
26
+ <td><%= stats.size %></td>
27
+ <td><%= stats.values.inject(0){|agg, r| r[:count] + agg } %> </td>
28
+ <td><%= stats.values.inject(0){|agg, r| r[:duration] + agg }.round %> </td>
29
+ </tr>
30
+ </table>
31
+ </div>
32
+ </div>
33
+ <h2>Details</h2>
34
+ <table class="table table-condensed">
35
+ <tr>
36
+ <th>order</th>
37
+ <th>calls</th>
38
+ <th>millis</th>
39
+ <th>SQL</th>
40
+ <th>Explain</th>
41
+ <th>caller</th>
42
+ </tr>
43
+ <% stats.each do |k, stat| %>
44
+ <tr>
45
+ <td><%= stat[:order] %></td>
46
+ <td><%= stat[:count] %></td>
47
+ <td><%= stat[:duration].round %></td>
48
+ <td><textarea><%= debug_rb(k.first) %><%= pretty_sql(stat[:payload][:sql]) %></textarea></td>
49
+ <td><textarea><%= explain(stat[:payload]) %></textarea></td>
50
+ <td><textarea><%= k.map{|m| trim_caller(m)}.join("\\n") %></textarea></td>
51
+ </tr>
52
+ <% end %>
53
+ </table>
54
+ </body>
55
+ </html>
@@ -0,0 +1,102 @@
1
+ require 'sql_event_analyzer/version'
2
+ require 'active_support/notifications'
3
+ require 'active_record'
4
+
5
+ # Captures Rails SQL events and crunches some statistics
6
+ class SqlEventAnalyzer
7
+
8
+ attr_reader :stats, :name
9
+
10
+ # captures the events and writes out the result
11
+ # @return [File] file containing html output
12
+ def self.start(name: Time.current)
13
+ instance = SqlEventAnalyzer.new(name: name)
14
+
15
+ subscription_name = ActiveSupport::Notifications.subscribe('sql.active_record') do |*args|
16
+ event = ActiveSupport::Notifications::Event.new(*args)
17
+ instance.process_event(event)
18
+ end
19
+
20
+ if block_given?
21
+ begin
22
+ yield
23
+ ensure
24
+ ActiveSupport::Notifications.unsubscribe(subscription_name)
25
+ end
26
+ write_html(instance)
27
+ end
28
+ end
29
+
30
+ def self.write_html(instance)
31
+ output_dir = 'tmp/sql_event_analyzer'
32
+ output_path = File.join(output_dir, "#{instance.name}.html")
33
+ FileUtils.mkdir_p(output_dir)
34
+ html = instance.render_as_html
35
+ File.open(output_path, 'w') {|f| f << html}
36
+ end
37
+
38
+ def initialize(name:)
39
+ @name = name
40
+ @root = Rails.root.to_s
41
+ @stats = {}
42
+ end
43
+
44
+ # @param [ActiveSupport::Notifications::Event] SQL event from Rails
45
+ def process_event(event)
46
+ # Callers from Rails root and not the subscriber itself
47
+ backtrace = caller.select{|line| relevent_caller?(line) }
48
+
49
+ # Initialize a uniq SQL use.
50
+ # `:payload` will always be the first instance of the call.
51
+ stat = @stats[backtrace] ||= {
52
+ order: @stats.size,
53
+ count: 0,
54
+ duration: 0,
55
+ payload: event.payload
56
+ }
57
+
58
+ # increment stats
59
+ stat[:count] += 1
60
+ stat[:duration] += event.duration
61
+ end
62
+
63
+ def render_as_html
64
+ ERB.new(erb_template).result(binding)
65
+ end
66
+
67
+ private
68
+
69
+ def relevent_caller?(c)
70
+ c.start_with?(@root) && !c.start_with?(__FILE__)
71
+ end
72
+
73
+ def pretty_sql(sql)
74
+ sql
75
+ .delete('"')
76
+ .gsub(/\b+(SELECT|FROM|WHERE|GROUP|HAVING|LEFT JOIN|LEFT OUTER JOIN|INNER JOIN|JOIN|RIGHT|ORDER|WINDOW)\b+/, "\n\\1")
77
+ end
78
+
79
+ def explain(payload)
80
+ ActiveRecord::Base.connection.explain(payload[:sql], payload[:binds])
81
+ end
82
+
83
+ def erb_template
84
+ File.read(File.join(__dir__, 'sql_event_analyzer.erb'))
85
+ end
86
+
87
+ def trim_caller(c)
88
+ c.gsub(@root, '.')
89
+ end
90
+
91
+ def debug_rb(c)
92
+ first = trim_caller(c)
93
+ file, line = if first =~ /(.*\.rb):(\d+)/
94
+ [$1, $2]
95
+ end
96
+ rb_src = File.readlines(file)[line.to_i - 1].strip
97
+ <<-EOF.strip_heredoc
98
+ -- #{first}
99
+ -- #{rb_src}
100
+ EOF
101
+ end
102
+ end
@@ -0,0 +1,25 @@
1
+ # coding: utf-8
2
+ lib = File.expand_path('../lib', __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require 'sql_event_analyzer/version'
5
+
6
+ Gem::Specification.new do |spec|
7
+ spec.name = 'sql_event_analyzer'
8
+ spec.version = SqlEventAnalyzer::VERSION
9
+ spec.authors = ['Scott Pierce']
10
+ spec.email = ['ddrscott@gmail.com']
11
+
12
+ spec.summary = 'SQL events collector and statistics generator'
13
+ spec.homepage = 'https://github.com/ddrscott/sql_event_analyzer'
14
+ spec.license = 'MIT'
15
+
16
+ spec.files = `git ls-files -z`.split("\x0").reject { |f| f.match(%r{^(test|spec|features)/}) }
17
+ spec.bindir = 'exe'
18
+ spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
19
+ spec.require_paths = ['lib']
20
+
21
+ spec.add_development_dependency 'bundler', '~> 1.12'
22
+ spec.add_development_dependency 'rake', '~> 10.0'
23
+ spec.add_development_dependency 'rspec', '~> 3.0'
24
+ spec.add_dependency 'rails', '>= 4.0'
25
+ end
metadata ADDED
@@ -0,0 +1,114 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: sql_event_analyzer
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ platform: ruby
6
+ authors:
7
+ - Scott Pierce
8
+ autorequire:
9
+ bindir: exe
10
+ cert_chain: []
11
+ date: 2017-02-24 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: bundler
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - "~>"
18
+ - !ruby/object:Gem::Version
19
+ version: '1.12'
20
+ type: :development
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - "~>"
25
+ - !ruby/object:Gem::Version
26
+ version: '1.12'
27
+ - !ruby/object:Gem::Dependency
28
+ name: rake
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - "~>"
32
+ - !ruby/object:Gem::Version
33
+ version: '10.0'
34
+ type: :development
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - "~>"
39
+ - !ruby/object:Gem::Version
40
+ version: '10.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.0'
48
+ type: :development
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - "~>"
53
+ - !ruby/object:Gem::Version
54
+ version: '3.0'
55
+ - !ruby/object:Gem::Dependency
56
+ name: rails
57
+ requirement: !ruby/object:Gem::Requirement
58
+ requirements:
59
+ - - ">="
60
+ - !ruby/object:Gem::Version
61
+ version: '4.0'
62
+ type: :runtime
63
+ prerelease: false
64
+ version_requirements: !ruby/object:Gem::Requirement
65
+ requirements:
66
+ - - ">="
67
+ - !ruby/object:Gem::Version
68
+ version: '4.0'
69
+ description:
70
+ email:
71
+ - ddrscott@gmail.com
72
+ executables: []
73
+ extensions: []
74
+ extra_rdoc_files: []
75
+ files:
76
+ - ".gitignore"
77
+ - ".rspec"
78
+ - ".rubocop.yml"
79
+ - ".travis.yml"
80
+ - Gemfile
81
+ - LICENSE.txt
82
+ - README.md
83
+ - Rakefile
84
+ - bin/console
85
+ - bin/setup
86
+ - lib/sql_event_analyzer.erb
87
+ - lib/sql_event_analyzer.rb
88
+ - lib/sql_event_analyzer/version.rb
89
+ - sql_event_analyzer.gemspec
90
+ homepage: https://github.com/ddrscott/sql_event_analyzer
91
+ licenses:
92
+ - MIT
93
+ metadata: {}
94
+ post_install_message:
95
+ rdoc_options: []
96
+ require_paths:
97
+ - lib
98
+ required_ruby_version: !ruby/object:Gem::Requirement
99
+ requirements:
100
+ - - ">="
101
+ - !ruby/object:Gem::Version
102
+ version: '0'
103
+ required_rubygems_version: !ruby/object:Gem::Requirement
104
+ requirements:
105
+ - - ">="
106
+ - !ruby/object:Gem::Version
107
+ version: '0'
108
+ requirements: []
109
+ rubyforge_project:
110
+ rubygems_version: 2.4.5.1
111
+ signing_key:
112
+ specification_version: 4
113
+ summary: SQL events collector and statistics generator
114
+ test_files: []