From 1a636d2c97368bc4c4019f4ddcff991ec5eccbce Mon Sep 17 00:00:00 2001 From: Patrick J Cherry Date: Tue, 23 Aug 2011 18:35:48 +0100 Subject: Added events viewer * Fixes #1286 * Version bump --- lib/mauve/alert.rb | 12 ++-- lib/mauve/history.rb | 33 ++++++++-- lib/mauve/version.rb | 2 +- lib/mauve/web_interface.rb | 148 +++++++++++++++++++++++++++++++++++++++++++-- 4 files changed, 177 insertions(+), 18 deletions(-) (limited to 'lib/mauve') diff --git a/lib/mauve/alert.rb b/lib/mauve/alert.rb index c294e8c..75aa35e 100644 --- a/lib/mauve/alert.rb +++ b/lib/mauve/alert.rb @@ -60,10 +60,10 @@ module Mauve include DataMapper::Resource property :id, Serial - property :alert_id, String, :required => true, :unique_index => :alert_index, :length=>256 - property :source, String, :required => true, :unique_index => :alert_index, :length=>512 - property :subject, String, :length=>512 - property :summary, String, :length=>1024 + property :alert_id, String, :required => true, :unique_index => :alert_index, :length=>256, :lazy => false + property :source, String, :required => true, :unique_index => :alert_index, :length=>512, :lazy => false + property :subject, String, :length=>512, :lazy => false + property :summary, String, :length=>1024, :lazy => false property :detail, Text, :length=>65535 property :importance, Integer, :default => 50 @@ -71,8 +71,8 @@ module Mauve property :cleared_at, Time property :updated_at, Time property :acknowledged_at, Time - property :acknowledged_by, String - property :update_type, String + property :acknowledged_by, String, :lazy => false + property :update_type, String, :lazy => false property :will_clear_at, Time property :will_raise_at, Time diff --git a/lib/mauve/history.rb b/lib/mauve/history.rb index 91a6fdc..74c5e3a 100644 --- a/lib/mauve/history.rb +++ b/lib/mauve/history.rb @@ -10,8 +10,8 @@ module Mauve property :alert_id, Integer, :key => true property :history_id, Integer, :key => true - belongs_to :alert - belongs_to :history + belongs_to :alert + belongs_to :history def self.migrate! # @@ -53,8 +53,8 @@ module Mauve # If these properties change.. then the migration above will break horribly. FIXME. # property :id, Serial - property :type, String, :required => true, :default => "unknown" - property :event, Text, :required => true, :default => "Nothing set" + property :type, String, :required => true, :default => "unknown", :lazy => false + property :event, Text, :required => true, :default => "Nothing set", :lazy => false property :created_at, Time, :required => true has n, :alerts, :through => :alerthistory @@ -88,9 +88,32 @@ module Mauve def set_created_at(context = :default) self.created_at = Time.now unless self.created_at.is_a?(Time) end - + public + # + # Blasted datamapper not eager-loading my model. + # + def add_to_cached_alerts(a) + @cached_alerts ||= [] + if a.is_a?(Array) and a.all?{|m| m.is_a?(Alert)} + @cached_alerts += a + elsif a.is_a?(Alert) + @cached_alerts << a + else + raise ArgumentError, "#{a.inspect} not an Alert" + end + end + + def alerts + @cached_alerts ||= super + end + + def reload + @cached_alerts = nil + super + end + def logger Log4r::Logger.new self.class.to_s end diff --git a/lib/mauve/version.rb b/lib/mauve/version.rb index 7e94be0..7571fea 100644 --- a/lib/mauve/version.rb +++ b/lib/mauve/version.rb @@ -1,5 +1,5 @@ module Mauve - VERSION="3.4.3" + VERSION="3.4.4" end diff --git a/lib/mauve/web_interface.rb b/lib/mauve/web_interface.rb index fb82f2e..386dfac 100644 --- a/lib/mauve/web_interface.rb +++ b/lib/mauve/web_interface.rb @@ -394,13 +394,92 @@ EOF end ######################################################################## - - get '/events' do - find_active_alerts - find_recent_alerts - haml :events + + + + get '/events/alert/:id' do + end - + + get '/events/calendar' do + redirect "/events/calendar/"+Time.now.strftime("%Y-%m") + end + + get '/events/calendar/:start' do + + # + # Sort out the parameters + # + + # + # Start must be a Monday + # + if params[:start] =~ /\A(\d{4,4})-(\d{1,2})/ + @month = Time.local($1.to_i,$2.to_i,1,0,0,0,0) + else + t = Time.now + @month = Time.local(t.year, t.month, 1, 0, 0, 0, 0) + end + + start = @month + finish = start + 31.days + + start -= (start.wday == 0 ? 6 : (start.wday - 1)).day + finish -= finish.day if finish.month == @month.month+1 + finish += (finish.wday == 0 ? 0 : (7 - finish.wday)).days + + weeks = ((finish - start)/1.week).ceil + + query = {:history => {}} + query[:history][:created_at.gte] = start + query[:history][:created_at.lt] = finish + + # + # Now sort events into a per-week per-weekday array. Have to use the + # proc syntax here to prevent an array of pointers being created..?! + # + @events = find_events(query) + @events_by_week = Array.new(weeks){ Array.new(7) { Array.new } } + + @events.each do |event| + event_week = ((event.created_at - start)/(7.days)).floor + event_day = (event.created_at.wday == 0 ? 6 : (event.created_at.wday - 1)) + @events_by_week[event_week] ||= Array.new(7) { Array.new } + @events_by_week[event_week][event_day] << event + end + + # + # Make sure we have all our weeks filled out. + # + @events_by_week.each_with_index do |e, i| + @events_by_week[i] = Array.new(7) { Array.new } if e.nil? + end + + @today = start + haml :events_calendar + end + + get '/events/list' do + redirect "/events/list/"+Time.now.strftime("%Y-%m-%d") + end + + get '/events/list/:start' do + if params[:start] =~ /\A(\d{4,4})-(\d{1,2})-(\d{1,2})\Z/ + @start = Time.local($1.to_i,$2.to_i,$3.to_i,0,0,0,0) + else + t = Time.now + @start = Time.local(t.year, t.month, t.day, 0,0,0,0) + end + + query = {:history => {}} + query[:history][:created_at.gte] = @start + query[:history][:created_at.lt] = @start + 1.day + + @events = find_events(query) + + haml :events_list + end + ######################################################################## helpers do @@ -475,6 +554,63 @@ EOF list[@cycle] end + def find_events(query = Hash.new) + + if params["history"] + query[:history] ||= Hash.new + + if params["history"]["type"] and !params["history"]["type"].empty? + query[:history][:type] = params["history"]["type"] + end + end + + if !query[:history] or !query[:history][:type] + query[:history] ||= Hash.new + query[:history][:type] = "update" + + params["history"] ||= Hash.new + params["history"]["type"] = "update" + end + + if params["alert"] + query[:alert] ||= Hash.new + + if params["alert"]["subject"] and !params["alert"]["subject"].empty? + query[:alert][:subject.like] = params["alert"]["subject"] + end + + if params["alert"]["source"] and !params["alert"]["source"].empty? + query[:alert][:source.like] = params["alert"]["source"] + end + + if params["alert"]["id"] and !params["alert"]["id"].empty? + query[:alert][:id] = params["alert"]["id"] + end + end + + # + # + # THIS IS NOT EAGER LOADING. But I've no idea how the best way would be to do it. + # + alert_histories = AlertHistory.all(query) + + histories = alert_histories.history.to_a + alerts = alert_histories.alert.to_a + + + alert_histories.each do |ah| + history = histories.find{|h| ah.history_id == h.id} + alert = alerts.find{|a| ah.alert_id == a.id} + next if alert.nil? + history.add_to_cached_alerts( alert ) + end + + # + # Present the histories in time-ascending order (which is not the default..) + # + histories.reverse + end + end error DataMapper::ObjectNotFoundError do -- cgit v1.2.1