-
Notifications
You must be signed in to change notification settings - Fork 0
/
MMM-AuthenticatedUptimeKuma.js
323 lines (277 loc) · 10.9 KB
/
MMM-AuthenticatedUptimeKuma.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
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
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
Module.register("MMM-AuthenticatedUptimeKuma", {
defaults: {
url: "",
token: "",
displayType: "list",
widgetSettings: {
titleColor: "black",
backgroundColor: "#FFFFFF",
descriptionColor: "#666",
minWidth: "200px",
},
monitors: []
},
start: function () {
this.monitors = {}; // To store monitor information
this.error = null;
// Initiate the connection with node_helper
this.sendSocketNotification("START_CONNECTION", {
url: this.config.url,
token: this.config.token,
});
},
// Handle incoming data from node_helper.js
socketNotificationReceived: function (notification, payload) {
switch (notification) {
case "MONITOR_LIST":
this.updateMonitors(payload);
break;
case "HEARTBEAT":
this.updateHeartbeat(payload);
break;
case "UPTIME":
this.updateUptime(payload);
break;
case "AVG_PING":
this.updateAvgPing(payload);
break;
case "HEARTBEAT_LIST":
this.updateHeartbeatList(payload);
break;
case "DISCONNECTED":
case "ERROR":
this.error = payload;
break;
}
this.updateDom();
},
// Update monitor data
updateMonitors: function (monitorList) {
for (let monitorId in monitorList) {
monitorId = parseInt(monitorId);
const monitor = monitorList[monitorId];
if (!this.monitors[monitorId]) {
this.monitors[monitorId] = {
id: monitorId,
name: monitor.name,
active: monitor.active,
};
} else {
this.monitors[monitorId].name = monitor.name;
this.monitors[monitorId].id = monitorId;
this.monitors[monitorId].active = monitor.active;
}
}
},
// Update monitor heartbeat data
updateHeartbeat: function (heartbeat) {
const monitorId = parseInt(heartbeat.monitorID);
if (this.monitors[monitorId]) {
this.monitors[monitorId].heartbeat = heartbeat;
}
},
// Update monitor uptime data
updateAvgPing: function (avgPingData) {
if (this.monitors[avgPingData.monitorID]) {
this.monitors[avgPingData.monitorID].avgPing = avgPingData.avgPing;
}
},
// Update monitor uptime data
updateUptime: function (uptimeData) {
if (this.monitors[uptimeData.monitorID]) {
const period = uptimeData.period;
const percent = (uptimeData.percent * 100).toFixed(2);
if (period === 24) {
this.monitors[uptimeData.monitorID].uptime24 = percent;
} else if (period === 720) {
this.monitors[uptimeData.monitorID].uptime30 = percent;
}
}
},
// Update monitor heartbeat list
updateHeartbeatList: function (heartbeatListData) {
if (this.monitors[heartbeatListData.monitorID]) {
if (heartbeatListData.heartbeatList.length > 0) {
this.monitors[heartbeatListData.monitorID].heartbeat = heartbeatListData.heartbeatList.at(-1);
}
}
},
// Generate the DOM for display
getDom: function () {
var wrapper = document.createElement("div");
if (this.error) {
wrapper.innerHTML = `Error: ${this.error}`;
return wrapper;
}
if (!Object.keys(this.monitors).length) {
wrapper.innerHTML = "Loading monitor data...";
return wrapper;
}
switch (this.config.displayType) {
case "list":
return this.renderList(wrapper);
case "widget":
return this.renderWidget(wrapper);
default:
wrapper.innerHTML = "Invalid display type";
}
return wrapper;
},
renderList: function (wrapper) {
var table = document.createElement("table");
table.classList.add("small");
// Iterate through the configured monitors
this.config.monitors.forEach((monitorConfig) => {
const monitor = monitorConfig.id ? this.monitors[monitorConfig.id] : false;
if (!monitor) {
return;
}
// Create a table row for each monitor
var row = document.createElement("tr");
// Current state indicator (circle)
var stateCell = document.createElement("td");
stateCell.classList.add("state-indicator");
stateCell.appendChild(this.getStateIndicator(monitor));
row.appendChild(stateCell);
// Monitor name
var nameCell = document.createElement("td");
nameCell.innerHTML = monitorConfig.name ?? monitor.name;
nameCell.classList.add("title");
row.appendChild(nameCell);
// Monitor data (ping, uptime, etc.)
var dataCell = document.createElement("td");
dataCell.style.paddingLeft = "20px"; // Add padding for better alignment
dataCell.innerHTML = this.getMonitorData(monitorConfig, monitor);
row.appendChild(dataCell);
table.appendChild(row);
});
wrapper.appendChild(table);
return wrapper;
},
renderWidget: function (wrapper) {
// Create a container for the list of widgets
var listContainer = document.createElement("div");
listContainer.classList.add("widget-list-container");
// Iterate through the configured monitors and create a widget for each
this.config.monitors.forEach((monitorConfig) => {
const monitor = this.monitors[monitorConfig.id];
if (!monitor) {
return;
}
// Create a widget container for each monitor
var widgetContainer = document.createElement("div");
widgetContainer.classList.add("monitor-widget");
widgetContainer.style.backgroundColor = this.config.widgetSettings.backgroundColor;
widgetContainer.style.minWidth = this.config.widgetSettings.minWidth;
// Monitor name
var nameDisplay = document.createElement("div");
nameDisplay.classList.add("monitor-name");
nameDisplay.innerHTML = monitorConfig.name ?? monitor.name;
nameDisplay.style.color = this.config.widgetSettings.titleColor;
// Monitor data
var dataDisplay = document.createElement("div");
dataDisplay.classList.add("monitor-data");
dataDisplay.innerHTML = this.getMonitorData(monitorConfig, monitor);
// Apply color based on monitor status
if (!monitor.active) {
dataDisplay.style.color = "orange";
} else if (monitor.heartbeat) {
if (monitor.heartbeat.status) {
if (monitor.heartbeat.status === 3) {
dataDisplay.style.color = "blue";
} else {
dataDisplay.style.color = "green";
}
} else {
dataDisplay.style.color = "red";
}
} else {
dataDisplay.style.color = "gray";
}
// Data display name
var dataDisplayName = document.createElement("div");
dataDisplayName.classList.add("monitor-data-name");
dataDisplayName.innerHTML = this.getDataDisplayName(monitorConfig.display);
dataDisplayName.style.color = this.config.widgetSettings.descriptionColor;
// Append elements to the widget container
widgetContainer.appendChild(dataDisplayName);
widgetContainer.appendChild(nameDisplay);
widgetContainer.appendChild(dataDisplay);
// Append the widget to the list container
listContainer.appendChild(widgetContainer);
});
// Append the list container to the wrapper
wrapper.appendChild(listContainer);
return wrapper;
},
// Get the color-coded circle based on the status
getStateIndicator: function (monitor) {
var indicator = document.createElement("div");
indicator.classList.add("circle-indicator");
if (!monitor) {
indicator.style.backgroundColor = "gray";
return indicator;
}
if (!monitor.active) {
indicator.style.backgroundColor = "orange";
return indicator;
}
if (monitor.heartbeat) {
if (monitor.heartbeat.status) {
if (monitor.heartbeat.status === 3) {
indicator.style.backgroundColor = "blue";
return indicator;
}
indicator.style.backgroundColor = "green";
return indicator;
} else {
indicator.style.backgroundColor = "red";
return indicator;
}
}
indicator.style.backgroundColor = "gray";
return indicator;
},
// Get the monitor data based on the configuration
getMonitorData: function (monitorConfig, monitor) {
if (!monitor || !monitor.heartbeat) {
return "N/A";
}
switch (monitorConfig.display) {
case "ping":
return `${monitor.heartbeat.ping || "N/A"} ms`;
case "avgPing":
return `${monitor.avgPing || "N/A"} ms (⌀24h)`;
case "uptime24":
return `${monitor.uptime24 || "N/A"}% (24h)`;
case "uptime30":
return `${monitor.uptime30 || "N/A"}% (30 days)`;
default:
return "N/A";
}
},
getDataDisplayName: function (dataName) {
switch (dataName) {
case "ping":
return "Ping";
case "avgPing":
return "Average Ping";
case "uptime24":
return "Uptime (24h)";
case "uptime30":
return "Uptime (30 days)";
default:
return "";
}
},
getStyles: function () {
switch (this.config.displayType) {
case "list":
return ["MMM-AuthenticatedUptimeKuma.css"];
case "widget":
return ["MMM-AuthenticatedUptimeKumaWidget.css"];
default:
return [];
}
},
});