forked from PeterJCLaw/nwatchlive
-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.html
110 lines (103 loc) · 3.53 KB
/
index.html
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
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<title>Monitor</title>
<link rel="stylesheet" href="static/main.css">
</head>
<body>
<table id="services">
<tr>
<th>Service</th><th>Status</th>
</tr>
<tr class="ok" id="srv-Monitor"><td>Monitor</td><td>Connecting...</td></tr>
</table>
<script type="text/javascript">
if (!console) {
var nop = function(){};
console = {
'log': nop,
'dir': nop,
};
}
var es = new EventSource('stream');
var markError = function(row) {
var setError = function() {
if (row.className == 'errorf')
row.className = 'error';
};
var setErrorFlash = function() {
if (row.className == 'error')
row.className = 'errorf';
};
row.className = 'errorf';
for (var i = 0; i < 17; ++i) {
if (i % 2 == 0) {
setTimeout(setError, 100*i);
} else {
setTimeout(setErrorFlash, 100*i);
}
}
};
var receiveData = function(dat) {
var tab = document.getElementById('services');
for (var service in dat) {
var serviceID = 'srv-' + service;
var elem = document.getElementById(serviceID);
if (elem === null) {
elem = document.createElement('tr');
elem.setAttribute('id', serviceID);
var key = document.createElement('td');
var keyN = document.createTextNode(service);
key.appendChild(keyN);
var value = document.createElement('td');
elem.appendChild(key);
elem.appendChild(value);
tab.appendChild(elem);
}
var newStatus = dat[service];
var statusField = elem.childNodes[1];
if (newStatus === null) {
elem.className = 'ok';
statusField.innerHTML = 'OK';
} else {
if (elem.className !== 'error') {
markError(elem);
}
statusField.innerHTML = newStatus;
}
}
};
var pingHandle;
es.onmessage = function(evt) {
var dat = JSON.parse(evt.data);
console.log("Got message:");
console.dir(dat);
receiveData({'Monitor': null});
if ('ping' in dat) {
if (pingHandle) {
clearTimeout(pingHandle);
pingHandle = null;
}
var interval = dat.ping;
var timeToError = interval * 2.5;
pingHandle = setTimeout(function() {
var msg = "No pings for " + interval + " seconds.";
receiveData({'Monitor': msg});
}, timeToError * 1000);
}
var statuses = dat.statuses;
receiveData(statuses);
};
es.onerror = function(err) {
console.log('stream error:');
console.dir(err);
receiveData({'Monitor': 'offline'});
};
es.onopen = function() {
console.log('stream open.');
receiveData({'Monitor': null});
};
</script>
</body>
</html>