diff options
author | Patrick J Cherry <patrick@bytemark.co.uk> | 2012-05-03 19:42:20 +0100 |
---|---|---|
committer | Patrick J Cherry <patrick@bytemark.co.uk> | 2012-05-03 19:42:20 +0100 |
commit | 58f5a950200f5957307a8552b92ce9e92f948b73 (patch) | |
tree | e7e5dbf2178dfd46e88e8e6d60d6665c58b538fa /lib | |
parent | 3a008fd3fff00852533d0df1f597595505373c6b (diff) |
* Person and people lists have now become interchangeable, with notifications
only recording the username, rather than the Person/PeopleList. This means
that notifications in alert groups can use default #during clauses and #every
values from people lists and persons.
* Notify clauses can now take an array of persons/people_lists, and people_lists
can refer to other people lists.
* PeopleList names are now in the same namespace as usernames.
* Tests to go with.
Diffstat (limited to 'lib')
-rw-r--r-- | lib/mauve/alert_group.rb | 24 | ||||
-rw-r--r-- | lib/mauve/configuration_builders/alert_group.rb | 38 | ||||
-rw-r--r-- | lib/mauve/configuration_builders/people_list.rb | 15 | ||||
-rw-r--r-- | lib/mauve/configuration_builders/person.rb | 1 | ||||
-rw-r--r-- | lib/mauve/notification.rb | 70 | ||||
-rw-r--r-- | lib/mauve/people_list.rb | 15 | ||||
-rw-r--r-- | lib/mauve/person.rb | 9 |
7 files changed, 101 insertions, 71 deletions
diff --git a/lib/mauve/alert_group.rb b/lib/mauve/alert_group.rb index 05488fd..76c2e98 100644 --- a/lib/mauve/alert_group.rb +++ b/lib/mauve/alert_group.rb @@ -157,9 +157,17 @@ module Mauve # # This is where we set the reminder -- i.e. on a per-alert-group basis. - + # remind_at = nil - notifications.each do |notification| + + these_notifications = self.resolve_notifications(at) + + these_notifications.each do |notification| + # + # Make sure the level is set. + # + notification.level = self.level + # # Create a new during_runner for this notification clause, and keep it # handy. @@ -208,7 +216,7 @@ module Mauve # The notifications are specified in the config file. # sent_to = [] - notifications.each do |notification| + these_notifications.each do |notification| sent_to << notification.notify(alert, sent_to, during_runners.shift) end @@ -240,6 +248,16 @@ module Mauve [LEVELS.index(self.level), self.name] <=> [LEVELS.index(other.level), other.name] end + # + # + # + def resolve_notifications(at = Time.now) + self.notifications.collect do |notification| + notification.people.collect do |person| + person.resolve_notifications(notification.every, notification.during, at) + end + end.flatten.compact + end end end diff --git a/lib/mauve/configuration_builders/alert_group.rb b/lib/mauve/configuration_builders/alert_group.rb index 851ab7f..a813ee9 100644 --- a/lib/mauve/configuration_builders/alert_group.rb +++ b/lib/mauve/configuration_builders/alert_group.rb @@ -18,22 +18,8 @@ module Mauve # @raise [ArgumentError] if a username doesn't exist. # # @return [Mauve::Notification] New notification instance. - def builder_setup(who) - who = if who.is_a?(Mauve::Person) or who.is_a?(Mauve::PeopleList) - who - - elsif @context.people[who] - @context.people[who] - - elsif @context.people_lists[who] - @context.people_lists[who] - - else - raise ArgumentError.new("You have not declared who #{who} is") - - end - - @result = Mauve::Notification.new(who) + def builder_setup(*who) + @result = Mauve::Notification.new(*who) end is_attribute "every" @@ -65,25 +51,7 @@ module Mauve # def created_notify(notification) @result.notifications ||= [] - - if notification.during.nil? and notification.every.nil? - @result.notifications += notification.person.notifications.collect do |n| - # - # Set up a new notification for each one defined for this person. - # - new_notification = Mauve::Notification.new(notification.person) - new_notification.level = @result.level - new_notification.every = n.every - new_notification.during = n.during - new_notification - end - else - # - # Set the level for this notification - # - notification.level = @result.level - @result.notifications << notification - end + @result.notifications << notification end end diff --git a/lib/mauve/configuration_builders/people_list.rb b/lib/mauve/configuration_builders/people_list.rb index fffa15c..7a5f3ab 100644 --- a/lib/mauve/configuration_builders/people_list.rb +++ b/lib/mauve/configuration_builders/people_list.rb @@ -9,9 +9,9 @@ module Mauve class PeopleList < ObjectBuilder - is_builder "notification", Notification + is_builder "notification", ConfigurationBuilders::Notification - def builder_setup(label, list) + def builder_setup(label, *list) @result = Mauve::PeopleList.new(label) @result += list @result @@ -41,14 +41,9 @@ module Mauve # @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 + name = people_list.username + raise ArgumentError.new("Duplicate person '#{name}'") if @result.people[name] + @result.people[name] = people_list end - end end diff --git a/lib/mauve/configuration_builders/person.rb b/lib/mauve/configuration_builders/person.rb index 5c79474..bb4fe8d 100644 --- a/lib/mauve/configuration_builders/person.rb +++ b/lib/mauve/configuration_builders/person.rb @@ -5,6 +5,7 @@ require 'mauve/configuration_builder' require 'mauve/configuration_builders/alert_group' module Mauve + module ConfigurationBuilders class Person < ObjectBuilder diff --git a/lib/mauve/notification.rb b/lib/mauve/notification.rb index fcefcbc..82da2e8 100644 --- a/lib/mauve/notification.rb +++ b/lib/mauve/notification.rb @@ -121,10 +121,10 @@ module Mauve # @return [Boolean] # def no_one_in(people_list) - return true unless Configuration.current.people_lists.has_key?(people_list) - @test_time = @time if @test_time.nil? + return true unless Configuration.current.people[people_list].respond_to?(:people) - return Configuration.current.people_lists[people_list].people(@test_time).empty? + @test_time = @time if @test_time.nil? + return Configuration.current.people[people_list].people(@test_time).empty? end # Returns true if the current hour is in the list of hours given. @@ -133,7 +133,7 @@ module Mauve # @return [Boolean] def hours_in_day(*hours) @test_time = @time if @test_time.nil? - x_in_list_of_y(@test_time.hour, hours.flatten) + x_in_list_of_y(@test_time.hour, Configuration.parse_range(hours).flatten) end # Returns true if the current day is in the list of days given @@ -142,7 +142,7 @@ module Mauve # @return [Boolean] def days_in_week(*days) @test_time = @time if @test_time.nil? - x_in_list_of_y(@test_time.wday, days.flatten) + x_in_list_of_y(@test_time.wday, Configuration.parse_range(days,0...7).flatten) end # Tests if the alert has not been acknowledged within a certain time. @@ -223,14 +223,21 @@ module Mauve # class Notification - attr_reader :during, :every, :level, :person + attr_reader :during, :every, :level, :usernames # Set up a new notification # - # @param [Array] person List of Mauve::Person to notify + # @param [Array] usernames List of Mauve::Person to notify # @param [Symbol] level Level at which to notify - def initialize(person) - @person = person + def initialize(*usernames) + @usernames = usernames.flatten.collect do |u| + if u.respond_to?(:username) + u.username + else + u.to_s + end + end.flatten + @during = nil @every = nil @level = nil @@ -238,7 +245,7 @@ module Mauve # @return [String] def to_s - "#<Notification:of #{person} at level #{level} every #{every}>" + "#<Notification:of #{usernames} at level #{level} every #{every}>" end # @return Log4r::Logger @@ -256,8 +263,14 @@ module Mauve @level = arg end - def person=(arg) - @person = arg + def usernames=(arg) + @usernames = arg + end + + def people + usernames.sort.collect do |username| + Configuration.current.people[username] + end.compact.uniq end # Push a notification on to the queue for this alert. The Mauve::Notifier @@ -269,8 +282,8 @@ module Mauve # @return [Array] The list of people that have received this alert. def notify(alert, already_sent_to = [], during_runner = nil) - if person.nil? - logger.warn "No person found in for notification #{list}" + if usernames.nil? or usernames.empty? + logger.warn "No usernames found for notification #{list}" return end @@ -280,15 +293,16 @@ module Mauve # Should we notify at all? return already_sent_to unless during_runner.now? - 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| + people.collect do |person| + case person + when PeopleList + person.people(during_runner.time) + when Person + person + else + nil + end + end.flatten.compact.uniq.each do |person| # # A bit of alert de-bouncing. # @@ -310,8 +324,16 @@ module Mauve # @return [Time or nil] The time a reminder should get sent, or nil if it # should never get sent again. def remind_at_next(alert, during_runner = nil) + # + # Don't remind on acknowledgements / clears. + # return nil unless alert.raised? + # + # Never remind if every is not set. + # + return nil unless every + # Set up a during_runner during_runner ||= DuringRunner.new(Time.now, alert, &self.during) @@ -325,4 +347,6 @@ module Mauve end + class NotificationDummy < Struct.new(:during, :every) ; end + end diff --git a/lib/mauve/people_list.rb b/lib/mauve/people_list.rb index f632f78..e6c6c36 100644 --- a/lib/mauve/people_list.rb +++ b/lib/mauve/people_list.rb @@ -78,6 +78,21 @@ module Mauve l end + def resolve_notifications(default_every=nil, default_during=nil, at = nil) + self.people(at).collect do |person| + if self.notifications.empty? + person.resolve_notifications(default_every, default_during, at) + else + self.notifications.collect do |notification| + this_notification = Notification.new(person) + this_notification.every = default_every || notification.every + this_notification.during = default_during || notification.during + this_notification + end + end + end.flatten.compact + end + end end diff --git a/lib/mauve/person.rb b/lib/mauve/person.rb index 6178fa0..d5e5ea9 100644 --- a/lib/mauve/person.rb +++ b/lib/mauve/person.rb @@ -302,6 +302,15 @@ module Mauve return CalendarInterface.is_user_off_sick?(self.username, at) end + def resolve_notifications(default_every=nil, default_during=nil, at = nil) + self.notifications.collect do |notification| + this_notification = Notification.new(self) + this_notification.every = default_every || notification.every + this_notification.during = default_during || notification.during + this_notification + end.flatten.compact + end + end end |