diff options
author | Patrick J Cherry <patrick@bytemark.co.uk> | 2011-11-03 16:48:52 +0000 |
---|---|---|
committer | Patrick J Cherry <patrick@bytemark.co.uk> | 2011-11-03 16:48:52 +0000 |
commit | 4d6b2e47ac5b135097bc8eb7b2b1ebae65668647 (patch) | |
tree | 8f6174fafcc7c356087fc209991b89dea0c3295d /lib | |
parent | f93f0948e028c33694cf7f90c7d5918cbf27971c (diff) | |
parent | 6f689f2fa872f507527c10fa9b44cdbb96ebd3e0 (diff) |
Merge
Diffstat (limited to 'lib')
-rw-r--r-- | lib/mauve/alert.rb | 39 | ||||
-rw-r--r-- | lib/mauve/alert_changed.rb | 15 | ||||
-rw-r--r-- | lib/mauve/datamapper.rb | 7 | ||||
-rw-r--r-- | lib/mauve/history.rb | 6 | ||||
-rw-r--r-- | lib/mauve/server.rb | 15 | ||||
-rw-r--r-- | lib/mauve/web_interface.rb | 13 |
6 files changed, 67 insertions, 28 deletions
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! diff --git a/lib/mauve/web_interface.rb b/lib/mauve/web_interface.rb index 716aaf3..b998ad8 100644 --- a/lib/mauve/web_interface.rb +++ b/lib/mauve/web_interface.rb @@ -5,7 +5,18 @@ require 'json' require 'mauve/authentication' -require 'sinatra/tilt' +tilt_lib = "tilt" +begin + require tilt_lib +rescue LoadError => ex + if tilt_lib == "tilt" + tilt_lib = "sinatra/tilt" + retry + end + + raise ex +end + require 'sinatra/base' require 'sinatra-partials' |