This commit is contained in:
Alex Holliday committed 2025-07-21 10:34:16 -07:00
1 parent 846564a17e
commit bfa6832beb
83 files changed
+338 -1249

No files matched your search

+6 -22
View File
@@ -15,9 +15,7 @@ const calculatePercentileUptimeDetails = (arr, percentile) => {
const upper = lower + 1;
const weight = index % 1;
if (upper >= sorted.length) return sorted[lower].avgResponseTime;
return (
sorted[lower].avgResponseTime * (1 - weight) + sorted[upper].avgResponseTime * weight
);
return sorted[lower].avgResponseTime * (1 - weight) + sorted[upper].avgResponseTime * weight;
};
const NormalizeData = (checks, rangeMin, rangeMax) => {
@@ -28,15 +26,11 @@ const NormalizeData = (checks, rangeMin, rangeMax) => {
const normalizedChecks = checks.map((check) => {
const originalResponseTime = check.responseTime;
// Normalize the response time between 1 and 100
let normalizedResponseTime =
rangeMin + ((check.responseTime - min) * (rangeMax - rangeMin)) / (max - min);
let normalizedResponseTime = rangeMin + ((check.responseTime - min) * (rangeMax - rangeMin)) / (max - min);
// Put a floor on the response times so we don't have extreme outliers
// Better visuals
normalizedResponseTime = Math.max(
rangeMin,
Math.min(rangeMax, normalizedResponseTime)
);
normalizedResponseTime = Math.max(rangeMin, Math.min(rangeMax, normalizedResponseTime));
return {
...check,
responseTime: normalizedResponseTime,
@@ -60,15 +54,11 @@ const NormalizeDataUptimeDetails = (checks, rangeMin, rangeMax) => {
const normalizedChecks = checks.map((check) => {
const originalResponseTime = check.avgResponseTime;
// Normalize the response time between 1 and 100
let normalizedResponseTime =
rangeMin + ((check.avgResponseTime - min) * (rangeMax - rangeMin)) / (max - min);
let normalizedResponseTime = rangeMin + ((check.avgResponseTime - min) * (rangeMax - rangeMin)) / (max - min);
// Put a floor on the response times so we don't have extreme outliers
// Better visuals
normalizedResponseTime = Math.max(
rangeMin,
Math.min(rangeMax, normalizedResponseTime)
);
normalizedResponseTime = Math.max(rangeMin, Math.min(rangeMax, normalizedResponseTime));
return {
...check,
avgResponseTime: normalizedResponseTime,
@@ -107,10 +97,4 @@ const safelyParseFloat = (value, defaultValue = 0) => {
return parsedValue;
};
export {
safelyParseFloat,
calculatePercentile,
NormalizeData,
calculatePercentileUptimeDetails,
NormalizeDataUptimeDetails,
};
export { safelyParseFloat, calculatePercentile, NormalizeData, calculatePercentileUptimeDetails, NormalizeDataUptimeDetails };
+9 -53
View File
@@ -10,67 +10,31 @@ class AppError extends Error {
}
}
export const createError = (
message,
status = 500,
service = null,
method = null,
details = null
) => {
export const createError = (message, status = 500, service = null, method = null, details = null) => {
return new AppError(message, status, service, method, details);
};
export const createValidationError = (
message,
details = null,
service = null,
method = null
) => {
export const createValidationError = (message, details = null, service = null, method = null) => {
return createError(message, 422, service, method, details);
};
export const createAuthError = (
message,
details = null,
service = null,
method = null
) => {
export const createAuthError = (message, details = null, service = null, method = null) => {
return createError(message, 401, service, method, details);
};
export const createForbiddenError = (
message,
details = null,
service = null,
method = null
) => {
export const createForbiddenError = (message, details = null, service = null, method = null) => {
return createError(message, 403, service, method, details);
};
export const createNotFoundError = (
message,
details = null,
service = null,
method = null
) => {
export const createNotFoundError = (message, details = null, service = null, method = null) => {
return createError(message, 404, service, method, details);
};
export const createConflictError = (
message,
details = null,
service = null,
method = null
) => {
export const createConflictError = (message, details = null, service = null, method = null) => {
return createError(message, 409, service, method, details);
};
export const createServerError = (
message,
details = null,
service = null,
method = null
) => {
export const createServerError = (message, details = null, service = null, method = null) => {
return createError(message, 500, service, method, details);
};
@@ -81,12 +45,7 @@ export const asyncHandler = (fn, serviceName, methodName) => {
} catch (error) {
// Handle validation errors
if (error.isJoi || error.name === "ValidationError") {
const validationError = createValidationError(
error.message,
error.details,
serviceName,
methodName
);
const validationError = createValidationError(error.message, error.details, serviceName, methodName);
return next(validationError);
}
@@ -107,10 +66,7 @@ export const asyncHandler = (fn, serviceName, methodName) => {
}
// For unknown errors, create a server error
const appError = createServerError(
error.message || "An unexpected error occurred",
{ originalError: error.message, stack: error.stack }
);
const appError = createServerError(error.message || "An unexpected error occurred", { originalError: error.message, stack: error.stack });
appError.service = serviceName;
appError.method = methodName;
appError.stack = error.stack; // Preserve original stack
+35 -42
View File
@@ -6,43 +6,41 @@ class Logger {
constructor() {
this.logCache = [];
this.maxCacheSize = 1000;
const consoleFormat = format.printf(
({ level, message, service, method, details, timestamp, stack }) => {
if (message instanceof Object) {
message = JSON.stringify(message, null, 2);
}
if (details instanceof Object) {
details = JSON.stringify(details, null, 2);
}
let msg = `${timestamp} ${level}:`;
service && (msg += ` [${service}]`);
method && (msg += `(${method})`);
message && (msg += ` ${message}`);
details && (msg += ` (details: ${details})`);
if (typeof stack !== "undefined") {
const stackTrace = stack
?.split("\n")
.slice(1) // Remove first line (error message)
.map((line) => {
const match = line.match(/at\s+(.+?)\s+\((.+?):(\d+):(\d+)\)/);
if (match) {
return {
function: match[1],
file: match[2],
line: parseInt(match[3]),
column: parseInt(match[4]),
};
}
return line.trim();
});
stack && (msg += ` (stack: ${JSON.stringify(stackTrace, null, 2)})`);
}
return msg;
const consoleFormat = format.printf(({ level, message, service, method, details, timestamp, stack }) => {
if (message instanceof Object) {
message = JSON.stringify(message, null, 2);
}
);
if (details instanceof Object) {
details = JSON.stringify(details, null, 2);
}
let msg = `${timestamp} ${level}:`;
service && (msg += ` [${service}]`);
method && (msg += `(${method})`);
message && (msg += ` ${message}`);
details && (msg += ` (details: ${details})`);
if (typeof stack !== "undefined") {
const stackTrace = stack
?.split("\n")
.slice(1) // Remove first line (error message)
.map((line) => {
const match = line.match(/at\s+(.+?)\s+\((.+?):(\d+):(\d+)\)/);
if (match) {
return {
function: match[1],
file: match[2],
line: parseInt(match[3]),
column: parseInt(match[4]),
};
}
return line.trim();
});
stack && (msg += ` (stack: ${JSON.stringify(stackTrace, null, 2)})`);
}
return msg;
});
const logLevel = process.env.LOG_LEVEL || "info";
@@ -51,12 +49,7 @@ class Logger {
format: format.combine(format.timestamp()),
transports: [
new transports.Console({
format: format.combine(
format.colorize(),
format.prettyPrint(),
format.json(),
consoleFormat
),
format: format.combine(format.colorize(), format.prettyPrint(), format.json(), consoleFormat),
}),
new transports.File({
format: format.combine(format.json()),
+2 -8
View File
@@ -6,12 +6,7 @@
const ParseBoolean = (value) => {
if (value === true || value === "true") {
return true;
} else if (
value === false ||
value === "false" ||
value === null ||
value === undefined
) {
} else if (value === false || value === "false" || value === null || value === undefined) {
return false;
}
};
@@ -21,8 +16,7 @@ const getTokenFromHeaders = (headers) => {
if (!authorizationHeader) throw new Error("No auth headers");
const parts = authorizationHeader.split(" ");
if (parts.length !== 2 || parts[0] !== "Bearer")
throw new Error("Invalid auth headers");
if (parts.length !== 2 || parts[0] !== "Bearer") throw new Error("Invalid auth headers");
return parts[1];
};