Class: ActiveRecord::LogSubscriber

Inherits:
Object
  • Object
show all
Defined in:
lib/time_bandits/monkey_patches/active_record/log_subscriber.rb

Constant Summary collapse

IGNORE_PAYLOAD_NAMES =
["SCHEMA", "EXPLAIN"]

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.call_countObject



15
16
17
18
# File 'lib/time_bandits/monkey_patches/active_record/log_subscriber.rb', line 15

def self.call_count
  Thread.current.thread_variable_get(:active_record_sql_call_count) ||
    Thread.current.thread_variable_set(:active_record_sql_call_count, 0)
end

.call_count=(value) ⇒ Object



11
12
13
# File 'lib/time_bandits/monkey_patches/active_record/log_subscriber.rb', line 11

def self.call_count=(value)
  Thread.current.thread_variable_set(:active_record_sql_call_count, value)
end

.query_cache_hitsObject



24
25
26
27
# File 'lib/time_bandits/monkey_patches/active_record/log_subscriber.rb', line 24

def self.query_cache_hits
  Thread.current.thread_variable_get(:active_record_sql_query_cache_hits) ||
    Thread.current.thread_variable_set(:active_record_sql_query_cache_hits, 0)
end

.query_cache_hits=(value) ⇒ Object



20
21
22
# File 'lib/time_bandits/monkey_patches/active_record/log_subscriber.rb', line 20

def self.query_cache_hits=(value)
  Thread.current.thread_variable_set(:active_record_sql_query_cache_hits, value)
end

.reset_call_countObject



29
30
31
32
33
# File 'lib/time_bandits/monkey_patches/active_record/log_subscriber.rb', line 29

def self.reset_call_count
  calls = call_count
  self.call_count = 0
  calls
end

.reset_query_cache_hitsObject



35
36
37
38
39
# File 'lib/time_bandits/monkey_patches/active_record/log_subscriber.rb', line 35

def self.reset_query_cache_hits
  hits = query_cache_hits
  self.query_cache_hits = 0
  hits
end

Instance Method Details

#sql(event) ⇒ Object



42
43
44
45
46
47
48
49
50
51
52
53
54
# File 'lib/time_bandits/monkey_patches/active_record/log_subscriber.rb', line 42

def sql(event)
  payload = event.payload

  self.class.runtime += event.duration
  self.class.call_count += 1
  self.class.query_cache_hits += 1 if payload[:cached] || payload[:name] == "CACHE"

  return unless logger.debug?

  return if IGNORE_PAYLOAD_NAMES.include?(payload[:name])

  log_sql_statement(payload, event)
end