mirror of
https://github.com/Fishwaldo/huginn.git
synced 2025-03-16 11:51:43 +00:00
* Keep X events to make Digest from Added a configuration option to save a fixed number of events for the Digest. Currently all received events are purged when an event is emitted by this agent. This option allows received events to be remembered and reused for future Digests. * added validate_options for new config Added a check to prevent negative numbers, which would cause breakage. Also gave an upper limit at 999, just to set a sane maximum. * removed unnecessary while loop As suggested by @dsander here: https://github.com/huginn/huginn/pull/2041#discussion_r123965060 * Updated name of feature and description * ensure retained_events is an Integer * typo in check if retained events is int * specs for digest_agent * removed validate as integer * removed check for integer status * Rspec mostly working Got rspec mostly working. The #'working?' check for events received in last few days is failing for me, but it was failing for other agents too on my setup. Likely a local problem, so lets see if this passes the automated builds. * removed comments ack. forgot about those
70 lines
2.7 KiB
Ruby
70 lines
2.7 KiB
Ruby
module Agents
|
|
class DigestAgent < Agent
|
|
include FormConfigurable
|
|
|
|
default_schedule "6am"
|
|
|
|
description <<-MD
|
|
The Digest Agent collects any Events sent to it and emits them as a single event.
|
|
|
|
The resulting Event will have a payload message of `message`. You can use liquid templating in the `message`, have a look at the [Wiki](https://github.com/cantino/huginn/wiki/Formatting-Events-using-Liquid) for details.
|
|
|
|
Set `expected_receive_period_in_days` to the maximum amount of time that you'd expect to pass between Events being received by this Agent.
|
|
|
|
If `retained_events` is set to 0 (the default), all received events are cleared after a digest is sent. Set `retained_events` to a value larger than 0 to keep a certain number of events around on a rolling basis to re-send in future digests.
|
|
|
|
For instance, say `retained_events` is set to 3 and the Agent has received Events `5`, `4`, and `3`. When a digest is sent, Events `5`, `4`, and `3` are retained for a future digest. After Event `6` is received, the next digest will contain Events `6`, `5`, and `4`.
|
|
MD
|
|
|
|
event_description <<-MD
|
|
Events look like this:
|
|
|
|
{
|
|
"events": [ event list ],
|
|
"message": "Your message"
|
|
}
|
|
MD
|
|
|
|
def default_options
|
|
{
|
|
"expected_receive_period_in_days" => "2",
|
|
"message" => "{{ events | map: 'message' | join: ',' }}",
|
|
"retained_events" => "0"
|
|
}
|
|
end
|
|
|
|
form_configurable :message, type: :text
|
|
form_configurable :expected_receive_period_in_days
|
|
form_configurable :retained_events
|
|
|
|
def validate_options
|
|
errors.add(:base, 'retained_events must be 0 to 999') unless options['retained_events'].to_i >= 0 && options['retained_events'].to_i < 1000
|
|
end
|
|
|
|
def working?
|
|
last_receive_at && last_receive_at > interpolated["expected_receive_period_in_days"].to_i.days.ago && !recent_error_logs?
|
|
end
|
|
|
|
def receive(incoming_events)
|
|
self.memory["queue"] ||= []
|
|
incoming_events.each do |event|
|
|
self.memory["queue"] << event.id
|
|
end
|
|
if interpolated["retained_events"].to_i > 0 && memory["queue"].length > interpolated["retained_events"].to_i
|
|
memory["queue"].shift(memory["queue"].length - interpolated["retained_events"].to_i)
|
|
end
|
|
end
|
|
|
|
def check
|
|
if self.memory["queue"] && self.memory["queue"].length > 0
|
|
events = received_events.where(id: self.memory["queue"]).order(id: :asc).to_a
|
|
payload = { "events" => events.map { |event| event.payload } }
|
|
payload["message"] = interpolated(payload)["message"]
|
|
create_event :payload => payload
|
|
if interpolated["retained_events"].to_i == 0
|
|
self.memory["queue"] = []
|
|
end
|
|
end
|
|
end
|
|
end
|
|
end
|