db_debug 0.0.1
Sign up to get free protection for your applications and to get access to all the features.
- data/MIT-LICENSE +20 -0
- data/README.rdoc +3 -0
- data/Rakefile +27 -0
- data/lib/db_debug/action_controller.rb +41 -0
- data/lib/db_debug/active_record.rb +36 -0
- data/lib/db_debug/engine.rb +5 -0
- data/lib/db_debug/logger.rb +19 -0
- data/lib/db_debug/middleware.rb +26 -0
- data/lib/db_debug/version.rb +3 -0
- data/lib/db_debug.rb +9 -0
- metadata +72 -0
data/MIT-LICENSE
ADDED
@@ -0,0 +1,20 @@
|
|
1
|
+
Copyright 2012 YOURNAME
|
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.rdoc
ADDED
data/Rakefile
ADDED
@@ -0,0 +1,27 @@
|
|
1
|
+
#!/usr/bin/env rake
|
2
|
+
begin
|
3
|
+
require 'bundler/setup'
|
4
|
+
rescue LoadError
|
5
|
+
puts 'You must `gem install bundler` and `bundle install` to run rake tasks'
|
6
|
+
end
|
7
|
+
begin
|
8
|
+
require 'rdoc/task'
|
9
|
+
rescue LoadError
|
10
|
+
require 'rdoc/rdoc'
|
11
|
+
require 'rake/rdoctask'
|
12
|
+
RDoc::Task = Rake::RDocTask
|
13
|
+
end
|
14
|
+
|
15
|
+
RDoc::Task.new(:rdoc) do |rdoc|
|
16
|
+
rdoc.rdoc_dir = 'rdoc'
|
17
|
+
rdoc.title = 'DbDebug'
|
18
|
+
rdoc.options << '--line-numbers'
|
19
|
+
rdoc.rdoc_files.include('README.rdoc')
|
20
|
+
rdoc.rdoc_files.include('lib/**/*.rb')
|
21
|
+
end
|
22
|
+
|
23
|
+
|
24
|
+
|
25
|
+
|
26
|
+
Bundler::GemHelper.install_tasks
|
27
|
+
|
@@ -0,0 +1,41 @@
|
|
1
|
+
module DbDebug
|
2
|
+
module ActionController
|
3
|
+
module Base
|
4
|
+
class << self
|
5
|
+
def included(klass)
|
6
|
+
klass.class_eval do
|
7
|
+
around_filter :hack_to_pass_shit_to_rack_middleware
|
8
|
+
|
9
|
+
protected
|
10
|
+
|
11
|
+
def hack_to_pass_shit_to_rack_middleware
|
12
|
+
klasses = [ActiveRecord::Base, ActiveRecord::Base.class]
|
13
|
+
methods = ["session", "cookies", "params", "request"]
|
14
|
+
|
15
|
+
methods.each do |shenanigan|
|
16
|
+
oops = instance_variable_get(:"@_#{shenanigan}")
|
17
|
+
|
18
|
+
klasses.each do |klass|
|
19
|
+
klass.send(:define_method, shenanigan, proc { oops })
|
20
|
+
end
|
21
|
+
end
|
22
|
+
|
23
|
+
yield
|
24
|
+
|
25
|
+
methods.each do |shenanigan|
|
26
|
+
klasses.each do |klass|
|
27
|
+
klass.send :remove_method, shenanigan
|
28
|
+
end
|
29
|
+
end
|
30
|
+
end
|
31
|
+
end
|
32
|
+
end
|
33
|
+
end
|
34
|
+
end
|
35
|
+
end
|
36
|
+
|
37
|
+
end
|
38
|
+
|
39
|
+
|
40
|
+
|
41
|
+
ActionController::Base.send(:include, DbDebug::ActionController::Base)
|
@@ -0,0 +1,36 @@
|
|
1
|
+
module DbDebug
|
2
|
+
module ActiveRecord
|
3
|
+
module Base
|
4
|
+
class << self
|
5
|
+
def included(klass)
|
6
|
+
klass.extend ClassMethods
|
7
|
+
klass.class_eval do
|
8
|
+
class << self
|
9
|
+
alias_method_chain :find_by_sql, :record_counter
|
10
|
+
end
|
11
|
+
end
|
12
|
+
include InstanceMethods
|
13
|
+
end
|
14
|
+
end
|
15
|
+
|
16
|
+
module ClassMethods
|
17
|
+
def find_by_sql_with_record_counter(*args)
|
18
|
+
resp = nil
|
19
|
+
request.env['db.count'] = request.env['db.count'] + 1
|
20
|
+
time = Benchmark.realtime { resp = find_by_sql_without_record_counter(*args) } * 1000
|
21
|
+
if request.params[:db_debug]
|
22
|
+
Logger.log "DB Query took: #{time} ms"
|
23
|
+
Logger.log caller.delete_if{ |str| str.include?("/gems/") || str.include?("script/rails") }.join("\n")
|
24
|
+
end
|
25
|
+
request.env['db.time'] = request.env['db.time'] + time
|
26
|
+
resp
|
27
|
+
end
|
28
|
+
end
|
29
|
+
|
30
|
+
module InstanceMethods
|
31
|
+
end
|
32
|
+
end
|
33
|
+
end
|
34
|
+
end
|
35
|
+
|
36
|
+
ActiveRecord::Base.send(:include, DbDebug::ActiveRecord::Base)
|
@@ -0,0 +1,19 @@
|
|
1
|
+
module DbDebug
|
2
|
+
module Logger
|
3
|
+
COLOR_MAP = {
|
4
|
+
:black => 30,
|
5
|
+
:red => 31,
|
6
|
+
:green => 32,
|
7
|
+
:yellow => 33,
|
8
|
+
:blue => 34,
|
9
|
+
:magenta => 35,
|
10
|
+
:cyan => 36,
|
11
|
+
:white => 37
|
12
|
+
}
|
13
|
+
|
14
|
+
def self.log color, str
|
15
|
+
code = COLOR_MAP[color]
|
16
|
+
Rails.logger.debug "\033[0;#{code}m#{str}\033[0;#37m"
|
17
|
+
end
|
18
|
+
end
|
19
|
+
end
|
@@ -0,0 +1,26 @@
|
|
1
|
+
module DbDebug
|
2
|
+
class Middleware
|
3
|
+
def initialize(app)
|
4
|
+
@app = app
|
5
|
+
end
|
6
|
+
|
7
|
+
def call(env)
|
8
|
+
env['db.count'] = 0
|
9
|
+
env['db.time'] = 0.0
|
10
|
+
resp = @app.call(env)
|
11
|
+
color = determine_color env['db.count'], env['db.time']
|
12
|
+
Logger.log color, "You hit the database #{env['db.count']} times and it took %5.3f ms" % env['db.time']
|
13
|
+
resp
|
14
|
+
end
|
15
|
+
|
16
|
+
def determine_color count, time
|
17
|
+
if count <= 10 || time < 50.0
|
18
|
+
:green
|
19
|
+
elsif count <= 20 || time < 100.0
|
20
|
+
:yellow
|
21
|
+
else
|
22
|
+
:red
|
23
|
+
end
|
24
|
+
end
|
25
|
+
end
|
26
|
+
end
|
data/lib/db_debug.rb
ADDED
metadata
ADDED
@@ -0,0 +1,72 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: db_debug
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.0.1
|
5
|
+
prerelease:
|
6
|
+
platform: ruby
|
7
|
+
authors:
|
8
|
+
- Phil Monroe
|
9
|
+
- Identified
|
10
|
+
autorequire:
|
11
|
+
bindir: bin
|
12
|
+
cert_chain: []
|
13
|
+
date: 2012-07-18 00:00:00.000000000 Z
|
14
|
+
dependencies:
|
15
|
+
- !ruby/object:Gem::Dependency
|
16
|
+
name: rails
|
17
|
+
requirement: !ruby/object:Gem::Requirement
|
18
|
+
none: false
|
19
|
+
requirements:
|
20
|
+
- - ~>
|
21
|
+
- !ruby/object:Gem::Version
|
22
|
+
version: 3.2.0
|
23
|
+
type: :runtime
|
24
|
+
prerelease: false
|
25
|
+
version_requirements: !ruby/object:Gem::Requirement
|
26
|
+
none: false
|
27
|
+
requirements:
|
28
|
+
- - ~>
|
29
|
+
- !ruby/object:Gem::Version
|
30
|
+
version: 3.2.0
|
31
|
+
description: Description of DbDebug.
|
32
|
+
email:
|
33
|
+
- phil@identified.com
|
34
|
+
executables: []
|
35
|
+
extensions: []
|
36
|
+
extra_rdoc_files: []
|
37
|
+
files:
|
38
|
+
- lib/db_debug/action_controller.rb
|
39
|
+
- lib/db_debug/active_record.rb
|
40
|
+
- lib/db_debug/engine.rb
|
41
|
+
- lib/db_debug/logger.rb
|
42
|
+
- lib/db_debug/middleware.rb
|
43
|
+
- lib/db_debug/version.rb
|
44
|
+
- lib/db_debug.rb
|
45
|
+
- MIT-LICENSE
|
46
|
+
- Rakefile
|
47
|
+
- README.rdoc
|
48
|
+
homepage: https://github.com/Identified/db_debug
|
49
|
+
licenses: []
|
50
|
+
post_install_message:
|
51
|
+
rdoc_options: []
|
52
|
+
require_paths:
|
53
|
+
- lib
|
54
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
55
|
+
none: false
|
56
|
+
requirements:
|
57
|
+
- - ! '>='
|
58
|
+
- !ruby/object:Gem::Version
|
59
|
+
version: '0'
|
60
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
61
|
+
none: false
|
62
|
+
requirements:
|
63
|
+
- - ! '>='
|
64
|
+
- !ruby/object:Gem::Version
|
65
|
+
version: '0'
|
66
|
+
requirements: []
|
67
|
+
rubyforge_project:
|
68
|
+
rubygems_version: 1.8.23
|
69
|
+
signing_key:
|
70
|
+
specification_version: 3
|
71
|
+
summary: Summary of DbDebug.
|
72
|
+
test_files: []
|