Class: QueryReviewer::SqlQueryCollection

Inherits:
Object
  • Object
show all
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

Instance Method Summary collapse

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_timeObject

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_hashObject (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_warningsObject



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_severityObject



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_warningsObject



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_warningsObject



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

#queriesObject



13
14
15
# File 'lib/query_reviewer/sql_query_collection.rb', line 13

def queries
  query_hash.values
end

#query_countObject



21
22
23
# File 'lib/query_reviewer/sql_query_collection.rb', line 21

def query_count
  queries.collect(&:count).sum
end

#total_durationObject



17
18
19
# File 'lib/query_reviewer/sql_query_collection.rb', line 17

def total_duration
  self.queries.collect(&:durations).flatten.sum
end

#total_severityObject



83
84
85
# File 'lib/query_reviewer/sql_query_collection.rb', line 83

def total_severity
  warnings.collect(&:severity).sum
end

#total_with_warningsObject



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_warningsObject



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(options)
  @warnings << QueryWarning.new(options)
end

#warningsObject



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_warningsObject



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