fix: 🐛 status & ping badge duration

This commit is contained in:
Qi Zhao 2024-06-12 18:17:16 +08:00 committed by Qi Zhao
parent 39c1283ba6
commit e5ff86e6ac
2 changed files with 17 additions and 21 deletions

View File

@ -229,13 +229,9 @@ router.get("/api/badge/:id/uptime/:duration?", cache("5 minutes"), async (reques
try { try {
const requestedMonitorId = parseInt(request.params.id, 10); const requestedMonitorId = parseInt(request.params.id, 10);
// if no duration is given, set value to 24 (h) // 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); const overrideValue = value && parseFloat(value);
if (requestedDuration === "24") {
requestedDuration = "24h";
}
let publicMonitor = await R.getRow(` let publicMonitor = await R.getRow(`
SELECT monitor_group.monitor_id FROM monitor_group, \`group\` SELECT monitor_group.monitor_id FROM monitor_group, \`group\`
WHERE monitor_group.group_id = \`group\`.id 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; badgeValues.color = badgeConstants.naColor;
} else { } else {
const uptimeCalculator = await UptimeCalculator.getUptimeCalculator(requestedMonitorId); 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 // limit the displayed uptime percentage to four (two, when displayed as percent) decimal digits
const cleanUptime = (uptime * 100).toPrecision(4); 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); const requestedMonitorId = parseInt(request.params.id, 10);
// Default duration is 24 (h) if not defined in queryParam, limited to 720h (30d) // 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); const overrideValue = value && parseFloat(value);
if (requestedDuration === "24") {
requestedDuration = "24h";
}
// Check if monitor is public // Check if monitor is public
const uptimeCalculator = await UptimeCalculator.getUptimeCalculator(requestedMonitorId); const uptimeCalculator = await UptimeCalculator.getUptimeCalculator(requestedMonitorId);
const publicAvgPing = uptimeCalculator.getDataByDuration(requestedDuration).avgPing; const publicAvgPing = uptimeCalculator.getDataByDuration(`${requestedDuration}h`).avgPing;
const badgeValues = { style }; const badgeValues = { style };

View File

@ -741,21 +741,25 @@ class UptimeCalculator {
} }
/** /**
* Get the uptime data by duration * Get the uptime data for given duration.
* @param {'24h'|'30d'|'1y'} duration Only accept 24h, 30d, 1y * @param {string} duration A string with a number and a unit (h, d, or y), such as 24h, 30d, 1y.
* @returns {UptimeDataResult} UptimeDataResult * @returns {UptimeDataResult} UptimeDataResult
* @throws {Error} Invalid duration * @throws {Error} Invalid duration
*/ */
getDataByDuration(duration) { getDataByDuration(duration) {
if (duration === "24h") { const unit = duration.substring(duration.length - 1);
return this.get24Hour(); const num = parseInt(duration.substring(0, duration.length - 1));
} else if (duration === "30d") {
return this.get30Day(); const typeMap = {
} else if (duration === "1y") { h: "hour",
return this.get1Year(); d: "day",
} else { y: "year"
};
if (!Object.keys(typeMap).includes(unit)) {
throw new Error("Invalid duration"); throw new Error("Invalid duration");
} }
return this.getData(num, typeMap[unit]);
} }
/** /**