Files
Checkmate/Server/utils/dataUtils.js
T

92 lines
2.9 KiB
JavaScript
Raw Normal View History

2024-07-17 12:33:24 -07:00
const calculatePercentile = (arr, percentile) => {
2024-10-17 13:09:50 +08:00
const sorted = arr.slice().sort((a, b) => a.responseTime - b.responseTime);
const index = (percentile / 100) * (sorted.length - 1);
const lower = Math.floor(index);
const upper = lower + 1;
const weight = index % 1;
if (upper >= sorted.length) return sorted[lower].responseTime;
return sorted[lower].responseTime * (1 - weight) + sorted[upper].responseTime * weight;
2024-07-17 12:33:24 -07:00
};
2024-12-31 13:42:49 -08:00
const calculatePercentileUptimeDetails = (arr, percentile) => {
const sorted = arr.slice().sort((a, b) => a.avgResponseTime - b.avgResponseTime);
const index = (percentile / 100) * (sorted.length - 1);
const lower = Math.floor(index);
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
);
};
2024-08-08 11:20:54 -07:00
const NormalizeData = (checks, rangeMin, rangeMax) => {
2024-10-17 13:09:50 +08:00
if (checks.length > 1) {
// Get the 5th and 95th percentile
const min = calculatePercentile(checks, 0);
const max = calculatePercentile(checks, 95);
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);
2024-07-17 12:33:24 -07:00
2024-10-17 13:09:50 +08:00
// Put a floor on the response times so we don't have extreme outliers
// Better visuals
normalizedResponseTime = Math.max(
rangeMin,
Math.min(rangeMax, normalizedResponseTime)
);
return {
2025-01-07 16:27:35 -08:00
...check,
2024-10-17 13:09:50 +08:00
responseTime: normalizedResponseTime,
originalResponseTime: originalResponseTime,
};
});
2024-07-17 12:33:24 -07:00
2024-10-17 13:09:50 +08:00
return normalizedChecks;
} else {
return checks.map((check) => {
2025-01-08 10:17:51 -08:00
return { ...check, originalResponseTime: check.responseTime };
2024-10-17 13:09:50 +08:00
});
}
2024-07-17 12:33:24 -07:00
};
2024-12-31 13:42:49 -08:00
const NormalizeDataUptimeDetails = (checks, rangeMin, rangeMax) => {
if (checks.length > 1) {
// Get the 5th and 95th percentile
const min = calculatePercentileUptimeDetails(checks, 0);
const max = calculatePercentileUptimeDetails(checks, 95);
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);
// Put a floor on the response times so we don't have extreme outliers
// Better visuals
normalizedResponseTime = Math.max(
rangeMin,
Math.min(rangeMax, normalizedResponseTime)
);
return {
...check,
avgResponseTime: normalizedResponseTime,
originalAvgResponseTime: originalResponseTime,
};
});
return normalizedChecks;
} else {
return checks.map((check) => {
2025-01-01 17:11:38 -08:00
return { ...check, originalResponseTime: check.responseTime };
2024-12-31 13:42:49 -08:00
});
}
};
2024-08-08 11:20:54 -07:00
2024-12-31 13:42:49 -08:00
export {
calculatePercentile,
NormalizeData,
calculatePercentileUptimeDetails,
NormalizeDataUptimeDetails,
};