Skip to content

Commit

Permalink
version 0.1.0
Browse files Browse the repository at this point in the history
  • Loading branch information
triskweline committed Jun 23, 2010
0 parents commit 19ee114
Show file tree
Hide file tree
Showing 40 changed files with 708 additions and 0 deletions.
5 changes: 5 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
doc
pkg
*.gem
.idea

5 changes: 5 additions & 0 deletions .idea/encodings.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

25 changes: 25 additions & 0 deletions .idea/misc.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

9 changes: 9 additions & 0 deletions .idea/modules.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

25 changes: 25 additions & 0 deletions .idea/query_diet.iml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

8 changes: 8 additions & 0 deletions .idea/vcs.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

20 changes: 20 additions & 0 deletions MIT-LICENSE
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
Copyright (c) 2009 Henning Koch

Permission is hereby granted, free of charge, to any person obtaining
a copy of this software and associated documentation files (the
"Software"), to deal in the Software without restriction, including
without limitation the rights to use, copy, modify, merge, publish,
distribute, sublicense, and/or sell copies of the Software, and to
permit persons to whom the Software is furnished to do so, subject to
the following conditions:

The above copyright notice and this permission notice shall be
included in all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
34 changes: 34 additions & 0 deletions README.rdoc
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
= query_diet

query_diet counts the number of database queries for the last request and *subtly* displays it in the upper right corner of your screen.
The display turns red if too many queries are run, or if they take too long.
This is useful to prevent {N + 1 queries}[http://guides.rubyonrails.org/active_record_querying.html#eager-loading-associations] from creeping into your code.

== Installation

Install the gem with
sudo gem install query_diet

There is no further setup necessary after you add the <tt>query_diet</tt> dependency to your Rails application.

We recommend you only use the gem with the development environment.

== Changing warning thresholds

You can define when the counter turns into a red warning. The default threshold is 8 queries and 5000 miliseconds.
To change the default, add the following to <tt>config/initializers/query_diet.rb</tt>:

if defined?(QueryDiet)
QueryDiet::Logger.bad_count = 8
QueryDiet::Logger.bad_time = 5000
end

== Rails 3 compatibility

We cannot guarantee Rails 3 compatibility at this point, but we will upgrade the gem when Rails 3 is released.

=== Credits

Henning Koch

{www.makandra.de}[http://www.makandra.de/]
37 changes: 37 additions & 0 deletions Rakefile
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
require 'rake'
# require 'rake/testtask'
require 'rake/rdoctask'
require 'spec/rake/spectask'

desc 'Default: Run all specs.'
task :default => :spec

desc "Run all specs"
Spec::Rake::SpecTask.new() do |t|
t.spec_opts = ['--options', "\"spec/support/spec.opts\""]
t.spec_files = FileList['spec/**/*_spec.rb']
end

desc 'Generate documentation for the query_diet gem.'
Rake::RDocTask.new(:rdoc) do |rdoc|
rdoc.rdoc_dir = 'rdoc'
rdoc.title = 'query_diet'
rdoc.options << '--line-numbers' << '--inline-source'
rdoc.rdoc_files.include('README')
rdoc.rdoc_files.include('lib/**/*.rb')
end

begin
require 'jeweler'
Jeweler::Tasks.new do |gemspec|
gemspec.name = "query_diet"
gemspec.summary = "Rails database query counter that stays out of your way"
gemspec.email = "github@makandra.de"
gemspec.homepage = "http://github.com/makandra/query_diet"
gemspec.description = "Rails database query counter that stays out of your way"
gemspec.authors = ["Henning Koch"]
end
rescue LoadError
puts "Jeweler not available. Install it with: sudo gem install jeweler"
end

1 change: 1 addition & 0 deletions VERSION
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
0.1.0
5 changes: 5 additions & 0 deletions lib/query_diet.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
require 'query_diet/logger'
require 'query_diet/widget'
require 'query_diet/active_record_ext'
require 'query_diet/action_controller_ext'

15 changes: 15 additions & 0 deletions lib/query_diet/action_controller_ext.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
require_dependency 'application_controller'

ApplicationController.class_eval do

around_filter :query_diet_logging

private

def query_diet_logging
QueryDiet::Logger.reset
yield
QueryDiet::Widget.render(response)
end

end
11 changes: 11 additions & 0 deletions lib/query_diet/active_record_ext.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
ActiveRecord::Base.connection.class.class_eval do

def execute_with_query_diet(query, name = nil)
QueryDiet::Logger.log(query) do
execute_without_query_diet(query, name)
end
end

alias_method_chain :execute, :query_diet

end
41 changes: 41 additions & 0 deletions lib/query_diet/logger.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
module QueryDiet
class Logger
class << self

attr_accessor :count, :time, :bad_count, :bad_time

def reset
self.count = 0
self.time = 0
end

def log(query, &execution)
result = nil
seconds = Benchmark.realtime do
result = execution.call
end
if log_query?(query)
self.time += (seconds * 1000).to_i
self.count += 1
end
result
end

def bad?
count >= bad_count or time >= bad_time
end

private

def log_query?(query)
query =~ /^(select|create|update|delete|insert)\b/i
end

end

reset
self.bad_count = 8
self.bad_time = 5000

end
end
58 changes: 58 additions & 0 deletions lib/query_diet/widget.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
module QueryDiet
class Widget
class << self

def render(response)
body = response.body
if response.content_type == "text/html" && position = body.index('</body>')
body.insert(position, css)
body.insert(position, html)
end
end

private

def css
<<-EOF
<style type="text/css"><!--
div#query_diet {
position: absolute;
top: 0px;
right: 0px;
background-color: black;
color: white;
z-index: 999;
padding: 4px 6px;
font-family; arial, sans-serif;
font-size: 12px;
line-height: 12px;
font-weight: bold;
cursor: pointer;
}
div#query_diet.good {
xbackground-color: #160;
filter:alpha(opacity=30);
-moz-opacity:0.3;
-khtml-opacity: 0.3;
opacity: 0.3;
}
div#query_diet.bad {
background-color: red;
font-size: 16px;
line-height: 16px;
}
--></style>
EOF
end

def html
<<-EOF
<div id="query_diet" class="#{QueryDiet::Logger.bad? ? 'bad' : 'good' }" onclick="this.parentNode.removeChild(this);">
#{QueryDiet::Logger.count} / #{QueryDiet::Logger.time}ms
</div>
EOF
end

end
end
end
Loading

0 comments on commit 19ee114

Please sign in to comment.