Merge pull request #568 from bluewave-labs/feat/monitor-details-performance

Prep monitor details page for #567
This commit is contained in:
Alexander Holliday authored and Alex Holliday committed 2024-08-08 11:03:06 -07:00
commit 7928bd7822
5 files changed
+102 -84

No files matched your search

+5 -1
View File
@@ -9,7 +9,7 @@ import {
Pagination,
PaginationItem,
} from "@mui/material";
import React, { useState } from "react";
import React, { useState, useEffect } from "react";
import PropTypes from "prop-types";
import "./index.css";
import ArrowBackRoundedIcon from "@mui/icons-material/ArrowBackRounded";
@@ -73,6 +73,10 @@ const BasicTable = ({ data, paginated, reversed, rowsPerPage = 5 }) => {
const [page, setPage] = useState(0);
useEffect(() => {
setPage(0);
}, [data]);
const handleChangePage = (event, newPage) => {
setPage(newPage);
};
@@ -47,37 +47,9 @@ const MonitorDetailsAreaChart = ({ checks, filter }) => {
});
};
const filterChecks = (checks, filter, numToDisplay) => {
const limits = {
day: 24 * 60 * 60 * 1000,
week: 24 * 60 * 60 * 1000 * 7,
month: 24 * 60 * 60 * 1000 * 30, //TODO better monthly calculations
};
const now = new Date().getTime();
let result = [];
for (let i = 0; i < checks.length; i++) {
const checkTime = new Date(checks[i].createdAt).getTime();
if (now - checkTime < limits[filter]) {
result.push(checks[i]);
}
}
// If more than numToDisplay checks, pick every nth check
if (result.length > numToDisplay) {
const n = Math.ceil(result.length / numToDisplay);
result = result.filter((_, idx) => {
return idx % n === 0;
});
}
return result;
};
let normalizedChecks = [];
if (checks && checks.length > 0) {
const filteredChecks = filterChecks(checks, filter, 75);
normalizedChecks = NormalizeData(filteredChecks, 10, 100);
normalizedChecks = NormalizeData(checks, 10, 100);
}
return (
+74 -52
View File
@@ -1,4 +1,4 @@
import React, { useEffect, useState } from "react";
import React, { useEffect, useState, useCallback } from "react";
import PropTypes from "prop-types";
import { Box, Skeleton, Stack, Typography, useTheme } from "@mui/material";
import { useSelector } from "react-redux";
@@ -115,62 +115,84 @@ const DetailsPage = () => {
const { monitorId } = useParams();
const { authToken } = useSelector((state) => state.auth);
const [filter, setFilter] = useState("day");
const navigate = useNavigate();
const fetchDataForTable = useCallback(async () => {
try {
const limit = 0;
const res = await axiosInstance.get(
`/monitors/${monitorId}?sortOrder=asc&limit=${limit}`,
{
headers: {
Authorization: `Bearer ${authToken}`,
},
}
);
const data = {
cols: [
{ id: 1, name: "Status" },
{ id: 2, name: "Date & Time" },
{ id: 3, name: "Message" },
],
rows: res.data.data.checks.map((check, idx) => {
const status = check.status === true ? "up" : "down";
return {
id: check._id,
data: [
{
id: idx,
data: (
<StatusLabel
status={status}
text={status}
customStyles={{ textTransform: "capitalize" }}
/>
),
},
{
id: idx + 1,
data: new Date(check.createdAt).toLocaleString(),
},
{ id: idx + 2, data: check.statusCode },
],
};
}),
};
setData(data);
} catch (error) {
console.log(error);
}
}, [authToken, monitorId]);
const fetchMonitor = useCallback(async () => {
try {
const res = await axiosInstance.get(
`/monitors/${monitorId}?sortOrder=asc&filter=${filter}&numToDisplay=50`,
{
headers: {
Authorization: `Bearer ${authToken}`,
},
}
);
setMonitor(res.data.data);
} catch (error) {
console.error("Error fetching monitor of id: " + monitorId);
navigate("/not-found");
}
}, [authToken, monitorId, navigate, filter]);
useEffect(() => {
const fetchMonitor = async () => {
try {
const res = await axiosInstance.get(
`/monitors/${monitorId}?sortOrder=asc`,
{
headers: {
Authorization: `Bearer ${authToken}`,
},
}
);
setMonitor(res.data.data);
const data = {
cols: [
{ id: 1, name: "Status" },
{ id: 2, name: "Date & Time" },
{ id: 3, name: "Message" },
],
rows: res.data.data.checks.map((check, idx) => {
const status = check.status === true ? "up" : "down";
return {
id: check._id,
data: [
{
id: idx,
data: (
<StatusLabel
status={status}
text={status}
customStyles={{ textTransform: "capitalize" }}
/>
),
},
{
id: idx + 1,
data: new Date(check.createdAt).toLocaleString(),
},
{ id: idx + 2, data: check.statusCode },
],
};
}),
};
setData(data);
} catch (error) {
console.error("Error fetching monitor of id: " + monitorId);
navigate("/not-found");
}
};
fetchMonitor();
}, [monitorId, authToken]);
}, [fetchMonitor]);
useEffect(() => {
fetchDataForTable();
}, [fetchDataForTable]);
const theme = useTheme();
const navigate = useNavigate();
/**
* Function to calculate uptime duration based on the most recent check.
+20 -2
View File
@@ -32,7 +32,13 @@ const getAllMonitors = async (req, res) => {
const getMonitorById = async (req, res) => {
try {
const { monitorId } = req.params;
let { status, limit, sortOrder } = req.query;
let { status, limit, sortOrder, filter, numToDisplay } = req.query;
const filterLookup = {
day: new Date(new Date().setDate(new Date().getDate() - 1)),
week: new Date(new Date().setDate(new Date().getDate() - 7)),
month: new Date(new Date().setMonth(new Date().getMonth() - 1)),
};
// This effectively removes limit, returning all checks
if (limit === undefined) limit = 0;
@@ -52,18 +58,30 @@ const getMonitorById = async (req, res) => {
checksQuery.status = status;
}
// Filter checks by "day", "week", or "month"
if (filter !== undefined) {
checksQuery.createdAt = { $gte: filterLookup[filter] };
}
// Determine model type
let model =
monitor.type === "http" || monitor.type === "ping"
? Check
: PageSpeedCheck;
const checks = await model
let checks = await model
.find(checksQuery)
.sort({
createdAt: sortOrder,
})
.limit(limit);
// If more than numToDisplay checks, pick every nth check
if (numToDisplay !== undefined && checks && checks.length > numToDisplay) {
const n = Math.ceil(checks.length / numToDisplay);
checks = checks.filter((_, index) => index % n === 0);
}
const notifications = await Notification.find({ monitorId: monitor._id });
const monitorWithChecks = { ...monitor.toObject(), checks, notifications };
return monitorWithChecks;
+2
View File
@@ -126,6 +126,8 @@ const getMonitorByIdQueryValidation = joi.object({
status: joi.boolean(),
sortOrder: joi.string().valid("asc", "desc"),
limit: joi.number(),
filter: joi.string().valid("day", "week", "month"),
numToDisplay: joi.number(),
});
const getMonitorsByUserIdValidation = joi.object({