slack and rocket.chat use the primary base url

env var to show time logger
This commit is contained in:
LouisLam 2021-10-07 17:39:58 +08:00
parent b69550f5b9
commit e5f6d7f047
9 changed files with 93 additions and 36 deletions

View file

@ -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") { if (this.type === "http") {
bean.status = UP; bean.status = UP;
@ -530,6 +532,7 @@ class Monitor extends BeanModel {
const uptime = await this.calcUptime(duration, monitorID); const uptime = await this.calcUptime(duration, monitorID);
io.to(userID).emit("uptime", monitorID, duration, uptime); io.to(userID).emit("uptime", monitorID, duration, uptime);
} }
} }
module.exports = Monitor; module.exports = Monitor;

View file

@ -1,5 +1,8 @@
const NotificationProvider = require("./notification-provider"); const NotificationProvider = require("./notification-provider");
const axios = require("axios"); const axios = require("axios");
const Slack = require("./slack");
const { setting } = require("../util-server");
const { getMonitorRelativeURL, UP, DOWN } = require("../../src/util");
class RocketChat extends NotificationProvider { class RocketChat extends NotificationProvider {
@ -10,16 +13,17 @@ class RocketChat extends NotificationProvider {
try { try {
if (heartbeatJSON == null) { if (heartbeatJSON == null) {
let data = { let data = {
"text": "Uptime Kuma Rocket.chat testing successful.", "text": msg,
"channel": notification.rocketchannel, "channel": notification.rocketchannel,
"username": notification.rocketusername, "username": notification.rocketusername,
"icon_emoji": notification.rocketiconemo, "icon_emoji": notification.rocketiconemo,
} };
await axios.post(notification.rocketwebhookURL, data) await axios.post(notification.rocketwebhookURL, data);
return okMsg; return okMsg;
} }
const time = heartbeatJSON["time"]; const time = heartbeatJSON["time"];
let data = { let data = {
"text": "Uptime Kuma Alert", "text": "Uptime Kuma Alert",
"channel": notification.rocketchannel, "channel": notification.rocketchannel,
@ -28,16 +32,32 @@ class RocketChat extends NotificationProvider {
"attachments": [ "attachments": [
{ {
"title": "Uptime Kuma Alert *Time (UTC)*\n" + time, "title": "Uptime Kuma Alert *Time (UTC)*\n" + time,
"title_link": notification.rocketbutton,
"text": "*Message*\n" + msg, "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; return okMsg;
} catch (error) { } catch (error) {
this.throwGeneralAxiosError(error) this.throwGeneralAxiosError(error);
} }
} }

View file

@ -1,21 +1,40 @@
const NotificationProvider = require("./notification-provider"); const NotificationProvider = require("./notification-provider");
const axios = require("axios"); const axios = require("axios");
const { setSettings, setting } = require("../util-server");
const { getMonitorRelativeURL } = require("../../src/util");
class Slack extends NotificationProvider { class Slack extends NotificationProvider {
name = "slack"; 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) { async send(notification, msg, monitorJSON = null, heartbeatJSON = null) {
let okMsg = "Sent Successfully. "; let okMsg = "Sent Successfully. ";
try { try {
if (heartbeatJSON == null) { if (heartbeatJSON == null) {
let data = { let data = {
"text": "Uptime Kuma Slack testing successful.", "text": msg,
"channel": notification.slackchannel, "channel": notification.slackchannel,
"username": notification.slackusername, "username": notification.slackusername,
"icon_emoji": notification.slackiconemo, "icon_emoji": notification.slackiconemo,
} };
await axios.post(notification.slackwebhookURL, data) await axios.post(notification.slackwebhookURL, data);
return okMsg; return okMsg;
} }
@ -42,26 +61,35 @@ class Slack extends NotificationProvider {
"type": "mrkdwn", "type": "mrkdwn",
"text": "*Time (UTC)*\n" + time, "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; return okMsg;
} catch (error) { } catch (error) {
this.throwGeneralAxiosError(error) this.throwGeneralAxiosError(error);
} }
} }

View file

@ -47,6 +47,7 @@
import HeartbeatBar from "../components/HeartbeatBar.vue"; import HeartbeatBar from "../components/HeartbeatBar.vue";
import Uptime from "../components/Uptime.vue"; import Uptime from "../components/Uptime.vue";
import Tag from "../components/Tag.vue"; import Tag from "../components/Tag.vue";
import { getMonitorRelativeURL } from "../util.ts";
export default { export default {
components: { components: {
@ -109,7 +110,7 @@ export default {
}, },
methods: { methods: {
monitorURL(id) { monitorURL(id) {
return "/dashboard/" + id; return getMonitorRelativeURL(id);
}, },
clearSearchText() { clearSearchText() {
this.searchText = ""; this.searchText = "";

View file

@ -8,8 +8,6 @@
<input id="rocket-iconemo" v-model="$parent.notification.rocketiconemo" type="text" class="form-control"> <input id="rocket-iconemo" v-model="$parent.notification.rocketiconemo" type="text" class="form-control">
<label for="rocket-channel" class="form-label">{{ $t("Channel Name") }}</label> <label for="rocket-channel" class="form-label">{{ $t("Channel Name") }}</label>
<input id="rocket-channel-name" v-model="$parent.notification.rocketchannel" type="text" class="form-control"> <input id="rocket-channel-name" v-model="$parent.notification.rocketchannel" type="text" class="form-control">
<label for="rocket-button-url" class="form-label">{{ $t("Uptime Kuma URL") }}</label>
<input id="rocket-button" v-model="$parent.notification.rocketbutton" type="text" class="form-control">
<div class="form-text"> <div class="form-text">
<span style="color: red;"><sup>*</sup></span>{{ $t("Required") }} <span style="color: red;"><sup>*</sup></span>{{ $t("Required") }}
<i18n-t tag="p" keypath="aboutWebhooks" style="margin-top: 8px;"> <i18n-t tag="p" keypath="aboutWebhooks" style="margin-top: 8px;">

View file

@ -8,8 +8,7 @@
<input id="slack-iconemo" v-model="$parent.notification.slackiconemo" type="text" class="form-control"> <input id="slack-iconemo" v-model="$parent.notification.slackiconemo" type="text" class="form-control">
<label for="slack-channel" class="form-label">{{ $t("Channel Name") }}</label> <label for="slack-channel" class="form-label">{{ $t("Channel Name") }}</label>
<input id="slack-channel-name" v-model="$parent.notification.slackchannel" type="text" class="form-control"> <input id="slack-channel-name" v-model="$parent.notification.slackchannel" type="text" class="form-control">
<label for="slack-button-url" class="form-label">{{ $t("Uptime Kuma URL") }}</label>
<input id="slack-button" v-model="$parent.notification.slackbutton" type="text" class="form-control">
<div class="form-text"> <div class="form-text">
<span style="color: red;"><sup>*</sup></span>{{ $t("Required") }} <span style="color: red;"><sup>*</sup></span>{{ $t("Required") }}
<i18n-t tag="p" keypath="aboutWebhooks" style="margin-top: 8px;"> <i18n-t tag="p" keypath="aboutWebhooks" style="margin-top: 8px;">

View file

@ -111,7 +111,7 @@
<label class="form-label" for="primaryBaseURL">Primary Base URL</label> <label class="form-label" for="primaryBaseURL">Primary Base URL</label>
<div class="input-group mb-3"> <div class="input-group mb-3">
<input id="primaryBaseURL" v-model="settings.primaryBaseURL" class="form-control" name="primaryBaseURL" placeholder="https://"> <input id="primaryBaseURL" v-model="settings.primaryBaseURL" class="form-control" name="primaryBaseURL" placeholder="https://" pattern="https?://.+">
<button class="btn btn-outline-primary" type="button" @click="autoGetPrimaryBaseURL">Auto Get</button> <button class="btn btn-outline-primary" type="button" @click="autoGetPrimaryBaseURL">Auto Get</button>
</div> </div>

View file

@ -7,7 +7,7 @@
// Backend uses the compiled file util.js // Backend uses the compiled file util.js
// Frontend uses util.ts // Frontend uses util.ts
Object.defineProperty(exports, "__esModule", { value: true }); 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 = require("dayjs");
const dayjs = _dayjs; const dayjs = _dayjs;
exports.isDev = process.env.NODE_ENV === "development"; exports.isDev = process.env.NODE_ENV === "development";
@ -74,7 +74,7 @@ class TimeLogger {
this.startTime = dayjs().valueOf(); this.startTime = dayjs().valueOf();
} }
print(name) { print(name) {
if (exports.isDev) { if (exports.isDev && process && process.env.TIMELOGGER === "1") {
console.log(name + ": " + (dayjs().valueOf() - this.startTime) + "ms"); console.log(name + ": " + (dayjs().valueOf() - this.startTime) + "ms");
} }
} }
@ -112,3 +112,7 @@ function genSecret(length = 64) {
return secret; return secret;
} }
exports.genSecret = genSecret; exports.genSecret = genSecret;
function getMonitorRelativeURL(id) {
return "/dashboard/" + id;
}
exports.getMonitorRelativeURL = getMonitorRelativeURL;

View file

@ -86,7 +86,7 @@ export class TimeLogger {
} }
print(name: string) { print(name: string) {
if (isDev) { if (isDev && process && process.env.TIMELOGGER === "1") {
console.log(name + ": " + (dayjs().valueOf() - this.startTime) + "ms") console.log(name + ": " + (dayjs().valueOf() - this.startTime) + "ms")
} }
} }
@ -123,3 +123,7 @@ export function genSecret(length = 64) {
} }
return secret; return secret;
} }
export function getMonitorRelativeURL(id: string) {
return "/dashboard/" + id;
}