forked from mrVragec/MMM-uptimerobot
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathnode_helper.js
105 lines (90 loc) · 3.01 KB
/
node_helper.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
/* Magic Mirror
* Node Helper: uptimekuma
*
* By Mike Bishop
* Based on uptimerobot by Simon Crnko
* MIT Licensed.
*/
const NodeHelper = require("node_helper");
const axios = require('axios');
module.exports = NodeHelper.create({
start: function () {
this.config = null;
},
// Override socketNotificationReceived method.
/* socketNotificationReceived(notification, payload)
* This method is called when a socket notification arrives.
*
* argument notification string - The identifier of the noitication.
* argument payload mixed - The payload of the notification.
*/
socketNotificationReceived: async function (notification, payload) {
if (notification === "uptimekuma-getData") {
this.config = payload;
await this.getData();
}
},
urlJoin: function (...args) {
// Remove trailing slashes from all components except last
var parts = args.map((part, i) => {
if (i === args.length - 1) {
return part;
}
return part.replace(/\/+$/, '');
});
// Remove leading slashes from all components
parts = parts.map(part => part.replace(/^\/+/, ''));
// Will throw if input isn't valid
return new URL(parts.join('/'));
},
/*
* getData
* function example return data and show it in the module wrapper
* get a URL request
*
*/
getData: async function () {
var self = this;
var config = self.config;
const statusPagePromise = axios.get(
this.urlJoin(config.baseUrl, 'api/status-page', config.statusPage)
);
const heartbeatPromise = axios.get(
this.urlJoin(config.baseUrl, 'api/status-page/heartbeat/', config.statusPage)
);
try {
const statusPage = await statusPagePromise;
const heartbeat = await heartbeatPromise;
var groups = statusPage.data.publicGroupList;
var heartbeatsPerMonitor = heartbeat.data.heartbeatList;
self.sendSocketNotification("uptimekuma-processData",
groups.map(group => ({
name: group.name,
monitors: group.monitorList.map(monitor => ({
name: monitor.name,
status: heartbeatsPerMonitor[monitor.id].at(-1).status,
})),
})));
}
catch (error) {
console.log(error);
}
},
/* scheduleUpdate()
* Schedule next update.
*
* argument delay number - Milliseconds before next update.
* If empty, this.config.updateInterval is used.
*/
scheduleUpdate: function (delay) {
var nextLoad = this.config.updateInterval;
if (typeof delay !== "undefined" && delay >= 0) {
nextLoad = delay;
}
nextLoad = nextLoad;
var self = this;
setTimeout(function () {
self.getData();
}, nextLoad);
}
});