Class: QueryReviewer::SqlQueryCollection
- Inherits:
-
Object
- Object
- QueryReviewer::SqlQueryCollection
- Defined in:
- lib/query_reviewer/sql_query_collection.rb
Overview
a collection of SQL SELECT queries
Constant Summary collapse
- COMMANDS =
%w(SELECT DELETE INSERT UPDATE)
Instance Attribute Summary collapse
-
#overhead_time ⇒ Object
Returns the value of attribute overhead_time.
-
#query_hash ⇒ Object
readonly
Returns the value of attribute query_hash.
Instance Method Summary collapse
- #analyze! ⇒ Object
- #collection_warnings ⇒ Object
- #count_of_command(command, only_no_warnings = false) ⇒ Object
- #find_or_create_sql_query(sql, cols, run_time, profile, command, affected_rows) ⇒ Object
-
#initialize(query_hash = {}) ⇒ SqlQueryCollection
constructor
A new instance of SqlQueryCollection.
- #max_severity ⇒ Object
- #only_of_command(command, only_no_warnings = false) ⇒ Object
- #percent_with_warnings ⇒ Object
- #percent_without_warnings ⇒ Object
- #queries ⇒ Object
- #query_count ⇒ Object
- #total_duration ⇒ Object
- #total_severity ⇒ Object
- #total_with_warnings ⇒ Object
- #total_without_warnings ⇒ Object
- #warn(options) ⇒ Object
- #warnings ⇒ Object
- #without_warnings ⇒ Object
Constructor Details
#initialize(query_hash = {}) ⇒ SqlQueryCollection
Returns a new instance of SqlQueryCollection.
8 9 10 11 |
# File 'lib/query_reviewer/sql_query_collection.rb', line 8 def initialize(query_hash = {}) @query_hash = query_hash @overhead_time = 0.0 end |
Instance Attribute Details
#overhead_time ⇒ Object
Returns the value of attribute overhead_time.
7 8 9 |
# File 'lib/query_reviewer/sql_query_collection.rb', line 7 def overhead_time @overhead_time end |
#query_hash ⇒ Object (readonly)
Returns the value of attribute query_hash.
6 7 8 |
# File 'lib/query_reviewer/sql_query_collection.rb', line 6 def query_hash @query_hash end |
Instance Method Details
#analyze! ⇒ Object
25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 |
# File 'lib/query_reviewer/sql_query_collection.rb', line 25 def analyze! self.queries.collect(&:analyze!) @warnings = [] crit_severity = 9# ((QueryReviewer::CONFIGURATION["critical_severity"] + 10)/2).to_i warn_severity = QueryReviewer::CONFIGURATION["critical_severity"] - 1 # ((QueryReviewer::CONFIGURATION["warn_severity"] + QueryReviewer::CONFIGURATION["critical_severity"])/2).to_i COMMANDS.each do |command| count = count_of_command(command) if count > QueryReviewer::CONFIGURATION["critical_#{command.downcase}_count"] warn(:severity => crit_severity, :problem => "#{count} #{command} queries on this page", :description => "Too many #{command} queries can severely slow down a page") elsif count > QueryReviewer::CONFIGURATION["warn_#{command.downcase}_count"] warn(:severity => warn_severity, :problem => "#{count} #{command} queries on this page", :description => "Too many #{command} queries can slow down a page") end end end |
#collection_warnings ⇒ Object
66 67 68 |
# File 'lib/query_reviewer/sql_query_collection.rb', line 66 def collection_warnings @warnings end |
#count_of_command(command, only_no_warnings = false) ⇒ Object
79 80 81 |
# File 'lib/query_reviewer/sql_query_collection.rb', line 79 def count_of_command(command, only_no_warnings = false) only_of_command(command, only_no_warnings).collect(&:durations).collect(&:size).sum end |
#find_or_create_sql_query(sql, cols, run_time, profile, command, affected_rows) ⇒ Object
43 44 45 46 47 48 49 50 51 52 |
# File 'lib/query_reviewer/sql_query_collection.rb', line 43 def find_or_create_sql_query(sql, cols, run_time, profile, command, affected_rows) sanitized_sql = SqlQuery.sanitize_strings_and_numbers_from_sql(sql) trace = SqlQuery.generate_full_trace(Kernel.caller) key = [sanitized_sql, trace] if query_hash[key] query_hash[key].add(sql, run_time, profile) else query_hash[key] = SqlQuery.new(sql, cols, trace, run_time, profile, command, affected_rows, sanitized_sql) end end |
#max_severity ⇒ Object
70 71 72 |
# File 'lib/query_reviewer/sql_query_collection.rb', line 70 def max_severity warnings.empty? && collection_warnings.empty? ? 0 : [warnings.empty? ? 0 : warnings.collect(&:severity).flatten.max, collection_warnings.empty? ? 0 : collection_warnings.collect(&:severity).flatten.max].max end |
#only_of_command(command, only_no_warnings = false) ⇒ Object
74 75 76 77 |
# File 'lib/query_reviewer/sql_query_collection.rb', line 74 def only_of_command(command, only_no_warnings = false) qs = only_no_warnings ? self.without_warnings : self.queries qs.select{|q| q.command == command} end |
#percent_with_warnings ⇒ Object
95 96 97 |
# File 'lib/query_reviewer/sql_query_collection.rb', line 95 def percent_with_warnings queries.empty? ? 0 : (100.0 * total_with_warnings / queries.length).to_i end |
#percent_without_warnings ⇒ Object
99 100 101 |
# File 'lib/query_reviewer/sql_query_collection.rb', line 99 def percent_without_warnings queries.empty? ? 0 : (100.0 * total_without_warnings / queries.length).to_i end |
#queries ⇒ Object
13 14 15 |
# File 'lib/query_reviewer/sql_query_collection.rb', line 13 def queries query_hash.values end |
#query_count ⇒ Object
21 22 23 |
# File 'lib/query_reviewer/sql_query_collection.rb', line 21 def query_count queries.collect(&:count).sum end |
#total_duration ⇒ Object
17 18 19 |
# File 'lib/query_reviewer/sql_query_collection.rb', line 17 def total_duration self.queries.collect(&:durations).flatten.sum end |
#total_severity ⇒ Object
83 84 85 |
# File 'lib/query_reviewer/sql_query_collection.rb', line 83 def total_severity warnings.collect(&:severity).sum end |
#total_with_warnings ⇒ Object
87 88 89 |
# File 'lib/query_reviewer/sql_query_collection.rb', line 87 def total_with_warnings queries.select(&:has_warnings?).length end |
#total_without_warnings ⇒ Object
91 92 93 |
# File 'lib/query_reviewer/sql_query_collection.rb', line 91 def total_without_warnings queries.length - total_with_warnings end |
#warn(options) ⇒ Object
54 55 56 |
# File 'lib/query_reviewer/sql_query_collection.rb', line 54 def warn() @warnings << QueryWarning.new() end |
#warnings ⇒ Object
58 59 60 |
# File 'lib/query_reviewer/sql_query_collection.rb', line 58 def warnings self.queries.collect(&:warnings).flatten.sort{|a,b| b.severity <=> a.severity} end |
#without_warnings ⇒ Object
62 63 64 |
# File 'lib/query_reviewer/sql_query_collection.rb', line 62 def without_warnings self.queries.reject{|q| q.has_warnings?}.sort{|a,b| b.duration <=> a.duration} end |