lib/flapjack/data/contact.rb in flapjack-0.7.35 vs lib/flapjack/data/contact.rb in flapjack-0.8.0

- old
+ new

@@ -8,17 +8,21 @@ require 'flapjack/data/entity' require 'flapjack/data/notification_rule' require 'flapjack/data/tag' require 'flapjack/data/tag_set' +require 'securerandom' + module Flapjack module Data class Contact - attr_accessor :id, :first_name, :last_name, :email, :media, :media_intervals, :media_rollup_thresholds, :pagerduty_credentials + attr_accessor :id, :first_name, :last_name, :email, :media, + :media_intervals, :media_rollup_thresholds, :pagerduty_credentials, + :linked_entity_ids TAG_PREFIX = 'contact_tag' ALL_MEDIA = ['email', 'sms', 'jabber', 'pagerduty'] def self.all(options = {}) @@ -44,10 +48,26 @@ contact = self.new(:id => contact_id, :redis => redis, :logger => logger) contact.refresh contact end + def self.find_by_ids(contact_ids, options = {}) + raise "Redis connection not set" unless redis = options[:redis] + logger = options[:logger] + + contact_ids.map do |id| + self.find_by_id(id, options) + end + end + + def self.exists_with_id?(contact_id, options = {}) + raise "Redis connection not set" unless redis = options[:redis] + raise "No id value passed" unless contact_id + + redis.exists("contact:#{contact_id}") + end + def self.add(contact_data, options = {}) raise "Redis connection not set" unless redis = options[:redis] contact_id = contact_data['id'] raise "Contact id value not provided" if contact_id.nil? @@ -167,10 +187,40 @@ end ret }.values end + def self.entities_jsonapi(contact_ids, options = {}) + raise "Redis connection not set" unless redis = options[:redis] + + entity_data = [] + linked_entity_ids = {} + + temp_set = SecureRandom.uuid + redis.sadd(temp_set, contact_ids) + + redis.keys('contacts_for:*').each do |k| + contact_ids = redis.sinter(k, temp_set) + next if contact_ids.empty? + next unless k =~ /^contacts_for:([a-zA-Z0-9][a-zA-Z0-9\.\-]*[a-zA-Z0-9])(?::(\w+))?$/ + + entity_id = $1 + check = $2 + + entity_data << {:id => entity_id, :name => redis.hget("entity:#{entity_id}", 'name')} + + contact_ids.each do |contact_id| + linked_entity_ids[contact_id] ||= [] + linked_entity_ids[contact_id] << entity_id + end + end + + redis.del(temp_set) + + [entity_data, linked_entity_ids] + end + def name [(self.first_name || ''), (self.last_name || '')].join(" ").strip end # return an array of the notification rules of this contact @@ -406,15 +456,21 @@ 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, - "tags" => self.tags.to_a }.to_json + { "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, + "links" => {:entities => @linked_entity_ids || []} + }.to_json end private def initialize(options = {}) @@ -431,10 +487,16 @@ # TODO check that the rest of this is safe for the update case redis.hmset("contact:#{contact_id}", *['first_name', 'last_name', 'email'].collect {|f| [f, contact_data[f]]}) + 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}") redis.del("contact_pagerduty:#{contact_id}") @@ -449,9 +511,19 @@ redis.hset("contact_media:#{contact_id}", medium, details['address']) redis.hset("contact_media_intervals:#{contact_id}", medium, details['interval']) if details['interval'] redis.hset("contact_media_rollup_thresholds:#{contact_id}", medium, details['rollup_threshold']) if details['rollup_threshold'] end } + end + if contact_data.key?('timezone') + tz = contact_data['timezone'] + if tz.nil? + redis.del("contact_tz:#{contact_id}") + else + # ActiveSupport::TimeZone or String + redis.set("contact_tz:#{contact_id}", + tz.respond_to?(:name) ? tz.name : tz ) + end end end end