From 6f689f2fa872f507527c10fa9b44cdbb96ebd3e0 Mon Sep 17 00:00:00 2001 From: Patrick J Cherry Date: Thu, 3 Nov 2011 16:47:32 +0000 Subject: Fixed everything up to use EpochTime everywhere instead of regular Times. Datamapper can't handle timezones. --- lib/mauve/alert.rb | 39 ++++++++++++++++++++++++--------------- lib/mauve/alert_changed.rb | 15 ++++++++++++--- lib/mauve/datamapper.rb | 7 +++---- lib/mauve/history.rb | 6 +++--- lib/mauve/server.rb | 15 +++++++++++++-- 5 files changed, 55 insertions(+), 27 deletions(-) (limited to 'lib/mauve') diff --git a/lib/mauve/alert.rb b/lib/mauve/alert.rb index 25cee54..37a8f2e 100644 --- a/lib/mauve/alert.rb +++ b/lib/mauve/alert.rb @@ -16,7 +16,7 @@ module Mauve property :id, Serial property :alert_id, Integer - property :earliest, Time + property :earliest, EpochTime belongs_to :alert, :model => "Alert" # 1) Shame we can't get this called automatically from DataMapper.auto_upgrade! @@ -28,7 +28,7 @@ module Mauve # http://www.mail-archive.com/datamapper@googlegroups.com/msg02314.html # def self.create_view! - the_distant_future = Time.now + 2000.days # it is the year 2000 - the humans are dead + the_distant_future = (Time.now + 2000.days).to_i # it is the year 2000 - the humans are dead ["BEGIN TRANSACTION", "DROP VIEW IF EXISTS mauve_alert_earliest_dates", "CREATE VIEW @@ -76,21 +76,22 @@ module Mauve property :detail, Text, :length=>65535 property :importance, Integer, :default => 50 - property :raised_at, Time - property :cleared_at, Time - property :updated_at, Time - property :acknowledged_at, Time + property :raised_at, EpochTime + property :cleared_at, EpochTime + property :updated_at, EpochTime + property :acknowledged_at, EpochTime property :acknowledged_by, String, :lazy => false property :update_type, String, :lazy => false - property :will_clear_at, Time - property :will_raise_at, Time - property :will_unacknowledge_at, Time + property :will_clear_at, EpochTime + property :will_raise_at, EpochTime + property :will_unacknowledge_at, EpochTime has n, :changes, :model => AlertChanged has n, :histories, :through => :alerthistory has 1, :alert_earliest_date + before :valid?, :do_set_timestamps before :save, :do_sanitize_html before :save, :take_copy_of_changes after :save, :notify_if_needed @@ -204,7 +205,11 @@ module Mauve attribute_set(key, Alert.clean_html(val)) end end - + + def do_set_timestamps(context = :default) + self.updated_at = Time.now unless self.original_attributes.has_key?("updated_at") + end + # This is to stop datamapper inserting duff dates into the database. # def check_dates @@ -255,7 +260,7 @@ module Mauve h = History.new(:alerts => [self], :type => "update") if self.update_type == "acknowledged" - h.event = "ACKNOWLEDGEDuntil #{self.will_unacknowledge_at}" + h.event = "ACKNOWLEDGED until #{self.will_unacknowledge_at}" h.user = self.acknowledged_by elsif is_a_change @@ -378,7 +383,7 @@ module Mauve # Don't clear will_clear_at self.update_type = "raised" if self.update_type.nil? or self.update_type != "changed" or self.original_attributes[Alert.properties[:update_type]] == "cleared" end - + unless save logger.error("Couldn't save #{self}") false @@ -386,7 +391,7 @@ module Mauve true end end - + # Clear an alert at a specified time # # @param [Time] at The time at which the alert should be cleared. @@ -576,7 +581,11 @@ module Mauve # @return [NilClass] def receive_update(update, reception_time = Time.now, ip_source="network") - update = Proto::AlertUpdate.parse_from_string(update) unless update.kind_of?(Proto::AlertUpdate) + unless update.kind_of?(Proto::AlertUpdate) + new_update = Proto::AlertUpdate.new + new_update.parse_from_string(update) + update = new_update + end alerts_updated = [] @@ -679,7 +688,7 @@ module Mauve alert_db.importance = alert.importance if alert.importance != 0 - alert_db.updated_at = reception_time + alert_db.updated_at = reception_time if alert_db.raised? # diff --git a/lib/mauve/alert_changed.rb b/lib/mauve/alert_changed.rb index c36c4c8..9b2bea4 100644 --- a/lib/mauve/alert_changed.rb +++ b/lib/mauve/alert_changed.rb @@ -15,14 +15,23 @@ module Mauve property :id, Serial property :alert_id, Integer, :required => true property :person, String, :required => true - property :at, Time, :required => true + property :at, EpochTime, :required => true property :was_relevant, Boolean, :required => true, :default => true property :level, String, :required => true property :update_type, String, :required => true - property :remind_at, Time + property :remind_at, EpochTime, :required => false belongs_to :alert - + + before :valid?, :do_set_timestamps + + protected + + def do_set_timestamps(context = :default) + self.at = Time.now unless self.original_attributes.has_key?("at") + end + + public # @return [String] def to_s diff --git a/lib/mauve/datamapper.rb b/lib/mauve/datamapper.rb index 12f95dc..abf56c7 100644 --- a/lib/mauve/datamapper.rb +++ b/lib/mauve/datamapper.rb @@ -4,12 +4,11 @@ # # require 'dm-core' -require 'dm-validations' +require 'dm-migrations' +require 'dm-serializer' require 'dm-sqlite-adapter-with-mutex' require 'dm-types' -require 'dm-serializer' -require 'dm-migrations' -require 'dm-timestamps' +require 'dm-validations' # DataMapper::Model.raise_on_save_failure = true diff --git a/lib/mauve/history.rb b/lib/mauve/history.rb index d9ab8e9..bcdd41a 100644 --- a/lib/mauve/history.rb +++ b/lib/mauve/history.rb @@ -80,11 +80,11 @@ module Mauve property :type, String, :required => true, :default => "unknown", :lazy => false property :event, Text, :required => true, :default => "Nothing set", :lazy => false property :user, String - property :created_at, Time, :required => true + property :created_at, EpochTime, :required => true has n, :alerts, :through => :alerthistory - before :valid?, :set_created_at + before :valid?, :do_set_created_at before :save, :do_sanitize_html protected @@ -110,7 +110,7 @@ module Mauve # Update the created_at time on the object # - def set_created_at(context = :default) + def do_set_created_at(context = :default) self.created_at = Time.now unless self.created_at.is_a?(Time) end diff --git a/lib/mauve/server.rb b/lib/mauve/server.rb index 41a76dc..233695d 100644 --- a/lib/mauve/server.rb +++ b/lib/mauve/server.rb @@ -108,14 +108,25 @@ module Mauve # # Update any tables. # - Mauve.constants.each do |c| + Mauve.constants.each do |c| next if %w(AlertEarliestDate).include?(c) m = Mauve.const_get(c) - m.auto_upgrade! if m.respond_to?("auto_upgrade!") + next unless m.respond_to?("auto_upgrade!") + m.auto_upgrade! # # Don't want to use automigrate, since this trashes the tables. # # m.auto_migrate! if m.respond_to?("auto_migrate!") + # + # + m.properties.each do |prop| + next unless prop.is_a?(DataMapper::Property::EpochTime) + logger.info("Updating #{c}.#{prop.name}") + statement = "UPDATE mauve_#{DataMapper::Inflector.tableize(c)} SET #{prop.name} = strftime(\"%s\",#{prop.name}) WHERE #{prop.name} LIKE \"%-%-%\";" + DataMapper.repository(:default).adapter.execute("BEGIN TRANSACTION;") + DataMapper.repository(:default).adapter.execute(statement) + DataMapper.repository(:default).adapter.execute("COMMIT TRANSACTION;") + end end AlertHistory.migrate! -- cgit v1.2.1