perfectline-rack-bug 0.1.1

Sign up to get free protection for your applications and to get access to all the features.
Files changed (74) hide show
  1. data/.gitignore +4 -0
  2. data/History.txt +0 -0
  3. data/MIT-LICENSE.txt +19 -0
  4. data/README.rdoc +30 -0
  5. data/Rakefile +40 -0
  6. data/VERSION.yml +4 -0
  7. data/lib/rack/bug.rb +24 -0
  8. data/lib/rack/bug/options.rb +90 -0
  9. data/lib/rack/bug/panel.rb +50 -0
  10. data/lib/rack/bug/panel_app.rb +35 -0
  11. data/lib/rack/bug/panels/active_record_panel.rb +46 -0
  12. data/lib/rack/bug/panels/active_record_panel/activerecord_extensions.rb +18 -0
  13. data/lib/rack/bug/panels/cache_panel.rb +51 -0
  14. data/lib/rack/bug/panels/cache_panel/memcache_extension.rb +129 -0
  15. data/lib/rack/bug/panels/cache_panel/panel_app.rb +50 -0
  16. data/lib/rack/bug/panels/cache_panel/stats.rb +97 -0
  17. data/lib/rack/bug/panels/env_panel.rb +25 -0
  18. data/lib/rack/bug/panels/log_panel.rb +39 -0
  19. data/lib/rack/bug/panels/log_panel/rails_extension.rb +11 -0
  20. data/lib/rack/bug/panels/memory_panel.rb +27 -0
  21. data/lib/rack/bug/panels/rails_info_panel.rb +23 -0
  22. data/lib/rack/bug/panels/request_variables_panel.rb +25 -0
  23. data/lib/rack/bug/panels/sql_panel.rb +55 -0
  24. data/lib/rack/bug/panels/sql_panel/panel_app.rb +39 -0
  25. data/lib/rack/bug/panels/sql_panel/query.rb +73 -0
  26. data/lib/rack/bug/panels/sql_panel/sql_extension.rb +11 -0
  27. data/lib/rack/bug/panels/templates_panel.rb +44 -0
  28. data/lib/rack/bug/panels/templates_panel/actionview_extension.rb +12 -0
  29. data/lib/rack/bug/panels/templates_panel/rendering.rb +67 -0
  30. data/lib/rack/bug/panels/templates_panel/trace.rb +34 -0
  31. data/lib/rack/bug/panels/timer_panel.rb +41 -0
  32. data/lib/rack/bug/params_signature.rb +65 -0
  33. data/lib/rack/bug/public/__rack_bug__/bookmarklet.html +10 -0
  34. data/lib/rack/bug/public/__rack_bug__/bookmarklet.js +215 -0
  35. data/lib/rack/bug/public/__rack_bug__/bug.css +186 -0
  36. data/lib/rack/bug/public/__rack_bug__/bug.js +69 -0
  37. data/lib/rack/bug/public/__rack_bug__/jquery-1.3.2.js +4376 -0
  38. data/lib/rack/bug/public/__rack_bug__/spinner.gif +0 -0
  39. data/lib/rack/bug/render.rb +67 -0
  40. data/lib/rack/bug/toolbar.rb +145 -0
  41. data/lib/rack/bug/views/error.html.erb +16 -0
  42. data/lib/rack/bug/views/panels/active_record.html.erb +17 -0
  43. data/lib/rack/bug/views/panels/cache.html.erb +93 -0
  44. data/lib/rack/bug/views/panels/env.html.erb +19 -0
  45. data/lib/rack/bug/views/panels/execute_sql.html.erb +32 -0
  46. data/lib/rack/bug/views/panels/explain_sql.html.erb +32 -0
  47. data/lib/rack/bug/views/panels/log.html.erb +23 -0
  48. data/lib/rack/bug/views/panels/profile_sql.html.erb +32 -0
  49. data/lib/rack/bug/views/panels/rails_info.html.erb +19 -0
  50. data/lib/rack/bug/views/panels/request_variables.html.erb +87 -0
  51. data/lib/rack/bug/views/panels/sql.html.erb +43 -0
  52. data/lib/rack/bug/views/panels/templates.html.erb +7 -0
  53. data/lib/rack/bug/views/panels/timer.html.erb +19 -0
  54. data/lib/rack/bug/views/panels/view_cache.html.erb +19 -0
  55. data/lib/rack/bug/views/redirect.html.erb +16 -0
  56. data/lib/rack/bug/views/toolbar.html.erb +41 -0
  57. data/rack-bug.gemspec +121 -0
  58. data/spec/fixtures/config.ru +8 -0
  59. data/spec/fixtures/dummy_panel.rb +2 -0
  60. data/spec/fixtures/sample_app.rb +29 -0
  61. data/spec/rack/bug/panels/active_record_panel_spec.rb +30 -0
  62. data/spec/rack/bug/panels/cache_panel_spec.rb +159 -0
  63. data/spec/rack/bug/panels/env_panel_spec.rb +24 -0
  64. data/spec/rack/bug/panels/log_panel_spec.rb +25 -0
  65. data/spec/rack/bug/panels/memory_panel_spec.rb +21 -0
  66. data/spec/rack/bug/panels/rails_info_panel_spec.rb +25 -0
  67. data/spec/rack/bug/panels/sql_panel_spec.rb +136 -0
  68. data/spec/rack/bug/panels/templates_panel_spec.rb +71 -0
  69. data/spec/rack/bug/panels/timer_panel_spec.rb +38 -0
  70. data/spec/rack/toolbar_spec.rb +100 -0
  71. data/spec/rcov.opts +1 -0
  72. data/spec/spec.opts +1 -0
  73. data/spec/spec_helper.rb +70 -0
  74. metadata +138 -0
@@ -0,0 +1,4 @@
1
+ coverage
2
+ pkg
3
+ rdoc
4
+ .idea
File without changes
@@ -0,0 +1,19 @@
1
+ Copyright (c) 2009 Bryan Helmkamp
2
+
3
+ Permission is hereby granted, free of charge, to any person obtaining a copy
4
+ of this software and associated documentation files (the "Software"), to deal
5
+ in the Software without restriction, including without limitation the rights
6
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
7
+ copies of the Software, and to permit persons to whom the Software is
8
+ furnished to do so, subject to the following conditions:
9
+
10
+ The above copyright notice and this permission notice shall be included in
11
+ all copies or substantial portions of the Software.
12
+
13
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
14
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
15
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
16
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
17
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
18
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
19
+ THE SOFTWARE.
@@ -0,0 +1,30 @@
1
+ Usage:
2
+
3
+ # as rails plugin
4
+ script/plugin install git://github.com/brynary/rack-bug.git
5
+
6
+ # as system-wide gem dependency
7
+ sudo gem install perfectline-rack-bug
8
+
9
+ # as rails project specific gem dependency
10
+ Rails::Initializer.run do |config|
11
+ config.gem "perfectline-rack-bug", :lib => "rack-bug", :source => "http://gems.github.com"
12
+ end
13
+
14
+ rake gems:install
15
+
16
+ # config/environments/development.rb
17
+ config.middleware.use "Rack::Bug"
18
+
19
+ # add bookmarklet to browser
20
+ open http://RAILS_APP/__rack_bug__/bookmarklet.html
21
+
22
+ Thanks to:
23
+
24
+ Django debug toolbar
25
+ Rails footnotes
26
+ Rack's ShowException middleware
27
+ Oink
28
+ Rack::Cache
29
+
30
+
@@ -0,0 +1,40 @@
1
+ require "rubygems"
2
+ require "rake/gempackagetask"
3
+ require "rake/clean"
4
+ require "spec/rake/spectask"
5
+
6
+ $LOAD_PATH.unshift File.dirname(__FILE__) + '/lib'
7
+
8
+ require "rack/bug"
9
+
10
+ Spec::Rake::SpecTask.new do |t|
11
+ t.spec_opts = ['--options', "\"#{File.dirname(__FILE__)}/spec/spec.opts\""]
12
+ end
13
+
14
+ desc "Run the specs"
15
+ task :default => :spec
16
+
17
+ desc "Run all specs in spec directory with RCov"
18
+ Spec::Rake::SpecTask.new(:rcov) do |t|
19
+ t.spec_opts = ['--options', "\"#{File.dirname(__FILE__)}/spec/spec.opts\""]
20
+ t.rcov = true
21
+ t.rcov_opts = lambda do
22
+ IO.readlines(File.dirname(__FILE__) + "/spec/rcov.opts").map {|l| l.chomp.split " "}.flatten
23
+ end
24
+ end
25
+
26
+ begin
27
+ require 'jeweler'
28
+ Jeweler::Tasks.new do |jewel|
29
+ jewel.name = 'rack-bug'
30
+ jewel.summary = 'Debugging toolbar for Rack applications implemented as middleware'
31
+ jewel.email = 'bryan@brynary.com'
32
+ jewel.homepage = 'http://github.com/brynary/rack-bug'
33
+ jewel.description = 'Debugging toolbar for Rack applications implemented as middleware'
34
+ jewel.authors = ["Bryan Helmkamp"]
35
+ jewel.has_rdoc = true
36
+ jewel.extra_rdoc_files = %w{README.rdoc MIT-LICENSE.txt}
37
+ end
38
+ rescue LoadError
39
+ puts "Jeweler not available. Install it with: sudo gem install technicalpickles-jeweler -s http://gems.github.com"
40
+ end
@@ -0,0 +1,4 @@
1
+ ---
2
+ :major: 0
3
+ :minor: 1
4
+ :patch: 1
@@ -0,0 +1,24 @@
1
+ require "rack"
2
+
3
+ module Rack::Bug
4
+ require "rack/bug/toolbar"
5
+
6
+ class SecurityError < StandardError
7
+ end
8
+
9
+ def self.enable
10
+ Thread.current["rack-bug.enabled"] = true
11
+ end
12
+
13
+ def self.disable
14
+ Thread.current["rack-bug.enabled"] = false
15
+ end
16
+
17
+ def self.enabled?
18
+ Thread.current["rack-bug.enabled"] == true
19
+ end
20
+
21
+ def self.new(*args, &block)
22
+ Toolbar.new(*args, &block)
23
+ end
24
+ end
@@ -0,0 +1,90 @@
1
+ module Rack::Bug
2
+
3
+ module Options
4
+ class << self
5
+ private
6
+ def option_accessor(key)
7
+ define_method(key) { || read_option(key) }
8
+ define_method("#{key}=") { |value| write_option(key, value) }
9
+ define_method("#{key}?") { || !! read_option(key) }
10
+ end
11
+ end
12
+
13
+ option_accessor :secret_key
14
+ option_accessor :ip_masks
15
+ option_accessor :password
16
+ option_accessor :panel_classes
17
+ option_accessor :intercept_redirects
18
+
19
+ # The underlying options Hash. During initialization (or outside of a
20
+ # request), this is a default values Hash. During a request, this is the
21
+ # Rack environment Hash. The default values Hash is merged in underneath
22
+ # the Rack environment before each request is processed.
23
+ def options
24
+ @env || @default_options
25
+ end
26
+
27
+ # Set multiple options.
28
+ def options=(hash={})
29
+ hash.each { |key,value| write_option(key, value) }
30
+ end
31
+
32
+ # Set an option. When +option+ is a Symbol, it is set in the Rack
33
+ # Environment as "rack-cache.option". When +option+ is a String, it
34
+ # exactly as specified. The +option+ argument may also be a Hash in
35
+ # which case each key/value pair is merged into the environment as if
36
+ # the #set method were called on each.
37
+ def set(option, value=self, &block)
38
+ if block_given?
39
+ write_option option, block
40
+ elsif value == self
41
+ self.options = option.to_hash
42
+ else
43
+ write_option option, value
44
+ end
45
+ end
46
+
47
+ private
48
+
49
+ def read_option(key)
50
+ options[option_name(key)]
51
+ end
52
+
53
+ def write_option(key, value)
54
+ options[option_name(key)] = value
55
+ end
56
+
57
+ def option_name(key)
58
+ case key
59
+ when Symbol ; "rack-bug.#{key}"
60
+ when String ; key
61
+ else raise ArgumentError
62
+ end
63
+ end
64
+
65
+ def initialize_options(options={})
66
+ @default_options = {
67
+ 'rack-bug.ip_masks' => [IPAddr.new("127.0.0.1")],
68
+ 'rack-bug.password' => nil,
69
+ 'rack-bug.verbose' => nil,
70
+ 'rack-bug.secret_key' => nil,
71
+ 'rack-bug.intercept_redirects' => false,
72
+ 'rack-bug.panels' => [],
73
+ 'rack-bug.panel_classes' => [
74
+ RailsInfoPanel,
75
+ TimerPanel,
76
+ RequestVariablesPanel,
77
+ EnvPanel,
78
+ SQLPanel,
79
+ ActiveRecordPanel,
80
+ CachePanel,
81
+ TemplatesPanel,
82
+ LogPanel,
83
+ MemoryPanel
84
+ ]
85
+ }
86
+ self.options = options
87
+ end
88
+
89
+ end
90
+ end
@@ -0,0 +1,50 @@
1
+ require "erb"
2
+
3
+ module Rack
4
+ module Bug
5
+
6
+ # Panels are also Rack middleware
7
+ class Panel
8
+ include Render
9
+ include ERB::Util
10
+
11
+ attr_reader :request
12
+
13
+ def initialize(app)
14
+ if panel_app
15
+ @app = Rack::Cascade.new([panel_app, app])
16
+ else
17
+ @app = app
18
+ end
19
+ end
20
+
21
+ def call(env)
22
+ before(env)
23
+ status, headers, body = @app.call(env)
24
+ @request = Request.new(env)
25
+ after(env, status, headers, body)
26
+ env["rack-bug.panels"] << self
27
+ return [status, headers, body]
28
+ end
29
+
30
+ def panel_app
31
+ nil
32
+ end
33
+
34
+ def has_content?
35
+ true
36
+ end
37
+
38
+ def before(env)
39
+ end
40
+
41
+ def after(env, status, headers, body)
42
+ end
43
+
44
+ def render(template)
45
+ end
46
+
47
+ end
48
+
49
+ end
50
+ end
@@ -0,0 +1,35 @@
1
+ require "rack/bug/params_signature"
2
+
3
+ module Rack
4
+ module Bug
5
+
6
+ class PanelApp
7
+ include Rack::Bug::Render
8
+
9
+ attr_reader :request
10
+
11
+ def call(env)
12
+ @request = Rack::Request.new(env)
13
+ dispatch
14
+ end
15
+
16
+ def render_template(*args)
17
+ Rack::Response.new([super]).to_a
18
+ end
19
+
20
+ def params
21
+ @request.GET
22
+ end
23
+
24
+ def not_found
25
+ [404, {}, []]
26
+ end
27
+
28
+ def validate_params
29
+ ParamsSignature.new(request).validate!
30
+ end
31
+
32
+ end
33
+
34
+ end
35
+ end
@@ -0,0 +1,46 @@
1
+ require "rack/bug/panel"
2
+ require "rack/bug/panels/active_record_panel/activerecord_extensions"
3
+
4
+ module Rack
5
+ module Bug
6
+
7
+ class ActiveRecordPanel < Panel
8
+
9
+ def self.record(class_name)
10
+ return unless Rack::Bug.enabled?
11
+ records[class_name] += 1
12
+ end
13
+
14
+ def self.reset
15
+ Thread.current["rack.bug.active_records"] = Hash.new { 0 }
16
+ end
17
+
18
+ def self.records
19
+ Thread.current["rack.bug.active_records"] ||= Hash.new { 0 }
20
+ end
21
+
22
+ def self.total
23
+ records.inject(0) do |memo, (key, value)|
24
+ memo + value
25
+ end
26
+ end
27
+
28
+ def name
29
+ "active_record"
30
+ end
31
+
32
+ def heading
33
+ "#{self.class.total} AR Objects"
34
+ end
35
+
36
+ def content
37
+ records = self.class.records.to_a.sort_by { |key, value| value }.reverse
38
+ result = render_template "panels/active_record", :records => records
39
+ self.class.reset
40
+ result
41
+ end
42
+
43
+ end
44
+
45
+ end
46
+ end
@@ -0,0 +1,18 @@
1
+ if defined?(ActiveRecord)
2
+ ActiveRecord::Base.class_eval do
3
+
4
+ if instance_methods.include?("after_initialize")
5
+ def after_initialize_with_rack_bug
6
+ Rack::Bug::ActiveRecordPanel.record(self.class.base_class.name)
7
+ after_initialize_without_rack_bug
8
+ end
9
+
10
+ alias_method_chain :after_initialize, :rack_bug
11
+ else
12
+ def after_initialize
13
+ Rack::Bug::ActiveRecordPanel.record(self.class.base_class.name)
14
+ end
15
+ end
16
+
17
+ end
18
+ end
@@ -0,0 +1,51 @@
1
+ require "rack/bug/panels/cache_panel/memcache_extension"
2
+
3
+ module Rack
4
+ module Bug
5
+
6
+ class CachePanel < Panel
7
+
8
+ require "rack/bug/panels/cache_panel/stats"
9
+ require "rack/bug/panels/cache_panel/panel_app"
10
+
11
+ def self.record(method, *keys, &block)
12
+ return block.call unless Rack::Bug.enabled?
13
+
14
+ start_time = Time.now
15
+ result = block.call
16
+ total_time = Time.now - start_time
17
+ hit = result.nil? ? false : true
18
+ stats.record_call(method, total_time * 1_000, hit, *keys)
19
+ return result
20
+ end
21
+
22
+ def self.reset
23
+ Thread.current["rack.bug.cache"] = Stats.new
24
+ end
25
+
26
+ def self.stats
27
+ Thread.current["rack.bug.cache"] ||= Stats.new
28
+ end
29
+
30
+ def panel_app
31
+ PanelApp.new
32
+ end
33
+
34
+ def name
35
+ "cache"
36
+ end
37
+
38
+ def heading
39
+ "Cache: %.2fms (#{self.class.stats.queries.size} calls)" % self.class.stats.time
40
+ end
41
+
42
+ def content
43
+ result = render_template "panels/cache", :stats => self.class.stats
44
+ self.class.reset
45
+ return result
46
+ end
47
+
48
+ end
49
+
50
+ end
51
+ end
@@ -0,0 +1,129 @@
1
+ if defined?(Memcached)
2
+ Memcached.class_eval do
3
+
4
+ def set_with_rack_bug(key, value, timeout=0, marshal=true)
5
+ Rack::Bug::CachePanel.record(:set, key) do
6
+ set_without_rack_bug(key, value, timeout, marshal)
7
+ end
8
+ end
9
+
10
+ def add_with_rack_bug(key, value, timeout=0, marshal=true)
11
+ Rack::Bug::CachePanel.record(:add, key) do
12
+ add_without_rack_bug(key, value, timeout, marshal)
13
+ end
14
+ end
15
+
16
+ def increment_with_rack_bug(key, offset=1)
17
+ Rack::Bug::CachePanel.record(:incr, key) do
18
+ increment_without_rack_bug(key, offset)
19
+ end
20
+ end
21
+
22
+ def decrement_with_rack_bug(key, offset=1)
23
+ Rack::Bug::CachePanel.record(:decr, key) do
24
+ decrement_without_rack_bug(key, offset)
25
+ end
26
+ end
27
+
28
+ def replace_with_rack_bug(key, value, timeout=0, marshal=true)
29
+ Rack::Bug::CachePanel.record(:replace, key) do
30
+ replace_without_rack_bug(key, value, timeout, marshal)
31
+ end
32
+ end
33
+
34
+ def append_with_rack_bug(key, value)
35
+ Rack::Bug::CachePanel.record(:append, key) do
36
+ append_without_rack_bug(key, value)
37
+ end
38
+ end
39
+
40
+ def prepend_with_rack_bug(key, value)
41
+ Rack::Bug::CachePanel.record(:prepend, key) do
42
+ prepend_without_rack_bug(key, value)
43
+ end
44
+ end
45
+
46
+ def delete_with_rack_bug(key)
47
+ Rack::Bug::CachePanel.record(:delete, key) do
48
+ delete_without_rack_bug(key)
49
+ end
50
+ end
51
+
52
+ def get_with_rack_bug(keys, marshal=true)
53
+ if keys.is_a? Array
54
+ Rack::Bug::CachePanel.record(:get_multi, *keys) do
55
+ get_without_rack_bug(keys, marshal)
56
+ end
57
+ else
58
+ Rack::Bug::CachePanel.record(:get, keys) do
59
+ get_without_rack_bug(keys, marshal)
60
+ end
61
+ end
62
+ end
63
+
64
+ alias_method_chain :decrement, :rack_bug
65
+ alias_method_chain :get, :rack_bug
66
+ alias_method_chain :increment, :rack_bug
67
+ alias_method_chain :set, :rack_bug
68
+ alias_method_chain :add, :rack_bug
69
+ alias_method_chain :replace, :rack_bug
70
+ alias_method_chain :delete, :rack_bug
71
+ alias_method_chain :prepend, :rack_bug
72
+ alias_method_chain :append, :rack_bug
73
+ end
74
+ end
75
+
76
+ if defined?(MemCache)
77
+ MemCache.class_eval do
78
+
79
+ def decr_with_rack_bug(key, amount = 1)
80
+ Rack::Bug::CachePanel.record(:decr, key) do
81
+ decr_without_rack_bug(key, amount)
82
+ end
83
+ end
84
+
85
+ def get_with_rack_bug(key, raw = false)
86
+ Rack::Bug::CachePanel.record(:get, key) do
87
+ get_without_rack_bug(key, raw)
88
+ end
89
+ end
90
+
91
+ def get_multi_with_rack_bug(*keys)
92
+ Rack::Bug::CachePanel.record(:get_multi, *keys) do
93
+ get_multi_without_rack_bug(*keys)
94
+ end
95
+ end
96
+
97
+ def incr_with_rack_bug(key, amount = 1)
98
+ Rack::Bug::CachePanel.record(:incr, key) do
99
+ incr_without_rack_bug(key, amount)
100
+ end
101
+ end
102
+
103
+ def set_with_rack_bug(key, value, expiry = 0, raw = false)
104
+ Rack::Bug::CachePanel.record(:set, key) do
105
+ set_without_rack_bug(key, value, expiry, raw)
106
+ end
107
+ end
108
+
109
+ def add_with_rack_bug(key, value, expiry = 0, raw = false)
110
+ Rack::Bug::CachePanel.record(:add, key) do
111
+ add_without_rack_bug(key, value, expiry, raw)
112
+ end
113
+ end
114
+
115
+ def delete_with_rack_bug(key, expiry = 0)
116
+ Rack::Bug::CachePanel.record(:delete, key) do
117
+ delete_without_rack_bug(key, expiry)
118
+ end
119
+ end
120
+
121
+ alias_method_chain :decr, :rack_bug
122
+ alias_method_chain :get, :rack_bug
123
+ alias_method_chain :get_multi, :rack_bug
124
+ alias_method_chain :incr, :rack_bug
125
+ alias_method_chain :set, :rack_bug
126
+ alias_method_chain :add, :rack_bug
127
+ alias_method_chain :delete, :rack_bug
128
+ end
129
+ end