An summarization action taken by an Observer.
The object to call when the action is triggered.
The number of events that cause the action to be called.
The Hash of recent events, keyed by their arrival time.
The schedule that applies to this action.
The number of seconds between calls to the action
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.
# File lib/arborist/observer/summarize.rb, line 21
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
Execute the action block.
# File lib/arborist/observer/summarize.rb, line 75
def call_block
self.block.call( self.event_history.dup )
ensure
self.event_history.clear
end
Returns true
if the number of events in the event history meet
or exceed the count_threshold.
# File lib/arborist/observer/summarize.rb, line 98
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
Call the action for the specified event
.
# File lib/arborist/observer/summarize.rb, line 61
def handle_event( event )
self.record_event( event )
self.call_block if self.should_run?
end
Handle a timing event by calling the block with any events in the history.
# File lib/arborist/observer/summarize.rb, line 68
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 the specified event
in the event history if within the
scheduled period(s).
# File lib/arborist/observer/summarize.rb, line 83
def record_event( event )
return if self.schedule && !self.schedule.now?
self.event_history[ Time.now ] = event
end
Returns true
if the count threshold is exceeded and the
current time is within the action's schedule.
# File lib/arborist/observer/summarize.rb, line 91
def should_run?
return self.count_threshold_exceeded?
end