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: ExplainResult, PanelApp, ProfileResult, QueryResult

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



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

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

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



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

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

  start_time = Time.now

  result = nil
  begin
    result = yield
  ensure
    queries << QueryResult.new(sql, Time.now - start_time, backtrace, result)
  end

  return result
end

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



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

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

.resetObject



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

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

.total_timeObject



44
45
46
47
48
# File 'lib/rack/bug/panels/sql_panel.rb', line 44

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

Instance Method Details

#contentObject



58
59
60
61
62
# File 'lib/rack/bug/panels/sql_panel.rb', line 58

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

#headingObject



54
55
56
# File 'lib/rack/bug/panels/sql_panel.rb', line 54

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

#nameObject



50
51
52
# File 'lib/rack/bug/panels/sql_panel.rb', line 50

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