Permalink
Browse files

minor cleanup

  • Loading branch information...
1 parent f3003ad commit 48c4474390e5a5e773768dcf33f4a2ed4b371c80 @dan-manges committed Aug 24, 2008
Showing with 39 additions and 54 deletions.
  1. +5 −1 CHANGELOG
  2. +11 −1 README.rdoc
  3. +0 −1 install.rb
  4. +1 −8 lib/query_stats.rb
  5. +0 −4 lib/query_stats/helper.rb
  6. +3 −7 lib/query_stats/holder.rb
  7. +10 −15 lib/query_stats/labeler.rb
  8. +8 −10 lib/query_stats/logger.rb
  9. +1 −7 lib/query_stats/recorder.rb
View
6 CHANGELOG
@@ -1,5 +1,9 @@
= Query Stats Change Log
+* July 12, 2008
+ * Rails 2.0 support
+ * Give statistics in response header
+
* February 14, 2007
* Fixed test_helper.rb so tests run without having the plugin in a Rails project.
* No longer need to include QueryStats in ApplicationController
@@ -8,4 +12,4 @@
* September 22, 2006
* Fixed bug causing project test methods to fail when using assigns(:variable)
- * Fixed bug which set label to :select after :columns, QueryStatsHolder#with_label will successfully ignore column queries.
+ * Fixed bug which set label to :select after :columns, QueryStatsHolder#with_label will successfully ignore column queries.
View
12 README.rdoc
@@ -22,9 +22,19 @@ QueryStats adds the number of queries to the log in the DB section:
* X-QueryCount will be set to the number of queries
* X-QueryRuntime will be set to the db runtime
-== Contributors
+== Requirements
+
+Tests pass with:
+* Rails 1.2.6: sqlite3
+* Rails 2.0.2: sqlite3, mysql
+* Rails 2.1.0: sqlite3, mysql
+
+== Maintainer
* Dan Manges
+
+== Contributors
+
* Guillaume Dufloux
== License
View
1 install.rb
@@ -1 +0,0 @@
-puts "Query Stats plugin successfully installed."
View
9 lib/query_stats.rb
@@ -1,9 +1,2 @@
-# QueryStats
-
-# The QueryStats module, which contains other modules for the plugin.
module QueryStats
- # Issues a warning that the QueryStats module no longer needs to be manually included in the ApplicationController.
- def self.included(*args)
- $stderr.puts "You no longer need to include the QueryStats module in your controller."
- end
-end
+end
View
4 lib/query_stats/helper.rb
@@ -1,9 +1,5 @@
-# QueryStats::Helper
-
module QueryStats
- # Helper methods to access query stats data.
module Helper
- # Provides access to the QueryStatsHolder
def queries
ActiveRecord::Base.connection.queries
end
View
10 lib/query_stats/holder.rb
@@ -3,13 +3,9 @@
module QueryStats
# QueryStatsHolder holds data on queries executed.
# QueryStatsHolder#stats will return an array of hashes containing the following keys:
- # * type: The type of SQL query based on methods in ActiveRecord::ConnectionAdapters::AbstractAdapter
- # * begin_db_transaction
- # * columns
- # * commit_db_transaction
+ # * type: The type of SQL query
# * delete
# * insert
- # * rollback_db_transaction
# * select
# * update
# * sql: The SQL executed.
@@ -68,7 +64,7 @@ def stats
# Return the total execution time for all queries in #stats.
def runtime
- @stats.inject(0) { |sum,query| sum + query[:seconds] }
+ @stats.map { |query| query[:seconds] }.sum
end
alias :total_time :runtime
@@ -99,4 +95,4 @@ def query_type_from_sql(sql)
end
end
-end
+end
View
25 lib/query_stats/labeler.rb
@@ -1,35 +1,30 @@
-# QueryStats::Labeler
-
module QueryStats
# Automatically labels queries as to whether they were executed
# in the controller or in the view.
module Labeler
+ def self.included(base)
+ base.class_eval do
+ alias_method_chain :log_processing, :query_stats
+ alias_method_chain :render, :query_stats
+ end
+ end
+
protected
- # When the controller logs processing, QueryStats clears existing statistics.
- def log_processing_with_query_stats()
+ def log_processing_with_query_stats
queries.clear
queries.label = :controller
log_processing_without_query_stats
end
- # When rendering starts, start labeling queries with :view
def render_with_query_stats(*args, &block)
queries.label = :view
render_without_query_stats(*args, &block)
end
- # Returns the QueryStats::Holder for the current database connection.
- def queries #:nodoc:
+ def queries
ActiveRecord::Base.connection.queries
end
- def self.included(base) #:nodoc:
- base.class_eval do
- alias_method_chain :log_processing, :query_stats
- alias_method_chain :render, :query_stats
- end
- end
-
end
-end
+end
View
18 lib/query_stats/logger.rb
@@ -1,10 +1,14 @@
-# QueryStats::Logger
-
module QueryStats
- # Adds the query count to the log;
module Logger
+ def self.included(base)
+ base.class_eval do
+ alias_method_chain :active_record_runtime, :query_stats
+ alias_method_chain :perform_action, :query_stats
+ end
+ end
+
protected
- # Append the query count to the active record data.
+
def active_record_runtime_with_query_stats(*args, &block)
active_record_runtime_without_query_stats(*args, &block) << " #{ActiveRecord::Base.connection.queries.count} queries"
end
@@ -15,11 +19,5 @@ def perform_action_with_query_stats
response.headers["X-QueryRuntime"] = "%.5f" % ActiveRecord::Base.connection.queries.runtime.to_s
end
- def self.included(base) #:nodoc:
- base.class_eval do
- alias_method_chain :active_record_runtime, :query_stats
- alias_method_chain :perform_action, :query_stats
- end
- end
end
end
View
8 lib/query_stats/recorder.rb
@@ -1,21 +1,15 @@
-# QueryStats::Recorder
-
module QueryStats
- # Captures query data from ActiveRecord::Base.
module Recorder
- def self.included(base) #:nodoc:
+ def self.included(base)
base.class_eval do
alias_method_chain :execute, :query_stats
end
end
- # Returns or initializes the QueryStats::Holder
def queries
@query_stats ||= QueryStats::Holder.new
end
- # Executes the query, capturing execution time and logging data to the
- # QueryStats::Holder
def execute_with_query_stats(*args)
result = nil
seconds = Benchmark.realtime do

0 comments on commit 48c4474

Please sign in to comment.