aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorPatrick J Cherry <patrick@bytemark.co.uk>2012-04-23 11:37:38 +0100
committerPatrick J Cherry <patrick@bytemark.co.uk>2012-04-23 11:37:38 +0100
commitca99b0dff974d2fc841d7132d03b3ad1d1bf9b1e (patch)
tree69f949ec5c8a3396222815eacf9109d516de8c84
parentc3592bdf6fce6f234de37959c677f75d97b1134d (diff)
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
-rw-r--r--lib/mauve/configuration_builder.rb9
-rw-r--r--lib/mauve/configuration_builders.rb1
-rw-r--r--lib/mauve/configuration_builders/alert_group.rb18
-rw-r--r--lib/mauve/configuration_builders/people_list.rb41
-rw-r--r--lib/mauve/configuration_builders/person.rb2
-rw-r--r--lib/mauve/notification.rb67
-rw-r--r--lib/mauve/people_list.rb20
-rw-r--r--lib/mauve/person.rb35
-rw-r--r--test/tc_mauve_configuration_builder.rb54
-rw-r--r--test/tc_mauve_configuration_builders_people_list.rb72
-rw-r--r--test/tc_mauve_configuration_builders_person.rb27
11 files changed, 255 insertions, 91 deletions
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
- "#<Notification:of #{people.map { |p| p.username }.join(',')} at level #{level} every #{every}>"
+ "#<Notification:of #{person} at level #{level} every #{every}>"
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.
diff --git a/test/tc_mauve_configuration_builder.rb b/test/tc_mauve_configuration_builder.rb
index faa0bfa..1288b5d 100644
--- a/test/tc_mauve_configuration_builder.rb
+++ b/test/tc_mauve_configuration_builder.rb
@@ -3,7 +3,7 @@ $:.unshift "../lib/"
require 'th_mauve'
require 'mauve/configuration_builder'
-class TcMauveConfigurationBuildersPeopleAndSourceLists < Mauve::UnitTest
+class TcMauveConfigurationBuilder < Mauve::UnitTest
def setup
setup_logger
@@ -13,58 +13,6 @@ class TcMauveConfigurationBuildersPeopleAndSourceLists < Mauve::UnitTest
teardown_logger
end
- def test_people_list
- config =<<EOF
-people_list "team sky", %w(
- geraint
- edvald
- bradley
- rigoberto
- ben
-)
-
-people_list "garmin-cervelo", %w(
- thor
- ryder
- tyler
- julian
-)
-
-EOF
- x = nil
- assert_nothing_raised { x = Mauve::ConfigurationBuilder.parse(config) }
- 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
-
- config=<<EOF
-
-people_list "htc-highroad",
- ["mark c", "mark r", "Lars"]
-
-people_list "htc-highroad",
- %w(Bernie Danny Lars)
-
-EOF
- x = nil
- #
- # This should generate two warnings:
- # * duplicate list
- # * Lars already being on a list
- #
- assert_nothing_raised { x = Mauve::ConfigurationBuilder.parse(config) }
-
- assert_match(/Lars/, logger_pop())
- assert_match(/Duplicate/, logger_pop())
-
- assert_equal(1, x.people_lists.keys.length)
- assert_equal(["mark c","mark r","Lars","Bernie","Danny"].sort, x.people_lists["htc-highroad"].list.sort)
- end
-
def test_source_list
config =<<EOF
source_list "sources", %w(
diff --git a/test/tc_mauve_configuration_builders_people_list.rb b/test/tc_mauve_configuration_builders_people_list.rb
new file mode 100644
index 0000000..61494d8
--- /dev/null
+++ b/test/tc_mauve_configuration_builders_people_list.rb
@@ -0,0 +1,72 @@
+$:.unshift "../lib/"
+
+require 'th_mauve'
+require 'mauve/configuration_builder'
+require 'mauve/configuration_builders/people_list'
+
+class TcMauveConfigurationBuildersPeopleList < Mauve::UnitTest
+
+ def setup
+ setup_logger
+ end
+
+ def teardown
+ teardown_logger
+ end
+
+ def test_people_list
+ config =<<EOF
+people_list "team sky", %w(
+ geraint
+ edvald
+ bradley
+ rigoberto
+ ben
+)
+
+people_list("garmin-cervelo", %w(
+ thor
+ ryder
+ tyler
+ julian
+)) {
+ every 20.minutes
+ during { working_hours? }
+}
+
+EOF
+ x = nil
+ assert_nothing_raised { x = Mauve::ConfigurationBuilder.parse(config) }
+ 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
+
+ config=<<EOF
+
+people_list "htc-highroad",
+ ["mark c", "mark r", "Lars"]
+
+people_list "htc-highroad",
+ %w(Bernie Danny Lars)
+
+EOF
+ x = nil
+ #
+ # This should generate two warnings:
+ # * duplicate list
+ # * Lars already being on a list
+ #
+ assert_nothing_raised { x = Mauve::ConfigurationBuilder.parse(config) }
+
+ assert_match(/Lars/, logger_pop())
+ assert_match(/Duplicate/, logger_pop())
+
+ assert_equal(1, x.people_lists.keys.length)
+ assert_equal(["mark c","mark r","Lars","Bernie","Danny"].sort, x.people_lists["htc-highroad"].list.sort)
+ end
+
+end
diff --git a/test/tc_mauve_configuration_builders_person.rb b/test/tc_mauve_configuration_builders_person.rb
index 61a6202..391d796 100644
--- a/test/tc_mauve_configuration_builders_person.rb
+++ b/test/tc_mauve_configuration_builders_person.rb
@@ -2,10 +2,37 @@ $:.unshift "../lib/"
require 'th_mauve'
require 'mauve/configuration_builders/person'
+require 'pp'
class TcMauveConfigurationBuildersPerson < Mauve::UnitTest
def test_load
+ 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"
+}
+EOF
+
+ x = nil
+ 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("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)
end