Permalink
Browse files

Added html snippet to show db statistics on every page

  • Loading branch information...
selvakn committed Oct 9, 2011
1 parent a3b4243 commit 70be76cd656bd53f46f065cd16b68abd654e7c94
Showing with 38 additions and 9 deletions.
  1. +33 −4 lib/rails_instrument.rb
  2. +1 −1 lib/rails_instrument/version.rb
  3. +4 −4 readme.md
View
@@ -44,15 +44,44 @@ def call(env)
RailsInstrument.reset!
status, headers, body = @app.call(env)
begin
- headers["X-View-Runtime"] = (RailsInstrument.data["process_action.action_controller"][:view_runtime] / 1000).to_s
- headers["X-DB-Runtime"] = (RailsInstrument.data["process_action.action_controller"][:db_runtime] / 1000).to_s
- headers["X-DB-Query-Count"] = RailsInstrument.sql_count.to_s
+ headers["X-View-Runtime"] = (view_runtime / 1000).to_s
+ headers["X-DB-Runtime"] = (db_runtime / 1000).to_s
+ headers["X-DB-Query-Count"] = sql_count.to_s
+
+ if html_reponse?(headers)
+ new_body = Rack::Response.new([], status, headers)
+ body.each do |fragment|
+ new_body.write fragment.gsub("</body>", "#{sql_html_overlay}</body>")
+ end
+ body = new_body
+ end
rescue => e
- # Do nothing
+ headers["X-Rails-Instrument"] = "Error"
end
[status, headers, body]
end
+
+ private
+ def html_reponse?(headers)
+ headers['Content-Type'] =~ /html/
+ end
+
+ def sql_html_overlay
+ %Q{<div style="position: fixed; bottom: 0pt; right: 0pt; cursor: pointer; border-style: solid; border-color: rgb(153, 153, 153); -moz-border-top-colors: none; -moz-border-right-colors: none; -moz-border-bottom-colors: none; -moz-border-left-colors: none; -moz-border-image: none; border-width: 2pt 0pt 0px 2px; padding: 5px; border-radius: 10pt 0pt 0pt 0px; background: none repeat scroll 0% 0% rgba(200, 200, 200, 0.8); color: rgb(119, 119, 119); font-size: 18px;" title="DB query counts / duration (For Development purpose only)">#{sql_count} / #{"%.3f" % db_runtime}</div>}
+ end
+
+ def db_runtime
+ RailsInstrument.data["process_action.action_controller"][:db_runtime]
+ end
+
+ def view_runtime
+ RailsInstrument.data["process_action.action_controller"][:view_runtime]
+ end
+
+ def sql_count
+ RailsInstrument.sql_count
+ end
end
class Engine < ::Rails::Engine #:nodoc:
@@ -1,3 +1,3 @@
module RailsInstrument
- VERSION = "0.0.2"
+ VERSION = "0.0.3"
end
View
@@ -1,7 +1,7 @@
# Rails Instrument
This gem is a middleware which add some instrumentation details like db
-runtime, view runtime, numner of sqls fired for each request in the
+runtime, view runtime, number of sqls fired on each request in the
response headers.
## Response headers added
@@ -20,12 +20,12 @@ Install the latest stable release:
In Rails >= 3, add it to your Gemfile:
``` ruby
-gem 'carrierwave'
+gem 'rails_instrument'
```
## TODO
-* Create chrome extension to show this information inline in the page
+* <strike>Create chrome extension to show this information inline in the page</strike>. Implemented as html fragment added by the middleware to html response.
* Add helper methods for tests. Ex: The number of sqls fired can be
asserted. - wip
-* Add tests coverage.
+* Add test coverage.

0 comments on commit 70be76c

Please sign in to comment.