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

476 lines
14 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-12-26 12:26:06 -08:00
import { Box, Button, Stack, Tooltip, 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";
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 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 { ChartBox } from "./styled";
import SkeletonLayout from "./skeleton";
2024-08-22 12:23:52 -04:00
import "./index.css";
2025-01-24 13:09:57 -08:00
import useUtils from "../Home/Hooks/useUtils";
2024-12-30 16:07:28 -08:00
import { formatDateWithTz, formatDurationSplit } from "../../../Utils/timeUtils";
import { useIsAdmin } from "../../../Hooks/useIsAdmin";
2024-12-18 14:11:11 -08:00
import IconBox from "../../../Components/IconBox";
2024-12-19 10:18:25 -08:00
import StatBox from "../../../Components/StatBox";
2024-12-31 13:43:03 -08:00
import UpBarChart from "./Charts/UpBarChart";
import DownBarChart from "./Charts/DownBarChart";
import ResponseGaugeChart from "./Charts/ResponseGaugeChart";
2024-07-05 21:24:59 -07:00
/**
* Details page component displaying monitor details and related information.
* @component
*/
const DetailsPage = () => {
2024-10-16 11:45:05 +08:00
const theme = useTheme();
2025-01-12 12:28:13 -05:00
const { statusColor, statusMsg, determineState } = useUtils();
const isAdmin = useIsAdmin();
2024-10-16 11:45:05 +08:00
const [monitor, setMonitor] = useState({});
const { monitorId } = useParams();
const { authToken } = useSelector((state) => state.auth);
const [dateRange, setDateRange] = useState("day");
const [certificateExpiry, setCertificateExpiry] = useState("N/A");
const navigate = useNavigate();
2024-09-12 12:17:31 +08:00
2024-12-26 12:26:06 -08:00
const certificateDateFormat = "MMM D, YYYY h A";
const dateFormat = dateRange === "day" ? "MMM D, h A" : "MMM D";
2024-10-16 11:45:05 +08:00
const uiTimezone = useSelector((state) => state.ui.timezone);
2024-07-10 10:10:02 -07:00
2024-10-16 11:45:05 +08:00
const fetchMonitor = useCallback(async () => {
try {
2024-12-30 16:07:28 -08:00
const res = await networkService.getUptimeDetailsById({
2024-10-16 11:45:05 +08:00
authToken: authToken,
monitorId: monitorId,
dateRange: dateRange,
normalize: true,
});
setMonitor(res?.data?.data ?? {});
} catch (error) {
logger.error(error);
navigate("/not-found", { replace: true });
}
}, [authToken, monitorId, navigate, dateRange]);
2024-09-02 17:10:42 -04:00
2024-10-16 11:45:05 +08:00
useEffect(() => {
fetchMonitor();
}, [fetchMonitor]);
2024-09-02 17:10:42 -04:00
2024-10-16 11:45:05 +08:00
useEffect(() => {
const fetchCertificate = async () => {
if (monitor?.type !== "http") {
return;
}
try {
const res = await networkService.getCertificateExpiry({
authToken: authToken,
monitorId: monitorId,
});
if (res?.data?.data?.certificateDate) {
const date = res.data.data.certificateDate;
2024-12-26 12:26:06 -08:00
setCertificateExpiry(
formatDateWithTz(date, certificateDateFormat, uiTimezone) ?? "N/A"
);
2024-10-16 11:45:05 +08:00
}
} catch (error) {
2024-10-24 11:37:18 +08:00
setCertificateExpiry("N/A");
2024-10-16 11:45:05 +08:00
console.error(error);
}
};
fetchCertificate();
2024-12-12 12:26:24 +08:00
}, [authToken, monitorId, monitor, uiTimezone, dateFormat]);
2024-10-16 11:45:05 +08:00
const splitDuration = (duration) => {
const { time, format } = formatDurationSplit(duration);
return (
<>
{time}
<Typography component="span">{format}</Typography>
</>
);
};
2024-10-16 11:45:05 +08:00
let loading = Object.keys(monitor).length === 0;
2024-09-03 20:00:48 -04:00
2024-10-16 11:45:05 +08:00
const [hoveredUptimeData, setHoveredUptimeData] = useState(null);
const [hoveredIncidentsData, setHoveredIncidentsData] = useState(null);
2024-09-04 12:57:08 -04:00
2024-12-10 14:23:46 +08:00
const BREADCRUMBS = [
{ name: "uptime", path: "/uptime" },
{ name: "details", path: `/uptime/${monitorId}` },
];
2024-10-16 11:45:05 +08:00
return (
<Box className="monitor-details">
{loading ? (
<SkeletonLayout />
) : (
<>
2024-12-10 14:23:46 +08:00
<Breadcrumbs list={BREADCRUMBS} />
2024-10-16 11:45:05 +08:00
<Stack
gap={theme.spacing(10)}
mt={theme.spacing(10)}
>
<Stack
direction="row"
gap={theme.spacing(2)}
>
<Box>
<Typography
component="h1"
variant="h1"
>
{monitor.name}
</Typography>
<Stack
direction="row"
alignItems="center"
height="fit-content"
gap={theme.spacing(2)}
>
2025-01-09 13:50:50 -05:00
{/* TODO there is a tooltip at BarChart component. Wrap the Tooltip on our own component */}
2024-10-16 11:45:05 +08:00
<Tooltip
title={statusMsg[determineState(monitor)]}
disableInteractive
slotProps={{
popper: {
modifiers: [
{
name: "offset",
options: {
offset: [0, -8],
},
},
],
2025-01-09 13:50:50 -05:00
sx: {
"& .MuiTooltip-tooltip": {
backgroundColor: theme.palette.secondary.main,
color: theme.palette.secondary.contrastText,
px: theme.spacing(4),
py: theme.spacing(3),
border: 1,
borderColor: theme.palette.primary.lowContrast,
borderRadius: theme.shape.borderRadius,
boxShadow: theme.shape.boxShadow,
/* TODO Font size should point to theme */
fontSize: 12,
fontWeight: 500,
},
},
2024-10-16 11:45:05 +08:00
},
}}
>
<Box>
<PulseDot color={statusColor[determineState(monitor)]} />
</Box>
</Tooltip>
<Typography
component="h2"
variant="h2"
>
{monitor.url?.replace(/^https?:\/\//, "") || "..."}
</Typography>
<Typography
position="relative"
variant="body2"
mt={theme.spacing(1)}
ml={theme.spacing(6)}
sx={{
"&:before": {
position: "absolute",
content: `""`,
width: 4,
height: 4,
borderRadius: "50%",
2025-01-12 09:39:31 -05:00
backgroundColor: theme.palette.primary.contrastTextTertiary,
2024-10-16 11:45:05 +08:00
opacity: 0.8,
left: -9,
top: "50%",
transform: "translateY(-50%)",
},
}}
>
2024-12-30 16:07:28 -08:00
{/* Checking every {formatDurationRounded(monitor?.interval)}. */}
2024-10-16 11:45:05 +08:00
</Typography>
</Stack>
</Box>
<Stack
direction="row"
height={34}
sx={{
ml: "auto",
alignSelf: "flex-end",
}}
>
{isAdmin && (
<Button
variant="contained"
color="secondary"
2024-12-10 14:23:46 +08:00
onClick={() => navigate(`/uptime/configure/${monitorId}`)}
2024-10-16 11:45:05 +08:00
sx={{
px: theme.spacing(5),
"& svg": {
mr: theme.spacing(3),
"& path": {
2025-01-09 13:50:50 -05:00
/* Should always be contrastText for the button color */
stroke: theme.palette.secondary.contrastText,
2024-10-16 11:45:05 +08:00
},
},
}}
>
<SettingsIcon /> Configure
</Button>
)}
</Stack>
</Stack>
<Stack
direction="row"
gap={theme.spacing(8)}
>
2024-12-18 13:45:02 -08:00
<StatBox
2025-01-12 12:28:13 -05:00
/* sx={getStatusStyles(determineState(monitor))} */
/* statusStyles[determineState(monitor)] */
gradient={true}
status={determineState(monitor)}
2024-12-18 13:45:02 -08:00
heading={"active for"}
2025-01-10 12:12:33 -08:00
subHeading={splitDuration(monitor?.uptimeStreak)}
2024-12-18 13:45:02 -08:00
/>
<StatBox
heading="last check"
2025-01-10 12:12:33 -08:00
subHeading={splitDuration(monitor?.timeSinceLastCheck)}
2024-12-18 13:45:02 -08:00
/>
<StatBox
heading="last response time"
subHeading={
<>
2025-01-10 12:12:33 -08:00
{monitor?.latestResponseTime}
2024-12-18 13:45:02 -08:00
<Typography component="span">{"ms"}</Typography>
</>
}
/>
2024-12-26 08:59:56 +00:00
<StatBox
heading="certificate expiry"
subHeading={
<Typography
component="span"
fontSize={13}
2025-01-12 09:39:31 -05:00
color={theme.palette.primary.contrastText}
2024-12-26 08:59:56 +00:00
>
{certificateExpiry}
</Typography>
}
/>
2024-10-16 11:45:05 +08:00
</Stack>
<Box>
<Stack
direction="row"
justifyContent="space-between"
alignItems="flex-end"
gap={theme.spacing(4)}
mb={theme.spacing(8)}
>
<Typography variant="body2">
Showing statistics for past{" "}
{dateRange === "day"
? "24 hours"
: dateRange === "week"
? "7 days"
: "30 days"}
.
</Typography>
<ButtonGroup sx={{ height: 32 }}>
<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">
<Box position="relative">
<Typography>Total Checks</Typography>
<Typography component="span">
{hoveredUptimeData !== null
? hoveredUptimeData.totalChecks
2025-01-10 12:12:33 -08:00
: (monitor?.groupedUpChecks?.reduce((count, checkGroup) => {
return count + checkGroup.totalChecks;
}, 0) ?? 0)}
2024-10-16 11:45:05 +08:00
</Typography>
{hoveredUptimeData !== null && hoveredUptimeData.time !== null && (
<Typography
component="h5"
position="absolute"
top="100%"
fontSize={11}
2025-01-12 09:39:31 -05:00
color={theme.palette.primary.contrastTextTertiary}
2024-10-16 11:45:05 +08:00
>
{formatDateWithTz(
hoveredUptimeData._id,
2024-10-16 11:45:05 +08:00
dateFormat,
uiTimezone
)}
</Typography>
)}
</Box>
<Box>
2024-12-31 13:43:03 -08:00
<Typography>
{hoveredUptimeData !== null
? "Avg Response Time"
: "Uptime Percentage"}
</Typography>
2024-10-16 11:45:05 +08:00
<Typography component="span">
{hoveredUptimeData !== null
2024-12-31 13:43:03 -08:00
? Math.floor(hoveredUptimeData?.avgResponseTime ?? 0)
2024-12-30 16:07:28 -08:00
: Math.floor(
2025-01-10 12:12:33 -08:00
((monitor?.upChecks?.totalChecks ?? 0) /
(monitor?.totalChecks ?? 1)) *
2024-12-30 22:23:46 -08:00
100
2024-12-30 16:07:28 -08:00
)}
2024-12-31 13:43:03 -08:00
<Typography component="span">
{hoveredUptimeData !== null ? " ms" : " %"}
</Typography>
2024-10-16 11:45:05 +08:00
</Typography>
</Box>
</Stack>
<UpBarChart
2025-01-10 12:12:33 -08:00
monitor={monitor}
2024-10-16 11:45:05 +08:00
type={dateRange}
onBarHover={setHoveredUptimeData}
/>
</ChartBox>
<ChartBox>
<Stack>
<IconBox>
<IncidentsIcon />
</IconBox>
<Typography component="h2">Incidents</Typography>
</Stack>
<Box position="relative">
<Typography>Total Incidents</Typography>
<Typography component="span">
{hoveredIncidentsData !== null
2024-12-30 19:49:28 -08:00
? hoveredIncidentsData.totalChecks
2025-01-10 12:12:33 -08:00
: (monitor?.groupedDownChecks?.reduce((count, checkGroup) => {
return count + checkGroup.totalChecks;
}, 0) ?? 0)}
2024-10-16 11:45:05 +08:00
</Typography>
{hoveredIncidentsData !== null &&
hoveredIncidentsData.time !== null && (
<Typography
component="h5"
position="absolute"
top="100%"
fontSize={11}
2025-01-12 09:39:31 -05:00
color={theme.palette.primary.contrastTextTertiary}
2024-10-16 11:45:05 +08:00
>
{formatDateWithTz(
hoveredIncidentsData._id,
2024-10-16 11:45:05 +08:00
dateFormat,
uiTimezone
)}
</Typography>
)}
</Box>
2024-12-30 19:49:28 -08:00
<DownBarChart
2025-01-10 12:12:33 -08:00
monitor={monitor}
2024-10-16 11:45:05 +08:00
type={dateRange}
onBarHover={setHoveredIncidentsData}
2024-12-30 19:49:28 -08:00
/>
2024-10-16 11:45:05 +08:00
</ChartBox>
<ChartBox justifyContent="space-between">
<Stack>
<IconBox>
<AverageResponseIcon />
</IconBox>
<Typography component="h2">Average Response Time</Typography>
</Stack>
2025-01-10 12:12:33 -08:00
<ResponseGaugeChart avgResponseTime={monitor.avgResponseTime ?? 0} />
2024-10-16 11:45:05 +08:00
</ChartBox>
<ChartBox sx={{ padding: 0 }}>
<Stack
pt={theme.spacing(8)}
pl={theme.spacing(8)}
>
<IconBox>
<ResponseTimeIcon />
</IconBox>
<Typography component="h2">Response Times</Typography>
</Stack>
<MonitorDetailsAreaChart
2025-01-10 12:12:33 -08:00
checks={monitor.groupedChecks ?? []}
dateRange={dateRange}
/>
2024-10-16 11:45:05 +08:00
</ChartBox>
<ChartBox
gap={theme.spacing(8)}
sx={{
flex: "100%",
height: "fit-content",
"& nav": { mt: theme.spacing(12) },
}}
>
<Stack mb={theme.spacing(8)}>
<IconBox>
<HistoryIcon />
</IconBox>
<Typography
component="h2"
2025-01-12 09:39:31 -05:00
color={theme.palette.primary.contrastTextSecondary}
2024-10-16 11:45:05 +08:00
>
History
</Typography>
</Stack>
2024-12-31 13:43:03 -08:00
<PaginationTable
2024-10-16 11:45:05 +08:00
monitorId={monitorId}
dateRange={dateRange}
2024-12-31 13:43:03 -08:00
/>
2024-10-16 11:45:05 +08:00
</ChartBox>
</Stack>
</Box>
</Stack>
</>
)}
</Box>
);
2024-07-05 19:51:07 -07:00
};
DetailsPage.propTypes = {
2024-10-16 11:45:05 +08:00
isAdmin: PropTypes.bool,
};
export default DetailsPage;