return false from working? when an agent's most recent log is an error

Andrew Cantino 10 years ago
parent
commit
7372244d0f

+ 1 - 1
app/controllers/agents_controller.rb

@@ -1,6 +1,6 @@
1 1
 class AgentsController < ApplicationController
2 2
   def index
3
-    @agents = current_user.agents.page(params[:page])
3
+    @agents = current_user.agents.preload(:most_recent_event, :most_recent_log).page(params[:page])
4 4
 
5 5
     respond_to do |format|
6 6
       format.html

+ 9 - 3
app/models/agent.rb

@@ -30,7 +30,9 @@ class Agent < ActiveRecord::Base
30 30
 
31 31
   belongs_to :user, :inverse_of => :agents
32 32
   has_many :events, :dependent => :delete_all, :inverse_of => :agent, :order => "events.id desc"
33
+  has_one  :most_recent_event, :inverse_of => :agent, :class_name => "Event", :order => "events.id desc"
33 34
   has_many :logs, :dependent => :delete_all, :inverse_of => :agent, :class_name => "AgentLog", :order => "agent_logs.id desc"
35
+  has_one  :most_recent_log, :inverse_of => :agent, :class_name => "AgentLog", :order => "agent_logs.id desc"
34 36
   has_many :received_events, :through => :sources, :class_name => "Event", :source => :events, :order => "events.id desc"
35 37
   has_many :links_as_source, :dependent => :delete_all, :foreign_key => "source_id", :class_name => "Link", :inverse_of => :source
36 38
   has_many :links_as_receiver, :dependent => :delete_all, :foreign_key => "receiver_id", :class_name => "Link", :inverse_of => :receiver
@@ -72,9 +74,13 @@ class Agent < ActiveRecord::Base
72 74
     raise "Implement me in your subclass"
73 75
   end
74 76
 
75
-  def event_created_within(seconds)
76
-    last_event = events.first
77
-    last_event && last_event.created_at > seconds.ago && last_event
77
+  def event_created_within(days)
78
+    event = most_recent_event
79
+    event && event.created_at > days.to_i.days.ago && event.payload.present? && event
80
+  end
81
+
82
+  def recent_error_logs?
83
+    most_recent_log.try(:level) == 4
78 84
   end
79 85
 
80 86
   def sources_are_owned

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

@@ -40,7 +40,7 @@ module Agents
40 40
     end
41 41
 
42 42
     def working?
43
-      (event = event_created_within(options[:expected_update_period_in_days].to_i.days)) && event.payload.present?
43
+      event_created_within(options[:expected_update_period_in_days]) && !recent_error_logs?
44 44
     end
45 45
 
46 46
     def validate_options

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

@@ -21,7 +21,7 @@ module Agents
21 21
     end
22 22
 
23 23
     def working?
24
-      last_receive_at && last_receive_at > options[:expected_receive_period_in_days].to_i.days.ago
24
+      last_receive_at && last_receive_at > options[:expected_receive_period_in_days].to_i.days.ago && !recent_error_logs?
25 25
     end
26 26
 
27 27
     def validate_options

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

@@ -63,7 +63,7 @@ module Agents
63 63
     end
64 64
 
65 65
     def working?
66
-      true
66
+      !recent_error_logs?
67 67
     end
68 68
 
69 69
     def value_constructor(value, payload)

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

@@ -43,7 +43,7 @@ module Agents
43 43
     end
44 44
 
45 45
     def working?
46
-      last_receive_at && last_receive_at > options[:expected_receive_period_in_days].to_i.days.ago
46
+      last_receive_at && last_receive_at > options[:expected_receive_period_in_days].to_i.days.ago && !recent_error_logs?
47 47
     end
48 48
 
49 49
     def receive(incoming_events)

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

@@ -16,7 +16,7 @@ module Agents
16 16
     end
17 17
 
18 18
     def working?
19
-      last_receive_at && last_receive_at > options[:expected_receive_period_in_days].to_i.days.ago
19
+      last_receive_at && last_receive_at > options[:expected_receive_period_in_days].to_i.days.ago && !recent_error_logs?
20 20
     end
21 21
 
22 22
     def validate_options

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

@@ -34,7 +34,7 @@ module Agents
34 34
     end
35 35
 
36 36
     def working?
37
-      last_receive_at && last_receive_at > options[:expected_receive_period_in_days].to_i.days.ago
37
+      last_receive_at && last_receive_at > options[:expected_receive_period_in_days].to_i.days.ago && !recent_error_logs?
38 38
     end
39 39
 
40 40
     def receive(incoming_events)

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

@@ -29,7 +29,7 @@ module Agents
29 29
     end
30 30
 
31 31
     def working?
32
-      last_receive_at && last_receive_at > options[:expected_receive_period_in_days].to_i.days.ago
32
+      last_receive_at && last_receive_at > options[:expected_receive_period_in_days].to_i.days.ago && !recent_error_logs?
33 33
     end
34 34
 
35 35
     def translate(text, to, access_token)

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

@@ -42,7 +42,7 @@ module Agents
42 42
     end
43 43
 
44 44
     def working?
45
-      last_receive_at && last_receive_at > options[:expected_receive_period_in_days].to_i.days.ago
45
+      last_receive_at && last_receive_at > options[:expected_receive_period_in_days].to_i.days.ago && !recent_error_logs?
46 46
     end
47 47
 
48 48
     def receive(incoming_events)

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

@@ -58,7 +58,7 @@ module Agents
58 58
     end
59 59
 
60 60
     def working?
61
-      last_receive_at && last_receive_at > options[:expected_receive_period_in_days].to_i.days.ago
61
+      last_receive_at && last_receive_at > options[:expected_receive_period_in_days].to_i.days.ago && !recent_error_logs?
62 62
     end
63 63
 
64 64
     def send_message(message)

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

@@ -29,7 +29,7 @@ module Agents
29 29
     end
30 30
 
31 31
     def working?
32
-      (event = event_created_within(options[:expected_update_period_in_days].to_i.days)) && event.payload.present? && event.payload[:success] == true
32
+      (event = event_created_within(options[:expected_update_period_in_days])) && event.payload[:success] == true && !recent_error_logs?
33 33
     end
34 34
 
35 35
     def default_options

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

@@ -63,7 +63,7 @@ module Agents
63 63
     end
64 64
 
65 65
     def working?
66
-      (event = event_created_within(options[:expected_update_period_in_days].to_i.days)) && event.payload.present?
66
+      event_created_within(options[:expected_update_period_in_days]) && !recent_error_logs?
67 67
     end
68 68
 
69 69
     def default_options

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

@@ -45,7 +45,7 @@ module Agents
45 45
     end
46 46
 
47 47
     def working?
48
-      (event = event_created_within(options[:expected_update_period_in_days].to_i.days)) && event.payload.present?
48
+      event_created_within(options[:expected_update_period_in_days]) && !recent_error_logs?
49 49
     end
50 50
 
51 51
     def default_options

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

@@ -30,7 +30,7 @@ module Agents
30 30
     MD
31 31
 
32 32
     def working?
33
-      (event = event_created_within(2.days)) && event.payload.present?
33
+      event_created_within(2) && !recent_error_logs?
34 34
     end
35 35
 
36 36
     def default_options

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

@@ -41,7 +41,7 @@ module Agents
41 41
     default_schedule "8pm"
42 42
 
43 43
     def working?
44
-      (event = event_created_within(2.days)) && event.payload.present?
44
+      event_created_within(2) && !recent_error_logs?
45 45
     end
46 46
 
47 47
     def wunderground

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

@@ -44,7 +44,7 @@ module Agents
44 44
     UNIQUENESS_LOOK_BACK = 30
45 45
 
46 46
     def working?
47
-      (event = event_created_within(options[:expected_update_period_in_days].to_i.days)) && event.payload.present?
47
+      event_created_within(options[:expected_update_period_in_days]) && !recent_error_logs?
48 48
     end
49 49
 
50 50
     def default_options

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

@@ -28,7 +28,7 @@ module Agents
28 28
     end
29 29
 
30 30
     def working?
31
-      (event = event_created_within(options[:expected_update_period_in_days].to_i.days)) && event.payload.present? && event.payload[:success] == true
31
+      (event = event_created_within(options[:expected_update_period_in_days])) && event.payload[:success] == true && !recent_error_logs?
32 32
     end
33 33
 
34 34
     def default_options

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

@@ -78,7 +78,7 @@ module Agents
78 78
     end
79 79
 
80 80
     def working?
81
-      (event = event_created_within(options[:expected_update_period_in_days].to_i.days)) && event.payload.present?
81
+      event_created_within(options[:expected_update_period_in_days]) && !recent_error_logs?
82 82
     end
83 83
 
84 84
     def default_options

+ 0 - 15
spec/helpers/logs_helper_spec.rb

@@ -1,15 +0,0 @@
1
-require 'spec_helper'
2
-
3
-# Specs in this file have access to a helper object that includes
4
-# the AgentLogsHelper. For example:
5
-#
6
-# describe AgentLogsHelper do
7
-#   describe "string concat" do
8
-#     it "concats two strings with spaces" do
9
-#       expect(helper.concat_strings("this","that")).to eq("this that")
10
-#     end
11
-#   end
12
-# end
13
-describe LogsHelper do
14
-  pending "add some examples to (or delete) #{__FILE__}"
15
-end

+ 1 - 1
spec/models/agents/adioso_agent_spec.rb

@@ -29,7 +29,7 @@ describe Agents::AdiosoAgent do
29 29
 		it "checks if its generating events as scheduled" do
30 30
 			@checker.should_not be_working
31 31
 			@checker.check
32
-			@checker.should be_working
32
+			@checker.reload.should be_working
33 33
 			three_days_from_now = 3.days.from_now
34 34
 			stub(Time).now { three_days_from_now }
35 35
 			@checker.should_not be_working

+ 18 - 0
spec/models/agents/website_agent_spec.rb

@@ -42,6 +42,24 @@ describe Agents::WebsiteAgent do
42 42
     end
43 43
   end
44 44
 
45
+  describe '#working?' do
46
+    it 'checks if events have been received within the expected receive period' do
47
+      @checker.should_not be_working # No events created
48
+      @checker.check
49
+      @checker.reload.should be_working # Just created events
50
+
51
+      @checker.error "oh no!"
52
+      @checker.reload.should_not be_working # The most recent log is an error
53
+
54
+      @checker.log "ok now"
55
+      @checker.reload.should be_working # The most recent log is no longer an error
56
+
57
+      two_days_from_now = 2.days.from_now
58
+      stub(Time).now { two_days_from_now }
59
+      @checker.reload.should_not be_working # Two days have passed without a new event having been created
60
+    end
61
+  end
62
+
45 63
   describe "parsing" do
46 64
     it "parses CSS" do
47 65
       @checker.check