Files
Checkmate/client/src/Components/monitors/charts/HistogramDetails.tsx
T

271 lines
6.5 KiB
TypeScript
Raw Normal View History

2026-07-29 11:45:49 -07:00
import Stack from "@mui/material/Stack";
2026-02-20 18:13:39 +00:00
import { BaseChart, BaseBox } from "@/Components/design-elements";
2026-01-26 02:06:37 +00:00
import { Clock } from "lucide-react";
2026-07-29 11:45:49 -07:00
import { Dot } from "@/Components/design-elements";
2026-01-26 02:06:37 +00:00
import {
AreaChart,
Area,
XAxis,
2026-07-29 10:57:20 -07:00
YAxis,
2026-01-26 02:06:37 +00:00
Tooltip,
CartesianGrid,
ResponsiveContainer,
} from "recharts";
import type { TooltipProps } from "recharts";
import type { NameType, ValueType } from "recharts/types/component/DefaultTooltipContent";
2026-01-26 02:06:37 +00:00
import Typography from "@mui/material/Typography";
2026-07-28 15:27:19 -07:00
import prettyMilliseconds from "pretty-ms";
2026-07-29 11:04:11 -07:00
import { XTick } from "@/Components/monitors/charts/XTick";
2026-01-26 02:06:37 +00:00
import { useSelector } from "react-redux";
2026-07-29 11:04:11 -07:00
import { formatDateWithTz, tooltipDateFormatLookup } from "@/Utils/TimeUtils";
2026-01-26 02:06:37 +00:00
import { useTheme } from "@mui/material/styles";
import type { Theme } from "@mui/material/styles";
2026-07-29 11:45:49 -07:00
import type { GroupedCheck, GroupedUptimeCheck } from "@/Types/Check";
2026-01-26 02:06:37 +00:00
import type { RootState } from "@/Types/state";
2026-07-29 10:57:20 -07:00
import { computeYAxisCap } from "@/Components/monitors/charts/ChartUtils";
2026-01-26 02:06:37 +00:00
2026-07-29 11:45:49 -07:00
import { useMemo, useState } from "react";
import { SPACING } from "@/Utils/Theme/constants";
import {
PHASE_COLOR_KEYS,
PHASE_KEYS,
PHASE_LABELS,
} from "@/Components/monitors/charts/ChartUtils";
type DetailsCheck = GroupedCheck &
Partial<Pick<GroupedUptimeCheck, (typeof PHASE_KEYS)[number]>>;
type ResponseTimeToolTipProps = TooltipProps<ValueType, NameType> & {
2026-01-26 02:06:37 +00:00
range: string;
theme: Theme;
2026-01-26 02:06:37 +00:00
uiTimezone: string;
};
const ResponseTimeToolTip = ({
active,
payload,
label,
range,
theme,
uiTimezone,
}: ResponseTimeToolTipProps) => {
if (!label) return null;
if (!payload) return null;
if (!active) return null;
const format = tooltipDateFormatLookup(range);
2026-07-29 10:57:20 -07:00
const responseTime = Math.floor(payload[0]?.payload?.avgResponseTime || 0);
2026-01-26 02:06:37 +00:00
return (
<BaseBox sx={{ py: theme.spacing(2), px: theme.spacing(4) }}>
<Typography>{formatDateWithTz(String(label), format, uiTimezone)}</Typography>
2026-07-28 15:27:19 -07:00
<Typography>
Response time:{" "}
{prettyMilliseconds(responseTime, { formatSubMilliseconds: true, compact: true })}
</Typography>
2026-01-26 02:06:37 +00:00
</BaseBox>
);
};
2026-07-29 11:45:49 -07:00
type TimingPhaseToolTipProps = TooltipProps<ValueType, NameType> & {
range: string;
theme: Theme;
uiTimezone: string;
};
const TimingPhasesToolTip = ({
active,
payload,
range,
theme,
uiTimezone,
}: TimingPhaseToolTipProps) => {
if (!active || !payload?.length) return null;
const bucket = payload[0].payload as GroupedUptimeCheck;
const total = PHASE_KEYS.reduce((sum, key) => sum + bucket[key], 0);
const format = tooltipDateFormatLookup(range);
return (
<BaseBox sx={{ py: theme.spacing(2), px: theme.spacing(4) }}>
<Typography>{formatDateWithTz(bucket.bucketDate, format, uiTimezone)}</Typography>
<Typography>Total: {prettyMilliseconds(total)}</Typography>
{PHASE_KEYS.map((key) => (
<Stack
alignItems={"center"}
direction="row"
key={key}
gap={SPACING.LG}
>
<Dot
size={"12px"}
color={theme.palette.chart.phases[PHASE_COLOR_KEYS[key]]}
/>
<Typography>
{PHASE_LABELS[key]}:{" "}
{prettyMilliseconds(bucket[key], {
formatSubMilliseconds: true,
compact: true,
})}
</Typography>
</Stack>
))}
</BaseBox>
);
};
2026-01-26 02:06:37 +00:00
export const HistogramDetails = ({
checks,
range,
}: {
2026-07-29 11:45:49 -07:00
checks: DetailsCheck[];
2026-01-26 02:06:37 +00:00
range: string;
}) => {
2026-07-29 11:45:49 -07:00
const [showTimingPhases, setShowTimingPhases] = useState(false);
2026-01-26 02:06:37 +00:00
const theme = useTheme();
const uiTimezone = useSelector((state: RootState) => state.ui.timezone);
2026-07-29 10:57:20 -07:00
const yMax = useMemo(() => {
2026-07-29 12:30:36 -07:00
const totals = checks.map((check) => {
const phaseSum = PHASE_KEYS.reduce((sum, key) => sum + (check[key] ?? 0), 0);
return Math.max(check.avgResponseTime ?? 0, phaseSum);
});
2026-07-29 10:57:20 -07:00
return computeYAxisCap(totals);
}, [checks]);
2026-07-29 11:45:49 -07:00
const hasTimingPhases = useMemo(
() => (checks ?? []).some((check) => PHASE_KEYS.some((key) => (check[key] ?? 0) > 0)),
[checks]
);
2026-01-26 02:06:37 +00:00
return (
<BaseChart
icon={
<Clock
size={20}
strokeWidth={1.5}
/>
}
2026-07-29 11:45:49 -07:00
title={showTimingPhases ? "Timing Phases" : "Response Time"}
onClick={() => {
if (!hasTimingPhases) return;
setShowTimingPhases((prev) => !prev);
}}
2026-01-26 02:06:37 +00:00
>
<ResponsiveContainer
width="100%"
height={300}
>
<AreaChart data={checks?.slice()}>
<CartesianGrid
stroke={theme.palette.divider}
strokeWidth={1}
strokeOpacity={1}
fill="transparent"
vertical={false}
/>
<defs>
<linearGradient
id="colorUv"
x1="0"
y1="0"
x2="0"
y2="1"
>
<stop
offset="0%"
stopColor={theme.palette.primary.main}
stopOpacity={0.8}
/>
<stop
offset="100%"
stopColor={theme.palette.primary.light}
stopOpacity={0}
/>
</linearGradient>
2026-07-29 11:45:49 -07:00
{PHASE_KEYS.map((key) => (
<linearGradient
key={key}
id={`phaseGradient-${key}`}
x1="0"
y1="0"
x2="0"
y2="1"
>
<stop
offset="0%"
stopColor={theme.palette.chart.phases[PHASE_COLOR_KEYS[key]]}
stopOpacity={0.8}
/>
<stop
offset="100%"
stopColor={theme.palette.chart.phases[PHASE_COLOR_KEYS[key]]}
stopOpacity={0.2}
/>
</linearGradient>
))}
2026-01-26 02:06:37 +00:00
</defs>
2026-07-29 10:57:20 -07:00
<YAxis
hide
// scale="sqrt"
domain={[0, yMax ?? "auto"]}
allowDataOverflow
/>
2026-01-26 02:06:37 +00:00
<XAxis
axisLine={false}
tickLine={false}
2026-01-26 03:19:33 +00:00
dataKey="bucketDate"
2026-01-26 02:06:37 +00:00
tick={(props) => (
<XTick
{...props}
range={range}
/>
)}
/>
2026-07-29 11:45:49 -07:00
{showTimingPhases ? (
<Tooltip
content={(props) => (
<TimingPhasesToolTip
{...props}
range={range}
theme={theme}
uiTimezone={uiTimezone}
/>
)}
/>
) : (
<Tooltip
content={(props) => (
<ResponseTimeToolTip
{...props}
range={range}
theme={theme}
uiTimezone={uiTimezone}
/>
)}
/>
)}
{!showTimingPhases && (
<Area
type="monotone"
dataKey="avgResponseTime"
stroke={theme.palette.primary.main}
fill="url(#colorUv)"
/>
)}
{hasTimingPhases &&
showTimingPhases &&
PHASE_KEYS.map((key) => {
return (
<Area
key={key}
type="monotone"
stackId={1}
dataKey={key}
stroke={theme.palette.chart.phases[PHASE_COLOR_KEYS[key]]}
fill={`url(#phaseGradient-${key})`}
/>
);
})}
2026-01-26 02:06:37 +00:00
</AreaChart>
</ResponsiveContainer>
</BaseChart>
);
};