Files
Checkmate/Client/src/Pages/Monitors/Details/index.jsx
T

280 lines
8.9 KiB
React
Raw Normal View History

import React, { useEffect, useState } from "react";
2024-07-17 11:29:25 -07:00
import PropTypes from "prop-types";
2024-07-18 19:50:08 -04:00
import { Box, Stack, Typography, useTheme } from "@mui/material";
2024-07-10 10:10:02 -07:00
import { useSelector } from "react-redux";
2024-07-18 19:50:08 -04:00
import { useNavigate, useParams } from "react-router-dom";
2024-07-17 20:15:45 -04:00
import axiosInstance from "../../../Utils/axiosConfig";
import BasicTable from "../../../Components/BasicTable";
import MonitorDetailsAreaChart from "../../../Components/Charts/MonitorDetailsAreaChart";
import { StatusLabel } from "../../../Components/Label";
2024-07-17 14:00:49 -07:00
import ButtonGroup from "@mui/material/ButtonGroup";
2024-07-17 21:54:56 -07:00
import Button from "../../../Components/Button";
2024-07-18 19:50:08 -04:00
import WestRoundedIcon from "@mui/icons-material/WestRounded";
import GreenCheck from "../../../assets/icons/checkbox-green.svg?react";
import RedCheck from "../../../assets/icons/checkbox-red.svg?react";
import SettingsIcon from "../../../assets/icons/settings.svg?react";
2024-07-30 01:59:58 -04:00
import {
formatDuration,
formatDurationRounded,
} from "../../../Utils/timeUtils";
2024-07-18 19:50:08 -04:00
import "./index.css";
2024-07-17 11:29:25 -07:00
const StatBox = ({ title, value }) => {
return (
2024-07-18 19:50:08 -04:00
<Box className="stat-box">
<Typography variant="h6" sx={{ fontWeight: 400 }}>
{title}
</Typography>
2024-07-17 11:29:25 -07:00
<Typography variant="h4">{value}</Typography>
</Box>
);
};
2024-07-05 21:24:59 -07:00
/**
* Details page component displaying monitor details and related information.
* @component
*/
const DetailsPage = () => {
const [monitor, setMonitor] = useState({});
2024-07-16 14:05:38 -07:00
const [data, setData] = useState({});
2024-07-10 10:10:02 -07:00
const { monitorId } = useParams();
const { authToken } = useSelector((state) => state.auth);
const [filter, setFilter] = useState("day");
useEffect(() => {
const fetchMonitor = async () => {
2024-07-26 18:56:39 -07:00
const res = await axiosInstance.get(
`/monitors/${monitorId}?sortOrder=asc`,
{
headers: {
Authorization: `Bearer ${authToken}`,
},
}
);
setMonitor(res.data.data);
2024-07-16 14:05:38 -07:00
const data = {
cols: [
{ id: 1, name: "Status" },
{ id: 2, name: "Date & Time" },
{ id: 3, name: "Message" },
],
rows: res.data.data.checks.map((check, idx) => {
2024-07-30 01:59:58 -04:00
const status = check.status === true ? "up" : "down";
2024-07-17 11:29:25 -07:00
2024-07-16 14:05:38 -07:00
return {
id: check._id,
data: [
2024-07-17 17:44:29 -04:00
{
id: idx,
data: (
<StatusLabel
status={status}
text={status}
customStyles={{ textTransform: "capitalize" }}
/>
),
2024-07-17 17:44:29 -04:00
},
2024-07-16 14:05:38 -07:00
{ id: idx + 1, data: new Date(check.createdAt).toLocaleString() },
{ id: idx + 2, data: check.statusCode },
],
};
}),
};
setData(data);
};
fetchMonitor();
}, [monitorId, authToken]);
2024-07-10 10:10:02 -07:00
const theme = useTheme();
2024-07-18 19:50:08 -04:00
const navigate = useNavigate();
2024-07-05 21:24:59 -07:00
/**
* Function to calculate uptime duration based on the most recent check.
* @param {Array} checks Array of check objects.
2024-07-15 22:28:03 -07:00
* @returns {number} Uptime duration in ms.
2024-07-05 21:24:59 -07:00
*/
2024-07-17 11:29:25 -07:00
// TODO: This can be done more efficiently by iteratting backwards
// and breaking when the first down check is found, calculate current time - downtime
const calculateUptimeDuration = (checks) => {
if (!checks || checks.length === 0) {
2024-07-15 22:28:03 -07:00
return 0;
}
2024-07-21 20:07:18 -07:00
const latestCheck = new Date(checks[checks.length - 1].createdAt);
let latestDownCheck = 0;
2024-07-21 20:07:18 -07:00
// Checks are ordered oldest -> newest
// So we iterate backwards and find the first down check
for (let i = checks.length - 1; i >= 0; i--) {
if (checks[i].status === false) {
latestDownCheck = new Date(checks[i].createdAt);
break;
2024-07-15 22:28:03 -07:00
}
2024-07-17 11:29:25 -07:00
}
2024-07-21 20:07:18 -07:00
// If no down check is found, uptime is from the first check to now
if (latestDownCheck === 0) {
2024-07-21 20:07:18 -07:00
return Date.now() - new Date(checks[0].createdAt);
}
2024-07-21 20:07:18 -07:00
// Otherwise the uptime is from the last check to the last down check
return latestCheck - latestDownCheck;
};
2024-07-05 21:24:59 -07:00
/**
2024-07-17 11:29:25 -07:00
* Helper function to get duration since last check
2024-07-05 21:24:59 -07:00
* @param {Array} checks Array of check objects.
2024-07-16 10:07:04 -07:00
* @returns {number} Timestamp of the most recent check.
2024-07-05 21:24:59 -07:00
*/
2024-07-17 11:29:25 -07:00
const getLastChecked = (checks) => {
if (!checks || checks.length === 0) {
2024-07-16 10:07:04 -07:00
return 0; // Handle case when no checks are available
}
2024-07-21 20:07:18 -07:00
// Data is sorted oldest -> newest, so last check is the most recent
return new Date() - new Date(checks[checks.length - 1].createdAt);
};
2024-07-05 21:24:59 -07:00
/**
* Helper function to count incidents (checks with status === false).
* @param {Array} checks Array of check objects.
* @returns {number} Number of incidents.
*/
const countIncidents = (checks) => {
if (!checks || checks.length === 0) {
2024-07-05 21:24:59 -07:00
return 0; // Handle case when no checks are available
}
2024-07-16 10:07:04 -07:00
return checks.reduce((acc, check) => {
2024-07-17 11:29:25 -07:00
return check.status === false ? (acc += 1) : acc;
2024-07-16 10:07:04 -07:00
}, 0);
};
2024-07-16 14:39:19 -07:00
return (
2024-07-26 12:39:18 -04:00
<Box className="monitor-details">
2024-07-18 19:50:08 -04:00
<Button
level="tertiary"
label="Back to Monitors"
2024-07-30 19:36:37 -04:00
animate="slideLeft"
2024-07-18 19:50:08 -04:00
img={<WestRoundedIcon />}
onClick={() => navigate("/monitors")}
sx={{
backgroundColor: "#f4f4f4",
mb: theme.gap.medium,
px: theme.gap.ml,
"& svg.MuiSvgIcon-root": {
2024-07-30 19:36:37 -04:00
mr: theme.gap.small,
fill: theme.palette.otherColors.slateGray,
2024-07-18 19:50:08 -04:00
},
2024-07-17 14:00:49 -07:00
}}
2024-07-18 19:50:08 -04:00
/>
<Stack gap={theme.gap.xl}>
<Stack direction="row" gap={theme.gap.small} mt={theme.gap.small}>
2024-07-18 20:05:28 -04:00
{monitor?.status ? <GreenCheck /> : <RedCheck />}
2024-07-18 19:50:08 -04:00
<Box>
<Typography component="h1" sx={{ lineHeight: 1 }}>
{monitor.url?.replace(/^https?:\/\//, "") || "..."}
</Typography>
<Typography mt={theme.gap.small}>
2024-07-18 20:05:28 -04:00
<Typography
component="span"
sx={{
color: monitor?.status
? "var(--env-var-color-17)"
: "var(--env-var-color-24)",
}}
>
Your site is {monitor?.status ? "up" : "down"}.
</Typography>{" "}
Checking every {formatDurationRounded(monitor?.interval)}. Last
time checked{" "}
{formatDurationRounded(getLastChecked(monitor?.checks))} ago.
2024-07-18 19:50:08 -04:00
</Typography>
</Box>
<Button
2024-07-18 19:50:08 -04:00
level="tertiary"
label="Configure"
2024-07-30 19:36:37 -04:00
animate="rotate90"
2024-07-18 19:50:08 -04:00
img={<SettingsIcon />}
2024-07-19 14:46:26 -04:00
onClick={() => navigate(`/monitors/configure/${monitorId}`)}
2024-07-18 19:50:08 -04:00
sx={{
ml: "auto",
alignSelf: "flex-end",
backgroundColor: "#f4f4f4",
2024-07-30 19:36:37 -04:00
px: theme.gap.medium,
2024-07-18 19:50:08 -04:00
"& svg": {
2024-07-30 19:36:37 -04:00
mr: "6px",
2024-07-18 19:50:08 -04:00
},
}}
/>
2024-07-18 19:50:08 -04:00
</Stack>
<Stack direction="row" justifyContent="space-between">
<StatBox
title="Currently up for"
value={formatDuration(calculateUptimeDuration(monitor.checks))}
/>
2024-07-18 19:50:08 -04:00
<StatBox
title="Last checked"
value={`${formatDuration(getLastChecked(monitor.checks))} ago`}
/>
2024-07-18 19:50:08 -04:00
<StatBox title="Incidents" value={countIncidents(monitor.checks)} />
</Stack>
<Box>
<Stack
direction="row"
justifyContent="space-between"
mb={theme.gap.ml}
>
<Typography component="h2" mb={theme.gap.small}>
Response Times
</Typography>
<ButtonGroup>
<Button
level="secondary"
label="Day"
onClick={() => setFilter("day")}
2024-07-30 19:36:37 -04:00
sx={{
backgroundColor:
filter === "day" && theme.palette.otherColors.fillGray,
}}
2024-07-18 19:50:08 -04:00
/>
<Button
level="secondary"
label="Week"
onClick={() => setFilter("week")}
2024-07-30 19:36:37 -04:00
sx={{
backgroundColor:
filter === "week" && theme.palette.otherColors.fillGray,
}}
2024-07-18 19:50:08 -04:00
/>
<Button
level="secondary"
label="Month"
onClick={() => setFilter("month")}
2024-07-30 19:36:37 -04:00
sx={{
backgroundColor:
filter === "month" && theme.palette.otherColors.fillGray,
}}
2024-07-18 19:50:08 -04:00
/>
</ButtonGroup>
</Stack>
<Box sx={{ height: "200px" }}>
<MonitorDetailsAreaChart checks={monitor.checks} filter={filter} />
</Box>
</Box>
<Box>
<Typography component="h2" mb={theme.gap.ml}>
History
</Typography>
<BasicTable data={data} paginated={true} reversed={true} />
2024-07-18 19:50:08 -04:00
</Box>
</Stack>
2024-07-26 12:39:18 -04:00
</Box>
2024-07-16 14:39:19 -07:00
);
2024-07-05 19:51:07 -07:00
};
2024-07-17 11:29:25 -07:00
StatBox.propTypes = {
title: PropTypes.string,
value: PropTypes.oneOfType([PropTypes.string, PropTypes.number]),
};
export default DetailsPage;