Moved normalization to BE

This commit is contained in:
Alex Holliday committed 2024-08-08 11:20:54 -07:00
1 parent 7928bd7822
commit 51058b6d2b
8 files changed
+28 -18

No files matched your search

+15 -3
View File
@@ -3,6 +3,7 @@ const Check = require("../../../models/Check");
const PageSpeedCheck = require("../../../models/PageSpeedCheck");
const { errorMessages } = require("../../../utils/messages");
const Notification = require("../../../models/Notification");
const { NormalizeData } = require("../../../utils/dataUtils");
/**
* Get all monitors
@@ -32,7 +33,8 @@ const getAllMonitors = async (req, res) => {
const getMonitorById = async (req, res) => {
try {
const { monitorId } = req.params;
let { status, limit, sortOrder, filter, numToDisplay } = req.query;
let { status, limit, sortOrder, filter, numToDisplay, normalize } =
req.query;
const filterLookup = {
day: new Date(new Date().setDate(new Date().getDate() - 1)),
@@ -82,6 +84,11 @@ const getMonitorById = async (req, res) => {
checks = checks.filter((_, index) => index % n === 0);
}
// Normalize checks if requested
if (normalize) {
checks = NormalizeData(checks, 10, 100);
}
const notifications = await Notification.find({ monitorId: monitor._id });
const monitorWithChecks = { ...monitor.toObject(), checks, notifications };
return monitorWithChecks;
@@ -100,7 +107,7 @@ const getMonitorById = async (req, res) => {
*/
const getMonitorsByUserId = async (req, res) => {
try {
let { limit, type, status, sortOrder } = req.query;
let { limit, type, status, sortOrder, normalize } = req.query;
const monitorQuery = { userId: req.params.userId };
if (type !== undefined) {
@@ -133,13 +140,18 @@ const getMonitorsByUserId = async (req, res) => {
: PageSpeedCheck;
// Checks are order newest -> oldest
const checks = await model
let checks = await model
.find(checksQuery)
.sort({
createdAt: sortOrder,
})
.limit(limit);
//Normalize checks if requested
if (normalize === true) {
checks = NormalizeData(checks, 10, 100);
}
// Get notifications
const notifications = await Notification.find({
monitorId: monitor._id,
+50
View File
@@ -0,0 +1,50 @@
const calculatePercentile = (arr, percentile) => {
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
);
};
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
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)
);
return {
...check,
responseTime: normalizedResponseTime,
originalResponseTime: originalResponseTime,
};
});
return normalizedChecks;
} else {
return checks.map((check) => {
return { ...check, originalResponseTime: check.responseTime };
});
}
};
module.exports = {
NormalizeData,
};
+2
View File
@@ -128,6 +128,7 @@ const getMonitorByIdQueryValidation = joi.object({
limit: joi.number(),
filter: joi.string().valid("day", "week", "month"),
numToDisplay: joi.number(),
normalize: joi.boolean(),
});
const getMonitorsByUserIdValidation = joi.object({
@@ -138,6 +139,7 @@ const getMonitorsByUserIdQueryValidation = joi.object({
status: joi.boolean(),
sortOrder: joi.string().valid("asc", "desc"),
limit: joi.number(),
normalize: joi.boolean(),
type: joi
.alternatives()
.try(