class SqlQueryStats::Reporter

SqlQueryStats Reporter

Constants

DEFAULTS

Attributes

stats[RW]

Public Class Methods

new() click to toggle source
Calls superclass method
# File lib/sql_query_stats/reporter.rb, line 20
def initialize
  super
end
reset() click to toggle source
# 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

Public Instance Methods

log(key, value) click to toggle source
# File lib/sql_query_stats/reporter.rb, line 24
def log(key, value)
  Thread.current['sql_query_stats'][key] = value
end
sql(event) click to toggle source
# 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

Private Instance Methods

check_cache_used(event) click to toggle source
# File lib/sql_query_stats/reporter.rb, line 42
def check_cache_used(event)
  return unless event.payload[:cached]
  log(:query_cache_used, stats[:query_cache_used] + 1)
end
check_slowest_query(event) click to toggle source
# File lib/sql_query_stats/reporter.rb, line 47
def check_slowest_query(event)
  reportable_queries = %w[SELECT INSERT UPDATE]
  return unless event.duration > stats[:slowest_query_duration]
  return unless event.payload[:sql].start_with?(*reportable_queries)

  report_slowest_query(event)
end
report_slowest_query(event) click to toggle source
# File lib/sql_query_stats/reporter.rb, line 55
def report_slowest_query(event)
  duration = stats[:slowest_query_duration] + event.duration

  log(:slowest_query, Sanitizer.sanitize(event.payload[:sql]))
  log(:slowest_query_duration, duration.round(1))
end