mirror of
https://github.com/louislam/uptime-kuma.git
synced 2025-08-09 07:39:12 +08:00
send notification for important heartbeat
This commit is contained in:
@@ -8,7 +8,7 @@ const axios = require("axios");
|
||||
const {tcping, ping} = require("../util-server");
|
||||
const {R} = require("redbean-node");
|
||||
const {BeanModel} = require("redbean-node/dist/bean-model");
|
||||
|
||||
const {Notification} = require("../notification")
|
||||
|
||||
/**
|
||||
* status:
|
||||
@@ -17,7 +17,18 @@ const {BeanModel} = require("redbean-node/dist/bean-model");
|
||||
*/
|
||||
class Monitor extends BeanModel {
|
||||
|
||||
toJSON() {
|
||||
async toJSON() {
|
||||
|
||||
let notificationIDList = {};
|
||||
|
||||
let list = await R.find("monitor_notification", " monitor_id = ? ", [
|
||||
this.id
|
||||
])
|
||||
|
||||
for (let bean of list) {
|
||||
notificationIDList[bean.notification_id] = true;
|
||||
}
|
||||
|
||||
return {
|
||||
id: this.id,
|
||||
name: this.name,
|
||||
@@ -29,6 +40,7 @@ class Monitor extends BeanModel {
|
||||
type: this.type,
|
||||
interval: this.interval,
|
||||
keyword: this.keyword,
|
||||
notificationIDList
|
||||
};
|
||||
}
|
||||
|
||||
@@ -96,6 +108,28 @@ class Monitor extends BeanModel {
|
||||
// Mark as important if status changed
|
||||
if (! previousBeat || previousBeat.status !== bean.status) {
|
||||
bean.important = true;
|
||||
|
||||
let notificationList = await R.getAll(`SELECT notification.* FROM notification, monitor_notification WHERE monitor_id = ? `, [
|
||||
this.id
|
||||
])
|
||||
|
||||
let promiseList = [];
|
||||
|
||||
let text;
|
||||
if (bean.status === 1) {
|
||||
text = "✅ Up"
|
||||
} else {
|
||||
text = "🔴 Down"
|
||||
}
|
||||
|
||||
let msg = `[${this.name}] [${text}] ${bean.msg}`;
|
||||
|
||||
for(let notification of notificationList) {
|
||||
promiseList.push(Notification.send(JSON.parse(notification.config), msg));
|
||||
}
|
||||
|
||||
await Promise.all(promiseList);
|
||||
|
||||
} else {
|
||||
bean.important = false;
|
||||
}
|
||||
|
@@ -111,12 +111,17 @@ let monitorList = {};
|
||||
socket.on("add", async (monitor, callback) => {
|
||||
try {
|
||||
checkLogin(socket)
|
||||
|
||||
let bean = R.dispense("monitor")
|
||||
|
||||
let notificationIDList = monitor.notificationIDList;
|
||||
delete monitor.notificationIDList;
|
||||
|
||||
bean.import(monitor)
|
||||
bean.user_id = socket.userID
|
||||
await R.store(bean)
|
||||
|
||||
await updateMonitorNotification(bean.id, notificationIDList)
|
||||
|
||||
await startMonitor(socket.userID, bean.id);
|
||||
await sendMonitorList(socket);
|
||||
|
||||
@@ -154,6 +159,8 @@ let monitorList = {};
|
||||
|
||||
await R.store(bean)
|
||||
|
||||
await updateMonitorNotification(bean.id, monitor.notificationIDList)
|
||||
|
||||
if (bean.active) {
|
||||
await restartMonitor(socket.userID, bean.id)
|
||||
}
|
||||
@@ -188,7 +195,7 @@ let monitorList = {};
|
||||
|
||||
callback({
|
||||
ok: true,
|
||||
monitor: bean.toJSON(),
|
||||
monitor: await bean.toJSON(),
|
||||
});
|
||||
|
||||
} catch (e) {
|
||||
@@ -391,6 +398,21 @@ let monitorList = {};
|
||||
|
||||
})();
|
||||
|
||||
async function updateMonitorNotification(monitorID, notificationIDList) {
|
||||
R.exec("DELETE FROM monitor_notification WHERE monitor_id = ? ", [
|
||||
monitorID
|
||||
])
|
||||
|
||||
for (let notificationID in notificationIDList) {
|
||||
if (notificationIDList[notificationID]) {
|
||||
let relation = R.dispense("monitor_notification");
|
||||
relation.monitor_id = monitorID;
|
||||
relation.notification_id = notificationID;
|
||||
await R.store(relation)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
async function checkOwner(userID, monitorID) {
|
||||
let row = await R.getRow("SELECT id FROM monitor WHERE id = ? AND user_id = ? ", [
|
||||
monitorID,
|
||||
@@ -445,7 +467,7 @@ async function getMonitorJSONList(userID) {
|
||||
])
|
||||
|
||||
for (let monitor of monitorList) {
|
||||
result[monitor.id] = monitor.toJSON();
|
||||
result[monitor.id] = await monitor.toJSON();
|
||||
}
|
||||
|
||||
return result;
|
||||
|
Reference in New Issue
Block a user