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

@@ -1,6 +1,5 @@
import PropTypes from "prop-types";
import { AreaChart, Area, XAxis, Tooltip, ResponsiveContainer } from "recharts";
import { NormalizeData } from "../ChartUtils";
import "./index.css";
const CustomToolTip = ({ active, payload, label }) => {
@@ -47,17 +46,12 @@ const MonitorDetailsAreaChart = ({ checks, filter }) => {
});
};
let normalizedChecks = [];
if (checks && checks.length > 0) {
normalizedChecks = NormalizeData(checks, 10, 100);
}
return (
<ResponsiveContainer width="100%" height="100%">
<AreaChart
width={500}
height={400}
data={normalizedChecks}
data={checks}
margin={{
top: 10,
right: 0,
@@ -1,21 +1,19 @@
import "./index.css";
import PropTypes from "prop-types";
import { BarChart, Bar, ResponsiveContainer, Cell } from "recharts";
import { NormalizeData } from "../ChartUtils";
const ResponseTimeChart = ({ checks = [] }) => {
const normalizedChecks = NormalizeData(checks, 1, 100);
return (
<div className="chart-container">
<ResponsiveContainer width="100%" height="100%">
<BarChart
width={150}
height={40}
data={normalizedChecks}
data={checks}
style={{ cursor: "pointer" }}
>
<Bar maxBarSize={10} dataKey="responseTime">
{normalizedChecks.map((check, index) => (
{checks.map((check, index) => (
<Cell
key={`cell-${index}`}
fill={
@@ -51,7 +51,7 @@ export const getUptimeMonitorsByUserId = createAsyncThunk(
const user = jwtDecode(token);
try {
const res = await axiosInstance.get(
`/monitors/user/${user._id}?limit=25&type=http&type=ping&sortOrder=desc`,
`/monitors/user/${user._id}?limit=25&type=http&type=ping&sortOrder=desc&normalize=true`,
{
headers: {
Authorization: `Bearer ${token}`,
+1 -1
View File
@@ -170,7 +170,7 @@ const DetailsPage = () => {
const fetchMonitor = useCallback(async () => {
try {
const res = await axiosInstance.get(
`/monitors/${monitorId}?sortOrder=asc&filter=${filter}&numToDisplay=50`,
`/monitors/${monitorId}?sortOrder=asc&filter=${filter}&numToDisplay=50&normalize=true`,
{
headers: {
Authorization: `Bearer ${authToken}`,
+1 -1
View File
@@ -145,7 +145,7 @@ const Monitors = () => {
useEffect(() => {
dispatch(getUptimeMonitorsByUserId(authState.authToken));
}, []);
}, [authState.authToken, dispatch]);
const up = monitorState.monitors.reduce((acc, cur) => {
return cur.status === true ? acc + 1 : acc;
+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,
@@ -11,7 +11,7 @@ const calculatePercentile = (arr, percentile) => {
);
};
export const NormalizeData = (checks, rangeMin, rangeMax) => {
const NormalizeData = (checks, rangeMin, rangeMax) => {
if (checks.length > 1) {
// Get the 5th and 95th percentile
const min = calculatePercentile(checks, 0);
@@ -44,3 +44,7 @@ export const NormalizeData = (checks, rangeMin, rangeMax) => {
});
}
};
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(