lib/flapjack/data/notification.rb in flapjack-0.7.20 vs lib/flapjack/data/notification.rb in flapjack-0.7.21
- old
+ new
@@ -46,21 +46,23 @@
def self.add(queue, event, opts = {})
raise "Redis connection not set" unless redis = opts[:redis]
last_state = opts[:last_state] || {}
+ tag_data = event.tags.is_a?(Set) ? event.tags.to_a : nil
notif = {'event_id' => event.id,
'state' => event.state,
'summary' => event.summary,
'last_state' => last_state[:state],
'last_summary' => last_state[:summary],
'details' => event.details,
'time' => event.time,
'duration' => event.duration || nil,
'type' => opts[:type] || type_for_event(event),
'severity' => opts[:severity],
- 'count' => event.counter }
+ 'count' => event.counter,
+ 'tags' => tag_data }
redis.rpush(queue, Oj.dump(notif))
end
def self.next(queue, opts = {})
@@ -94,11 +96,12 @@
'last_summary' => @last_event_summary,
'details' => @event_details,
'time' => @event_time,
'duration' => @event_duration,
'notification_type' => @type,
- 'event_count' => @event_count
+ 'event_count' => @event_count,
+ 'tags' => @tags
}
end
def messages(contacts, opts = {})
return [] if contacts.nil? || contacts.empty?
@@ -109,39 +112,43 @@
@messages ||= contacts.collect {|contact|
contact_id = contact.id
rules = contact.notification_rules
media = contact.media
- logger.debug "considering messages for contact id #{contact_id} #{@event_id} #{@event_state} (media) #{media.inspect}"
+ logger.debug "Notification#messages: creating messages for contact: #{contact_id} " +
+ "event_id: \"#{@event_id}\" state: #{@event_state} event_tags: #{@tags.to_json} media: #{media.inspect}"
rlen = rules.length
- logger.debug "found #{rlen} rule#{(rlen == 1) ? '' : 's'} for contact"
+ logger.debug "found #{rlen} rule#{(rlen == 1) ? '' : 's'} for contact #{contact_id}"
media_to_use = if rules.empty?
media
else
# matchers are rules of the contact that have matched the current event
- # for time and entity
+ # for time, entity and tags
matchers = rules.select do |rule|
- rule.match_entity?(@event_id) &&
+ logger.debug("considering rule with entities: #{rule.entities} and tags: #{rule.tags.to_json}")
+ (rule.match_entity?(@event_id) || rule.match_tags?(@tags) || ! rule.is_specific?) &&
rule_occurring_now?(rule, :contact => contact, :default_timezone => default_timezone)
end
- logger.debug "#{matchers.length} matchers remain for this contact:"
+ logger.debug "#{matchers.length} matchers remain for this contact after time, entity and tags are matched:"
matchers.each do |matcher|
logger.debug "matcher: #{matcher.to_json}"
end
- # delete any matchers for all entities if there are more specific matchers
+ # delete any general matchers if there are more specific matchers left
if matchers.any? {|matcher| matcher.is_specific? }
- logger.debug("general removal: found #{matchers.length} entity specific matchers")
num_matchers = matchers.length
matchers.reject! {|matcher| !matcher.is_specific? }
if num_matchers != matchers.length
logger.debug("notification: removal of general matchers when entity specific matchers are present: number of matchers changed from #{num_matchers} to #{matchers.length} for contact id: #{contact_id}")
+ matchers.each do |matcher|
+ logger.debug "matcher: #{matcher.to_json}"
+ end
end
end
# delete media based on blackholes
next if matchers.any? {|matcher| matcher.blackhole?(@event_state) }
@@ -177,22 +184,22 @@
private
# created from parsed JSON, so opts keys are in strings
def initialize(opts = {})
- @event_id = opts['event_id']
- @event_state = opts['state']
- @event_summary = opts['summary']
- @event_details = opts['details']
- @event_time = opts['time']
- @event_duration = opts['duration']
- @event_count = opts['count']
-
+ @event_id = opts['event_id']
+ @event_state = opts['state']
+ @event_summary = opts['summary']
+ @event_details = opts['details']
+ @event_time = opts['time']
+ @event_duration = opts['duration']
+ @event_count = opts['count']
@last_event_state = opts['last_state']
@last_event_summary = opts['last_summary']
-
- @type = opts['type']
- @severity = opts['severity']
+ @type = opts['type']
+ @severity = opts['severity']
+ tags = opts['tags']
+ @tags = tags.is_a?(Array) ? Flapjack::Data::TagSet.new(tags) : nil
end
# # time restrictions match?
# nil rule.time_restrictions matches
# times (start, end) within time restrictions will have any UTC offset removed and will be