aboutsummaryrefslogtreecommitdiff
path: root/lib/mauve/configuration_builders/person.rb
blob: 5f08d196eb54cc3fb03a11b44a2bd1801abf205d (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
# encoding: UTF-8
require 'object_builder'
require 'mauve/person'
require 'mauve/configuration_builder'
require 'mauve/configuration_builders/alert_group'

module Mauve

  module ConfigurationBuilders

    class Person < ObjectBuilder

      def builder_setup(username)
        @result = Mauve::Person.new(username)
      end

      is_builder "notification", Notification

      is_block_attribute "urgent"
      is_block_attribute "normal"
      is_block_attribute "low"

      is_attribute "password"
      is_attribute "sms"
      is_attribute "email"
      is_attribute "xmpp"

      is_flag_attribute "notify_when_on_holiday"
      is_flag_attribute "notify_when_off_sick"

      # Sets the block for all levels of alert
      #
      # @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
      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.each do |number_of_alerts,in_period|
          raise ArgumentError.new("notification_threshold must be specified as e.g. (10 => 1.minute)") unless number_of_alerts.is_a?(Integer) and in_period.is_a?(Integer)

          @result.suppress_notifications_after[in_period] = number_of_alerts
          # History.all(
          # :limit => number_of_alerts,
          # :order => :created_at.desc,
          # :type => "notification",
          # :event.like => '% succeeded')
        end
      end

      #
      #
      def notify_when_on_holday!
        result.notify_when_on_holiday = true
      end

      def notify_when_off_sick!
        result.notify_when_off_sick = true
      end

    end
  end

  class ConfigurationBuilder < ObjectBuilder

    is_builder "person", ConfigurationBuilders::Person

    # Method called once a person has been created to check for duplicate names
    #
    # @param [Mauve::Person] person
    # @raise [ArgumentError] if a person has already been declared.
    #
    def created_person(person)
      name = person.username
      raise ArgumentError.new("Duplicate person '#{name}'") if @result.people[name]

      #
      # Add a default notification threshold
      #
      person.suppress_notifications_after[600] = 5 if person.suppress_notifications_after.empty?

      #
      # Add a default notify clause
      #
      if person.notifications.empty?
        default_notification = Notification.new(person)
        default_notification.every = 30.minutes
        default_notification.during = Proc.new { working_hours? }
        person.notifications << default_notification
      end

      #
      # Set up some default notify levels.
      #
      if person.urgent.nil? and person.normal.nil? and person.low.nil?
        person.urgent = Proc.new { sms ; xmpp ; email }
        person.normal = Proc.new { xmpp ; email }
        person.low    = Proc.new { email }
      end

      @result.people[person.username] = person
    end

  end
end