From e5ff86e6acc000477cb500739242e7b7dae717da Mon Sep 17 00:00:00 2001 From: Qi Zhao Date: Wed, 12 Jun 2024 18:17:16 +0800 Subject: [PATCH] =?UTF-8?q?fix:=20=F0=9F=90=9B=20status=20&=20ping=20badge?= =?UTF-8?q?=20duration?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- server/routers/api-router.js | 16 ++++------------ server/uptime-calculator.js | 22 +++++++++++++--------- 2 files changed, 17 insertions(+), 21 deletions(-) diff --git a/server/routers/api-router.js b/server/routers/api-router.js index 9c572f686..761b488e4 100644 --- a/server/routers/api-router.js +++ b/server/routers/api-router.js @@ -229,13 +229,9 @@ router.get("/api/badge/:id/uptime/:duration?", cache("5 minutes"), async (reques try { const requestedMonitorId = parseInt(request.params.id, 10); // if no duration is given, set value to 24 (h) - let requestedDuration = request.params.duration !== undefined ? request.params.duration : "24h"; + let requestedDuration = request.params.duration !== undefined ? request.params.duration : "24"; const overrideValue = value && parseFloat(value); - if (requestedDuration === "24") { - requestedDuration = "24h"; - } - let publicMonitor = await R.getRow(` SELECT monitor_group.monitor_id FROM monitor_group, \`group\` WHERE monitor_group.group_id = \`group\`.id @@ -253,7 +249,7 @@ router.get("/api/badge/:id/uptime/:duration?", cache("5 minutes"), async (reques badgeValues.color = badgeConstants.naColor; } else { const uptimeCalculator = await UptimeCalculator.getUptimeCalculator(requestedMonitorId); - const uptime = overrideValue ?? uptimeCalculator.getDataByDuration(requestedDuration).uptime; + const uptime = overrideValue ?? uptimeCalculator.getDataByDuration(`${requestedDuration}h`).uptime; // limit the displayed uptime percentage to four (two, when displayed as percent) decimal digits const cleanUptime = (uptime * 100).toPrecision(4); @@ -299,17 +295,13 @@ router.get("/api/badge/:id/ping/:duration?", cache("5 minutes"), async (request, const requestedMonitorId = parseInt(request.params.id, 10); // Default duration is 24 (h) if not defined in queryParam, limited to 720h (30d) - let requestedDuration = request.params.duration !== undefined ? request.params.duration : "24h"; + let requestedDuration = request.params.duration !== undefined ? request.params.duration : "24"; const overrideValue = value && parseFloat(value); - if (requestedDuration === "24") { - requestedDuration = "24h"; - } - // Check if monitor is public const uptimeCalculator = await UptimeCalculator.getUptimeCalculator(requestedMonitorId); - const publicAvgPing = uptimeCalculator.getDataByDuration(requestedDuration).avgPing; + const publicAvgPing = uptimeCalculator.getDataByDuration(`${requestedDuration}h`).avgPing; const badgeValues = { style }; diff --git a/server/uptime-calculator.js b/server/uptime-calculator.js index 55059e960..37dfd6771 100644 --- a/server/uptime-calculator.js +++ b/server/uptime-calculator.js @@ -741,21 +741,25 @@ class UptimeCalculator { } /** - * Get the uptime data by duration - * @param {'24h'|'30d'|'1y'} duration Only accept 24h, 30d, 1y + * Get the uptime data for given duration. + * @param {string} duration A string with a number and a unit (h, d, or y), such as 24h, 30d, 1y. * @returns {UptimeDataResult} UptimeDataResult * @throws {Error} Invalid duration */ getDataByDuration(duration) { - if (duration === "24h") { - return this.get24Hour(); - } else if (duration === "30d") { - return this.get30Day(); - } else if (duration === "1y") { - return this.get1Year(); - } else { + const unit = duration.substring(duration.length - 1); + const num = parseInt(duration.substring(0, duration.length - 1)); + + const typeMap = { + h: "hour", + d: "day", + y: "year" + }; + + if (!Object.keys(typeMap).includes(unit)) { throw new Error("Invalid duration"); } + return this.getData(num, typeMap[unit]); } /**