Class: Footnotes::Notes::QueriesNote

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

Constant Summary collapse

@@alert_db_time =
0.16
@@alert_sql_number =
8
@@sql =
[]
@@include_when_new_relic_installed =
false
@@loaded =
false

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from AbstractNote

close!, #has_fieldset?, included?, #initialize, #javascript, #legend, #link, #onclick, #row, title, #to_sym, #valid?

Constructor Details

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

Class Method Details

.include_when_new_relic_installed=(include_me) ⇒ Object



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

def self.include_when_new_relic_installed=(include_me)
  @@include_when_new_relic_installed = include_me
  load if include_me
end

.loadObject



68
69
70
71
72
73
74
75
76
77
78
79
# File 'lib/rails-footnotes/notes/queries_note.rb', line 68

def self.load
  #only include when NewRelic is installed if configured to do so
  if !loaded and included? and defined?(ActiveRecord)
    ActiveRecord::ConnectionAdapters::AbstractAdapter.send :include, Footnotes::Extensions::AbstractAdapter
    ActiveRecord::ConnectionAdapters.local_constants.each do |adapter|
      next unless adapter =~ /.*[^Abstract]Adapter$/
      next if adapter =~ /(SQLite|Salesforce)Adapter$/
      eval("ActiveRecord::ConnectionAdapters::#{adapter}").send :include, Footnotes::Extensions::QueryAnalyzer
      self.loaded = true
    end
  end
end

.start!(controller) ⇒ Object



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

def self.start!(controller)
  @@sql = []
end

.to_symObject



24
25
26
# File 'lib/rails-footnotes/notes/queries_note.rb', line 24

def self.to_sym
  :queries
end

Instance Method Details

#contentObject



48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
# File 'lib/rails-footnotes/notes/queries_note.rb', line 48

def content
  html = ''

  @@sql.each_with_index do |item, i|
    sql_links = []
    sql_links << "<a href=\"javascript:Footnotes.toggle('qtable_#{i}')\" style=\"color:#A00;\">explain</a>" if item.explain
    sql_links << "<a href=\"javascript:Footnotes.toggle('qtrace_#{i}')\" style=\"color:#00A;\">trace</a>" if item.trace

    html << <<-HTML
  <b id="qtitle_#{i}">#{escape(item.type.to_s.upcase)}</b> (#{sql_links.join(' | ')})<br />
  #{print_name_and_time(item.name, item.time)}<br />
  <span id="explain_#{i}">#{print_query(item.query)}</span><br />
  #{print_explain(i, item.explain) if item.explain}
  <p id="qtrace_#{i}" style="display:none;">#{parse_trace(item.trace) if item.trace}</p><br />
    HTML
  end

  return html
end

#stylesheetObject



39
40
41
42
43
44
45
46
# File 'lib/rails-footnotes/notes/queries_note.rb', line 39

def stylesheet
  <<-STYLESHEET
  #queries_debug_info table td, #queries_debug_info table th{border:1px solid #A00; padding:0 3px; text-align:center;}
  #queries_debug_info table thead, #queries_debug_info table tbody {color:#A00;}
  #queries_debug_info p {background-color:#F3F3FF; border:1px solid #CCC; margin:12px; padding:4px 6px;}
  #queries_debug_info a:hover {text-decoration:underline;}
  STYLESHEET
end

#titleObject



28
29
30
31
32
33
34
35
36
37
# File 'lib/rails-footnotes/notes/queries_note.rb', line 28

def title
  db_time = @@sql.inject(0){|sum, item| sum += item.time }
  query_color = generate_red_color(@@sql.length, alert_sql_number)
  db_color    = generate_red_color(db_time, alert_db_time)

  <<-TITLE
  <span style="background-color:#{query_color}">Queries (#{@@sql.length})</span> 
  <span style="background-color:#{db_color}">DB (#{"%.6f" % db_time}s)</span>
  TITLE
end