From 942b55ca031b4a487d299c7e189052634fdb9c17 Mon Sep 17 00:00:00 2001 From: Nelson Chan Date: Tue, 3 Jan 2023 21:45:55 +0800 Subject: [PATCH 1/4] Fix: Add support for maintenance in badges --- server/config.js | 1 + server/routers/api-router.js | 36 +++++++++++++++++++++++++++++++++--- 2 files changed, 34 insertions(+), 3 deletions(-) diff --git a/server/config.js b/server/config.js index d46f24b75..398ddbb10 100644 --- a/server/config.js +++ b/server/config.js @@ -5,6 +5,7 @@ const badgeConstants = { naColor: "#999", defaultUpColor: "#66c20a", defaultDownColor: "#c2290a", + defaultMaintenanceColor: "#1747f5", defaultPingColor: "blue", // as defined by badge-maker / shields.io defaultStyle: "flat", defaultPingValueSuffix: "ms", diff --git a/server/routers/api-router.js b/server/routers/api-router.js index bbecbced3..45236d206 100644 --- a/server/routers/api-router.js +++ b/server/routers/api-router.js @@ -111,8 +111,10 @@ router.get("/api/badge/:id/status", cache("5 minutes"), async (request, response label, upLabel = "Up", downLabel = "Down", + maintenanceLabel = "Maintenance", upColor = badgeConstants.defaultUpColor, downColor = badgeConstants.defaultDownColor, + maintenanceColor = badgeConstants.defaultMaintenanceColor, style = badgeConstants.defaultStyle, value, // for demo purpose only } = request.query; @@ -139,11 +141,39 @@ router.get("/api/badge/:id/status", cache("5 minutes"), async (request, response badgeValues.color = badgeConstants.naColor; } else { const heartbeat = await Monitor.getPreviousHeartbeat(requestedMonitorId); - const state = overrideValue !== undefined ? overrideValue : heartbeat.status === 1; + const state = overrideValue !== undefined ? overrideValue : heartbeat.status; badgeValues.label = label ? label : ""; - badgeValues.color = state ? upColor : downColor; - badgeValues.message = label ?? state ? upLabel : downLabel; + switch (state) { + case 1: + badgeValues.color = upColor; + break; + case 3: + badgeValues.color = maintenanceColor; + break; + case 0: + badgeValues.color = downColor; + break; + default: + badgeValues.color = badgeConstants.naColor; + } + if (label !== undefined) { + badgeValues.message = label; + } else { + switch (state) { + case 1: + badgeValues.message = upLabel; + break; + case 3: + badgeValues.message = maintenanceLabel; + break; + case 0: + badgeValues.message = downLabel; + break; + default: + badgeValues.message = "N/A"; + } + } } // build the svg based on given values From dd82f36da3fa3b2852bd470e3f62374f27e8d8e2 Mon Sep 17 00:00:00 2001 From: Nelson Chan Date: Mon, 9 Jan 2023 00:16:18 +0800 Subject: [PATCH 2/4] Fix: Improve syntax & fix weird label logic --- server/routers/api-router.js | 23 +++++------------------ 1 file changed, 5 insertions(+), 18 deletions(-) diff --git a/server/routers/api-router.js b/server/routers/api-router.js index 45236d206..41608a049 100644 --- a/server/routers/api-router.js +++ b/server/routers/api-router.js @@ -143,36 +143,23 @@ router.get("/api/badge/:id/status", cache("5 minutes"), async (request, response const heartbeat = await Monitor.getPreviousHeartbeat(requestedMonitorId); const state = overrideValue !== undefined ? overrideValue : heartbeat.status; - badgeValues.label = label ? label : ""; + badgeValues.label = label ?? ""; switch (state) { case 1: badgeValues.color = upColor; + badgeValues.message = upLabel; break; case 3: badgeValues.color = maintenanceColor; + badgeValues.message = maintenanceLabel; break; case 0: badgeValues.color = downColor; + badgeValues.message = downLabel; break; default: badgeValues.color = badgeConstants.naColor; - } - if (label !== undefined) { - badgeValues.message = label; - } else { - switch (state) { - case 1: - badgeValues.message = upLabel; - break; - case 3: - badgeValues.message = maintenanceLabel; - break; - case 0: - badgeValues.message = downLabel; - break; - default: - badgeValues.message = "N/A"; - } + badgeValues.message = "N/A"; } } From 21b418230cb76d970f40a81fd6cf53145f3d7ae9 Mon Sep 17 00:00:00 2001 From: Nelson Chan Date: Tue, 10 Jan 2023 19:25:31 +0800 Subject: [PATCH 3/4] Chore: reorder cases Co-authored-by: Frank Elsinga --- server/routers/api-router.js | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/server/routers/api-router.js b/server/routers/api-router.js index 41608a049..f2e0874e2 100644 --- a/server/routers/api-router.js +++ b/server/routers/api-router.js @@ -145,6 +145,10 @@ router.get("/api/badge/:id/status", cache("5 minutes"), async (request, response badgeValues.label = label ?? ""; switch (state) { + case 0: + badgeValues.color = downColor; + badgeValues.message = downLabel; + break; case 1: badgeValues.color = upColor; badgeValues.message = upLabel; @@ -153,10 +157,6 @@ router.get("/api/badge/:id/status", cache("5 minutes"), async (request, response badgeValues.color = maintenanceColor; badgeValues.message = maintenanceLabel; break; - case 0: - badgeValues.color = downColor; - badgeValues.message = downLabel; - break; default: badgeValues.color = badgeConstants.naColor; badgeValues.message = "N/A"; From 0ed3dd5e4fd8e33ef2d1d579e1b9d882ce4f09c3 Mon Sep 17 00:00:00 2001 From: Nelson Chan Date: Thu, 12 Jan 2023 04:14:46 +0800 Subject: [PATCH 4/4] Fix: Add support for pending in badges --- server/config.js | 1 + server/routers/api-router.js | 6 ++++++ 2 files changed, 7 insertions(+) diff --git a/server/config.js b/server/config.js index 398ddbb10..0523e7078 100644 --- a/server/config.js +++ b/server/config.js @@ -5,6 +5,7 @@ const badgeConstants = { naColor: "#999", defaultUpColor: "#66c20a", defaultDownColor: "#c2290a", + defaultPendingColor: "#f8a306", defaultMaintenanceColor: "#1747f5", defaultPingColor: "blue", // as defined by badge-maker / shields.io defaultStyle: "flat", diff --git a/server/routers/api-router.js b/server/routers/api-router.js index f2e0874e2..6001d58e0 100644 --- a/server/routers/api-router.js +++ b/server/routers/api-router.js @@ -111,9 +111,11 @@ router.get("/api/badge/:id/status", cache("5 minutes"), async (request, response label, upLabel = "Up", downLabel = "Down", + pendingLabel = "Pending", maintenanceLabel = "Maintenance", upColor = badgeConstants.defaultUpColor, downColor = badgeConstants.defaultDownColor, + pendingColor = badgeConstants.defaultPendingColor, maintenanceColor = badgeConstants.defaultMaintenanceColor, style = badgeConstants.defaultStyle, value, // for demo purpose only @@ -153,6 +155,10 @@ router.get("/api/badge/:id/status", cache("5 minutes"), async (request, response badgeValues.color = upColor; badgeValues.message = upLabel; break; + case 2: + badgeValues.color = pendingColor; + badgeValues.message = pendingLabel; + break; case 3: badgeValues.color = maintenanceColor; badgeValues.message = maintenanceLabel;