Class: Arborist::Observer::Summarize
- Inherits:
-
Object
- Object
- Arborist::Observer::Summarize
- Extended by:
- Loggability
- Defined in:
- lib/arborist/observer/summarize.rb
Overview
An summarization action taken by an Observer.
Instance Attribute Summary collapse
-
#block ⇒ Object
readonly
The object to #call when the action is triggered.
-
#count_threshold ⇒ Object
readonly
The number of events that cause the action to be called.
-
#event_history ⇒ Object
readonly
The Hash of recent events, keyed by their arrival time.
-
#schedule ⇒ Object
readonly
The schedule that applies to this action.
-
#time_threshold ⇒ Object
readonly
The number of seconds between calls to the action.
Instance Method Summary collapse
-
#call_block ⇒ Object
Execute the action block.
-
#count_threshold_exceeded? ⇒ Boolean
Returns
true
if the number of events in the event history meet or exceed the #count_threshold. -
#handle_event(event) ⇒ Object
Call the action for the specified
event
. -
#initialize(every: 0, count: 0, during: nil, &block) ⇒ Summarize
constructor
Create a new Summary that will call the specified
block
during
the given schedule,every
specified number of seconds orcount
events, whichever is sooner. -
#on_timer ⇒ Object
Handle a timing event by calling the block with any events in the history.
-
#record_event(event) ⇒ Object
Record the specified
event
in the event history if within the scheduled period(s). -
#should_run? ⇒ Boolean
Returns
true
if the count threshold is exceeded and the current time is within the action’s schedule.
Constructor Details
#initialize(every: 0, count: 0, during: nil, &block) ⇒ Summarize
Create a new Summary that will call the specified block
during
the given schedule, every
specified number of seconds or count
events, whichever is sooner.
22 23 24 25 26 27 28 29 30 31 32 33 |
# File 'lib/arborist/observer/summarize.rb', line 22 def initialize( every: 0, count: 0, during: nil, &block ) raise ArgumentError, "Summarize requires a block" unless block raise ArgumentError, "Summarize requires a value for `every` or `count`." if every.zero? && count.zero? @time_threshold = every @count_threshold = count @schedule = Schedulability::Schedule.parse( during ) if during @block = block @event_history = {} end |
Instance Attribute Details
#block ⇒ Object (readonly)
The object to #call when the action is triggered.
42 43 44 |
# File 'lib/arborist/observer/summarize.rb', line 42 def block @block end |
#count_threshold ⇒ Object (readonly)
The number of events that cause the action to be called.
50 51 52 |
# File 'lib/arborist/observer/summarize.rb', line 50 def count_threshold @count_threshold end |
#event_history ⇒ Object (readonly)
The Hash of recent events, keyed by their arrival time.
58 59 60 |
# File 'lib/arborist/observer/summarize.rb', line 58 def event_history @event_history end |
#schedule ⇒ Object (readonly)
The schedule that applies to this action.
54 55 56 |
# File 'lib/arborist/observer/summarize.rb', line 54 def schedule @schedule end |
#time_threshold ⇒ Object (readonly)
The number of seconds between calls to the action
46 47 48 |
# File 'lib/arborist/observer/summarize.rb', line 46 def time_threshold @time_threshold end |
Instance Method Details
#call_block ⇒ Object
Execute the action block.
76 77 78 79 80 |
# File 'lib/arborist/observer/summarize.rb', line 76 def call_block self.block.call( self.event_history.dup ) ensure self.event_history.clear end |
#count_threshold_exceeded? ⇒ Boolean
Returns true
if the number of events in the event history meet or exceed the #count_threshold.
99 100 101 102 103 |
# File 'lib/arborist/observer/summarize.rb', line 99 def count_threshold_exceeded? return false if self.count_threshold.zero? self.log.debug "Event history has %d events" % [ self.event_history.size ] return self.event_history.size >= self.count_threshold end |
#handle_event(event) ⇒ Object
Call the action for the specified event
.
62 63 64 65 |
# File 'lib/arborist/observer/summarize.rb', line 62 def handle_event( event ) self.record_event( event ) self.call_block if self.should_run? end |
#on_timer ⇒ Object
Handle a timing event by calling the block with any events in the history.
69 70 71 72 |
# File 'lib/arborist/observer/summarize.rb', line 69 def on_timer( * ) self.log.debug "Timer event: %d pending event/s" % [ self.event_history.size ] self.call_block unless self.event_history.empty? end |
#record_event(event) ⇒ Object
Record the specified event
in the event history if within the scheduled period(s).
84 85 86 87 |
# File 'lib/arborist/observer/summarize.rb', line 84 def record_event( event ) return if self.schedule && !self.schedule.now? self.event_history[ Time.now ] = event end |
#should_run? ⇒ Boolean
Returns true
if the count threshold is exceeded and the current time is within the action’s schedule.
92 93 94 |
# File 'lib/arborist/observer/summarize.rb', line 92 def should_run? return self.count_threshold_exceeded? end |