2022-07-11 09:20:44 +00:00
|
|
|
'use strict';
|
|
|
|
|
|
|
|
const { makeId } = require('core/makeid');
|
|
|
|
|
|
|
|
const decoder = new TextDecoder('utf-8');
|
|
|
|
let uws;
|
|
|
|
|
2022-07-12 07:40:49 +00:00
|
|
|
const mapService = (s, beat) => ({
|
|
|
|
id: s.id,
|
|
|
|
name: s.name,
|
|
|
|
cluster: s.cluster,
|
|
|
|
lastBeat: beat,
|
|
|
|
});
|
|
|
|
|
2022-07-11 09:20:44 +00:00
|
|
|
async function createDashboardSocket(server) {
|
|
|
|
uws = server.ws({
|
|
|
|
route: '/statusdashboard/socket',
|
|
|
|
onOpen: async ws => {
|
|
|
|
async function sendStatuses() {
|
|
|
|
const services = await server.storage
|
|
|
|
.store('smartyellow/webservice')
|
|
|
|
.find()
|
|
|
|
.toArray();
|
|
|
|
const heartbeats = await server.storage
|
|
|
|
.store('smartyellow/webserviceheartbeat')
|
|
|
|
.find({ webservice: { $in: services.map(s => s.id) } })
|
|
|
|
.sort({ date: -1 })
|
|
|
|
.toArray();
|
|
|
|
|
2022-07-12 07:40:49 +00:00
|
|
|
const servicesUp = [];
|
|
|
|
const servicesDown = [];
|
|
|
|
const servicesUnknown = [];
|
|
|
|
console.log(heartbeats);
|
|
|
|
|
|
|
|
for (let service of services) {
|
|
|
|
const beat = heartbeats.find(b => b.webservice === service.id);
|
|
|
|
service = mapService(service, beat);
|
|
|
|
|
|
|
|
if (!beat) {
|
|
|
|
servicesUnknown.push(service);
|
|
|
|
}
|
|
|
|
else if (beat.down) {
|
|
|
|
servicesDown.push(service);
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
servicesUp.push(service);
|
|
|
|
}
|
2022-07-11 09:20:44 +00:00
|
|
|
}
|
|
|
|
|
2022-07-12 08:18:29 +00:00
|
|
|
const total = [
|
|
|
|
...servicesUp,
|
|
|
|
...servicesDown,
|
|
|
|
...servicesUnknown,
|
|
|
|
].length;
|
|
|
|
|
2022-07-11 12:21:57 +00:00
|
|
|
try {
|
|
|
|
ws.send(JSON.stringify({
|
|
|
|
cmd: 'data',
|
2022-07-12 07:40:49 +00:00
|
|
|
servicesUp,
|
|
|
|
servicesDown,
|
|
|
|
servicesUnknown,
|
2022-07-12 08:18:29 +00:00
|
|
|
total,
|
2022-07-11 12:21:57 +00:00
|
|
|
}));
|
|
|
|
}
|
|
|
|
catch {
|
|
|
|
return;
|
|
|
|
}
|
2022-07-11 09:20:44 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
sendStatuses();
|
|
|
|
setInterval(sendStatuses, 5000);
|
|
|
|
},
|
|
|
|
onUpgrade: async () => ({ id: makeId(10) }),
|
|
|
|
onMessage: async (ws, msg) => {
|
|
|
|
msg = JSON.parse(decoder.decode(msg));
|
|
|
|
|
|
|
|
if (!msg || !msg.command) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
switch (msg.command) {
|
|
|
|
case 'data':
|
|
|
|
ws.send('data');
|
|
|
|
return;
|
|
|
|
|
|
|
|
default:
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
},
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
module.exports = createDashboardSocket;
|