From 50d6ea36a4bc9395d05a5ceadaddaa2a33f6258a Mon Sep 17 00:00:00 2001 From: Peace Date: Sat, 12 Oct 2024 23:24:56 +0200 Subject: [PATCH 1/4] feat: show child status on group error --- server/model/monitor.js | 36 ++++++++++++++++++++++++++++++++---- 1 file changed, 32 insertions(+), 4 deletions(-) diff --git a/server/model/monitor.js b/server/model/monitor.js index 5b7e5871a..5cafe9a07 100644 --- a/server/model/monitor.js +++ b/server/model/monitor.js @@ -382,7 +382,8 @@ class Monitor extends BeanModel { if (children.length > 0) { bean.status = UP; - bean.msg = "All children up and running"; + bean.msg = ""; + let errorChildNames = []; for (const child of children) { if (!child.active) { // Ignore inactive childs @@ -399,11 +400,24 @@ class Monitor extends BeanModel { } else if (bean.status === PENDING && lastBeat.status === DOWN) { bean.status = lastBeat.status; } + + if(lastBeat && (lastBeat.status === PENDING || lastBeat.status === DOWN)) { + console.log(lastBeat.monitor_id); + const childMonitor = await Monitor.getMonitor(lastBeat.monitor_id); + console.log(childMonitor) + if(errorChildNames.length > 0) + bean.msg += "\r\n"; + + bean.msg += "- " + childMonitor.name + ":\r\n" + lastBeat.msg.trim().replace(/^/gm, " "); + errorChildNames.push(childMonitor.name); + } } - if (bean.status !== UP) { - bean.msg = "Child inaccessible"; - } + if (bean.status === UP) { + bean.msg = "All children up and running"; + } else if(bean.status === PENDING || bean.status === DOWN) { + bean.msg = "Some Children are having problems (" + errorChildNames.join(', ') + ")\r\n" + bean.msg; + } } else { // Set status pending if group is empty bean.status = PENDING; @@ -1604,6 +1618,20 @@ class Monitor extends BeanModel { }; } + /** + * Gets Monitor with specific ID + * @param {number} monitorID ID of monitor to get + * @returns {Promise>} Children + */ + static async getMonitor(monitorID) { + return await R.getRow(` + SELECT * FROM monitor + WHERE id = ? + `, [ + monitorID, + ]); + } + /** * Gets Parent of the monitor * @param {number} monitorID ID of monitor to get From 90b9173cfb75fbc345d4d22b95182e2ff1559640 Mon Sep 17 00:00:00 2001 From: Peace Date: Sat, 12 Oct 2024 23:27:34 +0200 Subject: [PATCH 2/4] style: fix formatting --- server/model/monitor.js | 33 +++++++++++++++++---------------- 1 file changed, 17 insertions(+), 16 deletions(-) diff --git a/server/model/monitor.js b/server/model/monitor.js index 5cafe9a07..943dec4f1 100644 --- a/server/model/monitor.js +++ b/server/model/monitor.js @@ -382,8 +382,8 @@ class Monitor extends BeanModel { if (children.length > 0) { bean.status = UP; - bean.msg = ""; - let errorChildNames = []; + bean.msg = ""; + let errorChildNames = []; for (const child of children) { if (!child.active) { // Ignore inactive childs @@ -401,23 +401,24 @@ class Monitor extends BeanModel { bean.status = lastBeat.status; } - if(lastBeat && (lastBeat.status === PENDING || lastBeat.status === DOWN)) { - console.log(lastBeat.monitor_id); - const childMonitor = await Monitor.getMonitor(lastBeat.monitor_id); - console.log(childMonitor) - if(errorChildNames.length > 0) - bean.msg += "\r\n"; + if (lastBeat && (lastBeat.status === PENDING || lastBeat.status === DOWN)) { + console.log(lastBeat.monitor_id); + const childMonitor = await Monitor.getMonitor(lastBeat.monitor_id); + console.log(childMonitor); + if (errorChildNames.length > 0) { + bean.msg += "\r\n"; + } - bean.msg += "- " + childMonitor.name + ":\r\n" + lastBeat.msg.trim().replace(/^/gm, " "); - errorChildNames.push(childMonitor.name); - } + bean.msg += "- " + childMonitor.name + ":\r\n" + lastBeat.msg.trim().replace(/^/gm, " "); + errorChildNames.push(childMonitor.name); + } } if (bean.status === UP) { - bean.msg = "All children up and running"; - } else if(bean.status === PENDING || bean.status === DOWN) { - bean.msg = "Some Children are having problems (" + errorChildNames.join(', ') + ")\r\n" + bean.msg; - } + bean.msg = "All children up and running"; + } else if (bean.status === PENDING || bean.status === DOWN) { + bean.msg = "Some Children are having problems (" + errorChildNames.join(", ") + ")\r\n" + bean.msg; + } } else { // Set status pending if group is empty bean.status = PENDING; @@ -1618,7 +1619,7 @@ class Monitor extends BeanModel { }; } - /** + /** * Gets Monitor with specific ID * @param {number} monitorID ID of monitor to get * @returns {Promise>} Children From a3376069d07444d100457a2d0189dbce042a79c8 Mon Sep 17 00:00:00 2001 From: Peace Date: Sat, 12 Oct 2024 23:39:35 +0200 Subject: [PATCH 3/4] fix: remove test logs --- server/model/monitor.js | 2 -- 1 file changed, 2 deletions(-) diff --git a/server/model/monitor.js b/server/model/monitor.js index 943dec4f1..241dc10d2 100644 --- a/server/model/monitor.js +++ b/server/model/monitor.js @@ -402,9 +402,7 @@ class Monitor extends BeanModel { } if (lastBeat && (lastBeat.status === PENDING || lastBeat.status === DOWN)) { - console.log(lastBeat.monitor_id); const childMonitor = await Monitor.getMonitor(lastBeat.monitor_id); - console.log(childMonitor); if (errorChildNames.length > 0) { bean.msg += "\r\n"; } From 94af1d98d74438be24ad88c905fe0a46b52b7601 Mon Sep 17 00:00:00 2001 From: Peace Date: Thu, 17 Oct 2024 19:46:32 +0200 Subject: [PATCH 4/4] feat: opt-out option to show path instead of name for notifications --- .../2024-10-17-0000-notifications-path-as-name.js | 12 ++++++++++++ server/client.js | 8 ++++++++ server/model/monitor.js | 6 ++++-- server/notification.js | 5 +++++ src/components/NotificationDialog.vue | 12 ++++++++++++ 5 files changed, 41 insertions(+), 2 deletions(-) create mode 100644 db/knex_migrations/2024-10-17-0000-notifications-path-as-name.js diff --git a/db/knex_migrations/2024-10-17-0000-notifications-path-as-name.js b/db/knex_migrations/2024-10-17-0000-notifications-path-as-name.js new file mode 100644 index 000000000..3a4e6be91 --- /dev/null +++ b/db/knex_migrations/2024-10-17-0000-notifications-path-as-name.js @@ -0,0 +1,12 @@ +exports.up = function (knex) { + return knex.schema + .alterTable("notification", function (table) { + table.boolean("use_path_as_name").notNullable().defaultTo(true); + }); +}; + +exports.down = function (knex) { + return knex.schema.alterTable("notification", function (table) { + table.dropColumn("use_path_as_name"); + }); +}; diff --git a/server/client.js b/server/client.js index 72f0a4e8e..f37a84330 100644 --- a/server/client.js +++ b/server/client.js @@ -26,7 +26,15 @@ async function sendNotificationList(socket) { for (let bean of list) { let notificationObject = bean.export(); notificationObject.isDefault = (notificationObject.isDefault === 1); + notificationObject.usePathAsName = (notificationObject.usePathAsName === 1); notificationObject.active = (notificationObject.active === 1); + + const configObject = JSON.parse(notificationObject.config); + if ( !("usePathAsName" in configObject)) { + configObject.usePathAsName = notificationObject.usePathAsName; + notificationObject.config = JSON.stringify(configObject); + } + result.push(notificationObject); } diff --git a/server/model/monitor.js b/server/model/monitor.js index 241dc10d2..2f50119ea 100644 --- a/server/model/monitor.js +++ b/server/model/monitor.js @@ -1336,8 +1336,10 @@ class Monitor extends BeanModel { for (let notification of notificationList) { try { const heartbeatJSON = bean.toJSON(); - const monitorData = [{ id: monitor.id, - active: monitor.active + const monitorData = [{ + id: monitor.id, + name: monitor.name, + active: monitor.active, }]; const preloadData = await Monitor.preparePreloadData(monitorData); // Prevent if the msg is undefined, notifications such as Discord cannot send out. diff --git a/server/notification.js b/server/notification.js index 26daeb042..bf00af46a 100644 --- a/server/notification.js +++ b/server/notification.js @@ -176,6 +176,9 @@ class Notification { * @throws Error with fail msg */ static async send(notification, msg, monitorJSON = null, heartbeatJSON = null) { + if (notification.usePathAsName && monitorJSON) { + monitorJSON["name"] = monitorJSON["pathName"]; + } if (this.providerList[notification.type]) { return this.providerList[notification.type].send(notification, msg, monitorJSON, heartbeatJSON); } else { @@ -211,6 +214,8 @@ class Notification { bean.user_id = userID; bean.config = JSON.stringify(notification); bean.is_default = notification.isDefault || false; + bean.use_path_as_name = notification.usePathAsName; + await R.store(bean); if (notification.applyExisting) { diff --git a/src/components/NotificationDialog.vue b/src/components/NotificationDialog.vue index ec86d15f8..483280080 100644 --- a/src/components/NotificationDialog.vue +++ b/src/components/NotificationDialog.vue @@ -41,6 +41,16 @@
+
+ + +
+
+ If your monitor is inside a group, the name will show the full path of the monitor. For example "Group A / Monitor 1". +
+ +
+
@@ -95,6 +105,7 @@ export default { /** @type { null | keyof NotificationFormList } */ type: null, isDefault: false, + usePathAsName: false, // Do not set default value here, please scroll to show() } }; @@ -263,6 +274,7 @@ export default { name: "", type: "telegram", isDefault: false, + usePathAsName: true, }; }