From c9132adfc709d8fc0549e39dd98a53c8f77041d2 Mon Sep 17 00:00:00 2001 From: Frank Elsinga Date: Thu, 13 Jun 2024 17:00:11 +0200 Subject: [PATCH] made sure that the address extraction is extracted into its own method --- server/notification-providers/discord.js | 22 +------------ .../notification-provider.js | 31 +++++++++++++++++++ server/notification-providers/sevenio.js | 23 +------------- server/notification-providers/smtp.js | 7 +---- server/notification-providers/squadcast.js | 20 +----------- server/notification-providers/teams.js | 17 +--------- server/notification-providers/zoho-cliq.js | 18 ----------- 7 files changed, 36 insertions(+), 102 deletions(-) diff --git a/server/notification-providers/discord.js b/server/notification-providers/discord.js index 129e334b..38b88fe9 100644 --- a/server/notification-providers/discord.js +++ b/server/notification-providers/discord.js @@ -33,26 +33,6 @@ class Discord extends NotificationProvider { return okMsg; } - let address; - - switch (monitorJSON["type"]) { - case "ping": - address = monitorJSON["hostname"]; - break; - case "port": - case "dns": - case "gamedig": - case "steam": - address = monitorJSON["hostname"]; - if (monitorJSON["port"]) { - address += ":" + monitorJSON["port"]; - } - break; - default: - address = monitorJSON["url"]; - break; - } - // If heartbeatJSON is not null, we go into the normal alerting loop. if (heartbeatJSON["status"] === DOWN) { let discorddowndata = { @@ -105,7 +85,7 @@ class Discord extends NotificationProvider { }, { name: monitorJSON["type"] === "push" ? "Service Type" : "Service URL", - value: monitorJSON["type"] === "push" ? "Heartbeat" : address, + value: this.extractAdress(monitorJSON), }, { name: `Time (${heartbeatJSON["timezone"]})`, diff --git a/server/notification-providers/notification-provider.js b/server/notification-providers/notification-provider.js index 9b4f0bb0..1a3a3612 100644 --- a/server/notification-providers/notification-provider.js +++ b/server/notification-providers/notification-provider.js @@ -19,6 +19,37 @@ class NotificationProvider { throw new Error("Have to override Notification.send(...)"); } + /** + * Extracts the address from a monitor JSON object based on its type. + * + * @param {?object} monitorJSON Monitor details (For Up/Down only) + * @returns {string} The extracted address based on the monitor type. + */ + extractAdress(monitorJSON){ + if (!monitorJSON) { + return "" + } + switch (monitorJSON["type"]) { + case "push": + return "Heartbeat"; + case "ping": + return monitorJSON["hostname"]; + case "port": + case "dns": + case "gamedig": + case "steam": + if (monitorJSON["port"]) { + return monitorJSON["hostname"] + ":" + monitorJSON["port"]; + } + return monitorJSON["hostname"] + default: + if (![ "https://", "http://", "" ].includes(monitorJSON["url"])) { + return monitorJSON["url"]; + } + return ""; + } + } + /** * Throws an error * @param {any} error The error to throw diff --git a/server/notification-providers/sevenio.js b/server/notification-providers/sevenio.js index d363e95d..e422ff1f 100644 --- a/server/notification-providers/sevenio.js +++ b/server/notification-providers/sevenio.js @@ -32,28 +32,7 @@ class SevenIO extends NotificationProvider { return okMsg; } - let address = ""; - - switch (monitorJSON["type"]) { - case "ping": - address = monitorJSON["hostname"]; - break; - case "port": - case "dns": - case "gamedig": - case "steam": - address = monitorJSON["hostname"]; - if (monitorJSON["port"]) { - address += ":" + monitorJSON["port"]; - } - break; - default: - if (![ "https://", "http://", "" ].includes(monitorJSON["url"])) { - address = monitorJSON["url"]; - } - break; - } - + let address = extractAdress(monitorJSON); if (address !== "") { address = `(${address}) `; } diff --git a/server/notification-providers/smtp.js b/server/notification-providers/smtp.js index b403117b..801e0e69 100644 --- a/server/notification-providers/smtp.js +++ b/server/notification-providers/smtp.js @@ -93,12 +93,7 @@ class SMTP extends NotificationProvider { if (monitorJSON !== null) { monitorName = monitorJSON["name"]; - - if (monitorJSON["type"] === "http" || monitorJSON["type"] === "keyword" || monitorJSON["type"] === "json-query") { - monitorHostnameOrURL = monitorJSON["url"]; - } else { - monitorHostnameOrURL = monitorJSON["hostname"]; - } + monitorHostnameOrURL = this.extractAdress(monitorJSON); } let serviceStatus = "⚠️ Test"; diff --git a/server/notification-providers/squadcast.js b/server/notification-providers/squadcast.js index 1c653d14..9074e9b7 100644 --- a/server/notification-providers/squadcast.js +++ b/server/notification-providers/squadcast.js @@ -34,25 +34,7 @@ class Squadcast extends NotificationProvider { data.status = "resolve"; } - let address; - switch (monitorJSON["type"]) { - case "ping": - address = monitorJSON["hostname"]; - break; - case "port": - case "dns": - case "steam": - address = monitorJSON["hostname"]; - if (monitorJSON["port"]) { - address += ":" + monitorJSON["port"]; - } - break; - default: - address = monitorJSON["url"]; - break; - } - - data.tags["AlertAddress"] = address; + data.tags["AlertAddress"] = this.extractAdress(monitorJSON); monitorJSON["tags"].forEach(tag => { data.tags[tag["name"]] = { diff --git a/server/notification-providers/teams.js b/server/notification-providers/teams.js index f34ba1ce..2fcea707 100644 --- a/server/notification-providers/teams.js +++ b/server/notification-providers/teams.js @@ -216,21 +216,6 @@ class Teams extends NotificationProvider { return okMsg; } - let monitorUrl; - - switch (monitorJSON["type"]) { - case "http": - case "keywork": - monitorUrl = monitorJSON["url"]; - break; - case "docker": - monitorUrl = monitorJSON["docker_host"]; - break; - default: - monitorUrl = monitorJSON["hostname"]; - break; - } - const baseURL = await setting("primaryBaseURL"); let dashboardUrl; if (baseURL) { @@ -240,7 +225,7 @@ class Teams extends NotificationProvider { const payload = this._notificationPayloadFactory({ heartbeatJSON: heartbeatJSON, monitorName: monitorJSON.name, - monitorUrl: monitorUrl, + monitorUrl: this.extractAdress(monitorJSON), dashboardUrl: dashboardUrl, }); diff --git a/server/notification-providers/zoho-cliq.js b/server/notification-providers/zoho-cliq.js index e741603c..76a05968 100644 --- a/server/notification-providers/zoho-cliq.js +++ b/server/notification-providers/zoho-cliq.js @@ -82,25 +82,7 @@ class ZohoCliq extends NotificationProvider { return okMsg; } - let address; - switch (monitorJSON["type"]) { - case "ping": - address = monitorJSON["hostname"]; - break; - case "port": - case "dns": - case "gamedig": - case "steam": - address = monitorJSON["hostname"]; - if (monitorJSON["port"]) { - address += ":" + monitorJSON["port"]; - } - break; - default: - address = monitorJSON["url"]; - break; - } const payload = this._notificationPayloadFactory({ monitorMessage: heartbeatJSON.msg,