Class: Rack::Bug::SQLPanel

Inherits:
Panel
  • Object
show all
Defined in:
lib/rack/bug/panels/sql_panel.rb,
lib/rack/bug/panels/sql_panel/query.rb,
lib/rack/bug/panels/sql_panel/panel_app.rb

Defined Under Namespace

Classes: PanelApp, Query

Instance Attribute Summary

Attributes inherited from Panel

#request

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from Panel

#after, #before, #call, #has_content?, #initialize, #render

Methods included from Render

#compile, #compile!, #compiled_source, #method_name, #method_name_without_locals, #render_template, #signed_params

Constructor Details

This class inherits a constructor from Rack::Bug::Panel

Class Method Details

.queriesObject



35
36
37
# File 'lib/rack/bug/panels/sql_panel.rb', line 35

def self.queries
  Thread.current["rack.test.queries"] ||= []
end

.record(sql, backtrace = [], &block) ⇒ Object



16
17
18
19
20
21
22
23
24
# File 'lib/rack/bug/panels/sql_panel.rb', line 16

def self.record(sql, backtrace = [], &block)
  return block.call unless Rack::Bug.enabled?

  start_time = Time.now
  result = block.call
  queries << Query.new(sql, Time.now - start_time, backtrace)

  return result
end

.record_event(sql, duration, backtrace = []) ⇒ Object



26
27
28
29
# File 'lib/rack/bug/panels/sql_panel.rb', line 26

def self.record_event(sql, duration, backtrace = [])
  return unless Rack::Bug.enabled?
  queries << Query.new(sql, duration, backtrace)
end

.resetObject



31
32
33
# File 'lib/rack/bug/panels/sql_panel.rb', line 31

def self.reset
  Thread.current["rack.test.queries"] = []
end

.total_timeObject



39
40
41
# File 'lib/rack/bug/panels/sql_panel.rb', line 39

def self.total_time
  (queries.inject(0) { |memo, query| memo + query.time}) * 1_000
end

Instance Method Details

#contentObject



51
52
53
54
55
# File 'lib/rack/bug/panels/sql_panel.rb', line 51

def content
  result = render_template "panels/sql", :queries => self.class.queries
  self.class.reset
  return result
end

#headingObject



47
48
49
# File 'lib/rack/bug/panels/sql_panel.rb', line 47

def heading
  "#{self.class.queries.size} Queries (%.2fms)" % self.class.total_time
end

#nameObject



43
44
45
# File 'lib/rack/bug/panels/sql_panel.rb', line 43

def name
  "sql"
end

#panel_appObject



12
13
14
# File 'lib/rack/bug/panels/sql_panel.rb', line 12

def panel_app
  PanelApp.new
end