Merge pull request #608 from bluewave-labs/feat/fe/refactor-pagespeed

Refactor pagespeed details to use stats endpoint
This commit is contained in:
Alexander Holliday authored and GitHub committed 2024-08-12 15:21:00 -07:00
commit 17d703ae45
3 files changed
+55 -59

No files matched your search

+1 -2
View File
@@ -5,7 +5,6 @@ import { useSelector } from "react-redux";
import { useNavigate, useParams } from "react-router-dom"; import { useNavigate, useParams } from "react-router-dom";
import axiosInstance from "../../../Utils/axiosConfig"; import axiosInstance from "../../../Utils/axiosConfig";
import MonitorDetailsAreaChart from "../../../Components/Charts/MonitorDetailsAreaChart"; import MonitorDetailsAreaChart from "../../../Components/Charts/MonitorDetailsAreaChart";
import { StatusLabel } from "../../../Components/Label";
import ButtonGroup from "@mui/material/ButtonGroup"; import ButtonGroup from "@mui/material/ButtonGroup";
import Button from "../../../Components/Button"; import Button from "../../../Components/Button";
import WestRoundedIcon from "@mui/icons-material/WestRounded"; import WestRoundedIcon from "@mui/icons-material/WestRounded";
@@ -267,7 +266,7 @@ const DetailsPage = () => {
</ButtonGroup> </ButtonGroup>
</Stack> </Stack>
<Box sx={{ height: "200px" }}> <Box sx={{ height: "200px" }}>
<MonitorDetailsAreaChart checks={monitor.checks} /> <MonitorDetailsAreaChart checks={monitor.checks.reverse()} />
</Box> </Box>
</Box> </Box>
<Stack gap={theme.gap.ml}> <Stack gap={theme.gap.ml}>
+11 -14
View File
@@ -5,7 +5,11 @@ import { useEffect, useState } from "react";
import { useTheme } from "@emotion/react"; import { useTheme } from "@emotion/react";
import { useNavigate, useParams } from "react-router-dom"; import { useNavigate, useParams } from "react-router-dom";
import { useSelector } from "react-redux"; import { useSelector } from "react-redux";
import { formatDate, formatDurationRounded } from "../../../Utils/timeUtils"; import {
formatDate,
formatDuration,
formatDurationRounded,
} from "../../../Utils/timeUtils";
import { getLastChecked } from "../../../Utils/monitorUtils"; import { getLastChecked } from "../../../Utils/monitorUtils";
import axiosInstance from "../../../Utils/axiosConfig"; import axiosInstance from "../../../Utils/axiosConfig";
import Button from "../../../Components/Button"; import Button from "../../../Components/Button";
@@ -185,7 +189,7 @@ const PageSpeedDetails = () => {
const fetchMonitor = async () => { const fetchMonitor = async () => {
try { try {
const res = await axiosInstance.get( const res = await axiosInstance.get(
`/monitors/${monitorId}?sortOrder=desc`, `/monitors/stats/${monitorId}?sortOrder=desc&limit=50`,
{ {
headers: { headers: {
Authorization: `Bearer ${authToken}`, Authorization: `Bearer ${authToken}`,
@@ -369,14 +373,13 @@ const PageSpeedDetails = () => {
title="Last checked" title="Last checked"
value={ value={
<> <>
{formatDate(getLastChecked(monitor?.checks, false))}{" "} {formatDuration(monitor?.lastChecked)}{" "}
<Typography <Typography
component="span" component="span"
fontStyle="italic" fontStyle="italic"
sx={{ opacity: 0.8 }} sx={{ opacity: 0.8 }}
> >
({formatDurationRounded(getLastChecked(monitor?.checks))}{" "} ago
ago)
</Typography> </Typography>
</> </>
} }
@@ -386,17 +389,13 @@ const PageSpeedDetails = () => {
title="Checks since" title="Checks since"
value={ value={
<> <>
{formatDate(new Date(monitor?.createdAt))}{" "} {formatDuration(monitor?.uptimeDuration)}{" "}
<Typography <Typography
component="span" component="span"
fontStyle="italic" fontStyle="italic"
sx={{ opacity: 0.8 }} sx={{ opacity: 0.8 }}
> >
( ago
{formatDurationRounded(
new Date() - new Date(monitor?.createdAt)
)}{" "}
ago)
</Typography> </Typography>
</> </>
} }
@@ -409,9 +408,7 @@ const PageSpeedDetails = () => {
</Stack> </Stack>
<Typography component="h2">Score history</Typography> <Typography component="h2">Score history</Typography>
<Box height="300px"> <Box height="300px">
<PageSpeedLineChart <PageSpeedLineChart pageSpeedChecks={monitor?.checks?.reverse()} />
pageSpeedChecks={monitor?.checks?.slice(0, 25)}
/>
</Box> </Box>
<Typography component="h2">Performance report</Typography> <Typography component="h2">Performance report</Typography>
<Stack direction="row" alignItems="center" overflow="hidden"> <Stack direction="row" alignItems="center" overflow="hidden">
+43 -43
View File
@@ -140,6 +140,13 @@ const getStatusBarValues = (checks) => {
* @throws {Error} * @throws {Error}
*/ */
const getMonitorStatsById = async (req) => { const getMonitorStatsById = async (req) => {
const filterLookup = {
hour: new Date(new Date().getTime() - 60 * 60 * 1000),
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)),
};
try { try {
const { monitorId } = req.params; const { monitorId } = req.params;
let { status, limit, sortOrder, filter, numToDisplay, normalize } = let { status, limit, sortOrder, filter, numToDisplay, normalize } =
@@ -154,39 +161,45 @@ const getMonitorStatsById = async (req) => {
// Get monitor // Get monitor
const monitor = await Monitor.findById(monitorId); const monitor = await Monitor.findById(monitorId);
// Get all checks to calculate stats
const checksQuery = { monitorId: monitor._id };
let model = let model =
monitor.type === "http" || monitor.type === "ping" monitor.type === "http" || monitor.type === "ping"
? Check ? Check
: PageSpeedCheck; : PageSpeedCheck;
const filterLookup = { const monitorStats = {
hour: new Date(new Date().getTime() - 60 * 60 * 1000), ...monitor.toObject(),
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)),
}; };
// Get all checks to calculate stats
const checksQuery = { monitorId: monitor._id };
// Get all checks // Get all checks
const checksAll = await model.find(checksQuery).sort({ const checksAll = await model.find(checksQuery).sort({
createdAt: sortOrder, createdAt: sortOrder,
}); });
// Get checks in 24 hours
checksQuery.createdAt = { $gte: filterLookup.day }; if (monitor.type === "http" || monitor.type === "ping") {
const checks24Hours = await model.find(checksQuery).sort({ // Get checks in 24 hours
createdAt: sortOrder, checksQuery.createdAt = { $gte: filterLookup.day };
}); const checks24Hours = await model.find(checksQuery).sort({
// Get checks in 30 days createdAt: sortOrder,
checksQuery.createdAt = { $gte: filterLookup.month }; });
const checks30Days = await model.find(checksQuery).sort({ // Get checks in 30 days
createdAt: sortOrder, checksQuery.createdAt = { $gte: filterLookup.month };
}); const checks30Days = await model.find(checksQuery).sort({
// Get checks in 60 mins createdAt: sortOrder,
checksQuery.createdAt = { $gte: filterLookup.hour }; });
const checks60Mins = await model // Get checks in 60 mins
.find(checksQuery) checksQuery.createdAt = { $gte: filterLookup.hour };
.sort({ createdAt: sortOrder }); const checks60Mins = await model
.find(checksQuery)
.sort({ createdAt: sortOrder });
// HTTP/PING Specific stats
monitorStats.avgResponseTime24hours =
getAverageResponseTime24Hours(checks24Hours);
monitorStats.uptime24Hours = getUptimePercentage(checks24Hours);
monitorStats.uptime30Days = getUptimePercentage(checks30Days);
monitorStats.statusBar = getStatusBarValues(checks60Mins);
}
//Get checks for dateRange //Get checks for dateRange
@@ -205,7 +218,6 @@ const getMonitorStatsById = async (req) => {
createdAt: sortOrder, createdAt: sortOrder,
}) })
.limit(limit); .limit(limit);
const incidents = getIncidents(dateRangeChecks);
// If more than numToDisplay checks, pick every nth check // If more than numToDisplay checks, pick every nth check
if ( if (
@@ -222,25 +234,13 @@ const getMonitorStatsById = async (req) => {
dateRangeChecks = NormalizeData(dateRangeChecks, 1, 100); dateRangeChecks = NormalizeData(dateRangeChecks, 1, 100);
} }
const uptimeDuration = caluclteUptimeDuration(checksAll); // Add common stats
const lastChecked = getLastChecked(checksAll); const incidents = getIncidents(dateRangeChecks);
const latestResponseTime = getLatestResponseTime(checksAll); monitorStats.uptimeDuration = caluclteUptimeDuration(checksAll);
const avgResponseTime24hours = getAverageResponseTime24Hours(checks24Hours); monitorStats.lastChecked = getLastChecked(checksAll);
const uptime24Hours = getUptimePercentage(checks24Hours); monitorStats.latestResponseTime = getLatestResponseTime(checksAll);
const uptime30Days = getUptimePercentage(checks30Days); monitorStats.incidents = incidents;
const statusBar = getStatusBarValues(checks60Mins); monitorStats.checks = dateRangeChecks;
const monitorStats = {
...monitor.toObject(),
uptimeDuration,
lastChecked,
latestResponseTime,
avgResponseTime24hours,
uptime24Hours,
uptime30Days,
statusBar,
incidents,
dateRangeChecks,
};
return monitorStats; return monitorStats;
} catch (error) { } catch (error) {