From f1608b8098d0302f7e5c0e7989c78fa310d7d424 Mon Sep 17 00:00:00 2001 From: Patrick J Cherry Date: Wed, 10 Aug 2011 16:09:08 +0100 Subject: Removed MauveTime. Added more tests. --- lib/mauve/alert.rb | 24 ++++++------ lib/mauve/alert_changed.rb | 4 +- lib/mauve/authentication.rb | 4 +- lib/mauve/calendar_interface.rb | 2 +- lib/mauve/heartbeat.rb | 4 +- lib/mauve/mauve_time.rb | 38 +++++++++---------- lib/mauve/notification.rb | 14 ++++--- lib/mauve/person.rb | 8 ++-- lib/mauve/processor.rb | 4 +- lib/mauve/sender.rb | 4 +- lib/mauve/server.rb | 2 +- lib/mauve/source_list.rb | 4 +- lib/mauve/timer.rb | 4 +- lib/mauve/udp_server.rb | 2 +- lib/mauve/web_interface.rb | 4 +- test/tc_mauve_notification.rb | 83 ++++++++++++++++++++++++++++++++++++++--- 16 files changed, 138 insertions(+), 67 deletions(-) diff --git a/lib/mauve/alert.rb b/lib/mauve/alert.rb index 20a0861..6f6f3d1 100644 --- a/lib/mauve/alert.rb +++ b/lib/mauve/alert.rb @@ -24,7 +24,7 @@ module Mauve # http://www.mail-archive.com/datamapper@googlegroups.com/msg02314.html # def self.create_view! - the_distant_future = MauveTime.now + 86400000 # it is the year 2000 - the humans are dead + the_distant_future = Time.now + 86400000 # it is the year 2000 - the humans are dead ["BEGIN TRANSACTION", "DROP VIEW IF EXISTS mauve_alert_earliest_dates", "CREATE VIEW @@ -244,7 +244,7 @@ module Mauve raise ArgumentError, "Cannot acknowledge a cleared alert" if self.cleared? self.acknowledged_by = person.username - self.acknowledged_at = MauveTime.now + self.acknowledged_at = Time.now self.will_unacknowledge_at = ack_until self.update_type = "acknowledged" @@ -260,7 +260,7 @@ module Mauve logger.error("Couldn't save #{self}") unless save end - def raise!(at = MauveTime.now) + def raise!(at = Time.now) self.acknowledged_by = nil self.acknowledged_at = nil self.will_unacknowledge_at = nil @@ -273,7 +273,7 @@ module Mauve logger.error("Couldn't save #{self}") unless save end - def clear!(at = MauveTime.now) + def clear!(at = Time.now) self.acknowledged_by = nil self.acknowledged_at = nil self.will_unacknowledge_at = nil @@ -295,9 +295,9 @@ module Mauve end def poll - raise! if (will_unacknowledge_at and will_unacknowledge_at.to_time <= MauveTime.now) or - (will_raise_at and will_raise_at.to_time <= MauveTime.now) - clear! if will_clear_at && will_clear_at.to_time <= MauveTime.now + raise! if (will_unacknowledge_at and will_unacknowledge_at.to_time <= Time.now) or + (will_raise_at and will_raise_at.to_time <= Time.now) + clear! if will_clear_at && will_clear_at.to_time <= Time.now logger.info("Polled #{self.inspect}") end @@ -382,7 +382,7 @@ module Mauve earliest_alert ? earliest_alert.alert : nil end - def all_overdue(at = MauveTime.now) + def all_overdue(at = Time.now) AlertEarliestDate.all(:earliest.lt => at, :order => [:earliest]).collect do |earliest_alert| earliest_alert ? earliest_alert.alert : nil end @@ -391,7 +391,7 @@ module Mauve # # Receive an AlertUpdate buffer from the wire. # - def receive_update(update, reception_time = MauveTime.now, ip_source="network") + def receive_update(update, reception_time = Time.now, ip_source="network") update = Proto::AlertUpdate.parse_from_string(update) unless update.kind_of?(Proto::AlertUpdate) @@ -403,7 +403,7 @@ module Mauve # Transmission time helps us determine any time offset # if update.transmission_time and update.transmission_time > 0 - transmission_time = MauveTime.at(update.transmission_time) + transmission_time = Time.at(update.transmission_time) else transmission_time = reception_time end @@ -422,8 +422,8 @@ module Mauve # Infer some actions from our pure data structure (hmm, wonder if # this belongs in our protobuf-derived class? # - clear_time = alert.clear_time == 0 ? nil : MauveTime.at(alert.clear_time + time_offset) - raise_time = alert.raise_time == 0 ? nil : MauveTime.at(alert.raise_time + time_offset) + clear_time = alert.clear_time == 0 ? nil : Time.at(alert.clear_time + time_offset) + raise_time = alert.raise_time == 0 ? nil : Time.at(alert.raise_time + time_offset) if raise_time.nil? && clear_time.nil? # diff --git a/lib/mauve/alert_changed.rb b/lib/mauve/alert_changed.rb index 40bcad1..ef062be 100644 --- a/lib/mauve/alert_changed.rb +++ b/lib/mauve/alert_changed.rb @@ -158,7 +158,7 @@ module Mauve end def poll # mimic interface from Alert - remind if remind_at.to_time <= MauveTime.now + remind if remind_at.to_time <= Time.now end class << self @@ -170,7 +170,7 @@ module Mauve next_reminder end - def all_overdue(at = MauveTime.now) + def all_overdue(at = Time.now) all(:remind_at.not => nil, :remind_at.lt => at, :order => [:remind_at]).to_a end end diff --git a/lib/mauve/authentication.rb b/lib/mauve/authentication.rb index f01fb5e..74c6780 100644 --- a/lib/mauve/authentication.rb +++ b/lib/mauve/authentication.rb @@ -77,12 +77,12 @@ module Mauve ## Not really needed. def ping () begin - MauveTimeout.timeout(@timeout) do + Timeout.timeout(@timeout) do s = TCPSocket.open(@srv, @port) s.close() return true end - rescue MauveTimeout::Error => ex + rescue Timeout::Error => ex return false rescue => ex return false diff --git a/lib/mauve/calendar_interface.rb b/lib/mauve/calendar_interface.rb index ab2bc5b..20c884c 100644 --- a/lib/mauve/calendar_interface.rb +++ b/lib/mauve/calendar_interface.rb @@ -119,7 +119,7 @@ module Mauve rescue Errno::EHOSTUNREACH => ex logger.warn("no route to host.") return Array.new - rescue MauveTimeout::Error => ex + rescue Timeout::Error => ex logger.warn("time out reached.") return Array.new rescue ArgumentError => ex diff --git a/lib/mauve/heartbeat.rb b/lib/mauve/heartbeat.rb index add0cdf..39d3499 100644 --- a/lib/mauve/heartbeat.rb +++ b/lib/mauve/heartbeat.rb @@ -66,8 +66,8 @@ module Mauve message.id = "mauve-heartbeat" message.summary = self.summary message.detail = self.detail - message.raise_time = (MauveTime.now.to_f+self.raise_after).to_i - message.clear_time = MauveTime.now.to_i + message.raise_time = (Time.now.to_f+self.raise_after).to_i + message.clear_time = Time.now.to_i update.alert << message diff --git a/lib/mauve/mauve_time.rb b/lib/mauve/mauve_time.rb index e51cf6d..4435098 100644 --- a/lib/mauve/mauve_time.rb +++ b/lib/mauve/mauve_time.rb @@ -17,14 +17,14 @@ class Integer end class Date - def to_mauvetime - Mauve::MauveTime.parse(self.to_s) + def to_time + Time.parse(self.to_s) end end class DateTime - def to_mauvetime - Mauve::MauveTime.parse(self.to_s) + def to_time + Time.parse(self.to_s) end def to_s_relative(*args) @@ -42,10 +42,6 @@ class DateTime end class Time - def to_mauvetime - Mauve::MauveTime.at(self.to_i,self.usec) - end - def in_x_hours(n, type="wallclock") t = self.dup # @@ -204,16 +200,16 @@ class Time end -module Mauve - class MauveTime < Time - - def to_s - self.iso8601 - end - - def to_mauvetime - self - end - - end -end +#module Mauve +# class Time < Time +# +# def to_s +# self.iso8601 +# end +# +# def to_mauvetime +# self +# end +# +# end +#end diff --git a/lib/mauve/notification.rb b/lib/mauve/notification.rb index adb61c3..71dd69c 100644 --- a/lib/mauve/notification.rb +++ b/lib/mauve/notification.rb @@ -15,13 +15,13 @@ module Mauve # # ... later on ... # - # DuringRunner.new(MauveTime.now, my_alert, &during).inside? + # DuringRunner.new(Time.now, my_alert, &during).inside? # # ... or to ask when an alert will next be cued ... # - # DuringRunner.new(MauveTime.now, my_alert, &during).find_next + # DuringRunner.new(Time.now, my_alert, &during).find_next # - # which will return a MauveTime object, or nil if the time period will + # which will return a Time object, or nil if the time period will # not be valid again, at least not in the next week. # class DuringRunner @@ -171,7 +171,7 @@ module Mauve end # Should we notify at all? - is_relevant = DuringRunner.new(MauveTime.now, alert, &during).now? + is_relevant = DuringRunner.new(Time.now, alert, &during).now? people.collect do |person| case person @@ -191,8 +191,10 @@ module Mauve end def remind_at_next(alert) - return nil unless alert.raised? - DuringRunner.new(MauveTime.now, alert, &during).find_next(every) + + return DuringRunner.new(Time.now, alert, &during).find_next(every) if alert.raised? + + return nil end end diff --git a/lib/mauve/person.rb b/lib/mauve/person.rb index 58d696c..9d541a9 100644 --- a/lib/mauve/person.rb +++ b/lib/mauve/person.rb @@ -159,7 +159,7 @@ module Mauve end def do_send_alert(level, alert) - now = MauveTime.now + now = Time.now was_suppressed = self.suppressed? @@ -168,7 +168,7 @@ module Mauve # Choose the second one as the first. # first = previous_alert_times[1] - first.is_a?(MauveTime) and (now - first) < period + first.is_a?(Time) and (now - first) < period end if self.suppressed? @@ -179,7 +179,7 @@ module Mauve end - if Server.instance.started_at > alert.updated_at.to_time and (Server.instance.started_at + Server.instance.initial_sleep) > MauveTime.now + if Server.instance.started_at > alert.updated_at.to_time and (Server.instance.started_at + Server.instance.initial_sleep) > Time.now logger.info("Alert last updated in prior run of mauve -- ignoring for initial sleep period.") return true end @@ -211,7 +211,7 @@ module Mauve # # Hmm.. not sure how to make this thread-safe. # - @notification_thresholds[period].push MauveTime.now + @notification_thresholds[period].push Time.now @notification_thresholds[period].shift end true diff --git a/lib/mauve/processor.rb b/lib/mauve/processor.rb index 059ca15..86ed5dd 100644 --- a/lib/mauve/processor.rb +++ b/lib/mauve/processor.rb @@ -74,7 +74,7 @@ module Mauve logger.debug ex.backtrace.join("\n") ensure - @transmission_id_cache[update.transmission_id.to_s] = MauveTime.now + @transmission_id_cache[update.transmission_id.to_s] = Time.now end end @@ -87,7 +87,7 @@ module Mauve end def expire_transmission_id_cache - now = MauveTime.now + now = Time.now # # Only check once every minute. # diff --git a/lib/mauve/sender.rb b/lib/mauve/sender.rb index ef06d0f..6685de5 100644 --- a/lib/mauve/sender.rb +++ b/lib/mauve/sender.rb @@ -115,13 +115,13 @@ module Mauve # update.alert.each do |alert| next if alert.raise_time || alert.clear_time - alert.raise_time = MauveTime.now.to_i + alert.raise_time = Time.now.to_i end # # Make sure we set the transmission time # - update.transmission_time = MauveTime.now.to_i + update.transmission_time = Time.now.to_i data = update.serialize_to_string diff --git a/lib/mauve/server.rb b/lib/mauve/server.rb index 34ea155..5cc8484 100644 --- a/lib/mauve/server.rb +++ b/lib/mauve/server.rb @@ -34,7 +34,7 @@ module Mauve @hostname = "localhost" @database = "sqlite3:///./mauvealert.db" - @started_at = MauveTime.now + @started_at = Time.now @initial_sleep = 300 # diff --git a/lib/mauve/source_list.rb b/lib/mauve/source_list.rb index c905786..caa9f9a 100644 --- a/lib/mauve/source_list.rb +++ b/lib/mauve/source_list.rb @@ -90,7 +90,7 @@ module Mauve # # Redo resolution every thirty minutes # - resolve if @resolved_list.empty? or @last_resolved_at.nil? or (MauveTime.now - 1800) > @last_resolved_at + resolve if @resolved_list.empty? or @last_resolved_at.nil? or (Time.now - 1800) > @last_resolved_at # # Pick out hostnames from URIs. @@ -136,7 +136,7 @@ module Mauve end def resolve - @last_resolved_at = MauveTime.now + @last_resolved_at = Time.now new_list = @list.collect do |host| if host.is_a?(String) diff --git a/lib/mauve/timer.rb b/lib/mauve/timer.rb index 1c8c37c..00af255 100644 --- a/lib/mauve/timer.rb +++ b/lib/mauve/timer.rb @@ -21,7 +21,7 @@ module Mauve # If we didn't find an alert, or the alert we found is due in the future, # look for the next alert_changed object. # - if next_alert.nil? or next_alert.due_at > MauveTime.now + if next_alert.nil? or next_alert.due_at > Time.now next_alert_changed = AlertChanged.find_next_with_event end @@ -50,7 +50,7 @@ module Mauve # La la la nothing to do. # logger.info("Next to notify: #{next_to_notify} #{next_to_notify.is_a?(AlertChanged) ? "(reminder)" : "(heartbeat)"} -- snoozing until #{next_to_notify.due_at.iso8601}") - sleep_loops = ((next_to_notify.due_at - MauveTime.now).to_f / 0.1).round.to_i + sleep_loops = ((next_to_notify.due_at - Time.now).to_f / 0.1).round.to_i end sleep_loops = 1 if sleep_loops.nil? or sleep_loops < 1 diff --git a/lib/mauve/udp_server.rb b/lib/mauve/udp_server.rb index 4e6296d..99bfab1 100644 --- a/lib/mauve/udp_server.rb +++ b/lib/mauve/udp_server.rb @@ -80,7 +80,7 @@ module Mauve begin # packet = @socket.recvfrom_nonblock(65535) packet = @socket.recvfrom(65535) - received_at = MauveTime.now + received_at = Time.now rescue Errno::EAGAIN, Errno::EWOULDBLOCK => ex IO.select([@socket]) retry unless self.should_stop? diff --git a/lib/mauve/web_interface.rb b/lib/mauve/web_interface.rb index 805a3e3..bf2c667 100644 --- a/lib/mauve/web_interface.rb +++ b/lib/mauve/web_interface.rb @@ -222,7 +222,7 @@ EOF a.acknowledge!(@person, ack_until) logger.debug note unless note.to_s.empty? - h = History.new(:alert_id => a.id, :type => "note", :event => note.to_s) + h = History.new(:alerts => [a], :type => "note", :event => note.to_s) logger.debug h.errors unless h.save end succeeded << a @@ -455,7 +455,7 @@ EOF end def find_recent_alerts - since = params['since'] ? MauveTime.parse(params['since']) : (MauveTime.now-86400) + since = params['since'] ? Time.parse(params['since']) : (Time.now-86400) @alerts = Alert.all(:updated_at.gt => since, :order => [:raised_at.desc, :cleared_at.desc, :acknowledged_at.desc, :updated_at.desc, ]) end diff --git a/test/tc_mauve_notification.rb b/test/tc_mauve_notification.rb index 3414292..03e6b96 100644 --- a/test/tc_mauve_notification.rb +++ b/test/tc_mauve_notification.rb @@ -5,8 +5,11 @@ require 'mauve/alert' require 'mauve/notification' require 'mauve/configuration' require 'mauve/configuration_builder' +require 'mauve/configuration_builders' require 'mauve/mauve_time' require 'th_mauve_resolv' +require 'th_mauve_time' +require 'th_logger' require 'pp' @@ -53,7 +56,7 @@ class TcMauveDuringRunner < Test::Unit::TestCase # # This should give us midnight last sunday night. # - now = Mauve::MauveTime.now + now = Time.now midnight_sunday = now - (now.hour.hours + now.min.minutes + now.sec.seconds + now.wday.days) # @@ -101,11 +104,81 @@ end class TcMauveNotification < Test::Unit::TestCase def test_notify - end + t = Time.now + + config=<= #{(t + 1.hour).to_i} } + every 10.minutes + } + + notify("test3") { + during { unacknowledged( 2.hours ) } + every 10.minutes + } + +} +EOF - def remind_at_next + + assert_nothing_raised { + Mauve::Configuration.current = Mauve::ConfigurationBuilder.parse(config) + Mauve::Server.instance.setup + alert = Mauve::Alert.new( + :alert_id => "test", + :source => "test", + :subject => "test" + ) + alert.raise! + } + + assert_equal(1, Mauve::Alert.count) + + reminder_times = { + "test1" => t + 10.minutes, + "test2" => t + 1.hour, + "test3" => t + 2.hours + } + + Mauve::AlertChanged.all.each do |a| + pp a + assert_equal("urgent", a.level, "Level is wrong") + assert_equal("raised", a.update_type, "Update type is wrong") + assert_in_delta(reminder_times[a.person].to_f, a.remind_at.to_time.to_f, 10.0, "reminder time is wrong for #{a.person}") + end end end - - -- cgit v1.2.1