many update again

This commit is contained in:
LouisLam
2021-06-30 21:04:58 +08:00
parent 9fa84a0a2b
commit 46f07fc17e
9 changed files with 345 additions and 45 deletions

View File

@@ -66,6 +66,8 @@ class Monitor extends BeanModel {
io.to(this.user_id).emit("heartbeat", bean.toJSON());
Monitor.sendStats(io, this.id, this.user_id)
await R.store(bean)
previousBeat = bean;
@@ -78,6 +80,29 @@ class Monitor extends BeanModel {
stop() {
clearInterval(this.heartbeatInterval)
}
static async sendStats(io, monitorID, userID) {
Monitor.sendAvgPing(24, io, monitorID, userID);
//Monitor.sendUptime(24, io, this.id);
//Monitor.sendUptime(24 * 30, io, this.id);
}
static async sendAvgPing(duration, io, monitorID, userID) {
let avgPing = parseInt(await R.getCell(`
SELECT AVG(ping)
FROM heartbeat
WHERE time > DATE('now', ? || ' hours')
AND monitor_id = ? `, [
-duration,
monitorID
]));
io.to(userID).emit("avgPing", monitorID, avgPing);
}
sendUptime(duration) {
}
}
module.exports = Monitor;

View File

@@ -39,10 +39,6 @@ let monitorList = {};
// Public API
/*
firstConnect - true = send monitor list + heartbeat list history
false = do not send
*/
socket.on("loginByToken", async (token, callback) => {
try {
@@ -336,6 +332,8 @@ async function afterLogin(socket, user) {
for (let monitorID in monitorList) {
await sendHeartbeatList(socket, monitorID);
await sendImportantHeartbeatList(socket, monitorID);
await Monitor.sendStats(io, monitorID, user.id)
}
}
@@ -348,11 +346,8 @@ async function getMonitorJSONList(userID) {
for (let monitor of monitorList) {
result[monitor.id] = monitor.toJSON();
}
return result;
}
@@ -455,3 +450,15 @@ async function sendHeartbeatList(socket, monitorID) {
socket.emit("heartbeatList", monitorID, result)
}
async function sendImportantHeartbeatList(socket, monitorID) {
let list = await R.find("heartbeat", `
monitor_id = ?
AND important = 1
ORDER BY time DESC
LIMIT 500
`, [
monitorID
])
socket.emit("importantHeartbeatList", monitorID, list)
}