From a704ace65369c4c9ca6fc71c0f5106203012bf16 Mon Sep 17 00:00:00 2001 From: Patrick J Cherry Date: Tue, 6 Sep 2011 11:05:30 +0100 Subject: Fixed up suppression some more, so it works. --- lib/mauve/configuration_builders/person.rb | 9 ++++--- lib/mauve/person.rb | 43 +++++++++++++++++------------- 2 files changed, 30 insertions(+), 22 deletions(-) (limited to 'lib') diff --git a/lib/mauve/configuration_builders/person.rb b/lib/mauve/configuration_builders/person.rb index 014987f..1c012f2 100644 --- a/lib/mauve/configuration_builders/person.rb +++ b/lib/mauve/configuration_builders/person.rb @@ -39,10 +39,13 @@ module Mauve end def suppress_notifications_after(h) - raise ArgumentError.new("notification_threshold must be specified as e.g. (10 => 1.minute)") unless - h.kind_of?(Hash) && h.keys[0].kind_of?(Integer) && h.values[0].kind_of?(Integer) + raise ArgumentError.new("notification_threshold must be specified as e.g. (10 => 1.minute)") unless h.kind_of?(Hash) - @result.notification_thresholds[h.values[0]] = Array.new(h.keys[0]) + h.each do |k,v| + raise ArgumentError.new("notification_threshold must be specified as e.g. (10 => 1.minute)") unless k.is_a?(Integer) and v.is_a?(Integer) + + @result.notification_thresholds[v] = Array.new(k) + end end end end diff --git a/lib/mauve/person.rb b/lib/mauve/person.rb index a656bf3..c066a6d 100644 --- a/lib/mauve/person.rb +++ b/lib/mauve/person.rb @@ -5,7 +5,7 @@ require 'log4r' module Mauve class Person < Struct.new(:username, :password, :holiday_url, :urgent, :normal, :low, :email, :xmpp, :sms) - attr_reader :notification_thresholds, :last_pop3_login + attr_reader :notification_thresholds, :last_pop3_login, :suppressed def initialize(*args) @notification_thresholds = nil @@ -18,9 +18,22 @@ module Mauve end def logger ; @logger ||= Log4r::Logger.new self.class.to_s ; end + + def suppressed? ; @suppressed ; end - def suppressed? - @suppressed + def should_suppress? + now = Time.now + + return self.notification_thresholds.any? do |period, previous_alert_times| + # + # Choose the second one as the first, apart from if the array is only one in length. + # + first = previous_alert_times.first + last = previous_alert_times.last + + (first.is_a?(Time) and (now - first) < period) or + (last.is_a?(Time) and @suppressed and (now - last) < period) + end end def notification_thresholds @@ -96,22 +109,9 @@ module Mauve def send_alert(level, alert) now = Time.now - was_suppressed = self.suppressed? + was_suppressed = @suppressed + @suppressed = self.should_suppress? - @suppressed = self.notification_thresholds.any? do |period, previous_alert_times| - # - # Choose the second one as the first. - # - first = previous_alert_times[1] - last = previous_alert_times[-1] - - first.is_a?(Time) and ( - (now - first) < period or - (was_suppressed and (now - last) < period) - ) - end - - logger.info "Starting to send notifications again for #{username}." if was_suppressed and not self.suppressed? # @@ -148,12 +148,17 @@ module Mauve self.notification_thresholds[period].shift end + # + # Re-run the suppression check, to see if we should be suppressed now. + # + @suppressed = self.should_suppress? + return true end return false end - + # # Returns the subset of current alerts that are relevant to this Person. # -- cgit v1.2.1