Class: QueryCounter::Collector

Inherits:
Object
  • Object
show all
Defined in:
lib/query_counter/collector.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeCollector

Returns a new instance of Collector.



6
7
8
# File 'lib/query_counter/collector.rb', line 6

def initialize
  reset
end

Instance Attribute Details

#notification_eventsObject (readonly)

Returns the value of attribute notification_events.



3
4
5
# File 'lib/query_counter/collector.rb', line 3

def notification_events
  @notification_events
end

#statsObject (readonly)

Returns the value of attribute stats.



3
4
5
# File 'lib/query_counter/collector.rb', line 3

def stats
  @stats
end

Instance Method Details

#add(collector) ⇒ Object



41
42
43
44
45
46
47
48
49
50
51
52
# File 'lib/query_counter/collector.rb', line 41

def add(collector)
  if collector.notification_events
    collector.notification_events.each do |resource, list|
      list.each { |event| record_event(resource, event) }
    end
  else
    collector.stats.each do |resource, stat|
      record(record, stat.time, stat.count)
    end
  end
  self
end

#count(resource) ⇒ Object



33
34
35
# File 'lib/query_counter/collector.rb', line 33

def count(resource)
  (@stats[resource] && @stats[resource].count) || 0
end

#events(resource) ⇒ Object



37
38
39
# File 'lib/query_counter/collector.rb', line 37

def events(resource)
  (@notification_events && @notification_events[resource]) || []
end

#record(resource, duration, by = 1) ⇒ Object



10
11
12
13
# File 'lib/query_counter/collector.rb', line 10

def record(resource, duration, by=1)
  @stats[resource] ||= ::QueryCounter::Stat.new
  @stats[resource].increment(duration, by)
end

#record_event(resource, event) ⇒ Object

used for debugging resource events



16
17
18
19
20
21
22
# File 'lib/query_counter/collector.rb', line 16

def record_event(resource, event)
  record(resource, event.duration)

  @notification_events ||= {}
  @notification_events[resource] ||= []
  @notification_events[resource] << event
end

#resetObject



24
25
26
27
28
29
30
31
# File 'lib/query_counter/collector.rb', line 24

def reset
  if instance_variable_defined?(:@notification_events)
    remove_instance_variable(:@notification_events)
  end

  # just away to return the current stats and reset them back to an empty hash
  @stats.tap { @stats = {} }
end