aboutsummaryrefslogtreecommitdiff
path: root/static/mywatch.js
blob: 02d0bf6e9e204b659c06b375dab397b3774bfdfc (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
$(function() {
  var info = $('#info');
  var infoHead = $('#info>h1');
  var infoAlert = $('#info>div');
  var main = $('#main');
  var plBody = $('#processList>tbody');

  var interval = null;

  var plCols = $('#processList>thead>tr>th')
    .map(function() {
      return $(this).text();
    }).get();


  function commonError(jqXHR, textStatus, errorThrown) {
    plBody.empty();
    main.hide();
    infoHead.text('An error has occured');
    infoAlert.text((0 == jqXHR.readyState) ? 'Service unavailable' : errorThrown);
    infoAlert.removeClass().addClass('alert alert-danger');
    info.show();
  }

  function getProcessList(server) {
    $.ajax({
      url: "server/" + server + "/processlist.json",
      method: "GET",
      error: commonError,
      success: function(procs) {
        plBody.empty();
        procs.map(function(p) {
          var tr = $('<tr>');
          plCols.map(function(c) {
            var td = $('<td>');
            td.text(p[c]);
            if ('info' === c) {
              td.css('white-space', 'pre-wrap');
            } else if ('time' === c) {
              td.css('text-align', 'right');
            } else if ('id' === c) {
              td.css('text-align', 'right');
            }
            tr.append(td);
          });
          plBody.append(tr);
        });
        info.hide();
        main.show();
      }
    });
  };

  $.ajax({
    url: "serverlist.json",
    method: "GET",
    error: commonError,
    success: function(servers) {
      var total = servers.length;
      var available = [];
      var checked = 0;
      $.each(servers, function(i, s) {
        $.ajax({
          url: "server/" + s + "/processlist.json",
          method: "HEAD",
          success: function() {
            available.push(s);
          },
          complete: function() {
            var menu = $('#serverList>ul');
            checked++;
            if (checked === total) {
              $.each(available.sort(), function(i, s) {
                menu.append('<li><a href="#">' + s + '</a></li>')
              });
              $("#serverList a").on("click", function() {
                var server = $(this).text();
                document.title = server + ' — ' + 'MyWatch';
                $(this).parent().parent().find('.active').removeClass('active');
                $(this).parent().addClass('active');
                clearInterval(interval);
                getProcessList(server);
                interval = setInterval(getProcessList, 60 * 1000, server);
              });
              info.hide();
            }
          }
        });
      });
    }
  });
});