From e5f6d7f0479ee35cb19e266014bfd357770d1209 Mon Sep 17 00:00:00 2001 From: LouisLam Date: Thu, 7 Oct 2021 17:39:58 +0800 Subject: [PATCH] slack and rocket.chat use the primary base url env var to show time logger --- server/model/monitor.js | 5 +- server/notification-providers/rocket-chat.js | 34 +++++++--- server/notification-providers/slack.js | 66 ++++++++++++++------ src/components/MonitorList.vue | 3 +- src/components/notifications/RocketChat.vue | 2 - src/components/notifications/Slack.vue | 3 +- src/pages/Settings.vue | 2 +- src/util.js | 8 ++- src/util.ts | 6 +- 9 files changed, 93 insertions(+), 36 deletions(-) diff --git a/server/model/monitor.js b/server/model/monitor.js index c551fa7d7..0e2d84ede 100644 --- a/server/model/monitor.js +++ b/server/model/monitor.js @@ -166,7 +166,9 @@ class Monitor extends BeanModel { } } - debug("Cert Info Query Time: " + (dayjs().valueOf() - certInfoStartTime) + "ms"); + if (process.env.TIMELOGGER === "1") { + debug("Cert Info Query Time: " + (dayjs().valueOf() - certInfoStartTime) + "ms"); + } if (this.type === "http") { bean.status = UP; @@ -530,6 +532,7 @@ class Monitor extends BeanModel { const uptime = await this.calcUptime(duration, monitorID); io.to(userID).emit("uptime", monitorID, duration, uptime); } + } module.exports = Monitor; diff --git a/server/notification-providers/rocket-chat.js b/server/notification-providers/rocket-chat.js index 149189650..a9bd40091 100644 --- a/server/notification-providers/rocket-chat.js +++ b/server/notification-providers/rocket-chat.js @@ -1,5 +1,8 @@ const NotificationProvider = require("./notification-provider"); const axios = require("axios"); +const Slack = require("./slack"); +const { setting } = require("../util-server"); +const { getMonitorRelativeURL, UP, DOWN } = require("../../src/util"); class RocketChat extends NotificationProvider { @@ -10,16 +13,17 @@ class RocketChat extends NotificationProvider { try { if (heartbeatJSON == null) { let data = { - "text": "Uptime Kuma Rocket.chat testing successful.", + "text": msg, "channel": notification.rocketchannel, "username": notification.rocketusername, "icon_emoji": notification.rocketiconemo, - } - await axios.post(notification.rocketwebhookURL, data) + }; + await axios.post(notification.rocketwebhookURL, data); return okMsg; } const time = heartbeatJSON["time"]; + let data = { "text": "Uptime Kuma Alert", "channel": notification.rocketchannel, @@ -28,16 +32,32 @@ class RocketChat extends NotificationProvider { "attachments": [ { "title": "Uptime Kuma Alert *Time (UTC)*\n" + time, - "title_link": notification.rocketbutton, "text": "*Message*\n" + msg, - "color": "#32cd32" } ] + }; + + // Color + if (heartbeatJSON.status === DOWN) { + data.attachments[0].color = "#ff0000"; + } else { + data.attachments[0].color = "#32cd32"; } - await axios.post(notification.rocketwebhookURL, data) + + if (notification.rocketbutton) { + await Slack.deprecateURL(notification.rocketbutton); + } + + const baseURL = await setting("primaryBaseURL"); + + if (baseURL) { + data.attachments[0].title_link = baseURL + getMonitorRelativeURL(monitorJSON.id); + } + + await axios.post(notification.rocketwebhookURL, data); return okMsg; } catch (error) { - this.throwGeneralAxiosError(error) + this.throwGeneralAxiosError(error); } } diff --git a/server/notification-providers/slack.js b/server/notification-providers/slack.js index 661df5a0f..271ee3dc0 100644 --- a/server/notification-providers/slack.js +++ b/server/notification-providers/slack.js @@ -1,21 +1,40 @@ const NotificationProvider = require("./notification-provider"); const axios = require("axios"); +const { setSettings, setting } = require("../util-server"); +const { getMonitorRelativeURL } = require("../../src/util"); class Slack extends NotificationProvider { name = "slack"; + /** + * Deprecated property notification.slackbutton + * Set it as primary base url if this is not yet set. + */ + static async deprecateURL(url) { + let currentPrimaryBaseURL = await setting("primaryBaseURL"); + + if (!currentPrimaryBaseURL) { + console.log("Move the url to be the primary base URL"); + await setSettings("general", { + primaryBaseURL: url, + }); + } else { + console.log("Already there, no need to move the primary base URL"); + } + } + async send(notification, msg, monitorJSON = null, heartbeatJSON = null) { let okMsg = "Sent Successfully. "; try { if (heartbeatJSON == null) { let data = { - "text": "Uptime Kuma Slack testing successful.", + "text": msg, "channel": notification.slackchannel, "username": notification.slackusername, "icon_emoji": notification.slackiconemo, - } - await axios.post(notification.slackwebhookURL, data) + }; + await axios.post(notification.slackwebhookURL, data); return okMsg; } @@ -42,26 +61,35 @@ class Slack extends NotificationProvider { "type": "mrkdwn", "text": "*Time (UTC)*\n" + time, }], - }, - { - "type": "actions", - "elements": [ - { - "type": "button", - "text": { - "type": "plain_text", - "text": "Visit Uptime Kuma", - }, - "value": "Uptime-Kuma", - "url": notification.slackbutton || "https://github.com/louislam/uptime-kuma", - }, - ], }], + }; + + if (notification.slackbutton) { + await Slack.deprecateURL(notification.slackbutton); } - await axios.post(notification.slackwebhookURL, data) + + const baseURL = await setting("primaryBaseURL"); + + // Button + if (baseURL) { + data.blocks.push({ + "type": "actions", + "elements": [{ + "type": "button", + "text": { + "type": "plain_text", + "text": "Visit Uptime Kuma", + }, + "value": "Uptime-Kuma", + "url": baseURL + getMonitorRelativeURL(monitorJSON.id), + }], + }); + } + + await axios.post(notification.slackwebhookURL, data); return okMsg; } catch (error) { - this.throwGeneralAxiosError(error) + this.throwGeneralAxiosError(error); } } diff --git a/src/components/MonitorList.vue b/src/components/MonitorList.vue index 54c5873ef..77efe564a 100644 --- a/src/components/MonitorList.vue +++ b/src/components/MonitorList.vue @@ -47,6 +47,7 @@ import HeartbeatBar from "../components/HeartbeatBar.vue"; import Uptime from "../components/Uptime.vue"; import Tag from "../components/Tag.vue"; +import { getMonitorRelativeURL } from "../util.ts"; export default { components: { @@ -109,7 +110,7 @@ export default { }, methods: { monitorURL(id) { - return "/dashboard/" + id; + return getMonitorRelativeURL(id); }, clearSearchText() { this.searchText = ""; diff --git a/src/components/notifications/RocketChat.vue b/src/components/notifications/RocketChat.vue index 465b0dc9d..ed90fb75d 100644 --- a/src/components/notifications/RocketChat.vue +++ b/src/components/notifications/RocketChat.vue @@ -8,8 +8,6 @@ - -
*{{ $t("Required") }} diff --git a/src/components/notifications/Slack.vue b/src/components/notifications/Slack.vue index 0e1e7cb08..6d220caad 100644 --- a/src/components/notifications/Slack.vue +++ b/src/components/notifications/Slack.vue @@ -8,8 +8,7 @@ - - +
*{{ $t("Required") }} diff --git a/src/pages/Settings.vue b/src/pages/Settings.vue index 6068f116d..259f334b7 100644 --- a/src/pages/Settings.vue +++ b/src/pages/Settings.vue @@ -111,7 +111,7 @@
- +
diff --git a/src/util.js b/src/util.js index 54649bd58..64ac2f6f6 100644 --- a/src/util.js +++ b/src/util.js @@ -7,7 +7,7 @@ // Backend uses the compiled file util.js // Frontend uses util.ts Object.defineProperty(exports, "__esModule", { value: true }); -exports.genSecret = exports.getRandomInt = exports.getRandomArbitrary = exports.TimeLogger = exports.polyfill = exports.debug = exports.ucfirst = exports.sleep = exports.flipStatus = exports.STATUS_PAGE_PARTIAL_DOWN = exports.STATUS_PAGE_ALL_UP = exports.STATUS_PAGE_ALL_DOWN = exports.PENDING = exports.UP = exports.DOWN = exports.appName = exports.isDev = void 0; +exports.getMonitorRelativeURL = exports.genSecret = exports.getRandomInt = exports.getRandomArbitrary = exports.TimeLogger = exports.polyfill = exports.debug = exports.ucfirst = exports.sleep = exports.flipStatus = exports.STATUS_PAGE_PARTIAL_DOWN = exports.STATUS_PAGE_ALL_UP = exports.STATUS_PAGE_ALL_DOWN = exports.PENDING = exports.UP = exports.DOWN = exports.appName = exports.isDev = void 0; const _dayjs = require("dayjs"); const dayjs = _dayjs; exports.isDev = process.env.NODE_ENV === "development"; @@ -74,7 +74,7 @@ class TimeLogger { this.startTime = dayjs().valueOf(); } print(name) { - if (exports.isDev) { + if (exports.isDev && process && process.env.TIMELOGGER === "1") { console.log(name + ": " + (dayjs().valueOf() - this.startTime) + "ms"); } } @@ -112,3 +112,7 @@ function genSecret(length = 64) { return secret; } exports.genSecret = genSecret; +function getMonitorRelativeURL(id) { + return "/dashboard/" + id; +} +exports.getMonitorRelativeURL = getMonitorRelativeURL; diff --git a/src/util.ts b/src/util.ts index 69dbe68a6..450050490 100644 --- a/src/util.ts +++ b/src/util.ts @@ -86,7 +86,7 @@ export class TimeLogger { } print(name: string) { - if (isDev) { + if (isDev && process && process.env.TIMELOGGER === "1") { console.log(name + ": " + (dayjs().valueOf() - this.startTime) + "ms") } } @@ -123,3 +123,7 @@ export function genSecret(length = 64) { } return secret; } + +export function getMonitorRelativeURL(id: string) { + return "/dashboard/" + id; +}