module Footnotes
module Notes
class QueriesNote < AbstractNote
cattr_accessor :alert_db_time, :alert_sql_number, :orm, :ignored_regexps, :instance_writter => false
@@alert_db_time = 16.0
@@alert_sql_number = 8
@@query_subscriber = nil
@@orm = [:active_record, :data_mapper]
@@ignored_regexps = [%r{(pg_table|pg_attribute|pg_namespace|show\stables|pragma|sqlite_master)}i]
def self.start!(controller)
self.query_subscriber.reset!
end
def self.query_subscriber
@@query_subscriber ||= Footnotes::Notes::QuerySubscriber.new(self.orm)
end
def events
self.class.query_subscriber.events
end
def title
queries = self.events.count
total_time = self.events.map(&:duration).sum
query_color = generate_red_color(self.events.count, alert_sql_number)
db_color = generate_red_color(total_time, alert_db_time)
<<-TITLE
Queries (#{queries})
DB (#{"%.3f" % total_time}ms)
TITLE
end
def content
html = ''
self.events.each_with_index do |event, index|
sql_links = []
sql_links << "trace"
html << <<-HTML
#{escape(event.type.to_s.upcase)} (#{sql_links.join(' | ')})
#{print_query(event.payload[:sql])}
#{print_name_and_time(event.payload[:name], event.duration / 1000.0)}