improve uptime calculation

This commit is contained in:
LouisLam 2021-07-06 13:44:33 +08:00
parent d4a651e211
commit ea1556ef81

View File

@ -158,7 +158,9 @@ class Monitor extends BeanModel {
]); ]);
let downtime = 0; let downtime = 0;
let uptime = 0;
if (downtimeList.length === 0) {
for (let row of downtimeList) { for (let row of downtimeList) {
let value = parseInt(row.duration) let value = parseInt(row.duration)
let time = row.time let time = row.time
@ -179,11 +181,32 @@ class Monitor extends BeanModel {
} }
} }
let uptime = (sec - downtime) / sec; uptime = (sec - downtime) / sec;
if (uptime < 0) { if (uptime < 0) {
uptime = 0; uptime = 0;
} }
} else {
// This case for someone who are not running UptimeKuma 24x7.
// If there is no heartbeat in this time range, use last heartbeat as reference
// If is down, uptime = 0
// If is up, uptime = 1
let lastHeartbeat = await R.findOne("heartbeat", " monitor_id = ? ORDER BY time DESC", [
monitorID
]);
if (lastHeartbeat) {
if (lastHeartbeat.status === 1) {
uptime = 1;
} else {
uptime = 0;
}
} else {
// No heartbeat is found, assume 100%
uptime = 1;
}
}
io.to(userID).emit("uptime", monitorID, duration, uptime); io.to(userID).emit("uptime", monitorID, duration, uptime);
} }