lib/flapjack/data/notification_rule.rb in flapjack-0.8.11 vs lib/flapjack/data/notification_rule.rb in flapjack-0.8.12
- old
+ new
@@ -14,46 +14,66 @@
attr_accessor :id, :contact_id, :entities, :regex_entities, :tags, :regex_tags,
:time_restrictions, :unknown_media, :warning_media, :critical_media,
:unknown_blackhole, :warning_blackhole, :critical_blackhole
+ def self.all(options = {})
+ raise "Redis connection not set" unless redis = options[:redis]
+ redis.keys("contact_notification_rules:*").inject([]) do |memo, contact_key|
+ redis.smembers(contact_key).each do |rule_id|
+ ret = self.find_by_id(rule_id, :redis => redis)
+ memo << ret unless ret.nil?
+ end
+ memo
+ end
+ end
+
def self.exists_with_id?(rule_id, options = {})
raise "Redis connection not set" unless redis = options[:redis]
raise "No id value passed" unless not (rule_id.nil? || rule_id == '')
- logger = options[:logger]
redis.exists("notification_rule:#{rule_id}")
end
def self.find_by_id(rule_id, options = {})
raise "Redis connection not set" unless redis = options[:redis]
raise "No id value passed" unless not (rule_id.nil? || rule_id == '')
- logger = options[:logger]
# sanity check
return unless redis.exists("notification_rule:#{rule_id}")
self.new({:id => rule_id.to_s}, {:redis => redis})
end
+ def self.find_by_ids(rule_ids, options = {})
+ raise "Redis connection not set" unless redis = options[:redis]
+
+ rule_ids.map do |id|
+ self.find_by_id(id, options)
+ end
+ end
+
# replacing save! etc
def self.add(rule_data, options = {})
raise "Redis connection not set" unless redis = options[:redis]
if rule_data[:id] && self.find_by_id(rule_data[:id], :redis => redis)
errors = ["a notification rule already exists with id '#{rule_data[:id]}'"]
return errors
end
rule_id = rule_data[:id] || SecureRandom.uuid
- errors = self.add_or_update(rule_data.merge(:id => rule_id), options)
+
+ errors = self.add_or_update(rule_data.merge(:id => rule_id), :redis => redis, :logger => options[:logger])
return errors unless errors.nil? || errors.empty?
+
self.find_by_id(rule_id, :redis => redis)
end
- def update(rule_data, opts = {})
- errors = self.class.add_or_update({:contact_id => @contact_id}.merge(rule_data.merge(:id => @id)),
- :redis => @redis, :logger => opts[:logger])
+ def update(update_data, opts = {})
+ rule_data = @redis.hgetall("notification_rule:#{@id}").merge(update_data.merge(:id => @id))
+ errors = self.class.add_or_update(rule_data, :redis => @redis, :logger => opts[:logger])
return errors unless errors.nil? || errors.empty?
+
refresh
nil
end
# NB: ice_cube doesn't have much rule data validation, and has
@@ -67,11 +87,10 @@
# but we do need to apply some sanity checking on the passed data.
def self.time_restriction_to_icecube_schedule(tr, timezone)
return unless !tr.nil? && tr.is_a?(Hash)
return if timezone.nil? && !timezone.is_a?(ActiveSupport::TimeZone)
return unless tr = prepare_time_restriction(tr, timezone)
-
IceCube::Schedule.from_hash(tr)
end
def to_json(*args)
self.class.hashify(:id, :contact_id, :tags, :regex_tags, :entities, :regex_entities,
@@ -214,42 +233,37 @@
def self.prepare_time_restriction(time_restriction, timezone = nil)
# this will hand back a 'deep' copy
tr = symbolize(time_restriction)
- return unless tr.has_key?(:start_time) && tr.has_key?(:end_time)
+ return unless (tr.has_key?(:start_time) || tr.has_key?(:start_date)) &&
+ (tr.has_key?(:end_time) || tr.has_key?(:end_date))
- parsed_time = proc {|t|
- if t.is_a?(Time)
- t
+ parsed_time = proc {|tr, field|
+ if t = tr.delete(field)
+ t = t.dup
+ t = t[:time] if t.is_a?(Hash)
+
+ if t.is_a?(Time)
+ t
+ else
+ begin; (timezone || Time).parse(t); rescue ArgumentError; nil; end
+ end
else
- begin; (timezone || Time).parse(t); rescue ArgumentError; nil; end
+ nil
end
}
- start_time = case tr[:start_time]
- when String, Time
- parsed_time.call(tr.delete(:start_time).dup)
- when Hash
- time_hash = tr.delete(:start_time).dup
- parsed_time.call(time_hash[:time])
- end
+ start_time = parsed_time.call(tr, :start_date) || parsed_time.call(tr, :start_time)
+ end_time = parsed_time.call(tr, :end_date) || parsed_time.call(tr, :end_time)
- end_time = case tr[:end_time]
- when String, Time
- parsed_time.call(tr.delete(:end_time).dup)
- when Hash
- time_hash = tr.delete(:end_time).dup
- parsed_time.call(time_hash[:time])
- end
-
return unless start_time && end_time
- tr[:start_date] = timezone ?
+ tr[:start_time] = timezone ?
{:time => start_time, :zone => timezone.name} :
start_time
- tr[:end_date] = timezone ?
+ tr[:end_time] = timezone ?
{:time => end_time, :zone => timezone.name} :
end_time
tr[:duration] = end_time - start_time