-
Notifications
You must be signed in to change notification settings - Fork 306
/
index.js
252 lines (213 loc) · 6.49 KB
/
index.js
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
const maxDays = 30;
async function genReportLog(container, key, url) {
const response = await fetch("logs/" + key + "_report.log");
let statusLines = "";
if (response.ok) {
statusLines = await response.text();
}
const normalized = normalizeData(statusLines);
const statusStream = constructStatusStream(key, url, normalized);
container.appendChild(statusStream);
}
function constructStatusStream(key, url, uptimeData) {
let streamContainer = templatize("statusStreamContainerTemplate");
for (var ii = maxDays - 1; ii >= 0; ii--) {
let line = constructStatusLine(key, ii, uptimeData[ii]);
streamContainer.appendChild(line);
}
const lastSet = uptimeData[0];
const color = getColor(lastSet);
const container = templatize("statusContainerTemplate", {
title: key,
url: url,
color: color,
status: getStatusText(color),
upTime: uptimeData.upTime,
});
container.appendChild(streamContainer);
return container;
}
function constructStatusLine(key, relDay, upTimeArray) {
let date = new Date();
date.setDate(date.getDate() - relDay);
return constructStatusSquare(key, date, upTimeArray);
}
function getColor(uptimeVal) {
return uptimeVal == null
? "nodata"
: uptimeVal == 1
? "success"
: uptimeVal < 0.3
? "failure"
: "partial";
}
function constructStatusSquare(key, date, uptimeVal) {
const color = getColor(uptimeVal);
let square = templatize("statusSquareTemplate", {
color: color,
tooltip: getTooltip(key, date, color),
});
const show = () => {
showTooltip(square, key, date, color);
};
square.addEventListener("mouseover", show);
square.addEventListener("mousedown", show);
square.addEventListener("mouseout", hideTooltip);
return square;
}
let cloneId = 0;
function templatize(templateId, parameters) {
let clone = document.getElementById(templateId).cloneNode(true);
clone.id = "template_clone_" + cloneId++;
if (!parameters) {
return clone;
}
applyTemplateSubstitutions(clone, parameters);
return clone;
}
function applyTemplateSubstitutions(node, parameters) {
const attributes = node.getAttributeNames();
for (var ii = 0; ii < attributes.length; ii++) {
const attr = attributes[ii];
const attrVal = node.getAttribute(attr);
node.setAttribute(attr, templatizeString(attrVal, parameters));
}
if (node.childElementCount == 0) {
node.innerText = templatizeString(node.innerText, parameters);
} else {
const children = Array.from(node.children);
children.forEach((n) => {
applyTemplateSubstitutions(n, parameters);
});
}
}
function templatizeString(text, parameters) {
if (parameters) {
for (const [key, val] of Object.entries(parameters)) {
text = text.replaceAll("$" + key, val);
}
}
return text;
}
function getStatusText(color) {
return color == "nodata"
? "No Data Available"
: color == "success"
? "Fully Operational"
: color == "failure"
? "Major Outage"
: color == "partial"
? "Partial Outage"
: "Unknown";
}
function getStatusDescriptiveText(color) {
return color == "nodata"
? "No Data Available: Health check was not performed."
: color == "success"
? "No downtime recorded on this day."
: color == "failure"
? "Major outages recorded on this day."
: color == "partial"
? "Partial outages recorded on this day."
: "Unknown";
}
function getTooltip(key, date, quartile, color) {
let statusText = getStatusText(color);
return `${key} | ${date.toDateString()} : ${quartile} : ${statusText}`;
}
function create(tag, className) {
let element = document.createElement(tag);
element.className = className;
return element;
}
function normalizeData(statusLines) {
const rows = statusLines.split("\n");
const dateNormalized = splitRowsByDate(rows);
let relativeDateMap = {};
const now = Date.now();
for (const [key, val] of Object.entries(dateNormalized)) {
if (key == "upTime") {
continue;
}
const relDays = getRelativeDays(now, new Date(key).getTime());
relativeDateMap[relDays] = getDayAverage(val);
}
relativeDateMap.upTime = dateNormalized.upTime;
return relativeDateMap;
}
function getDayAverage(val) {
if (!val || val.length == 0) {
return null;
} else {
return val.reduce((a, v) => a + v) / val.length;
}
}
function getRelativeDays(date1, date2) {
return Math.floor(Math.abs((date1 - date2) / (24 * 3600 * 1000)));
}
function splitRowsByDate(rows) {
let dateValues = {};
let sum = 0,
count = 0;
for (var ii = 0; ii < rows.length; ii++) {
const row = rows[ii];
if (!row) {
continue;
}
const [dateTimeStr, resultStr] = row.split(",", 2);
const dateTime = new Date(Date.parse(dateTimeStr.replace(/-/g, "/") + " GMT"));
const dateStr = dateTime.toDateString();
let resultArray = dateValues[dateStr];
if (!resultArray) {
resultArray = [];
dateValues[dateStr] = resultArray;
if (dateValues.length > maxDays) {
break;
}
}
let result = 0;
if (resultStr.trim() == "success") {
result = 1;
}
sum += result;
count++;
resultArray.push(result);
}
const upTime = count ? ((sum / count) * 100).toFixed(2) + "%" : "--%";
dateValues.upTime = upTime;
return dateValues;
}
let tooltipTimeout = null;
function showTooltip(element, key, date, color) {
clearTimeout(tooltipTimeout);
const toolTipDiv = document.getElementById("tooltip");
document.getElementById("tooltipDateTime").innerText = date.toDateString();
document.getElementById("tooltipDescription").innerText =
getStatusDescriptiveText(color);
const statusDiv = document.getElementById("tooltipStatus");
statusDiv.innerText = getStatusText(color);
statusDiv.className = color;
toolTipDiv.style.top = element.offsetTop + element.offsetHeight + 10;
toolTipDiv.style.left =
element.offsetLeft + element.offsetWidth / 2 - toolTipDiv.offsetWidth / 2;
toolTipDiv.style.opacity = "1";
}
function hideTooltip() {
tooltipTimeout = setTimeout(() => {
const toolTipDiv = document.getElementById("tooltip");
toolTipDiv.style.opacity = "0";
}, 1000);
}
async function genAllReports() {
const response = await fetch("urls.cfg");
const configText = await response.text();
const configLines = configText.split("\n");
for (let ii = 0; ii < configLines.length; ii++) {
const configLine = configLines[ii];
const [key, url] = configLine.split("=");
if (!key || !url) {
continue;
}
await genReportLog(document.getElementById("reports"), key, url);
}
}