From 5f57bc117ff110d21fe6c130816c208c35f0b987 Mon Sep 17 00:00:00 2001
From: Patrick J Cherry <patrick@bytemark.co.uk>
Date: Fri, 29 Jul 2011 12:11:56 +0100
Subject:  * Added initial logger  * Acknowledgement time calculations now in
 JS  * Updated logging to indicate where and update comes from

---
 static/javascript/mauve_utils.js | 136 ++++++++++++++++++++++++++++-----------
 1 file changed, 100 insertions(+), 36 deletions(-)

(limited to 'static/javascript')

diff --git a/static/javascript/mauve_utils.js b/static/javascript/mauve_utils.js
index 45a651d..2bea0ca 100644
--- a/static/javascript/mauve_utils.js
+++ b/static/javascript/mauve_utils.js
@@ -3,62 +3,126 @@ function updateDate() {
   //
   //  Date.getTime() returns *milliseconds*
   //
-  var this_date = workoutDate( $('#n_hours').val(), $('#type_hours').val() );
-  $('#ack_until_text').html("(until "+humanDate(this_date)+")");
-  $('#ack_until').val(this_date.getTime()/1000);
+  var this_date = workoutDate( $( '#n_hours' ).val(), $( '#type_hours' ).val() );
+  $( '#ack_until' ).val( this_date.getTime()/1000 );
+
+  // 
+  // Use a asynchronous ajax convert a date to a human string.  NB Date.getTime()
+  // returns *milliseconds*
+  //
+  $.ajax( {
+    url:     '/ajax/time_to_s_human/'+this_date.getTime()/1000, 
+    timeout: 1000,
+    success: function( data )  { $( '#ack_until_text' ).html( "( until "+data+" )" ); },
+    error:   function( a,b,c ) { $( '#ack_until_text' ).html( "( until "+this_date.toString()+" )" ); }
+  } );
 
   return false;
 }
 
-function workoutDate(h, t) {
-  var new_date = null;
+function workoutDate( h, type ) {
 
-  h = new Number(h);
-  h = ( h > 300 ? 300 : h );
+  n = new Number( h );
+  n *= 3600 * 1000;
 
-  // 
-  // Use a synchronous ajax request to fetch the date.  Note that
-  // Date.getTime() returns milliseconds..
+  if ( type == null ) {
+    type = "wallclock" ;
+  }
+
+  var step = 3600 * 1000;
   //
-  $.ajax({
-    url: '/ajax/time_in_x_hours/'+h+"/"+t,
-    async: false,
-    success: function(data) { new_date = new Date ( new Number(data) * 1000 ); }
-  });
+  // Get the time now, in milliseconds
+  //
+  var d    = new Date();
+  var t    = d.getTime();
+  //
+  // Can't ack longer than a week
+  //
+  var maxDate = new Date( d.getTime() + 1000 * 86400 * 8 )
 
-  return new_date;
-}
 
+  //
+  // Work out how much time to subtract now
+  //
+  while ( n >= 0 && t < maxDate.getTime() ) {
+    //
+    // If we're currently OK, and we won't be OK after the next step ( or
+    // vice-versa ) decrease step size, and try again
+    //
+    if ( doTimeTest( t, type ) != doTimeTest( t+step, type ) ) {
+      //
+      // Unless we're on the smallest step, try a smaller one.
+      //
+      if ( step > 1000 ) {
+        step /= 60;
+
+      } else {
+        if ( doTimeTest( t, type ) ) n -= step;
+        t += step;
+
+        //
+        // Set the step size back to an hour
+        //
+        step = 3600*1000;
+      }
 
-function humanDate(d) {
-  var new_date = null;
+      continue;
+    } 
 
-  if ( d == null ) {
-    d = new Date();
+    //
+    // Decrease the time by the step size if we're currently OK.
+    //
+    if ( doTimeTest( t, type ) ) n -= step;
+    t += step;
   }
 
-  // 
-  // Use a synchronous ajax convert a date to a human string.  NB Date.getTime()
-  // returns *milliseconds*
   //
-  $.ajax({
-    url: '/ajax/time_to_s_human/'+d.getTime()/1000, 
-    async: false,
-    success: function(data) { new_date = data; }
-  });
+  // Substract any overshoot.
+  //
+  if ( n < 0 ) t += n;
 
-  return new_date;
+  //
+  // Make sure we can't ack alerts too far in the future.
+  //
+  return ( t > maxDate.getTime() ? maxDate : new Date( t ) );
 }
 
-function fetchDetail(a) {
+function fetchDetail( a ) {
   // Use a synchronous ajax request to fetch the date.
-  $.get('/ajax/alerts_table_alert_detail/'+a,
-    function(data) { 
-      $('#tr_summary_'+a).after(data);
+  $.get( '/ajax/alerts_table_alert_detail/'+a,
+    function( data ) { 
+      $( '#tr_summary_'+a ).after( data );
       // Only fetch the data once.
-      $('#a_detail_'+a).attr("onclick",null).click(function() { $('#tr_detail_'+a).toggle(); return false; });
-  });
+      $( '#a_detail_'+a ).attr( "onclick",null ).click( function() { $( '#tr_detail_'+a ).toggle(); return false; } );
+  } );
 
   return false;
 }
 
+//
+// This expects its arguments as a time in milliseconds, and a type of "working", "daytime", or something else.
+//
+function doTimeTest( t, type ) {
+  
+  var d = new Date( t );
+  var r = false;
+
+  switch ( type ) {
+    case "working":
+      r = ( d.getDay() > 0 && d.getDay() < 6 && 
+          ( ( d.getHours() >= 9 && d.getHours() <= 16 ) || 
+            ( d.getHours() == 8 && d.getMinutes() >= 30 ) 
+          ) );
+      break;
+
+    case "daytime":
+      r = ( d.getHours() >= 8 && d.getHours() <= 21 );
+      break;
+
+    default:
+      r = true;
+  } 
+
+  return r;
+}
+
-- 
cgit v1.2.3