Class: Footnotes::Notes::QueriesNote

Inherits:
AbstractNote show all
Defined in:
lib/rails-footnotes/notes/queries_note.rb

Constant Summary collapse

@@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]

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from AbstractNote

close!, #escape, #has_fieldset?, #hash_to_xml_attributes, included?, #initialize, #javascript, #legend, #link, #mount_table, #mount_table_for_hash, #onclick, #prefix?, #row, #stylesheet, title, #to_sym, to_sym, #valid?

Constructor Details

This class inherits a constructor from Footnotes::Notes::AbstractNote

Class Method Details

.query_subscriberObject



15
16
17
# File 'lib/rails-footnotes/notes/queries_note.rb', line 15

def self.query_subscriber
  @@query_subscriber ||= Footnotes::Notes::QuerySubscriber.new(self.orm)
end

.start!(controller) ⇒ Object



11
12
13
# File 'lib/rails-footnotes/notes/queries_note.rb', line 11

def self.start!(controller)
  self.query_subscriber.reset!
end

Instance Method Details

#contentObject



35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
# File 'lib/rails-footnotes/notes/queries_note.rb', line 35

def content
  html = '<table>'
  self.events.each_with_index do |event, index|
    sql_links = []
    sql_links << "<a href=\"javascript:Footnotes.toggle('qtrace_#{index}')\" style=\"color:#00A;\">trace</a>"

    html << <<-HTML
    <tr>
      <td>
        <b id="qtitle_#{index}">#{escape(event.type.to_s.upcase)}</b> (#{sql_links.join(' | ')})
        <p id="qtrace_#{index}" style="display:none;">#{parse_trace(event.trace)}</p><br />
      </td>
      <td>
        <span id="sql_#{index}">#{print_query(event.payload[:sql])}</span>
      </td>
      <td>#{print_name_and_time(event.payload[:name], event.duration)}</td>
    </tr>
    HTML
  end
  html << '</table>'
  return html
end

#eventsObject



19
20
21
# File 'lib/rails-footnotes/notes/queries_note.rb', line 19

def events
  self.class.query_subscriber.events
end

#titleObject



23
24
25
26
27
28
29
30
31
32
33
# File 'lib/rails-footnotes/notes/queries_note.rb', line 23

def title
  queries = self.events.count
  total_time = self.events.map(&:duration).sum
  query_color = alert_color(self.events.count, alert_sql_number)
  db_color    = alert_color(total_time, alert_db_time)

  <<-TITLE
  <span style="background-color:#{query_color}">Queries (#{queries})</span>
  <span style="background-color:#{db_color}">DB (#{"%.3f" % total_time}ms)</span>
  TITLE
end