Explorar o código

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

Andrew Cantino %!s(int64=11) %!d(string=hai) anos
pai
achega
7372244d0f

+ 1 - 1
app/controllers/agents_controller.rb

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

+ 9 - 3
app/models/agent.rb

@@ -30,7 +30,9 @@ class Agent < ActiveRecord::Base
 
   belongs_to :user, :inverse_of => :agents
   has_many :events, :dependent => :delete_all, :inverse_of => :agent, :order => "events.id desc"
+  has_one  :most_recent_event, :inverse_of => :agent, :class_name => "Event", :order => "events.id desc"
   has_many :logs, :dependent => :delete_all, :inverse_of => :agent, :class_name => "AgentLog", :order => "agent_logs.id desc"
+  has_one  :most_recent_log, :inverse_of => :agent, :class_name => "AgentLog", :order => "agent_logs.id desc"
   has_many :received_events, :through => :sources, :class_name => "Event", :source => :events, :order => "events.id desc"
   has_many :links_as_source, :dependent => :delete_all, :foreign_key => "source_id", :class_name => "Link", :inverse_of => :source
   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
     raise "Implement me in your subclass"
   end
 
-  def event_created_within(seconds)
-    last_event = events.first
-    last_event && last_event.created_at > seconds.ago && last_event
+  def event_created_within(days)
+    event = most_recent_event
+    event && event.created_at > days.to_i.days.ago && event.payload.present? && event
+  end
+
+  def recent_error_logs?
+    most_recent_log.try(:level) == 4
   end
 
   def sources_are_owned

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

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

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

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

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

@@ -63,7 +63,7 @@ module Agents
     end
 
     def working?
-      true
+      !recent_error_logs?
     end
 
     def value_constructor(value, payload)

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

+ 0 - 15
spec/helpers/logs_helper_spec.rb

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

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

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

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

@@ -42,6 +42,24 @@ describe Agents::WebsiteAgent do
     end
   end
 
+  describe '#working?' do
+    it 'checks if events have been received within the expected receive period' do
+      @checker.should_not be_working # No events created
+      @checker.check
+      @checker.reload.should be_working # Just created events
+
+      @checker.error "oh no!"
+      @checker.reload.should_not be_working # The most recent log is an error
+
+      @checker.log "ok now"
+      @checker.reload.should be_working # The most recent log is no longer an error
+
+      two_days_from_now = 2.days.from_now
+      stub(Time).now { two_days_from_now }
+      @checker.reload.should_not be_working # Two days have passed without a new event having been created
+    end
+  end
+
   describe "parsing" do
     it "parses CSS" do
       @checker.check