Class: Arborist::Observer::Summarize

Inherits:
Object
  • Object
show all
Extended by:
Loggability
Defined in:
lib/arborist/observer/summarize.rb

Overview

An summarization action taken by an Observer.

Instance Attribute Summary collapse

Instance Method Summary collapse

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.

Raises:

  • (ArgumentError)


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

#blockObject (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_thresholdObject (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_historyObject (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

#scheduleObject (readonly)

The schedule that applies to this action.



54
55
56
# File 'lib/arborist/observer/summarize.rb', line 54

def schedule
  @schedule
end

#time_thresholdObject (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_blockObject

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.

Returns:

  • (Boolean)


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_timerObject

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.

Returns:

  • (Boolean)


92
93
94
# File 'lib/arborist/observer/summarize.rb', line 92

def should_run?
	return self.count_threshold_exceeded?
end