From 17bd1f5286bd04d06380400941e11bac4a191aa1 Mon Sep 17 00:00:00 2001 From: Alex Holliday Date: Tue, 31 Dec 2024 13:42:49 -0800 Subject: [PATCH] Add temporary normalization functoins --- Server/utils/dataUtils.js | 52 +++++++++++++++++++++++++++++++++++++-- 1 file changed, 50 insertions(+), 2 deletions(-) diff --git a/Server/utils/dataUtils.js b/Server/utils/dataUtils.js index b0f8a2fd1..25f26d439 100644 --- a/Server/utils/dataUtils.js +++ b/Server/utils/dataUtils.js @@ -8,12 +8,23 @@ const calculatePercentile = (arr, percentile) => { return sorted[lower].responseTime * (1 - weight) + sorted[upper].responseTime * weight; }; +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 + ); +}; + const NormalizeData = (checks, rangeMin, rangeMax) => { 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 @@ -40,5 +51,42 @@ const NormalizeData = (checks, rangeMin, rangeMax) => { }); } }; +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); -export { calculatePercentile, NormalizeData }; + 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) => { + return { ...check._doc, originalResponseTime: check.responseTime }; + }); + } +}; + +export { + calculatePercentile, + NormalizeData, + calculatePercentileUptimeDetails, + NormalizeDataUptimeDetails, +};