Class: QueryCounter::Collector
- Inherits:
-
Object
- Object
- QueryCounter::Collector
- Defined in:
- lib/query_counter/collector.rb
Instance Attribute Summary collapse
-
#notification_events ⇒ Object
readonly
Returns the value of attribute notification_events.
-
#stats ⇒ Object
readonly
Returns the value of attribute stats.
Instance Method Summary collapse
- #add(collector) ⇒ Object
- #count(resource) ⇒ Object
- #events(resource) ⇒ Object
-
#initialize ⇒ Collector
constructor
A new instance of Collector.
- #record(resource, duration, by = 1) ⇒ Object
-
#record_event(resource, event) ⇒ Object
used for debugging resource events.
- #reset ⇒ Object
Constructor Details
#initialize ⇒ Collector
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_events ⇒ Object (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 |
#stats ⇒ Object (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 |
#reset ⇒ Object
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 |