Pārlūkot izejas kodu

Merge pull request #1059 from cantino/gap_detector_agent

Add GapDetectorAgent, an Agent that alerts when no data has been received
Andrew Cantino 9 gadi atpakaļ
vecāks
revīzija
9aacb8e5f5

+ 67 - 0
app/models/agents/gap_detector_agent.rb

@@ -0,0 +1,67 @@
+module Agents
+  class GapDetectorAgent < Agent
+    default_schedule "every_10m"
+
+    description <<-MD
+      The Gap Detector Agent will watch for holes or gaps in a stream of incoming Events and generate "no data alerts".
+
+      The `value_path` value is a [JSONPath](http://goessner.net/articles/JsonPath/) to a value of interest. If either
+      this value is empty, or no Events are received, during `window_duration_in_days`, an Event will be created with
+      a payload of `message`.
+    MD
+
+    event_description <<-MD
+      Events look like:
+
+          {
+            "message": "No data has been received!",
+            "gap_started_at": "1234567890"
+          }
+    MD
+
+    def validate_options
+      unless options['message'].present?
+        errors.add(:base, "message is required")
+      end
+
+      unless options['window_duration_in_days'].present? && options['window_duration_in_days'].to_f > 0
+        errors.add(:base, "window_duration_in_days must be provided as an integer or floating point number")
+      end
+    end
+
+    def default_options
+      {
+        'window_duration_in_days' => "2",
+        'message' => "No data has been received!"
+      }
+    end
+
+    def working?
+      true
+    end
+
+    def receive(incoming_events)
+      incoming_events.sort_by(&:created_at).each do |event|
+        memory['newest_event_created_at'] ||= 0
+
+        if !interpolated['value_path'].present? || Utils.value_at(event.payload, interpolated['value_path']).present?
+          if event.created_at.to_i > memory['newest_event_created_at']
+            memory['newest_event_created_at'] = event.created_at.to_i
+            memory.delete('alerted_at')
+          end
+        end
+      end
+    end
+
+    def check
+      window = interpolated['window_duration_in_days'].to_f.days.ago
+      if memory['newest_event_created_at'].present? && Time.at(memory['newest_event_created_at']) < window
+        unless memory['alerted_at']
+          memory['alerted_at'] = Time.now.to_i
+          create_event payload: { message: interpolated['message'],
+                                  gap_started_at: memory['newest_event_created_at'] }
+        end
+      end
+    end
+  end
+end

+ 1 - 1
app/models/agents/peak_detector_agent.rb

@@ -7,7 +7,7 @@ module Agents
     description <<-MD
       The Peak Detector Agent will watch for peaks in an event stream.  When a peak is detected, the resulting Event will have a payload message of `message`.  You can include extractions in the message, for example: `I saw a bar of: {{foo.bar}}`, have a look at the [Wiki](https://github.com/cantino/huginn/wiki/Formatting-Events-using-Liquid) for details.
 
-      The `value_path` value is a [JSONPaths](http://goessner.net/articles/JsonPath/) to the value of interest.  `group_by_path` is a hash path that will be used to group values, if present.
+      The `value_path` value is a [JSONPath](http://goessner.net/articles/JsonPath/) to the value of interest.  `group_by_path` is a JSONPath that will be used to group values, if present.
 
       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.
 

+ 112 - 0
spec/models/agents/gap_detector_agent_spec.rb

@@ -0,0 +1,112 @@
+require 'spec_helper'
+
+describe Agents::GapDetectorAgent do
+  let(:valid_params) {
+    {
+      'name' => "my gap detector agent",
+      'options' => {
+        'window_duration_in_days' => "2",
+        'message' => "A gap was found!"
+      }
+    }
+  }
+
+  let(:agent) {
+    _agent = Agents::GapDetectorAgent.new(valid_params)
+    _agent.user = users(:bob)
+    _agent.save!
+    _agent
+  }
+
+  describe 'validation' do
+    before do
+      expect(agent).to be_valid
+    end
+
+    it 'should validate presence of message' do
+      agent.options['message'] = nil
+      expect(agent).not_to be_valid
+    end
+
+    it 'should validate presence of window_duration_in_days' do
+      agent.options['window_duration_in_days'] = ""
+      expect(agent).not_to be_valid
+
+      agent.options['window_duration_in_days'] = "wrong"
+      expect(agent).not_to be_valid
+
+      agent.options['window_duration_in_days'] = "1"
+      expect(agent).to be_valid
+
+      agent.options['window_duration_in_days'] = "0.5"
+      expect(agent).to be_valid
+    end
+  end
+
+  describe '#receive' do
+    it 'records the event if it has a created_at newer than the last seen' do
+      agent.receive([events(:bob_website_agent_event)])
+      expect(agent.memory['newest_event_created_at']).to eq events(:bob_website_agent_event).created_at.to_i
+
+      events(:bob_website_agent_event).created_at = 2.days.ago
+
+      expect {
+        agent.receive([events(:bob_website_agent_event)])
+      }.to_not change { agent.memory['newest_event_created_at'] }
+
+      events(:bob_website_agent_event).created_at = 2.days.from_now
+
+      expect {
+        agent.receive([events(:bob_website_agent_event)])
+      }.to change { agent.memory['newest_event_created_at'] }.to(events(:bob_website_agent_event).created_at.to_i)
+    end
+
+    it 'ignores the event if value_path is present and the value at the path is blank' do
+      agent.options['value_path'] = 'title'
+      agent.receive([events(:bob_website_agent_event)])
+      expect(agent.memory['newest_event_created_at']).to eq events(:bob_website_agent_event).created_at.to_i
+
+      events(:bob_website_agent_event).created_at = 2.days.from_now
+      events(:bob_website_agent_event).payload['title'] = ''
+
+      expect {
+        agent.receive([events(:bob_website_agent_event)])
+      }.to_not change { agent.memory['newest_event_created_at'] }
+
+      events(:bob_website_agent_event).payload['title'] = 'present!'
+
+      expect {
+        agent.receive([events(:bob_website_agent_event)])
+      }.to change { agent.memory['newest_event_created_at'] }.to(events(:bob_website_agent_event).created_at.to_i)
+    end
+
+    it 'clears any previous alert' do
+      agent.memory['alerted_at'] = 2.days.ago.to_i
+      agent.receive([events(:bob_website_agent_event)])
+      expect(agent.memory).to_not have_key('alerted_at')
+    end
+  end
+
+  describe '#check' do
+    it 'alerts once if no data has been received during window_duration_in_days' do
+      agent.memory['newest_event_created_at'] = 1.days.ago.to_i
+
+      expect {
+        agent.check
+      }.to_not change { agent.events.count }
+
+      agent.memory['newest_event_created_at'] = 3.days.ago.to_i
+
+      expect {
+        agent.check
+      }.to change { agent.events.count }.by(1)
+
+      expect(agent.events.last.payload).to eq ({ 'message' => 'A gap was found!',
+                                                 'gap_started_at' => agent.memory['newest_event_created_at'] })
+
+      expect {
+        agent.check
+      }.not_to change { agent.events.count }
+    end
+  end
+end