aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorPatrick J Cherry <patrick@bytemark.co.uk>2012-04-23 16:01:44 +0100
committerPatrick J Cherry <patrick@bytemark.co.uk>2012-04-23 16:01:44 +0100
commit6561f886ed03a79ca035a1733816ab97380576d2 (patch)
tree24282c1a9f85a6a1f1eb010df7c421f3c08315bb
parentca99b0dff974d2fc841d7132d03b3ad1d1bf9b1e (diff)
Persons/PeopleLists can now specify multiple notification preferences.
-rw-r--r--lib/mauve/configuration.rb7
-rw-r--r--lib/mauve/configuration_builders/alert_group.rb29
-rw-r--r--lib/mauve/configuration_builders/people_list.rb17
-rw-r--r--lib/mauve/configuration_builders/person.rb18
-rw-r--r--lib/mauve/notification.rb42
-rw-r--r--lib/mauve/people_list.rb23
-rw-r--r--lib/mauve/person.rb22
-rw-r--r--test/tc_mauve_configuration_builders_people_list.rb11
-rw-r--r--test/tc_mauve_configuration_builders_person.rb11
-rw-r--r--test/tc_mauve_notification.rb76
10 files changed, 167 insertions, 89 deletions
diff --git a/lib/mauve/configuration.rb b/lib/mauve/configuration.rb
index a74165b..65f5f71 100644
--- a/lib/mauve/configuration.rb
+++ b/lib/mauve/configuration.rb
@@ -20,10 +20,6 @@ module Mauve
# @return [Mauve::Server]
attr_accessor :server
- # The last AlertGroup to be configured
- # @return [Mauve::AlertGroup]
- attr_accessor :last_alert_group
-
# Notification methods
# @return [Hash]
attr_reader :notification_methods
@@ -49,10 +45,9 @@ module Mauve
#
def initialize
@server = nil
- @last_alert_group = nil
@notification_methods = {}
@people = {}
- @people_lists = Hash.new{|h,k| h[k] = Mauve::PeopleList.new(k)}
+ @people_lists = {}
@source_lists = Hash.new{|h,k| h[k] = Mauve::SourceList.new(k)}
@alert_groups = []
end
diff --git a/lib/mauve/configuration_builders/alert_group.rb b/lib/mauve/configuration_builders/alert_group.rb
index 40e5d9d..851ab7f 100644
--- a/lib/mauve/configuration_builders/alert_group.rb
+++ b/lib/mauve/configuration_builders/alert_group.rb
@@ -19,7 +19,10 @@ module Mauve
#
# @return [Mauve::Notification] New notification instance.
def builder_setup(who)
- who = if @context.people[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]
@@ -29,7 +32,8 @@ module Mauve
raise ArgumentError.new("You have not declared who #{who} is")
end
- @result = Mauve::Notification.new(who, @context.last_alert_group.level)
+
+ @result = Mauve::Notification.new(who)
end
is_attribute "every"
@@ -47,7 +51,6 @@ module Mauve
# @return [Mauve::AlertGroup] New alert group instance
def builder_setup(name="anonymous_name")
@result = Mauve::AlertGroup.new(name)
- @context.last_alert_group = @result
end
is_block_attribute "includes"
@@ -62,7 +65,25 @@ module Mauve
#
def created_notify(notification)
@result.notifications ||= []
- @result.notifications << notification
+
+ 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
end
end
diff --git a/lib/mauve/configuration_builders/people_list.rb b/lib/mauve/configuration_builders/people_list.rb
index e5f6715..fffa15c 100644
--- a/lib/mauve/configuration_builders/people_list.rb
+++ b/lib/mauve/configuration_builders/people_list.rb
@@ -2,19 +2,32 @@
require 'object_builder'
require 'mauve/people_list'
require 'mauve/configuration_builder'
+require 'mauve/configuration_builders/alert_group'
module Mauve
module ConfigurationBuilders
class PeopleList < ObjectBuilder
+ is_builder "notification", Notification
+
def builder_setup(label, list)
@result = Mauve::PeopleList.new(label)
@result += list
+ @result
+ end
+
+ #
+ # Notify is a shortcut for "notification"
+ #
+ def notify(&block)
+ notification(@result, &block)
end
- is_block_attribute "during"
- is_attribute "every"
+ def created_notification(notification)
+ @result.notifications ||= []
+ @result.notifications << notification
+ end
end
end
diff --git a/lib/mauve/configuration_builders/person.rb b/lib/mauve/configuration_builders/person.rb
index 7881d01..dff6f85 100644
--- a/lib/mauve/configuration_builders/person.rb
+++ b/lib/mauve/configuration_builders/person.rb
@@ -2,6 +2,7 @@
require 'object_builder'
require 'mauve/person'
require 'mauve/configuration_builder'
+require 'mauve/configuration_builders/alert_group'
module Mauve
module ConfigurationBuilders
@@ -12,11 +13,11 @@ module Mauve
@result = Mauve::Person.new(username)
end
+ is_builder "notification", Notification
+
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"
@@ -29,6 +30,18 @@ module Mauve
# @param [Block] block
def all(&block); urgent(&block); normal(&block); low(&block); end
+ #
+ # Notify is a shortcut for "notification"
+ #
+ def notify(&block)
+ notification(@result, &block)
+ end
+
+ def created_notification(notification)
+ @result.notifications ||= []
+ @result.notifications << notification
+ end
+
# Notification suppression hash
#
# @param [Hash] h
@@ -41,6 +54,7 @@ module Mauve
@result.notification_thresholds[v] = Array.new(k)
end
end
+
end
end
diff --git a/lib/mauve/notification.rb b/lib/mauve/notification.rb
index 3c7270e..d06e03a 100644
--- a/lib/mauve/notification.rb
+++ b/lib/mauve/notification.rb
@@ -182,17 +182,19 @@ module Mauve
# at a particular alert level, on a periodic basis, and optionally under
# certain conditions specified by a block of code.
#
- class Notification < Struct.new(:person, :level)
+ class Notification
+
+ attr_reader :during, :every, :level, :person
# Set up a new notification
#
# @param [Array] person List of Mauve::Person to notify
# @param [Symbol] level Level at which to notify
- def initialize(person, level)
- self.level = level
- self.every = nil
- self.during = nil
- self.person = person
+ def initialize(person)
+ @person = person
+ @during = nil
+ @every = nil
+ @level = nil
end
# @return [String]
@@ -203,34 +205,22 @@ module Mauve
# @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
+ def level=(arg)
+ @level = arg
+ end
+
+ def person=(arg)
+ @person = 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.
#
diff --git a/lib/mauve/people_list.rb b/lib/mauve/people_list.rb
index 1890ab1..eab4ae3 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, :during, :every
+ attr_reader :label, :list, :notifications
# Create a new list
#
@@ -17,31 +17,14 @@ module Mauve
# @raise [ArgumentError] if the label is not a string
#
def initialize(label)
- raise ArgumentError, "people_list label must be a string" unless label.is_a?(String)
+ raise ArgumentError, "people_list label must be a string #{label.inspect}" unless label.is_a?(String)
@label = label
@list = []
- @during = nil
- @every = nil
+ @notifications = []
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 4b4baf1..1cf4ea0 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, :urgent, :normal, :low, :email, :xmpp, :sms)
- attr_reader :notification_thresholds, :last_pop3_login, :suppressed, :every, :during
+ attr_reader :notification_thresholds, :last_pop3_login, :suppressed, :notifications
# Set up a new Person
#
@@ -26,9 +26,7 @@ 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
+ @notifications = []
super(*args)
end
@@ -41,22 +39,6 @@ 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
diff --git a/test/tc_mauve_configuration_builders_people_list.rb b/test/tc_mauve_configuration_builders_people_list.rb
index 61494d8..7e0840f 100644
--- a/test/tc_mauve_configuration_builders_people_list.rb
+++ b/test/tc_mauve_configuration_builders_people_list.rb
@@ -16,13 +16,13 @@ class TcMauveConfigurationBuildersPeopleList < Mauve::UnitTest
def test_people_list
config =<<EOF
-people_list "team sky", %w(
+people_list("team sky", %w(
geraint
edvald
bradley
rigoberto
ben
-)
+))
people_list("garmin-cervelo", %w(
thor
@@ -30,8 +30,10 @@ people_list("garmin-cervelo", %w(
tyler
julian
)) {
- every 20.minutes
- during { working_hours? }
+ notify {
+ every 20.minutes
+ during { working_hours? }
+ }
}
EOF
@@ -40,7 +42,6 @@ EOF
assert_equal(2, x.people_lists.keys.length)
assert_equal(["team sky","garmin-cervelo"].sort,x.people_lists.keys.sort)
assert_equal(%w(geraint edvald bradley rigoberto ben), x.people_lists["team sky"].list)
-
end
def test_duplicate_people_list
diff --git a/test/tc_mauve_configuration_builders_person.rb b/test/tc_mauve_configuration_builders_person.rb
index 391d796..d889a03 100644
--- a/test/tc_mauve_configuration_builders_person.rb
+++ b/test/tc_mauve_configuration_builders_person.rb
@@ -10,12 +10,14 @@ class TcMauveConfigurationBuildersPerson < Mauve::UnitTest
config=<<EOF
person("test1") {
all { "this should email on every level" }
- during { "this is the during block" }
- every 300
email "test1@example.com"
sms "01234567890"
xmpp "test1@chat.example.com"
password "topsekrit"
+ notify {
+ during { "this is the during block" }
+ every 300
+ }
}
EOF
@@ -23,13 +25,14 @@ EOF
assert_nothing_raised { x = Mauve::ConfigurationBuilder.parse(config) }
assert_equal(1, x.people.length)
assert_equal(%w(test1), x.people.keys)
- assert_equal(300, x.people["test1"].every)
assert_equal("test1@example.com", x.people["test1"].email)
assert_equal("01234567890", x.people["test1"].sms)
assert_equal("test1@chat.example.com", x.people["test1"].xmpp)
assert_equal("topsekrit", x.people["test1"].password)
- assert_equal("this is the during block", x.people["test1"].during.call)
+# assert_equal(300, x.people["test1"].every)
+# assert_equal("this is the during block", x.people["test1"].during.call)
+#
assert_equal("this should email on every level", x.people["test1"].urgent.call)
assert_equal("this should email on every level", x.people["test1"].normal.call)
assert_equal("this should email on every level", x.people["test1"].low.call)
diff --git a/test/tc_mauve_notification.rb b/test/tc_mauve_notification.rb
index 7ff0d79..16b6489 100644
--- a/test/tc_mauve_notification.rb
+++ b/test/tc_mauve_notification.rb
@@ -403,4 +403,80 @@ EOF
end
+ def test_individual_notification_preferences
+ config=<<EOF
+server {
+ use_notification_buffer false
+}
+
+notification_method("email") {
+ debug!
+ deliver_to_queue []
+ disable_normal_delivery!
+}
+
+person ("test1") {
+ email "test1@example.com"
+ all { email }
+ notify {
+ every 300
+ during { !working_hours? }
+ }
+}
+
+person ("test2") {
+ email "test2@example.com"
+ all { email }
+ notify {
+ every 300
+ during { working_hours? }
+ }
+}
+
+alert_group("test") {
+ level URGENT
+ notify("test1")
+ notify("test2")
+}
+
+EOF
+
+ Configuration.current = ConfigurationBuilder.parse(config)
+ notification_buffer = Configuration.current.notification_methods["email"].deliver_to_queue
+
+ Server.instance.setup
+
+ alert = Alert.new(
+ :alert_id => "test",
+ :source => "test",
+ :subject => "test"
+ )
+
+ #
+ # This should only alert test1
+ #
+ alert.raise!
+ assert_equal(1, notification_buffer.size, "Wrong number of notifications sent")
+ assert_equal("test1@example.com", notification_buffer.pop[2])
+
+ alert.clear!
+ assert_equal(1, notification_buffer.size, "Wrong number of notifications sent")
+ assert_equal("test1@example.com", notification_buffer.pop[2])
+
+ #
+ # Wind forward to 9am (working hours)
+ #
+ Timecop.freeze(Time.now+9.hours)
+ assert(Time.now.working_hours?)
+ alert.raise!
+ assert_equal(1, notification_buffer.size, "Wrong number of notifications sent")
+ assert_equal("test2@example.com", notification_buffer.pop[2])
+
+ alert.clear!
+ assert_equal(1, notification_buffer.size, "Wrong number of notifications sent")
+ assert_equal("test2@example.com", notification_buffer.pop[2])
+
+
+ end
+
end