From ca99b0dff974d2fc841d7132d03b3ad1d1bf9b1e Mon Sep 17 00:00:00 2001 From: Patrick J Cherry Date: Mon, 23 Apr 2012 11:37:38 +0100 Subject: People/PeopleLists can now specify individualy notification times/frequencies * Added PeopleList builder * Added Person#during, PeopleList#during, Person#every, PeopleList#every * Notification now uses #during/#every from the Person/PeopleList if nothing was specified * Added tests --- lib/mauve/configuration_builder.rb | 9 ++-- lib/mauve/configuration_builders.rb | 1 + lib/mauve/configuration_builders/alert_group.rb | 18 +++---- lib/mauve/configuration_builders/people_list.rb | 41 +++++++++++++++ lib/mauve/configuration_builders/person.rb | 2 + lib/mauve/notification.rb | 67 +++++++++++++++++-------- lib/mauve/people_list.rb | 20 +++++++- lib/mauve/person.rb | 35 +++++++++++-- 8 files changed, 155 insertions(+), 38 deletions(-) create mode 100644 lib/mauve/configuration_builders/people_list.rb (limited to 'lib/mauve') diff --git a/lib/mauve/configuration_builder.rb b/lib/mauve/configuration_builder.rb index 1d8d960..3f7138e 100644 --- a/lib/mauve/configuration_builder.rb +++ b/lib/mauve/configuration_builder.rb @@ -35,10 +35,11 @@ module Mauve # @param [Array] list # # @return [Array] the whole people list for label - def people_list(label, *list) - _logger.warn("Duplicate people_list '#{label}'") if @result.people_lists.has_key?(label) - @result.people_lists[label] += list - end + # ef people_list(label, *list) + # _logger.warn("Duplicate people_list '#{label}'") if @result.people_lists.has_key?(label) + # @result.people_lists[label] += list + # end + # Have to use the method _logger here, cos logger is defined as a builder elsewhere. # diff --git a/lib/mauve/configuration_builders.rb b/lib/mauve/configuration_builders.rb index 7ff6ac9..c90fff5 100644 --- a/lib/mauve/configuration_builders.rb +++ b/lib/mauve/configuration_builders.rb @@ -1,6 +1,7 @@ require 'mauve/configuration_builders/logger' require 'mauve/configuration_builders/notification_method' require 'mauve/configuration_builders/person' +require 'mauve/configuration_builders/people_list' require 'mauve/configuration_builders/server' require 'mauve/configuration_builders/alert_group' diff --git a/lib/mauve/configuration_builders/alert_group.rb b/lib/mauve/configuration_builders/alert_group.rb index 56c446d..40e5d9d 100644 --- a/lib/mauve/configuration_builders/alert_group.rb +++ b/lib/mauve/configuration_builders/alert_group.rb @@ -14,22 +14,20 @@ module Mauve # Sets up the notification # - # @param [Array] who List of usernames or people_lists to notify + # @param [String] who Username or people_list to notify # @raise [ArgumentError] if a username doesn't exist. # # @return [Mauve::Notification] New notification instance. - def builder_setup(*who) - who = who.map do |username| - if @context.people[username] - @context.people[username] + def builder_setup(who) + who = if @context.people[who] + @context.people[who] - elsif @context.people_lists[username] - @context.people_lists[username] + elsif @context.people_lists[who] + @context.people_lists[who] - else - raise ArgumentError.new("You have not declared who #{username} is") + else + raise ArgumentError.new("You have not declared who #{who} is") - end end @result = Mauve::Notification.new(who, @context.last_alert_group.level) end diff --git a/lib/mauve/configuration_builders/people_list.rb b/lib/mauve/configuration_builders/people_list.rb new file mode 100644 index 0000000..e5f6715 --- /dev/null +++ b/lib/mauve/configuration_builders/people_list.rb @@ -0,0 +1,41 @@ +# encoding: UTF-8 +require 'object_builder' +require 'mauve/people_list' +require 'mauve/configuration_builder' + +module Mauve + module ConfigurationBuilders + + class PeopleList < ObjectBuilder + + def builder_setup(label, list) + @result = Mauve::PeopleList.new(label) + @result += list + end + + is_block_attribute "during" + is_attribute "every" + + end + end + + class ConfigurationBuilder < ObjectBuilder + + is_builder "people_list", ConfigurationBuilders::PeopleList + + # Method called once a people_list has been created to check for duplicate labels + # + # @param [Mauve::PeopleList] people_list + # + def created_people_list(people_list) + label = people_list.label + if @result.people_lists.has_key?(label) + _logger.warn("Duplicate people_list '#{label}'") + @result.people_lists[label] += people_list.list + else + @result.people_lists[label] = people_list + end + end + + end +end diff --git a/lib/mauve/configuration_builders/person.rb b/lib/mauve/configuration_builders/person.rb index c5314c5..7881d01 100644 --- a/lib/mauve/configuration_builders/person.rb +++ b/lib/mauve/configuration_builders/person.rb @@ -15,6 +15,8 @@ module Mauve is_block_attribute "urgent" is_block_attribute "normal" is_block_attribute "low" + is_block_attribute "during" + is_attribute "every" is_attribute "password" is_attribute "sms" is_attribute "holiday_url" diff --git a/lib/mauve/notification.rb b/lib/mauve/notification.rb index 3c902a2..3c7270e 100644 --- a/lib/mauve/notification.rb +++ b/lib/mauve/notification.rb @@ -178,30 +178,59 @@ module Mauve end - # A Notification is an instruction to notify a list of people, at a - # particular alert level, on a periodic basis, and optionally under + # A Notification is an instruction to notify a person, or a list of people, + # at a particular alert level, on a periodic basis, and optionally under # certain conditions specified by a block of code. # - class Notification < Struct.new(:people, :level, :every, :during, :list) + class Notification < Struct.new(:person, :level) # Set up a new notification # - # @param [Array] people List of Mauve::Person to notify + # @param [Array] person List of Mauve::Person to notify # @param [Symbol] level Level at which to notify - def initialize(people, level) + def initialize(person, level) self.level = level - self.every = 300 - self.people = people + self.every = nil + self.during = nil + self.person = person end # @return [String] def to_s - "#" + "#" end # @return Log4r::Logger def logger ; Log4r::Logger.new self.class.to_s ; end + # + # + # + def during + @during ||= person.during + end + + # + # + # + def during=(arg) + @during = arg + end + + # + # + # + def every + @every ||= person.during + end + + # + # + # + def every=(arg) + @every = arg + end + # Push a notification on to the queue for this alert. The Mauve::Notifier # will then pop it off and do the notification in a separate thread. # @@ -211,8 +240,8 @@ module Mauve # @return [Array] The list of people that have received this alert. def notify(alert, already_sent_to = [], during_runner = nil) - if people.nil? or people.empty? - logger.warn "No people found in for notification #{list}" + if person.nil? + logger.warn "No person found in for notification #{list}" return end @@ -222,16 +251,14 @@ module Mauve # Should we notify at all? return already_sent_to unless during_runner.now? - people.collect do |person| - case person - when Person - person - when PeopleList - person.people - else - logger.warn "Unable to notify #{person} (unrecognised class #{person.class})" - [] - end + case person + when Person + [person] + when PeopleList + person.people + else + logger.warn "Unable to notify #{person} (unrecognised class #{person.class})" + [] end.flatten.uniq.each do |person| # # A bit of alert de-bouncing. diff --git a/lib/mauve/people_list.rb b/lib/mauve/people_list.rb index 6b2ba33..1890ab1 100644 --- a/lib/mauve/people_list.rb +++ b/lib/mauve/people_list.rb @@ -9,7 +9,7 @@ module Mauve # class PeopleList - attr_reader :label, :list + attr_reader :label, :list, :during, :every # Create a new list # @@ -20,10 +20,28 @@ module Mauve raise ArgumentError, "people_list label must be a string" unless label.is_a?(String) @label = label @list = [] + @during = nil + @every = nil end alias username label + # + # + # + def during=(arg) + raise "during must be a block" unless arg.is_a?(Proc) + @during = arg + end + + # + # + # + def every=(arg) + raise ArgumentError, "every must be numeric" unless arg.is_a?(Numeric) + @every = arg + end + # Append an Array or String to a list # # @param [Array or String] arr diff --git a/lib/mauve/person.rb b/lib/mauve/person.rb index a10e86a..4b4baf1 100644 --- a/lib/mauve/person.rb +++ b/lib/mauve/person.rb @@ -3,9 +3,9 @@ require 'timeout' require 'log4r' module Mauve - class Person < Struct.new(:username, :password, :holiday_url, :urgent, :normal, :low, :email, :xmpp, :sms) + class Person < Struct.new(:username, :password, :urgent, :normal, :low, :email, :xmpp, :sms) - attr_reader :notification_thresholds, :last_pop3_login, :suppressed + attr_reader :notification_thresholds, :last_pop3_login, :suppressed, :every, :during # Set up a new Person # @@ -26,6 +26,10 @@ module Mauve # TODO fix up web login so pop3 can be used as a proxy. # @last_pop3_login = {:from => nil, :at => nil} + + @every = nil + @during = nil + super(*args) end @@ -36,6 +40,24 @@ module Mauve # # @return [Boolean] def suppressed? ; @suppressed ; end + + # + # + # + def during=(arg) + raise "during must be a block" unless arg.is_a?(Proc) + @during = arg + end + + # + # + # + def every=(arg) + raise ArgumentError, "every must be numeric" unless arg.is_a?(Numeric) + @every = arg + end + + def holiday_url ; nil ; end # Works out if a notification should be suppressed. If no parameters are supplied, it will # @@ -212,7 +234,14 @@ module Mauve my_last_update && my_last_update.update_type != "cleared" end end - + + # + # + # + def alert_during + + end + # Whether the person is on holiday or not. # # @return [Boolean] True if person on holiday, false otherwise. -- cgit v1.2.1