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

358 lines
12 KiB
React
Raw Normal View History

import React, { useEffect, useState, useCallback } from "react";
2024-07-17 11:29:25 -07:00
import PropTypes from "prop-types";
2024-08-07 16:45:49 -04:00
import { Box, Skeleton, 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-08-18 12:11:24 -07:00
import axiosInstance from "../../../Utils/NetworkService";
2024-07-17 20:15:45 -04:00
import MonitorDetailsAreaChart from "../../../Components/Charts/MonitorDetailsAreaChart";
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-bold.svg?react";
2024-08-08 13:19:37 -07:00
import PaginationTable from "./PaginationTable";
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-08-13 12:36:19 -07:00
import MonitorDetails60MinChart from "../../../Components/Charts/MonitorDetails60MinChart";
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">
2024-08-14 15:53:46 -04:00
<Typography variant="h6" mb={1} fontWeight={500}>
{title}
</Typography>
2024-08-14 15:53:46 -04:00
<Typography variant="h4" fontWeight={500}>
{value}
</Typography>
2024-07-17 11:29:25 -07:00
</Box>
);
};
2024-08-07 16:45:49 -04:00
StatBox.propTypes = {
title: PropTypes.oneOfType([PropTypes.string, PropTypes.element]),
2024-08-07 16:45:49 -04:00
value: PropTypes.oneOfType([PropTypes.string, PropTypes.number]),
};
2024-08-07 19:49:08 -04:00
/**
* Renders a skeleton layout.
*
* @returns {JSX.Element}
*/
const SkeletonLayout = () => {
const theme = useTheme();
return (
<>
<Skeleton variant="rounded" width="20%" height={34} />
<Stack gap={theme.gap.xl} mt={theme.gap.medium}>
<Stack direction="row" gap={theme.gap.small} mt={theme.gap.small}>
<Skeleton
variant="circular"
style={{ minWidth: 24, minHeight: 24 }}
/>
<Box width="80%">
<Skeleton variant="rounded" width="50%" height={24} />
<Skeleton
variant="rounded"
width="50%"
height={18}
sx={{ mt: theme.gap.small }}
/>
</Box>
<Skeleton
variant="rounded"
width="20%"
height={34}
sx={{ alignSelf: "flex-end" }}
/>
</Stack>
<Stack
direction="row"
justifyContent="space-between"
gap={theme.gap.large}
>
<Skeleton variant="rounded" width="100%" height={80} />
<Skeleton variant="rounded" width="100%" height={80} />
<Skeleton variant="rounded" width="100%" height={80} />
</Stack>
<Box>
<Stack
direction="row"
justifyContent="space-between"
mb={theme.gap.ml}
>
<Skeleton
variant="rounded"
width="20%"
height={24}
sx={{ alignSelf: "flex-end" }}
/>
<Skeleton variant="rounded" width="20%" height={34} />
</Stack>
<Box sx={{ height: "200px" }}>
<Skeleton variant="rounded" width="100%" height="100%" />
</Box>
</Box>
<Stack gap={theme.gap.ml}>
<Skeleton variant="rounded" width="20%" height={24} />
<Skeleton variant="rounded" width="100%" height={200} />
<Skeleton variant="rounded" width="100%" height={50} />
</Stack>
</Stack>
</>
);
};
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-10 10:10:02 -07:00
const { monitorId } = useParams();
const { authToken } = useSelector((state) => state.auth);
const [dateRange, setDateRange] = useState("day");
2024-08-12 15:15:13 -07:00
const [certificateExpiry, setCertificateExpiry] = useState("N/A");
const navigate = useNavigate();
const fetchMonitor = useCallback(async () => {
try {
const res = await axiosInstance.get(
2024-08-13 10:32:29 -07:00
`/monitors/stats/${monitorId}?dateRange=${dateRange}&numToDisplay=50&normalize=true`,
{
headers: {
Authorization: `Bearer ${authToken}`,
},
}
);
setMonitor(res.data.data);
} catch (error) {
console.error("Error fetching monitor of id: " + monitorId);
navigate("/not-found");
}
}, [authToken, monitorId, navigate, dateRange]);
useEffect(() => {
fetchMonitor();
}, [fetchMonitor]);
2024-07-10 10:10:02 -07:00
2024-08-12 15:15:13 -07:00
useEffect(() => {
const fetchCertificate = async () => {
const res = await axiosInstance.get(
`/monitors/certificate/${monitorId}`,
{
headers: {
Authorization: `Bearer ${authToken}`,
},
}
);
setCertificateExpiry(res.data.data.certificateDate);
};
fetchCertificate();
}, [authToken, monitorId]);
2024-08-12 15:15:13 -07:00
const theme = useTheme();
2024-08-07 16:45:49 -04:00
let loading = Object.keys(monitor).length === 0;
2024-07-16 14:39:19 -07:00
return (
2024-07-26 12:39:18 -04:00
<Box className="monitor-details">
2024-08-07 16:45:49 -04:00
{loading ? (
2024-08-07 19:49:08 -04:00
<SkeletonLayout />
2024-08-07 16:45:49 -04:00
) : (
2024-08-07 19:49:08 -04:00
<>
<Button
level="tertiary"
2024-08-14 14:54:43 -04:00
label="Back"
2024-08-07 19:49:08 -04:00
animate="slideLeft"
img={<WestRoundedIcon />}
onClick={() => navigate("/monitors")}
sx={{
2024-08-14 14:54:43 -04:00
backgroundColor: theme.palette.otherColors.fillGray,
2024-08-07 19:49:08 -04:00
px: theme.gap.ml,
"& svg.MuiSvgIcon-root": {
mr: theme.gap.small,
fill: theme.palette.otherColors.slateGray,
},
}}
/>
2024-08-14 15:53:46 -04:00
<Stack gap={theme.gap.xl} mt={theme.gap.medium}>
2024-08-13 15:54:23 -07:00
<Stack
direction="row"
gap={theme.gap.small}
mt={theme.gap.small}
2024-08-14 14:54:43 -04:00
alignItems="baseline"
2024-08-13 15:54:23 -07:00
>
2024-08-07 19:49:08 -04:00
{monitor?.status ? <GreenCheck /> : <RedCheck />}
<Box>
2024-08-14 14:54:43 -04:00
<Stack direction="row">
<Typography
component="h1"
sx={{ lineHeight: 1, alignSelf: "flex-end" }}
>
{monitor.url?.replace(/^https?:\/\//, "") || "..."}
</Typography>
<MonitorDetails60MinChart data={monitor.statusBar} />
</Stack>
2024-08-07 16:45:49 -04:00
<Typography mt={theme.gap.small}>
<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)}.
</Typography>
2024-08-07 19:49:08 -04:00
</Box>
<Button
level="tertiary"
label="Configure"
animate="rotate90"
img={
<SettingsIcon
style={{
minWidth: theme.gap.mlplus,
minHeight: theme.gap.mlplus,
}}
/>
}
onClick={() => navigate(`/monitors/configure/${monitorId}`)}
sx={{
ml: "auto",
alignSelf: "flex-end",
backgroundColor: "#f4f4f4",
px: theme.gap.medium,
"& svg": {
mr: "6px",
},
}}
/>
</Stack>
<Stack
direction="row"
justifyContent="space-between"
gap={theme.gap.large}
2024-08-14 14:54:43 -04:00
flexWrap="wrap"
2024-08-07 19:49:08 -04:00
>
2024-08-07 16:45:49 -04:00
<StatBox
title="Currently up for"
2024-08-12 12:05:57 -07:00
value={formatDuration(monitor?.uptimeDuration)}
2024-08-07 16:45:49 -04:00
/>
<StatBox
2024-08-14 14:54:43 -04:00
title="Last check"
value={`${formatDurationRounded(monitor?.lastChecked)} ago`}
2024-08-07 16:45:49 -04:00
/>
2024-08-12 12:05:57 -07:00
<StatBox title="Incidents" value={monitor?.incidents} />
2024-08-12 15:15:13 -07:00
<StatBox title="Certificate Expiry" value={certificateExpiry} />
<StatBox
title="Latest response time"
value={monitor?.latestResponseTime}
/>
<StatBox
title={
<>
2024-08-14 15:53:46 -04:00
Avg. Response Time{" "}
<Typography component="span">(24-hr)</Typography>
</>
}
2024-08-12 15:15:13 -07:00
value={parseFloat(monitor?.avgResponseTime24hours)
.toFixed(2)
.replace(/\.?0+$/, "")}
/>
<StatBox
title={
<>
Uptime <Typography component="span">(24-hr)</Typography>
</>
}
2024-08-12 15:15:13 -07:00
value={`${parseFloat(monitor?.uptime24Hours)
.toFixed(2)
.replace(/\.?0+$/, "")}%`}
/>
<StatBox
title={
<>
Uptime <Typography component="span">(30-day)</Typography>
</>
}
2024-08-12 15:15:13 -07:00
value={`${parseFloat(monitor?.uptime30Days)
.toFixed(2)
.replace(/\.?0+$/, "")}%`}
/>
2024-08-07 19:49:08 -04:00
</Stack>
<Box>
<Stack
direction="row"
justifyContent="space-between"
mb={theme.gap.ml}
>
2024-08-14 14:54:43 -04:00
<Typography component="h2" alignSelf="flex-end">
2024-08-07 16:45:49 -04:00
Response Times
</Typography>
<ButtonGroup>
<Button
level="secondary"
label="Day"
onClick={() => setDateRange("day")}
2024-08-07 16:45:49 -04:00
sx={{
backgroundColor:
dateRange === "day" &&
theme.palette.otherColors.fillGray,
2024-08-07 16:45:49 -04:00
}}
/>
<Button
level="secondary"
label="Week"
onClick={() => setDateRange("week")}
2024-08-07 16:45:49 -04:00
sx={{
backgroundColor:
dateRange === "week" &&
theme.palette.otherColors.fillGray,
2024-08-07 16:45:49 -04:00
}}
/>
<Button
level="secondary"
label="Month"
onClick={() => setDateRange("month")}
2024-08-07 16:45:49 -04:00
sx={{
backgroundColor:
dateRange === "month" &&
2024-08-07 16:45:49 -04:00
theme.palette.otherColors.fillGray,
}}
/>
</ButtonGroup>
2024-08-07 19:49:08 -04:00
</Stack>
2024-08-14 14:54:43 -04:00
<Box
p={theme.gap.mlplus}
pb={theme.gap.xs}
backgroundColor={theme.palette.otherColors.white}
border={1}
borderColor={theme.palette.otherColors.graishWhite}
borderRadius={`${theme.shape.borderRadius}px`}
sx={{ height: "250px" }}
>
2024-08-12 15:15:13 -07:00
<MonitorDetailsAreaChart
checks={[...monitor.checks].reverse()}
/>
2024-08-07 19:49:08 -04:00
</Box>
</Box>
<Stack gap={theme.gap.ml}>
2024-08-07 16:45:49 -04:00
<Typography component="h2">History</Typography>
2024-08-08 13:19:37 -07:00
{/* TODO New Table */}
<PaginationTable monitorId={monitorId} dateRange={dateRange} />
2024-08-08 13:19:37 -07:00
{/* <BasicTable data={data} paginated={true} /> */}
2024-08-07 19:49:08 -04:00
</Stack>
</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
};
export default DetailsPage;