aboutsummaryrefslogtreecommitdiff
path: root/static/javascript/mauve_utils.js
blob: 5522c0875b3ca81577daf6857755f3fa46dfd214 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
function updateDate() {

  var n_hours;
  var type_hours;

  //
  // Make sure the number of hours is sane.
  //
  n_hours = new Number( $( '#n_hours' ).val() );

  if (isNaN(n_hours)) {
    n_hours = new Number(2);
  }

  //
  // Make sure the type is one of three things:
  //
  // type_hours = new String( $( '#type_hours' ).val() );

  switch( $( '#type_hours' ).val() ) {
    case "working":
      type_hours = "working"
      break;

    case "daytime":
      type_hours = "daytime"
      break;

    default:
      type_hours = "wallclock";
      break;
  }

  // 
  // Use a asynchronous ajax convert a date to a human string.  NB Date.getTime()
  // returns *milliseconds*
  //
  $.ajax( {
    url:     '/ajax/time_in_x_hours/'+n_hours.toString()+'/'+type_hours,
    timeout: 2000,
    success: function( data )  { 
      $( '#ack_until' ).val( data['time'].toString() );
      $( '#ack_until_text' ).html( "( until "+data['string'].toString()+" )" );
    },
    error:   function( a,b,c ) { 
      //
      //  Date.getTime() returns *milliseconds*
      //
      var this_date = workoutDate( n_hours, type_hours );
      $( '#ack_until' ).val( this_date.getTime()/1000 );
      $( '#ack_until_text' ).html( "( until "+this_date.toString()+" )" ); 
    }
  } );

  return false;
}

function workoutDate( h, type ) {

  n = new Number( h );
  n *= 3600 * 1000;

  if ( type == null ) {
    type = "wallclock" ;
  }

  var step = 3600 * 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 )


  //
  // 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;
      }

      continue;
    } 

    //
    // Decrease the time by the step size if we're currently OK.
    //
    if ( doTimeTest( t, type ) ) n -= step;
    t += step;
  }

  //
  // Substract any overshoot.
  //
  if ( n < 0 ) t += n;

  //
  // Make sure we can't ack alerts too far in the future.
  //
  return ( t > maxDate.getTime() ? maxDate : new Date( t ) );
}

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 );
      // Only fetch the data once.
      $( '#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() >= 10 && d.getHours() <= 16 )   || 
            ( d.getHours() == 9  && d.getMinutes() >= 30 ) ||
            ( d.getHours() == 17 && d.getMinutes() < 30 ) 
          ) );
      break;

    case "daytime":
      r = ( d.getHours() >= 8 && d.getHours() < 22 );
      break;

    default:
      r = true;
  } 

  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: 30000,
    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();
        //
        // Schedule next update.
        //
        setTimeout("updateAlertsTable('"+alert_type+"','"+group_by+"');", 120000);
      }
    },
    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"); 
      }
      //
      // Schedule next update.
      //
      setTimeout("updateAlertsTable('"+alert_type+"','"+group_by+"');", 120000);
    },
  });


  return false;
}

//
// Updates the alerts title tag
//
function updateAlertCounts() {
  $.ajax( {
    url: '/ajax/alert_counts',
    timeout: 30000,
    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').hide();
  }

  return false;
}