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

504 lines
16 KiB
React
Raw Normal View History

2024-07-17 11:29:25 -07:00
import PropTypes from "prop-types";
2024-08-28 14:42:38 -04:00
import { useEffect, useState, useCallback } from "react";
2024-08-31 14:53:15 -04:00
import {
Box,
Button,
2024-09-02 17:10:42 -04:00
Popover,
2024-08-31 14:53:15 -04:00
Skeleton,
Stack,
2024-09-02 17:10:42 -04:00
Tooltip,
2024-08-31 14:53:15 -04:00
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 17:42:49 -07:00
import { networkService } from "../../../main";
2024-08-22 12:23:52 -04:00
import { logger } from "../../../Utils/Logger";
import {
2024-09-02 17:10:42 -04:00
formatDate,
2024-08-22 12:23:52 -04:00
formatDuration,
formatDurationRounded,
formatDurationSplit,
2024-08-22 12:23:52 -04:00
} from "../../../Utils/timeUtils";
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";
import SettingsIcon from "../../../assets/icons/settings-bold.svg?react";
2024-09-02 17:10:42 -04:00
import CertificateIcon from "../../../assets/icons/certificate.svg?react";
2024-09-02 18:46:51 -04:00
import UptimeIcon from "../../../assets/icons/uptime-icon.svg?react";
import ResponseTimeIcon from "../../../assets/icons/response-time-icon.svg?react";
import AverageResponseIcon from "../../../assets/icons/average-response-icon.svg?react";
import IncidentsIcon from "../../../assets/icons/incidents.svg?react";
2024-09-02 19:31:20 -04:00
import HistoryIcon from "../../../assets/icons/history-icon.svg?react";
2024-08-08 13:19:37 -07:00
import PaginationTable from "./PaginationTable";
2024-08-17 17:26:48 -04:00
import Breadcrumbs from "../../../Components/Breadcrumbs";
2024-08-22 12:23:52 -04:00
import PulseDot from "../../../Components/Animated/PulseDot";
import { StatBox, ChartBox, IconBox } from "./styled";
2024-08-22 12:23:52 -04:00
import "./index.css";
2024-08-07 16:45:49 -04:00
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} />
2024-08-28 14:42:38 -04:00
<Stack gap={theme.spacing(20)} mt={theme.spacing(6)}>
<Stack direction="row" gap={theme.spacing(4)} mt={theme.spacing(4)}>
2024-08-07 19:49:08 -04:00
<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}
2024-08-28 14:42:38 -04:00
sx={{ mt: theme.spacing(4) }}
2024-08-07 19:49:08 -04:00
/>
</Box>
<Skeleton
variant="rounded"
width="20%"
height={34}
sx={{ alignSelf: "flex-end" }}
/>
</Stack>
<Stack
direction="row"
justifyContent="space-between"
2024-08-28 14:42:38 -04:00
gap={theme.spacing(12)}
2024-08-07 19:49:08 -04:00
>
<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"
2024-08-28 14:42:38 -04:00
mb={theme.spacing(8)}
2024-08-07 19:49:08 -04:00
>
<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>
2024-08-28 14:42:38 -04:00
<Stack gap={theme.spacing(8)}>
2024-08-07 19:49:08 -04:00
<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 = ({ isAdmin }) => {
const theme = useTheme();
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();
2024-09-02 17:10:42 -04:00
const [anchorEl, setAnchorEl] = useState(null);
const openCertificate = (event) => {
setAnchorEl(event.currentTarget);
};
const closeCertificate = () => {
setAnchorEl(null);
};
const fetchMonitor = useCallback(async () => {
try {
2024-08-18 17:42:49 -07:00
const res = await networkService.getStatsByMonitorId(
2024-08-18 17:18:16 -07:00
authToken,
monitorId,
null,
null,
dateRange,
50,
true
);
2024-08-19 13:33:50 -07:00
setMonitor(res?.data?.data ?? {});
} catch (error) {
logger.error(error);
navigate("/not-found", { replace: true });
}
}, [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 () => {
if (monitor?.type !== "http") {
return;
}
2024-08-19 13:33:50 -07:00
try {
const res = await networkService.getCertificateExpiry(
authToken,
monitorId
);
2024-09-02 17:10:42 -04:00
let [month, day, year] = res?.data?.data?.certificateDate.split("/");
const date = new Date(year, month - 1, day);
setCertificateExpiry(
formatDate(date, {
hour: undefined,
minute: undefined,
}) ?? "N/A"
);
2024-08-19 13:33:50 -07:00
} catch (error) {
console.error(error);
}
2024-08-12 15:15:13 -07:00
};
fetchCertificate();
}, [authToken, monitorId, monitor]);
const splitDuration = (duration) => {
const { time, format } = formatDurationSplit(duration);
return (
<>
{time}
<Typography component="span">{format}</Typography>
</>
);
};
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
<>
2024-08-17 17:26:48 -04:00
<Breadcrumbs
list={[
{ name: "monitors", path: "/monitors" },
{ name: "details", path: `/monitors/${monitorId}` },
]}
2024-08-07 19:49:08 -04:00
/>
<Stack gap={theme.spacing(10)} mt={theme.spacing(10)}>
2024-08-28 14:42:38 -04:00
<Stack direction="row" gap={theme.spacing(2)}>
2024-08-07 19:49:08 -04:00
<Box>
2024-08-28 14:42:38 -04:00
<Typography
component="h1"
2024-09-02 17:10:42 -04:00
fontSize={22}
fontWeight={500}
2024-08-28 14:42:38 -04:00
color={theme.palette.text.primary}
>
2024-09-02 17:10:42 -04:00
{monitor.name}
2024-08-17 17:26:48 -04:00
</Typography>
2024-09-02 17:10:42 -04:00
<Stack
direction="row"
alignItems="flex-end"
gap={theme.spacing(2)}
2024-08-28 14:42:38 -04:00
>
2024-09-02 17:10:42 -04:00
<Tooltip
title={`Your site is ${monitor?.status ? "up" : "down"}.`}
disableInteractive
slotProps={{
popper: {
modifiers: [
{
name: "offset",
options: {
offset: [0, -8],
},
},
],
},
}}
>
<Box>
<PulseDot
color={
monitor?.status
? theme.palette.success.main
: theme.palette.error.main
}
/>
</Box>
</Tooltip>
2024-08-07 16:45:49 -04:00
<Typography
2024-09-02 17:10:42 -04:00
component="h2"
color={theme.palette.text.secondary}
>
{monitor.url?.replace(/^https?:\/\//, "") || "..."}
</Typography>
<Typography
ml={theme.spacing(6)}
lineHeight="20px"
2024-09-02 19:31:20 -04:00
fontSize={12}
2024-09-02 17:10:42 -04:00
position="relative"
color={theme.palette.text.tertiary}
2024-08-07 16:45:49 -04:00
sx={{
2024-09-02 17:10:42 -04:00
"&:before": {
position: "absolute",
content: `""`,
width: 4,
height: 4,
borderRadius: "50%",
backgroundColor: theme.palette.text.tertiary,
opacity: 0.8,
left: -9,
top: "42%",
},
2024-08-07 16:45:49 -04:00
}}
>
2024-09-02 17:10:42 -04:00
Checking every {formatDurationRounded(monitor?.interval)}.
</Typography>
</Stack>
2024-08-07 19:49:08 -04:00
</Box>
2024-09-02 17:10:42 -04:00
<Stack
direction="row"
height={34}
sx={{
ml: "auto",
alignSelf: "flex-end",
}}
>
2024-09-02 18:46:51 -04:00
<IconBox
2024-09-02 17:10:42 -04:00
mr={theme.spacing(4)}
onClick={openCertificate}
sx={{
2024-09-02 17:10:42 -04:00
cursor: "pointer",
}}
2024-08-31 14:53:15 -04:00
>
2024-09-02 17:10:42 -04:00
<CertificateIcon />
2024-09-02 18:46:51 -04:00
</IconBox>
2024-09-02 17:10:42 -04:00
<Popover
id="certificate-dropdown"
anchorEl={anchorEl}
open={Boolean(anchorEl)}
onClose={closeCertificate}
disableScrollLock
marginThreshold={null}
anchorReference={anchorEl}
2024-09-02 17:10:42 -04:00
anchorOrigin={{
vertical: "bottom",
horizontal: "center",
}}
transformOrigin={{
vertical: "top",
horizontal: "center",
}}
slotProps={{
paper: {
sx: {
2024-09-02 19:31:20 -04:00
mt: theme.spacing(4),
py: theme.spacing(2),
px: theme.spacing(4),
2024-09-02 17:10:42 -04:00
width: 140,
backgroundColor: theme.palette.background.accent,
},
},
}}
>
<Typography fontSize={12} color={theme.palette.text.tertiary}>
Certificate Expiry
</Typography>
<Typography
component="span"
fontSize={13}
color={theme.palette.text.primary}
>
{certificateExpiry}
</Typography>
</Popover>
{isAdmin && (
<Button
variant="contained"
color="secondary"
onClick={() => navigate(`/monitors/configure/${monitorId}`)}
sx={{
px: theme.spacing(5),
"& svg": {
mr: theme.spacing(3),
"& path": {
stroke: theme.palette.text.tertiary,
},
},
}}
>
<SettingsIcon /> Configure
</Button>
)}
</Stack>
2024-08-07 19:49:08 -04:00
</Stack>
<Stack direction="row" gap={theme.spacing(8)} maxWidth={712}>
<StatBox>
2024-09-02 19:31:20 -04:00
<Typography component="h2">up for</Typography>
<Typography>
{splitDuration(monitor?.uptimeDuration)}
2024-09-02 19:31:20 -04:00
</Typography>
</StatBox>
<StatBox>
2024-09-02 19:31:20 -04:00
<Typography component="h2">last check</Typography>
<Typography>
{splitDuration(monitor?.lastChecked)}
2024-09-02 19:31:20 -04:00
<Typography component="span">ago</Typography>
</Typography>
</StatBox>
<StatBox>
2024-09-02 19:31:20 -04:00
<Typography component="h2">last response time</Typography>
<Typography>
{monitor?.latestResponseTime}
<Typography component="span">ms</Typography>
</Typography>
</StatBox>
2024-09-02 19:31:20 -04:00
</Stack>
2024-09-02 18:46:51 -04:00
<Box>
<Stack
direction="row"
2024-09-02 19:31:20 -04:00
justifyContent="space-between"
alignItems="flex-end"
2024-09-02 18:46:51 -04:00
gap={theme.spacing(4)}
2024-09-02 19:31:20 -04:00
mb={theme.spacing(8)}
2024-09-02 18:46:51 -04:00
>
2024-09-02 19:31:20 -04:00
<Typography fontSize={12} color={theme.palette.text.tertiary}>
Showing statistics for past{" "}
{dateRange === "day"
? "24 hours"
: dateRange === "week"
? "7 days"
: "30 days"}
.
</Typography>
<ButtonGroup sx={{ height: 32 }}>
2024-09-02 18:46:51 -04:00
<Button
variant="group"
filled={(dateRange === "day").toString()}
onClick={() => setDateRange("day")}
>
Day
</Button>
<Button
variant="group"
filled={(dateRange === "week").toString()}
onClick={() => setDateRange("week")}
>
Week
</Button>
<Button
variant="group"
filled={(dateRange === "month").toString()}
onClick={() => setDateRange("month")}
>
Month
</Button>
</ButtonGroup>
</Stack>
<Stack direction="row" flexWrap="wrap" gap={theme.spacing(8)}>
<ChartBox>
<Stack>
<IconBox>
<UptimeIcon />
</IconBox>
<Typography component="h2">Uptime</Typography>
</Stack>
<Stack justifyContent="space-between" mt={theme.spacing(8)}>
<Box>
<Typography>Total Checks</Typography>
<Typography component="span">87</Typography>
</Box>
<Box>
<Typography>Uptime Percentage</Typography>
<Typography component="span">
98.3<Typography component="span">%</Typography>
</Typography>
</Box>
</Stack>
</ChartBox>
<ChartBox>
<Stack>
<IconBox>
<IncidentsIcon />
</IconBox>
<Typography component="h2">Incidents</Typography>
</Stack>
<Box>
<Typography>Total Incidents</Typography>
<Typography component="span">0</Typography>
</Box>
</ChartBox>
<ChartBox>
<Stack>
<IconBox>
<AverageResponseIcon />
</IconBox>
<Typography component="h2">
Average response time
</Typography>
</Stack>
</ChartBox>
<ChartBox>
<Stack>
<IconBox>
<ResponseTimeIcon />
</IconBox>
<Typography component="h2">Response Times</Typography>
</Stack>
2024-09-02 19:31:20 -04:00
<Box>
{/* <MonitorDetailsAreaChart
checks={[...monitor.checks].reverse()}
/> */}
</Box>
2024-09-02 18:46:51 -04:00
</ChartBox>
2024-09-02 19:31:20 -04:00
<ChartBox
gap={theme.spacing(8)}
sx={{
flex: "100%",
height: "fit-content",
"& nav": { mt: theme.spacing(12) },
}}
2024-08-28 14:42:38 -04:00
>
2024-09-02 19:31:20 -04:00
<Stack mb={theme.spacing(8)}>
<IconBox>
<HistoryIcon />
</IconBox>
<Typography
component="h2"
color={theme.palette.text.secondary}
>
History
</Typography>
</Stack>
<PaginationTable
monitorId={monitorId}
dateRange={dateRange}
/>
</ChartBox>
2024-08-07 19:49:08 -04:00
</Stack>
</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
};
DetailsPage.propTypes = {
isAdmin: PropTypes.bool,
};
export default DetailsPage;