aboutsummaryrefslogtreecommitdiff
path: root/static/javascript/mauve_utils.js
blob: 2bea0caeee8d4a55ac9bb54c842be9ea8c62f353 (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
function updateDate() {
  //
  //  Date.getTime() returns *milliseconds*
  //
  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, 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() >= 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;
}