|
|
@ -64,38 +64,57 @@ router.get("/api/push/:pushToken", async (request, response) => {
|
|
|
|
|
|
|
|
|
|
|
|
const previousHeartbeat = await Monitor.getPreviousHeartbeat(monitor.id);
|
|
|
|
const previousHeartbeat = await Monitor.getPreviousHeartbeat(monitor.id);
|
|
|
|
|
|
|
|
|
|
|
|
if (monitor.isUpsideDown()) {
|
|
|
|
|
|
|
|
status = flipStatus(status);
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
let isFirstBeat = true;
|
|
|
|
let isFirstBeat = true;
|
|
|
|
let previousStatus = status;
|
|
|
|
|
|
|
|
let duration = 0;
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
let bean = R.dispense("heartbeat");
|
|
|
|
let bean = R.dispense("heartbeat");
|
|
|
|
bean.time = R.isoDateTimeMillis(dayjs.utc());
|
|
|
|
bean.time = R.isoDateTimeMillis(dayjs.utc());
|
|
|
|
|
|
|
|
bean.monitor_id = monitor.id;
|
|
|
|
|
|
|
|
bean.ping = ping;
|
|
|
|
|
|
|
|
bean.msg = msg;
|
|
|
|
|
|
|
|
bean.downCount = previousHeartbeat?.downCount || 0;
|
|
|
|
|
|
|
|
|
|
|
|
if (previousHeartbeat) {
|
|
|
|
if (previousHeartbeat) {
|
|
|
|
isFirstBeat = false;
|
|
|
|
isFirstBeat = false;
|
|
|
|
previousStatus = previousHeartbeat.status;
|
|
|
|
bean.duration = dayjs(bean.time).diff(dayjs(previousHeartbeat.time), "second");
|
|
|
|
duration = dayjs(bean.time).diff(dayjs(previousHeartbeat.time), "second");
|
|
|
|
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
if (await Monitor.isUnderMaintenance(monitor.id)) {
|
|
|
|
if (await Monitor.isUnderMaintenance(monitor.id)) {
|
|
|
|
msg = "Monitor under maintenance";
|
|
|
|
msg = "Monitor under maintenance";
|
|
|
|
status = MAINTENANCE;
|
|
|
|
bean.status = MAINTENANCE;
|
|
|
|
|
|
|
|
} else {
|
|
|
|
|
|
|
|
determineStatus(status, previousHeartbeat, monitor.maxretries, monitor.isUpsideDown(), bean);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// Calculate uptime
|
|
|
|
|
|
|
|
let uptimeCalculator = await UptimeCalculator.getUptimeCalculator(monitor.id);
|
|
|
|
|
|
|
|
let endTimeDayjs = await uptimeCalculator.update(bean.status, parseFloat(bean.ping));
|
|
|
|
|
|
|
|
bean.end_time = R.isoDateTimeMillis(endTimeDayjs);
|
|
|
|
|
|
|
|
|
|
|
|
log.debug("router", `/api/push/ called at ${dayjs().format("YYYY-MM-DD HH:mm:ss.SSS")}`);
|
|
|
|
log.debug("router", `/api/push/ called at ${dayjs().format("YYYY-MM-DD HH:mm:ss.SSS")}`);
|
|
|
|
log.debug("router", "PreviousStatus: " + previousStatus);
|
|
|
|
log.debug("router", "PreviousStatus: " + previousHeartbeat?.status);
|
|
|
|
log.debug("router", "Current Status: " + status);
|
|
|
|
log.debug("router", "Current Status: " + bean.status);
|
|
|
|
|
|
|
|
|
|
|
|
bean.important = Monitor.isImportantBeat(isFirstBeat, previousStatus, status);
|
|
|
|
bean.important = Monitor.isImportantBeat(isFirstBeat, previousHeartbeat?.status, status);
|
|
|
|
bean.monitor_id = monitor.id;
|
|
|
|
|
|
|
|
bean.status = status;
|
|
|
|
if (Monitor.isImportantForNotification(isFirstBeat, previousHeartbeat?.status, status)) {
|
|
|
|
bean.msg = msg;
|
|
|
|
// Reset down count
|
|
|
|
bean.ping = ping;
|
|
|
|
bean.downCount = 0;
|
|
|
|
bean.duration = duration;
|
|
|
|
|
|
|
|
|
|
|
|
log.debug("monitor", `[${this.name}] sendNotification`);
|
|
|
|
|
|
|
|
await Monitor.sendNotification(isFirstBeat, monitor, bean);
|
|
|
|
|
|
|
|
} else {
|
|
|
|
|
|
|
|
if (bean.status === DOWN && this.resendInterval > 0) {
|
|
|
|
|
|
|
|
++bean.downCount;
|
|
|
|
|
|
|
|
if (bean.downCount >= this.resendInterval) {
|
|
|
|
|
|
|
|
// Send notification again, because we are still DOWN
|
|
|
|
|
|
|
|
log.debug("monitor", `[${this.name}] sendNotification again: Down Count: ${bean.downCount} | Resend Interval: ${this.resendInterval}`);
|
|
|
|
|
|
|
|
await Monitor.sendNotification(isFirstBeat, this, bean);
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// Reset down count
|
|
|
|
|
|
|
|
bean.downCount = 0;
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
await R.store(bean);
|
|
|
|
await R.store(bean);
|
|
|
|
|
|
|
|
|
|
|
@ -107,11 +126,6 @@ router.get("/api/push/:pushToken", async (request, response) => {
|
|
|
|
response.json({
|
|
|
|
response.json({
|
|
|
|
ok: true,
|
|
|
|
ok: true,
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
|
|
if (Monitor.isImportantForNotification(isFirstBeat, previousStatus, status)) {
|
|
|
|
|
|
|
|
await Monitor.sendNotification(isFirstBeat, monitor, bean);
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
} catch (e) {
|
|
|
|
} catch (e) {
|
|
|
|
response.status(404).json({
|
|
|
|
response.status(404).json({
|
|
|
|
ok: false,
|
|
|
|
ok: false,
|
|
|
@ -562,4 +576,58 @@ router.get("/api/badge/:id/response", cache("5 minutes"), async (request, respon
|
|
|
|
}
|
|
|
|
}
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
|
|
|
* Determines the status of the next beat in the push route handling.
|
|
|
|
|
|
|
|
* @param {string} status - The reported new status.
|
|
|
|
|
|
|
|
* @param {object} previousHeartbeat - The previous heartbeat object.
|
|
|
|
|
|
|
|
* @param {number} maxretries - The maximum number of retries allowed.
|
|
|
|
|
|
|
|
* @param {boolean} isUpsideDown - Indicates if the monitor is upside down.
|
|
|
|
|
|
|
|
* @param {object} bean - The new heartbeat object.
|
|
|
|
|
|
|
|
* @returns {void}
|
|
|
|
|
|
|
|
*/
|
|
|
|
|
|
|
|
function determineStatus(status, previousHeartbeat, maxretries, isUpsideDown, bean) {
|
|
|
|
|
|
|
|
if (isUpsideDown) {
|
|
|
|
|
|
|
|
status = flipStatus(status);
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
if (previousHeartbeat) {
|
|
|
|
|
|
|
|
if (previousHeartbeat.status === UP && status === DOWN) {
|
|
|
|
|
|
|
|
// Going Down
|
|
|
|
|
|
|
|
if ((maxretries > 0) && (previousHeartbeat.retries < maxretries)) {
|
|
|
|
|
|
|
|
// Retries available
|
|
|
|
|
|
|
|
bean.retries = previousHeartbeat.retries + 1;
|
|
|
|
|
|
|
|
bean.status = PENDING;
|
|
|
|
|
|
|
|
} else {
|
|
|
|
|
|
|
|
// No more retries
|
|
|
|
|
|
|
|
bean.retries = 0;
|
|
|
|
|
|
|
|
bean.status = DOWN;
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
} else if (previousHeartbeat.status === PENDING && status === DOWN && previousHeartbeat.retries < maxretries) {
|
|
|
|
|
|
|
|
// Retries available
|
|
|
|
|
|
|
|
bean.retries = previousHeartbeat.retries + 1;
|
|
|
|
|
|
|
|
bean.status = PENDING;
|
|
|
|
|
|
|
|
} else {
|
|
|
|
|
|
|
|
// No more retries or not pending
|
|
|
|
|
|
|
|
if (status === DOWN) {
|
|
|
|
|
|
|
|
bean.retries = previousHeartbeat.retries + 1;
|
|
|
|
|
|
|
|
bean.status = status;
|
|
|
|
|
|
|
|
} else {
|
|
|
|
|
|
|
|
bean.retries = 0;
|
|
|
|
|
|
|
|
bean.status = status;
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
} else {
|
|
|
|
|
|
|
|
// First beat?
|
|
|
|
|
|
|
|
if (status === DOWN && maxretries > 0) {
|
|
|
|
|
|
|
|
// Retries available
|
|
|
|
|
|
|
|
bean.retries = 1;
|
|
|
|
|
|
|
|
bean.status = PENDING;
|
|
|
|
|
|
|
|
} else {
|
|
|
|
|
|
|
|
// Retires not enabled
|
|
|
|
|
|
|
|
bean.retries = 0;
|
|
|
|
|
|
|
|
bean.status = status;
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
module.exports = router;
|
|
|
|
module.exports = router;
|
|
|
|