mirror of
https://github.com/louislam/uptime-kuma.git
synced 2024-10-01 01:25:45 -04:00
perf: ⚡️ revert some overengineer code
This commit is contained in:
parent
58533e8f06
commit
4941b17a46
@ -11,17 +11,7 @@ const { R } = require("redbean-node");
|
|||||||
const apicache = require("../modules/apicache");
|
const apicache = require("../modules/apicache");
|
||||||
const Monitor = require("../model/monitor");
|
const Monitor = require("../model/monitor");
|
||||||
const dayjs = require("dayjs");
|
const dayjs = require("dayjs");
|
||||||
const {
|
const { UP, MAINTENANCE, DOWN, PENDING, flipStatus, log, badgeConstants } = require("../../src/util");
|
||||||
UP,
|
|
||||||
MAINTENANCE,
|
|
||||||
DOWN,
|
|
||||||
PENDING,
|
|
||||||
flipStatus,
|
|
||||||
log,
|
|
||||||
badgeConstants,
|
|
||||||
durationUnits,
|
|
||||||
isNumeric,
|
|
||||||
} = require("../../src/util");
|
|
||||||
const StatusPage = require("../model/status_page");
|
const StatusPage = require("../model/status_page");
|
||||||
const { UptimeKumaServer } = require("../uptime-kuma-server");
|
const { UptimeKumaServer } = require("../uptime-kuma-server");
|
||||||
const { makeBadge } = require("badge-maker");
|
const { makeBadge } = require("badge-maker");
|
||||||
@ -239,13 +229,12 @@ 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 : "24";
|
let requestedDuration = request.params.duration !== undefined ? request.params.duration : "24h";
|
||||||
const overrideValue = value && parseFloat(value);
|
const overrideValue = value && parseFloat(value);
|
||||||
|
|
||||||
if (isNumeric(requestedDuration)) { // all numeric only
|
if (/^[0-9]+$/.test(requestedDuration)) {
|
||||||
requestedDuration = `${requestedDuration}${durationUnits.HOUR}`;
|
requestedDuration = `${requestedDuration}h`;
|
||||||
}
|
}
|
||||||
const duration = requestedDuration.slice(0, -1);
|
|
||||||
|
|
||||||
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\`
|
||||||
@ -276,7 +265,7 @@ router.get("/api/badge/:id/uptime/:duration?", cache("5 minutes"), async (reques
|
|||||||
// build a label string. If a custom label is given, override the default one (requestedDuration)
|
// build a label string. If a custom label is given, override the default one (requestedDuration)
|
||||||
badgeValues.label = filterAndJoin([
|
badgeValues.label = filterAndJoin([
|
||||||
labelPrefix,
|
labelPrefix,
|
||||||
label ?? `Uptime (${duration}${labelSuffix})`,
|
label ?? `Uptime (${requestedDuration.slice(0, -1)}${labelSuffix})`,
|
||||||
]);
|
]);
|
||||||
badgeValues.message = filterAndJoin([ prefix, cleanUptime, suffix ]);
|
badgeValues.message = filterAndJoin([ prefix, cleanUptime, suffix ]);
|
||||||
}
|
}
|
||||||
@ -323,6 +312,7 @@ router.get("/api/badge/:id/ping/:duration?", cache("5 minutes"), async (request,
|
|||||||
const publicAvgPing = uptimeCalculator.getDataByDuration(requestedDuration).avgPing;
|
const publicAvgPing = uptimeCalculator.getDataByDuration(requestedDuration).avgPing;
|
||||||
|
|
||||||
const badgeValues = { style };
|
const badgeValues = { style };
|
||||||
|
|
||||||
if (!publicAvgPing) {
|
if (!publicAvgPing) {
|
||||||
// return a "N/A" badge in naColor (grey), if monitor is not public / not available / non exsitant
|
// return a "N/A" badge in naColor (grey), if monitor is not public / not available / non exsitant
|
||||||
|
|
||||||
|
@ -1,12 +1,5 @@
|
|||||||
const dayjs = require("dayjs");
|
const dayjs = require("dayjs");
|
||||||
const {
|
const { UP, MAINTENANCE, DOWN, PENDING } = require("../src/util");
|
||||||
UP,
|
|
||||||
MAINTENANCE,
|
|
||||||
DOWN,
|
|
||||||
PENDING,
|
|
||||||
durationUnits,
|
|
||||||
isNumeric
|
|
||||||
} = require("../src/util");
|
|
||||||
const { LimitQueue } = require("./utils/limit-queue");
|
const { LimitQueue } = require("./utils/limit-queue");
|
||||||
const { log } = require("../src/util");
|
const { log } = require("../src/util");
|
||||||
const { R } = require("redbean-node");
|
const { R } = require("redbean-node");
|
||||||
@ -758,24 +751,24 @@ class UptimeCalculator {
|
|||||||
getDataByDuration(duration) {
|
getDataByDuration(duration) {
|
||||||
const durationNumStr = duration.slice(0, -1);
|
const durationNumStr = duration.slice(0, -1);
|
||||||
|
|
||||||
if (!isNumeric(durationNumStr)) {
|
if (!/^[0-9]+$/.test(durationNumStr)) {
|
||||||
throw new Error(`Invalid duration: ${duration}`);
|
throw new Error(`Invalid duration: ${duration}`);
|
||||||
}
|
}
|
||||||
const num = Number(durationNumStr);
|
const num = Number(durationNumStr);
|
||||||
const unit = duration.slice(-1);
|
const unit = duration.slice(-1);
|
||||||
|
|
||||||
switch (unit) {
|
switch (unit) {
|
||||||
case durationUnits.MINUTE:
|
case "m":
|
||||||
return this.getData(num, "minute");
|
return this.getData(num, "minute");
|
||||||
case durationUnits.HOUR:
|
case "h":
|
||||||
return this.getData(num, "hour");
|
return this.getData(num, "hour");
|
||||||
case durationUnits.DAY:
|
case "d":
|
||||||
return this.getData(num, "day");
|
return this.getData(num, "day");
|
||||||
case durationUnits.WEEK:
|
case "w":
|
||||||
return this.getData(7 * num, "day");
|
return this.getData(7 * num, "day");
|
||||||
case durationUnits.MONTH:
|
case "M":
|
||||||
return this.getData(30 * num, "day");
|
return this.getData(30 * num, "day");
|
||||||
case durationUnits.YEAR:
|
case "y":
|
||||||
return this.getData(365 * num, "day");
|
return this.getData(365 * num, "day");
|
||||||
default:
|
default:
|
||||||
throw new Error(`Unsupported unit (${unit}) for badge duration ${duration}`
|
throw new Error(`Unsupported unit (${unit}) for badge duration ${duration}`
|
||||||
|
16
src/util.js
16
src/util.js
@ -10,8 +10,8 @@
|
|||||||
*/
|
*/
|
||||||
var _a;
|
var _a;
|
||||||
Object.defineProperty(exports, "__esModule", { value: true });
|
Object.defineProperty(exports, "__esModule", { value: true });
|
||||||
exports.flipStatus = exports.durationUnits = exports.badgeConstants = exports.CONSOLE_STYLE_BgGray = exports.CONSOLE_STYLE_BgWhite = exports.CONSOLE_STYLE_BgCyan = exports.CONSOLE_STYLE_BgMagenta = exports.CONSOLE_STYLE_BgBlue = exports.CONSOLE_STYLE_BgYellow = exports.CONSOLE_STYLE_BgGreen = exports.CONSOLE_STYLE_BgRed = exports.CONSOLE_STYLE_BgBlack = exports.CONSOLE_STYLE_FgPink = exports.CONSOLE_STYLE_FgBrown = exports.CONSOLE_STYLE_FgViolet = exports.CONSOLE_STYLE_FgLightBlue = exports.CONSOLE_STYLE_FgLightGreen = exports.CONSOLE_STYLE_FgOrange = exports.CONSOLE_STYLE_FgGray = exports.CONSOLE_STYLE_FgWhite = exports.CONSOLE_STYLE_FgCyan = exports.CONSOLE_STYLE_FgMagenta = exports.CONSOLE_STYLE_FgBlue = exports.CONSOLE_STYLE_FgYellow = exports.CONSOLE_STYLE_FgGreen = exports.CONSOLE_STYLE_FgRed = exports.CONSOLE_STYLE_FgBlack = exports.CONSOLE_STYLE_Hidden = exports.CONSOLE_STYLE_Reverse = exports.CONSOLE_STYLE_Blink = exports.CONSOLE_STYLE_Underscore = exports.CONSOLE_STYLE_Dim = exports.CONSOLE_STYLE_Bright = exports.CONSOLE_STYLE_Reset = exports.MIN_INTERVAL_SECOND = exports.MAX_INTERVAL_SECOND = exports.SQL_DATETIME_FORMAT_WITHOUT_SECOND = exports.SQL_DATETIME_FORMAT = exports.SQL_DATE_FORMAT = exports.STATUS_PAGE_MAINTENANCE = exports.STATUS_PAGE_PARTIAL_DOWN = exports.STATUS_PAGE_ALL_UP = exports.STATUS_PAGE_ALL_DOWN = exports.MAINTENANCE = exports.PENDING = exports.UP = exports.DOWN = exports.appName = exports.isNode = exports.isDev = void 0;
|
exports.sleep = exports.flipStatus = exports.badgeConstants = exports.CONSOLE_STYLE_BgGray = exports.CONSOLE_STYLE_BgWhite = exports.CONSOLE_STYLE_BgCyan = exports.CONSOLE_STYLE_BgMagenta = exports.CONSOLE_STYLE_BgBlue = exports.CONSOLE_STYLE_BgYellow = exports.CONSOLE_STYLE_BgGreen = exports.CONSOLE_STYLE_BgRed = exports.CONSOLE_STYLE_BgBlack = exports.CONSOLE_STYLE_FgPink = exports.CONSOLE_STYLE_FgBrown = exports.CONSOLE_STYLE_FgViolet = exports.CONSOLE_STYLE_FgLightBlue = exports.CONSOLE_STYLE_FgLightGreen = exports.CONSOLE_STYLE_FgOrange = exports.CONSOLE_STYLE_FgGray = exports.CONSOLE_STYLE_FgWhite = exports.CONSOLE_STYLE_FgCyan = exports.CONSOLE_STYLE_FgMagenta = exports.CONSOLE_STYLE_FgBlue = exports.CONSOLE_STYLE_FgYellow = exports.CONSOLE_STYLE_FgGreen = exports.CONSOLE_STYLE_FgRed = exports.CONSOLE_STYLE_FgBlack = exports.CONSOLE_STYLE_Hidden = exports.CONSOLE_STYLE_Reverse = exports.CONSOLE_STYLE_Blink = exports.CONSOLE_STYLE_Underscore = exports.CONSOLE_STYLE_Dim = exports.CONSOLE_STYLE_Bright = exports.CONSOLE_STYLE_Reset = exports.MIN_INTERVAL_SECOND = exports.MAX_INTERVAL_SECOND = exports.SQL_DATETIME_FORMAT_WITHOUT_SECOND = exports.SQL_DATETIME_FORMAT = exports.SQL_DATE_FORMAT = exports.STATUS_PAGE_MAINTENANCE = exports.STATUS_PAGE_PARTIAL_DOWN = exports.STATUS_PAGE_ALL_UP = exports.STATUS_PAGE_ALL_DOWN = exports.MAINTENANCE = exports.PENDING = exports.UP = exports.DOWN = exports.appName = exports.isNode = exports.isDev = void 0;
|
||||||
exports.isNumeric = exports.intHash = exports.localToUTC = exports.utcToLocal = exports.utcToISODateTime = exports.isoToUTCDateTime = exports.parseTimeFromTimeObject = exports.parseTimeObject = exports.getMaintenanceRelativeURL = exports.getMonitorRelativeURL = exports.genSecret = exports.getCryptoRandomInt = exports.getRandomInt = exports.getRandomArbitrary = exports.TimeLogger = exports.polyfill = exports.log = exports.debug = exports.ucfirst = exports.sleep = void 0;
|
exports.intHash = exports.localToUTC = exports.utcToLocal = exports.utcToISODateTime = exports.isoToUTCDateTime = exports.parseTimeFromTimeObject = exports.parseTimeObject = exports.getMaintenanceRelativeURL = exports.getMonitorRelativeURL = exports.genSecret = exports.getCryptoRandomInt = exports.getRandomInt = exports.getRandomArbitrary = exports.TimeLogger = exports.polyfill = exports.log = exports.debug = exports.ucfirst = void 0;
|
||||||
const dayjs = require("dayjs");
|
const dayjs = require("dayjs");
|
||||||
exports.isDev = process.env.NODE_ENV === "development";
|
exports.isDev = process.env.NODE_ENV === "development";
|
||||||
exports.isNode = typeof process !== "undefined" && ((_a = process === null || process === void 0 ? void 0 : process.versions) === null || _a === void 0 ? void 0 : _a.node);
|
exports.isNode = typeof process !== "undefined" && ((_a = process === null || process === void 0 ? void 0 : process.versions) === null || _a === void 0 ? void 0 : _a.node);
|
||||||
@ -96,14 +96,6 @@ exports.badgeConstants = {
|
|||||||
defaultCertExpireWarnDays: "14",
|
defaultCertExpireWarnDays: "14",
|
||||||
defaultCertExpireDownDays: "7"
|
defaultCertExpireDownDays: "7"
|
||||||
};
|
};
|
||||||
exports.durationUnits = {
|
|
||||||
MINUTE: "m",
|
|
||||||
HOUR: "h",
|
|
||||||
DAY: "d",
|
|
||||||
WEEK: "w",
|
|
||||||
MONTH: "M",
|
|
||||||
YEAR: "y"
|
|
||||||
};
|
|
||||||
function flipStatus(s) {
|
function flipStatus(s) {
|
||||||
if (s === exports.UP) {
|
if (s === exports.UP) {
|
||||||
return exports.DOWN;
|
return exports.DOWN;
|
||||||
@ -404,7 +396,3 @@ function intHash(str, length = 10) {
|
|||||||
return (hash % length + length) % length;
|
return (hash % length + length) % length;
|
||||||
}
|
}
|
||||||
exports.intHash = intHash;
|
exports.intHash = intHash;
|
||||||
function isNumeric(str) {
|
|
||||||
return /^([0-9]+)$/.test(str);
|
|
||||||
}
|
|
||||||
exports.isNumeric = isNumeric;
|
|
||||||
|
28
src/util.ts
28
src/util.ts
@ -93,6 +93,11 @@ const consoleLevelColors : Record<string, string> = {
|
|||||||
"DEBUG": CONSOLE_STYLE_FgGray,
|
"DEBUG": CONSOLE_STYLE_FgGray,
|
||||||
};
|
};
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Flip the status of s
|
||||||
|
* @param s input status: UP or DOWN
|
||||||
|
* @returns {number} UP or DOWN
|
||||||
|
*/
|
||||||
export const badgeConstants = {
|
export const badgeConstants = {
|
||||||
naColor: "#999",
|
naColor: "#999",
|
||||||
defaultUpColor: "#66c20a",
|
defaultUpColor: "#66c20a",
|
||||||
@ -113,20 +118,7 @@ export const badgeConstants = {
|
|||||||
defaultCertExpireDownDays: "7"
|
defaultCertExpireDownDays: "7"
|
||||||
};
|
};
|
||||||
|
|
||||||
export const durationUnits = {
|
/** Flip the status of s */
|
||||||
MINUTE: "m",
|
|
||||||
HOUR: "h",
|
|
||||||
DAY: "d",
|
|
||||||
WEEK: "w",
|
|
||||||
MONTH: "M",
|
|
||||||
YEAR: "y"
|
|
||||||
};
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Flip the status of s
|
|
||||||
* @param s input status: UP or DOWN
|
|
||||||
* @returns {number} UP or DOWN
|
|
||||||
*/
|
|
||||||
export function flipStatus(s: number) {
|
export function flipStatus(s: number) {
|
||||||
if (s === UP) {
|
if (s === UP) {
|
||||||
return DOWN;
|
return DOWN;
|
||||||
@ -651,11 +643,3 @@ export function intHash(str : string, length = 10) : number {
|
|||||||
return (hash % length + length) % length; // Ensure the result is non-negative
|
return (hash % length + length) % length; // Ensure the result is non-negative
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
|
||||||
* Check if a string is numeric.
|
|
||||||
* @param {string} str - The input string to check
|
|
||||||
* @returns {boolean} Returns true if the input string is numeric, false otherwise
|
|
||||||
*/
|
|
||||||
export function isNumeric(str: string): boolean {
|
|
||||||
return /^([0-9]+)$/.test(str);
|
|
||||||
}
|
|
||||||
|
Loading…
Reference in New Issue
Block a user