class QueryReviewer::SqlQueryCollection

a collection of SQL SELECT queries

Constants

COMMANDS

Attributes

overhead_time[RW]
query_hash[R]

Public Class Methods

new(query_hash = {}) click to toggle source
# File lib/query_reviewer/sql_query_collection.rb, line 8
def initialize(query_hash = {})
  @query_hash = query_hash
  @overhead_time = 0.0
end

Public Instance Methods

analyze!() click to toggle source
# 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() click to toggle source
# File lib/query_reviewer/sql_query_collection.rb, line 66
def collection_warnings
  @warnings
end
count_of_command(command, only_no_warnings = false) click to toggle source
# 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) click to toggle source
# 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() click to toggle source
# 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) click to toggle source
# 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() click to toggle source
# 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() click to toggle source
# 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() click to toggle source
# File lib/query_reviewer/sql_query_collection.rb, line 13
def queries
  query_hash.values
end
query_count() click to toggle source
# File lib/query_reviewer/sql_query_collection.rb, line 21
def query_count
  queries.collect(&:count).sum
end
total_duration() click to toggle source
# File lib/query_reviewer/sql_query_collection.rb, line 17
def total_duration
  self.queries.collect(&:durations).flatten.sum
end
total_severity() click to toggle source
# File lib/query_reviewer/sql_query_collection.rb, line 83
def total_severity
  warnings.collect(&:severity).sum
end
total_with_warnings() click to toggle source
# File lib/query_reviewer/sql_query_collection.rb, line 87
def total_with_warnings
  queries.select(&:has_warnings?).length
end
total_without_warnings() click to toggle source
# File lib/query_reviewer/sql_query_collection.rb, line 91
def total_without_warnings
  queries.length - total_with_warnings
end
warn(options) click to toggle source
# File lib/query_reviewer/sql_query_collection.rb, line 54
def warn(options)
  @warnings << QueryWarning.new(options)
end
warnings() click to toggle source
# 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() click to toggle source
# 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