Class: SqlQueryStats::Reporter

Inherits:
ActiveSupport::LogSubscriber
  • Object
show all
Defined in:
lib/sql_query_stats/reporter.rb

Overview

SqlQueryStats Reporter

Constant Summary collapse

DEFAULTS =
{
  total_queries: 0,
  total_duration: 0,
  query_cache_used: 0,
  slowest_query: '',
  slowest_query_duration: 0
}.freeze

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeReporter

Returns a new instance of Reporter.



20
21
22
# File 'lib/sql_query_stats/reporter.rb', line 20

def initialize
  super
end

Instance Attribute Details

#statsObject

Returns the value of attribute stats.



4
5
6
# File 'lib/sql_query_stats/reporter.rb', line 4

def stats
  @stats
end

Class Method Details

.resetObject



14
15
16
17
18
# File 'lib/sql_query_stats/reporter.rb', line 14

def self.reset
  stats = Thread.current['sql_query_stats']
  Thread.current['sql_query_stats'] = DEFAULTS.dup
  stats
end

Instance Method Details

#log(key, value) ⇒ Object



24
25
26
# File 'lib/sql_query_stats/reporter.rb', line 24

def log(key, value)
  Thread.current['sql_query_stats'][key] = value
end

#sql(event) ⇒ Object



32
33
34
35
36
37
38
# File 'lib/sql_query_stats/reporter.rb', line 32

def sql(event)
  log(:total_queries, stats[:total_queries] + 1)
  log(:total_duration, (stats[:total_duration] + event.duration).round(1))

  check_cache_used(event)
  check_slowest_query(event)
end