From 5624d257ce4e1edf519f7dc88c5382b285f815df Mon Sep 17 00:00:00 2001 From: 0xsid0703 Date: Sun, 18 Jan 2026 03:39:13 +0100 Subject: [PATCH] Fixed review from owner --- server/model/monitor.js | 70 ++++++++++++------------ server/notification-providers/discord.js | 1 + 2 files changed, 36 insertions(+), 35 deletions(-) diff --git a/server/model/monitor.js b/server/model/monitor.js index 0f1cc18da..bfc3099fe 100644 --- a/server/model/monitor.js +++ b/server/model/monitor.js @@ -1505,43 +1505,43 @@ class Monitor extends BeanModel { let msg = `[${monitor.name}] [${text}] ${bean.msg}`; + const heartbeatJSON = await bean.toJSONAsync({ decodeResponse: true }); + const monitorData = [{ id: monitor.id, active: monitor.active, name: monitor.name }]; + const preloadData = await Monitor.preparePreloadData(monitorData); + // Prevent if the msg is undefined, notifications such as Discord cannot send out. + if (!heartbeatJSON["msg"]) { + heartbeatJSON["msg"] = "N/A"; + } + + // Also provide the time in server timezone + heartbeatJSON["timezone"] = await UptimeKumaServer.getInstance().getTimezone(); + heartbeatJSON["timezoneOffset"] = UptimeKumaServer.getInstance().getTimezoneOffset(); + heartbeatJSON["localDateTime"] = dayjs + .utc(heartbeatJSON["time"]) + .tz(heartbeatJSON["timezone"]) + .format(SQL_DATETIME_FORMAT); + + // Calculate downtime tracking information when service comes back up + // This makes downtime information available to all notification providers + if (bean.status === UP && monitor.id) { + try { + const lastDownHeartbeat = await R.getRow( + "SELECT time FROM heartbeat WHERE monitor_id = ? AND status = ? ORDER BY time DESC LIMIT 1", + [monitor.id, DOWN] + ); + + if (lastDownHeartbeat && lastDownHeartbeat.time) { + heartbeatJSON["lastDownTime"] = lastDownHeartbeat.time; + } + } catch (error) { + // If we can't calculate downtime, just continue without it + // Silently fail to avoid disrupting notification sending + log.debug("monitor", `[${monitor.name}] Could not calculate downtime information: ${error.message}`); + } + } + for (let notification of notificationList) { try { - const heartbeatJSON = await bean.toJSONAsync({ decodeResponse: true }); - const monitorData = [{ id: monitor.id, active: monitor.active, name: monitor.name }]; - const preloadData = await Monitor.preparePreloadData(monitorData); - // Prevent if the msg is undefined, notifications such as Discord cannot send out. - if (!heartbeatJSON["msg"]) { - heartbeatJSON["msg"] = "N/A"; - } - - // Also provide the time in server timezone - heartbeatJSON["timezone"] = await UptimeKumaServer.getInstance().getTimezone(); - heartbeatJSON["timezoneOffset"] = UptimeKumaServer.getInstance().getTimezoneOffset(); - heartbeatJSON["localDateTime"] = dayjs - .utc(heartbeatJSON["time"]) - .tz(heartbeatJSON["timezone"]) - .format(SQL_DATETIME_FORMAT); - - // Calculate downtime tracking information when service comes back up - // This makes downtime information available to all notification providers - if (bean.status === UP && monitor.id) { - try { - const lastDownHeartbeat = await R.getRow( - "SELECT time FROM heartbeat WHERE monitor_id = ? AND status = ? ORDER BY time DESC LIMIT 1", - [monitor.id, DOWN] - ); - - if (lastDownHeartbeat && lastDownHeartbeat.time) { - heartbeatJSON["lastDownTime"] = lastDownHeartbeat.time; - } - } catch (error) { - // If we can't calculate downtime, just continue without it - // Silently fail to avoid disrupting notification sending - log.debug("monitor", `[${monitor.name}] Could not calculate downtime information: ${error.message}`); - } - } - await Notification.send( JSON.parse(notification.config), msg, diff --git a/server/notification-providers/discord.js b/server/notification-providers/discord.js index 3d5dcaa5f..4c3b1ee55 100644 --- a/server/notification-providers/discord.js +++ b/server/notification-providers/discord.js @@ -127,6 +127,7 @@ class Discord extends NotificationProvider { const durationSeconds = backOnlineTimestamp - wentOfflineTimestamp; // Format duration as human-readable string (e.g., "1h 23m", "45m 30s") + // TODO: Update below to Intl.DurationFormat("en", { style: "short" }).format(duration) once we are on a newer node version const hours = Math.floor(durationSeconds / 3600); const minutes = Math.floor((durationSeconds % 3600) / 60); const seconds = durationSeconds % 60;