lib/flapjack/data/contact.rb in flapjack-1.1.0 vs lib/flapjack/data/contact.rb in flapjack-1.2.0rc1

- old
+ new

@@ -5,12 +5,10 @@ require 'set' require 'ice_cube' require 'flapjack/data/entity' require 'flapjack/data/notification_rule' -require 'flapjack/data/tag' -require 'flapjack/data/tag_set' require 'securerandom' module Flapjack @@ -19,11 +17,10 @@ class Contact attr_accessor :id, :first_name, :last_name, :email, :media, :media_intervals, :media_rollup_thresholds, :pagerduty_credentials - TAG_PREFIX = 'contact_tag' ALL_MEDIA = ['email', 'sms', 'sms_twilio', 'jabber', 'pagerduty', 'sns'] def self.all(options = {}) raise "Redis connection not set" unless redis = options[:redis] @@ -73,13 +70,16 @@ if contact = self.find_by_id(contact_id, :redis => redis) contact.delete! end self.add_or_update(contact_id, contact_data, :redis => redis) - if contact = self.find_by_id(contact_id, :redis => redis) + contact = self.find_by_id(contact_id, :redis => redis) + + unless contact.nil? contact.notification_rules # invoke to create general rule end + contact end def self.delete_all(options = {}) raise "Redis connection not set" unless redis = options[:redis] @@ -126,13 +126,10 @@ # TODO if implemented, alerts_by_contact & alerts_by_check: # list all alerts from all matched keys, remove them from # the main alerts sorted set, remove all alerts_by sorted sets # for the contact - # remove this contact from all tags it's marked with - self.delete_tags(*self.tags.to_a) - # remove all associated notification rules self.notification_rules.each do |nr| self.delete_notification_rule(nr) end @@ -250,12 +247,12 @@ end if rules.all? {|r| r.is_specific? } # also true if empty rule = self.add_notification_rule({ :entities => [], :regex_entities => [], - :tags => Flapjack::Data::TagSet.new([]), - :regex_tags => Flapjack::Data::TagSet.new([]), + :tags => Set.new([]), + :regex_tags => Set.new([]), :time_restrictions => [], :warning_media => ALL_MEDIA, :critical_media => ALL_MEDIA, :warning_blackhole => false, :critical_blackhole => false, @@ -413,41 +410,10 @@ def count_alerting_checks_for_media(media) @redis.zcard("contact_alerting_checks:#{self.id}:media:#{media}") end - # FIXME - # do a mixin with the following tag methods, they will be the same - # across all objects we allow tags on - - # return the set of tags for this contact - def tags - @tags ||= Flapjack::Data::TagSet.new( @redis.keys("#{TAG_PREFIX}:*").inject([]) {|memo, tag| - if Flapjack::Data::Tag.find(tag, :redis => @redis).include?(@id.to_s) - memo << tag.sub(/^#{TAG_PREFIX}:/, '') - end - memo - } ) - end - - # adds tags to this contact - def add_tags(*enum) - enum.each do |t| - Flapjack::Data::Tag.create("#{TAG_PREFIX}:#{t}", [@id], :redis => @redis) - tags.add(t) - end - end - - # removes tags from this contact - def delete_tags(*enum) - enum.each do |t| - tag = Flapjack::Data::Tag.find("#{TAG_PREFIX}:#{t}", :redis => @redis) - tag.delete(@id) - tags.delete(t) - end - end - # return a list of media enabled for this contact # eg [ 'email', 'sms' ] def media_list @redis.hkeys("contact_media:#{self.id}") - ['pagerduty'] end @@ -486,36 +452,24 @@ @redis.set("contact_tz:#{self.id}", tz.respond_to?(:name) ? tz.name : tz ) end end - def to_json(*args) - { "id" => self.id, - "first_name" => self.first_name, - "last_name" => self.last_name, - "email" => self.email, - "media" => self.media, - "media_intervals" => self.media_intervals, - "media_rollup_thresholds" => self.media_rollup_thresholds, - "timezone" => self.timezone.name, - "tags" => self.tags.to_a - }.to_json - end - def to_jsonapi(opts = {}) - { "id" => self.id, + json_data = { + "id" => self.id, "first_name" => self.first_name, "last_name" => self.last_name, "email" => self.email, "timezone" => self.timezone.name, - "tags" => self.tags.to_a, "links" => { :entities => opts[:entity_ids] || [], :media => self.media_ids || [], :notification_rules => self.notification_rule_ids || [], } - }.to_json + } + Flapjack.dump_json(json_data) end private def initialize(options = {}) @@ -533,15 +487,9 @@ attrs = (['first_name', 'last_name', 'email'] & contact_data.keys).collect do |key| [key, contact_data[key]] end.flatten(1) redis.hmset("contact:#{contact_id}", *attrs) unless attrs.empty? - - if ( ! contact_data['tags'].nil? && contact_data['tags'].is_a?(Enumerable)) - contact_data['tags'].each do |t| - Flapjack::Data::Tag.create("#{TAG_PREFIX}:#{t}", [contact_id], :redis => redis) - end - end unless contact_data['media'].nil? redis.del("contact_media:#{contact_id}") redis.del("contact_media_intervals:#{contact_id}") redis.del("contact_media_rollup_thresholds:#{contact_id}")