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

510 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,
} 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-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 "./index.css";
2024-07-17 11:29:25 -07:00
const StatBox = ({ title, value }) => {
2024-08-28 14:42:38 -04:00
const theme = useTheme();
2024-07-17 11:29:25 -07:00
return (
2024-08-28 14:42:38 -04:00
<Box
className="stat-box"
flex="20%"
minWidth="100px"
px={theme.spacing(8)}
py={theme.spacing(4)}
border={1}
borderColor={theme.palette.border.light}
borderRadius={theme.shape.borderRadius}
backgroundColor={theme.palette.background.main}
>
<Typography
variant="h6"
mb={theme.spacing(2)}
fontSize={14}
fontWeight={500}
color={theme.palette.primary.main}
2024-08-28 14:42:38 -04:00
sx={{
"& span": {
color: theme.palette.text.accent,
fontSize: 13,
fontStyle: "italic",
},
}}
>
{title}
</Typography>
2024-08-28 14:42:38 -04:00
<Typography
variant="h4"
fontWeight={500}
fontSize={13}
color={theme.palette.text.secondary}
>
2024-08-14 15:53:46 -04:00
{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} />
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]);
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
/>
2024-09-02 17:10:42 -04:00
<Stack gap={theme.spacing(12)} 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"
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",
}}
>
<Box
height="100%"
position="relative"
minWidth={34}
border={1}
borderColor={theme.palette.border.dark}
borderRadius={theme.shape.borderRadius}
backgroundColor={theme.palette.background.accent}
mr={theme.spacing(4)}
onClick={openCertificate}
sx={{
2024-09-02 17:10:42 -04:00
cursor: "pointer",
"& svg": {
2024-09-02 17:10:42 -04:00
position: "absolute",
top: "50%",
left: "50%",
transform: "translate(-50%, -50%)",
width: 20,
height: 20,
2024-08-31 14:53:15 -04:00
"& path": {
2024-09-02 17:10:42 -04:00
stroke: theme.palette.text.tertiary,
2024-08-31 14:53:15 -04:00
},
},
}}
2024-08-31 14:53:15 -04:00
>
2024-09-02 17:10:42 -04:00
<CertificateIcon />
</Box>
<Popover
id="certificate-dropdown"
anchorEl={anchorEl}
open={Boolean(anchorEl)}
onClose={closeCertificate}
anchorOrigin={{
vertical: "bottom",
horizontal: "center",
}}
transformOrigin={{
vertical: "top",
horizontal: "center",
}}
slotProps={{
paper: {
sx: {
py: 4,
px: 6,
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"
justifyContent="space-between"
2024-09-02 17:10:42 -04:00
gap={theme.spacing(6)}
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="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"
2024-08-28 14:42:38 -04:00
mb={theme.spacing(8)}
2024-08-07 19:49:08 -04:00
>
2024-08-28 14:42:38 -04:00
<Typography
component="h2"
alignSelf="flex-end"
color={theme.palette.text.secondary}
>
2024-08-07 16:45:49 -04:00
Response Times
</Typography>
2024-08-31 14:53:15 -04:00
<ButtonGroup>
2024-08-07 16:45:49 -04:00
<Button
2024-08-31 14:53:15 -04:00
variant="group"
filled={(dateRange === "day").toString()}
onClick={() => setDateRange("day")}
2024-08-31 14:53:15 -04:00
>
Day
</Button>
2024-08-07 16:45:49 -04:00
<Button
2024-08-31 14:53:15 -04:00
variant="group"
filled={(dateRange === "week").toString()}
onClick={() => setDateRange("week")}
2024-08-31 14:53:15 -04:00
>
Week
</Button>
2024-08-07 16:45:49 -04:00
<Button
2024-08-31 14:53:15 -04:00
variant="group"
filled={(dateRange === "month").toString()}
onClick={() => setDateRange("month")}
2024-08-31 14:53:15 -04:00
>
Month
</Button>
2024-08-07 16:45:49 -04:00
</ButtonGroup>
2024-08-07 19:49:08 -04:00
</Stack>
2024-08-14 14:54:43 -04:00
<Box
2024-08-28 14:42:38 -04:00
p={theme.spacing(10)}
pb={theme.spacing(2)}
backgroundColor={theme.palette.background.main}
2024-08-14 14:54:43 -04:00
border={1}
2024-08-28 14:42:38 -04:00
borderColor={theme.palette.border.light}
borderRadius={theme.shape.borderRadius}
2024-08-14 14:54:43 -04:00
sx={{ height: "250px" }}
>
2024-09-02 17:10:42 -04:00
{/* <MonitorDetailsAreaChart
2024-08-12 15:15:13 -07:00
checks={[...monitor.checks].reverse()}
2024-09-02 17:10:42 -04:00
/> */}
2024-08-07 19:49:08 -04:00
</Box>
</Box>
2024-08-28 14:42:38 -04:00
<Stack gap={theme.spacing(8)}>
<Typography component="h2" color={theme.palette.text.secondary}>
History
</Typography>
2024-09-02 17:10:42 -04:00
{/* <PaginationTable monitorId={monitorId} dateRange={dateRange} /> */}
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
};
DetailsPage.propTypes = {
isAdmin: PropTypes.bool,
};
export default DetailsPage;