Files
Checkmate/client/src/Pages/v2/Uptime/Details.tsx
T

157 lines
4.1 KiB
TypeScript
Raw Normal View History

2025-10-06 13:59:31 -07:00
import { BasePage } from "@/Components/v2/DesignElements";
import { HeaderControls } from "@/Components/v2/Monitors/HeaderControls";
2025-10-06 15:08:55 -07:00
import Stack from "@mui/material/Stack";
import { StatBox } from "@/Components/v2/DesignElements";
2025-10-07 14:44:55 -07:00
import { HistogramStatus } from "@/Components/v2/Monitors/HistogramStatus";
import { ChartAvgResponse } from "@/Components/v2/Monitors/ChartAvgResponse";
2025-10-08 13:44:41 -07:00
import { ChartResponseTime } from "@/Components/v2/Monitors/ChartResponseTime";
import { HeaderRange } from "@/Components/v2/Monitors/HeaderRange";
import { CheckTable } from "@/Pages/v2/Uptime/CheckTable";
2025-10-06 13:59:31 -07:00
2025-10-08 13:44:41 -07:00
import type { IMonitor } from "@/Types/Monitor";
2025-10-07 14:44:55 -07:00
import { useMediaQuery } from "@mui/material";
2025-10-06 15:08:55 -07:00
import { useTheme } from "@mui/material/styles";
2025-10-06 13:59:31 -07:00
import { useParams } from "react-router";
import { useGet, usePatch, type ApiResponse } from "@/Hooks/v2/UseApi";
import { useState } from "react";
2025-10-06 15:08:55 -07:00
import { getStatusPalette } from "@/Utils/MonitorUtils";
import prettyMilliseconds from "pretty-ms";
2025-10-06 13:59:31 -07:00
const UptimeDetailsPage = () => {
const { id } = useParams();
2025-10-06 15:08:55 -07:00
const theme = useTheme();
2025-10-07 14:44:55 -07:00
const isSmall = useMediaQuery(theme.breakpoints.down("md"));
2025-10-06 13:59:31 -07:00
// Local state
2025-10-07 14:44:55 -07:00
const [range, setRange] = useState("2h");
2025-10-06 13:59:31 -07:00
2025-10-08 13:44:41 -07:00
const { response, isValidating, error, refetch } = useGet<ApiResponse>(
2025-10-06 15:08:55 -07:00
`/monitors/${id}?embedChecks=true&range=${range}`,
{},
2025-10-08 15:27:53 -07:00
{ refreshInterval: 30000, keepPreviousData: true }
2025-10-06 13:59:31 -07:00
);
2025-10-07 14:44:55 -07:00
const {
response: upResponse,
2025-10-08 13:44:41 -07:00
isValidating: upIsValidating,
2025-10-07 14:44:55 -07:00
error: upError,
} = useGet<ApiResponse>(
`/monitors/${id}?embedChecks=true&range=${range}&status=up`,
{},
2025-10-08 15:27:53 -07:00
{ keepPreviousData: true }
2025-10-07 14:44:55 -07:00
);
const {
response: downResponse,
error: downError,
2025-10-08 13:44:41 -07:00
isValidating: downIsValidating,
2025-10-07 14:44:55 -07:00
} = useGet<ApiResponse>(
`/monitors/${id}?embedChecks=true&range=${range}&status=down`,
{},
2025-10-08 15:27:53 -07:00
{ keepPreviousData: true }
2025-10-07 14:44:55 -07:00
);
2025-10-08 14:28:51 -07:00
const { patch, loading: isPatching, error: postError } = usePatch<ApiResponse>();
2025-10-06 13:59:31 -07:00
2025-10-08 13:44:41 -07:00
const monitor: IMonitor = response?.data?.monitor;
2025-10-06 13:59:31 -07:00
if (!monitor) {
return null;
}
2025-10-06 15:08:55 -07:00
const stats = response?.data?.stats || null;
2025-10-07 14:44:55 -07:00
const avgResponseTime = stats?.avgResponseTime || 0;
const maxResponseTime = stats?.maxResponseTime || 0;
2025-10-06 15:08:55 -07:00
const streakDuration = stats?.currentStreakStartedAt
? Date.now() - stats?.currentStreakStartedAt
: 0;
const lastChecked = stats?.lastCheckTimestamp
? Date.now() - stats?.lastCheckTimestamp
: -1;
2025-10-07 14:44:55 -07:00
const checks = response?.data?.checks || [];
2025-10-07 15:46:09 -07:00
const upChecks = upResponse?.data?.checks ? [...upResponse.data.checks].reverse() : [];
const downChecks = downResponse?.data?.checks
? [...downResponse.data.checks].reverse()
: [];
2025-10-06 15:08:55 -07:00
2025-10-08 15:27:53 -07:00
const palette = getStatusPalette(monitor?.status);
2025-10-06 15:08:55 -07:00
2025-10-08 13:44:41 -07:00
if (error || upError || downError || postError) {
console.error("Error fetching monitor data:", {
error,
upError,
downError,
postError,
});
}
2025-10-06 13:59:31 -07:00
return (
<BasePage>
<HeaderControls
monitor={monitor}
patch={patch}
isPatching={isPatching}
refetch={refetch}
/>
2025-10-06 15:08:55 -07:00
<Stack
direction="row"
gap={theme.spacing(8)}
>
<StatBox
palette={palette}
title="Active for"
subtitle={prettyMilliseconds(streakDuration, { secondsDecimalDigits: 0 })}
/>
<StatBox
title="Last check"
subtitle={
lastChecked >= 0
? `${prettyMilliseconds(lastChecked, { secondsDecimalDigits: 0 })} ago`
: "N/A"
}
/>
<StatBox
title="Last response time"
subtitle={stats?.lastResponseTime ? `${stats?.lastResponseTime} ms` : "N/A"}
/>
</Stack>
2025-10-07 15:46:09 -07:00
<HeaderRange
2025-10-08 13:44:41 -07:00
loading={isValidating || upIsValidating || downIsValidating}
2025-10-07 15:46:09 -07:00
range={range}
setRange={setRange}
/>
2025-10-07 14:44:55 -07:00
<Stack
direction={isSmall ? "column" : "row"}
gap={theme.spacing(8)}
>
<HistogramStatus
title="Uptime"
status={"up"}
2025-10-07 15:46:09 -07:00
checks={upChecks}
2025-10-07 14:44:55 -07:00
range={range}
/>
<HistogramStatus
title="Incidents"
2025-10-07 15:46:09 -07:00
checks={downChecks}
2025-10-07 14:44:55 -07:00
status={"down"}
range={range}
/>
<ChartAvgResponse
avg={avgResponseTime}
max={maxResponseTime}
/>
</Stack>
<ChartResponseTime
checks={checks}
range={range}
/>
2025-10-08 15:27:53 -07:00
<CheckTable monitorId={monitor?._id} />
2025-10-06 13:59:31 -07:00
</BasePage>
);
};
export default UptimeDetailsPage;