Files
Checkmate/client/src/Components/v2/common/charts/HistogramResponseTime.tsx
T

181 lines
4.5 KiB
TypeScript
Raw Normal View History

2026-01-23 23:54:58 +00:00
import Box from "@mui/material/Box";
import Stack from "@mui/material/Stack";
import Typography from "@mui/material/Typography";
2026-01-23 23:54:58 +00:00
import { useTheme } from "@mui/material/styles";
import type { ResponsiveStyleValue } from "@mui/system";
2026-01-27 19:23:34 +00:00
import type { CheckSnapshot } from "@/Types/Check";
2026-01-23 23:54:58 +00:00
import { HeatmapResponseTimeTooltip } from "./HeatmapResponseTimeTooltip";
import { useMemo } from "react";
import { useTranslation } from "react-i18next";
2026-01-23 23:54:58 +00:00
interface HistogramResponseTimeProps {
2026-01-27 19:23:34 +00:00
checks: CheckSnapshot[];
2026-01-23 23:54:58 +00:00
height?: ResponsiveStyleValue<number | string>;
gap?: ResponsiveStyleValue<number | string>;
showStats?: boolean;
statsPosition?: "left" | "right";
}
interface ResponseTimeStats {
max: number;
avg: number;
2026-01-23 23:54:58 +00:00
}
const DEFAULT_HEIGHT = 50;
const calculateResponseTimeStats = (checks: CheckSnapshot[]): ResponseTimeStats => {
if (!Array.isArray(checks) || checks.length === 0) {
return { max: 0, avg: 0 };
}
const validChecks = checks.filter(
(check) =>
(check as any).status !== "placeholder" &&
(check.originalResponseTime != null || check.responseTime != null)
);
if (validChecks.length === 0) {
return { max: 0, avg: 0 };
}
const responseTimes = validChecks.map(
(check) => check.originalResponseTime ?? check.responseTime ?? 0
);
const max = Math.max(...responseTimes);
const avg = Math.round(
responseTimes.reduce((sum, time) => sum + time, 0) / responseTimes.length
);
return { max, avg };
};
2026-01-23 23:54:58 +00:00
export const HistogramResponseTime = ({
checks,
height = DEFAULT_HEIGHT,
gap,
showStats = true,
statsPosition = "right",
2026-01-23 23:54:58 +00:00
}: HistogramResponseTimeProps) => {
const theme = useTheme();
const { t } = useTranslation();
const stats = useMemo(() => calculateResponseTimeStats(checks), [checks]);
2026-01-23 23:54:58 +00:00
if (!Array.isArray(checks) || checks.length === 0) return null;
const data =
checks.length !== 25
? [...checks, ...Array(25 - checks.length).fill({ status: "placeholder" })]
: checks;
2026-01-23 23:54:58 +00:00
const chartHeight = typeof height === "number" ? `${height}px` : height;
const gridGap = gap ?? theme.spacing(0.5);
const statsContent = showStats && (stats.max > 0 || stats.avg > 0) && (
<Stack
justifyContent="center"
alignItems={statsPosition === "left" ? "flex-end" : "flex-start"}
sx={{
minWidth: 70,
2026-02-13 11:14:25 -05:00
pr: statsPosition === "left" ? theme.spacing(8) : 0,
pl: statsPosition === "right" ? theme.spacing(8) : 0,
}}
>
<Typography
variant="caption"
2026-01-23 23:54:58 +00:00
sx={{
color: theme.palette.text.secondary,
fontSize: "0.65rem",
fontWeight: 500,
2026-02-13 11:14:25 -05:00
pt: theme.spacing(2),
2026-01-23 23:54:58 +00:00
}}
>
{t("common.charts.histogram.avg", { value: stats.avg })}
</Typography>
<Typography
variant="caption"
sx={{
color: theme.palette.text.secondary,
fontSize: "0.65rem",
fontWeight: 500,
2026-02-13 11:14:25 -05:00
pt: theme.spacing(2),
}}
>
{t("common.charts.histogram.max", { value: stats.max })}
</Typography>
</Stack>
);
return (
<Stack
direction="row"
alignItems="center"
sx={{ width: "100%" }}
>
{statsPosition === "left" && statsContent}
<Box sx={{ flex: 1 }}>
<Box
sx={{
width: "100%",
display: "grid",
gridTemplateColumns: "repeat(25, 1fr)",
gap: gridGap,
alignItems: "end",
height: chartHeight,
}}
>
{data.map((check, index) => {
const isPlaceholder = (check as any).status === "placeholder";
const heightPct = `${Math.max(0, Math.min(100, (check as any).responseTime ?? 0))}%`;
const barColor =
check.status === true
? theme.palette.success.light
: theme.palette.error.light;
const bar = (
2026-01-23 23:54:58 +00:00
<Box
sx={{
position: "relative",
2026-01-23 23:54:58 +00:00
width: "100%",
height: "100%",
borderRadius: theme.spacing(1),
bgcolor: theme.palette.action.hover,
overflow: "hidden",
2026-01-23 23:54:58 +00:00
}}
>
<Box
sx={{
position: "absolute",
bottom: 0,
left: 0,
width: "100%",
height: isPlaceholder ? 0 : heightPct,
bgcolor: barColor,
transition: "height 500ms cubic-bezier(0.4, 0, 0.2, 1)",
}}
/>
</Box>
);
return isPlaceholder ? (
<Box
key={index}
sx={{ height: "100%" }}
>
{bar}
</Box>
) : (
<HeatmapResponseTimeTooltip
key={index}
check={check}
>
{bar}
</HeatmapResponseTimeTooltip>
);
})}
</Box>
2026-01-23 23:54:58 +00:00
</Box>
{statsPosition === "right" && statsContent}
</Stack>
2026-01-23 23:54:58 +00:00
);
};