From f390a8caf1fea00348a3245b4c79d4315c125f3a Mon Sep 17 00:00:00 2001 From: OidaTiftla Date: Mon, 24 Jan 2022 21:59:25 +0100 Subject: [PATCH] Fix missing DB patch and use DATETIME as column format --- db/patch-heartbeat-add-last-notified-time.sql | 7 +++++++ server/database.js | 1 + server/model/monitor.js | 10 +++++----- 3 files changed, 13 insertions(+), 5 deletions(-) create mode 100644 db/patch-heartbeat-add-last-notified-time.sql diff --git a/db/patch-heartbeat-add-last-notified-time.sql b/db/patch-heartbeat-add-last-notified-time.sql new file mode 100644 index 000000000..af9c21c00 --- /dev/null +++ b/db/patch-heartbeat-add-last-notified-time.sql @@ -0,0 +1,7 @@ +-- You should not modify if this have pushed to Github, unless it does serious wrong with the db. +BEGIN TRANSACTION; + +ALTER TABLE heartbeat + ADD last_notified_time DATETIME default null; + +COMMIT; diff --git a/server/database.js b/server/database.js index ce4d50891..0aae8ffc9 100644 --- a/server/database.js +++ b/server/database.js @@ -54,6 +54,7 @@ class Database { "patch-notification_sent_history.sql": true, "patch-monitor-basic-auth.sql": true, "patch-monitor-add-resend-interval.sql": true, + "patch-heartbeat-add-last-notified-time.sql": true, } /** diff --git a/server/model/monitor.js b/server/model/monitor.js index f48033557..85a0e9445 100644 --- a/server/model/monitor.js +++ b/server/model/monitor.js @@ -136,7 +136,7 @@ class Monitor extends BeanModel { bean.monitor_id = this.id; bean.time = R.isoDateTime(dayjs.utc()); bean.status = DOWN; - bean.lastNotifiedTime = previousBeat?.lastNotifiedTime || null; // after first update lastNotifiedTime will be undefined + bean.lastNotifiedTime = previousBeat?.lastNotifiedTime; if (this.isUpsideDown()) { bean.status = flipStatus(bean.status); @@ -393,7 +393,7 @@ class Monitor extends BeanModel { await Monitor.sendNotification(isFirstBeat, this, bean); // Set last notified time to now - bean.lastNotifiedTime = dayjs().valueOf(); + bean.lastNotifiedTime = R.isoDateTime(dayjs.utc()); // Clear Status Page Cache debug(`[${this.name}] apicache clear`); @@ -403,14 +403,14 @@ class Monitor extends BeanModel { bean.important = false; if (bean.status === DOWN && this.resendInterval > 0) { - timeSinceLastNotified = (dayjs().valueOf() - (bean.lastNotifiedTime || 0)) / 60; // divide by 60 to convert from seconds to minutes + let timeSinceLastNotified = (dayjs.utc().valueOf() - (bean.lastNotifiedTime == null ? 0 : dayjs.utc(bean.lastNotifiedTime).valueOf())) / 1000 / 60; // divide by 1000 to convert from milliseconds to seconds and divide by 60 to convert from seconds to minutes if (timeSinceLastNotified >= this.resendInterval) { // Send notification again, because we are still DOWN - debug(`[${this.name}] sendNotification`); + debug(`[${this.name}] sendNotification again: lastNotifiedTime: ${bean.lastNotifiedTime} | current time: ${R.isoDateTime(dayjs.utc())}`); await Monitor.sendNotification(isFirstBeat, this, bean); // Set last notified time to now - bean.lastNotifiedTime = dayjs().valueOf(); + bean.lastNotifiedTime = R.isoDateTime(dayjs.utc()); } } }