From b718175601c02b8a251931622e7b79f469645111 Mon Sep 17 00:00:00 2001 From: Patrick J Cherry Date: Mon, 1 Aug 2011 15:35:05 +0100 Subject: Updated interface to auto-update nicely. --- lib/mauve/web_interface.rb | 5 +- static/javascript/mauve_utils.js | 108 +++++++++++++++++++++++++++++++++++++++ views/_navbar.haml | 2 + views/alerts.haml | 23 +-------- 4 files changed, 115 insertions(+), 23 deletions(-) diff --git a/lib/mauve/web_interface.rb b/lib/mauve/web_interface.rb index bd1ceb9..9369ee3 100644 --- a/lib/mauve/web_interface.rb +++ b/lib/mauve/web_interface.rb @@ -103,11 +103,12 @@ EOF # Uh-oh.. Intruder alert! # ok_urls = %w(/ /login /logout) + no_redirect_urls = %w(/ajax) unless ok_urls.include?(request.path_info) flash['error'] = "You must be logged in to access that page." status 403 - redirect "/login?next_page=#{request.path_info}" + redirect "/login?next_page=#{request.path_info}" unless no_redirect_urls.any?{|u| /^#{u}/ =~ request.path_info } end end end @@ -230,7 +231,7 @@ EOF end end - flash["errors"] = "Failed to acknowledge #{failed.length} alerts." if failed.length > 0 + flash["error"] = "Failed to acknowledge #{failed.length} alerts." if failed.length > 0 flash["notice"] = "Successfully acknowledged #{succeeded.length} alerts" if succeeded.length > 0 redirect "/alerts/raised" diff --git a/static/javascript/mauve_utils.js b/static/javascript/mauve_utils.js index 2bea0ca..674a3a9 100644 --- a/static/javascript/mauve_utils.js +++ b/static/javascript/mauve_utils.js @@ -126,3 +126,111 @@ function doTimeTest( t, type ) { return r; } + +// +// Updates the alerts table +// +function updateAlertsTable(alert_type, group_by) { + + // + // Do nothing if there is a checked box. + // + if ( $('input.alert:checked').length ) { + return false; + } + + $.ajax( { + url: '/ajax/alerts_table/'+alert_type+'/'+group_by, + timeout: 5000, + success: function( data ) { + if ( "" == data || null == data ) { + showError("No data returned by web server when updating alerts table.", "updateAlertsTable"); + } else { + $('#alerts_table').replaceWith(data); + clearError("updateAlertsTable"); + updateAlertCounts(); + } + }, + error: function( a,b,c ) { + if ( "timeout" == b ) { + showError("Web server timed out when updating alerts table.", "updateAlertsTable"); + } else { + showError("Got "+a.status+" "+a.statusText+" when updating alerts table.", "updateAlertsTable"); + } + }, + }); + + return false; +} + +// +// Updates the alerts title tag +// +function updateAlertCounts() { + $.ajax( { + url: '/ajax/alert_counts', + timeout: 5000, + success: function(counts) { + if ( "" == counts || null == counts) { + showError("No data returned by web server when updating alert counts.", "updateAlertCounts"); + } else { + $('#count_raised').html(counts[0]+counts[1]+counts[2]+""); + $('#count_ackd').html(counts[3]+""); + $('#count_cleared').html(counts[4]+""); + $('title').html("Mauve: [ "+counts[0]+" / "+counts[1]+" / "+counts[2]+" ] Alerts"); + clearError("updateAlertCounts"); + } + }, + error: function( a,b,c ) { + if ( "timeout" == b ) { + showError("Web server timed out when updating alert counts.", "updateAlertCounts"); + } else { + showError("Got "+a.status+" "+a.statusText+" when updating alert counts.", "updateAlertCounts"); + } + }, + }); + + return false; +} + + +// +// +// +function showError(text, func) { + + if ( null == text || "" == text ) return false; + + + // We need to add the p element. + if ( 0 == $('div.flash.error p#'+func).length ) { + // ugh.. standard DOM stuff. + var p = document.createElement('p'); + p.setAttribute("id",func); + $('div.flash.error').append(p); + } + + $('p#'+func).html(text); + // Show the error box + $('div.flash.error').fadeIn(2000); + + return false; +} + +function clearError(func) { + // + // Remove the element if it exists. + // + if ( $('div.flash.error p#'+func).length ) { + $('div.flash.error p#'+func).remove(); + } + + if ( $('div.flash.error').contents().length == 0 ) { + $('div.flash.error').fadeOut(2000); + } + + return false; +} + + + diff --git a/views/_navbar.haml b/views/_navbar.haml index 5df4991..283572f 100644 --- a/views/_navbar.haml +++ b/views/_navbar.haml @@ -28,3 +28,5 @@ #{flash[klass]} :javascript $(document).ready(function () { $('div.flash').delay(1000).fadeOut(2000);}); + - else + %div{:class => ["flash", klass], :style => "display: none"} diff --git a/views/alerts.haml b/views/alerts.haml index aedbc72..54bfff7 100644 --- a/views/alerts.haml +++ b/views/alerts.haml @@ -7,25 +7,6 @@ .notice#alerts_table %p No alerts to display. :javascript - $(document).ready( function() { - $.PeriodicalUpdater({ - url: '/ajax/alerts_table/#{@alert_type}/#{@group_by}', - minTimeout: 10000, - maxTimeout: 30000, - }, function(alerts_table, success, xhr, handle){ - $('#alerts_table').replaceWith(alerts_table); - }); - $.PeriodicalUpdater({ - url: '/ajax/alert_counts', - minTimeout: 10000, - maxTimeout: 30000, - }, function(alert_counts, success, xhr, handle){ - var counts = jQuery.parseJSON( alert_counts ); - $('#count_raised').html(counts[0]+counts[1]+counts[2]); - $('#count_ackd').html(counts[3]); - $('#count_cleared').html(counts[4]); - return false; - }); - - }); + // Do the magic updates.. + setInterval("updateAlertsTable('#{@alert_type}','#{@group_by}');", 30000) -- cgit v1.2.1