conditionally render charts

This commit is contained in:
Alex Holliday committed 2026-07-28 15:27:19 -07:00
1 parent 4e495eed33
commit 81aab35961
3 files changed
+227 -77

No files matched your search

@@ -12,6 +12,7 @@ import {
import type { TooltipProps } from "recharts"; import type { TooltipProps } from "recharts";
import type { NameType, ValueType } from "recharts/types/component/DefaultTooltipContent"; import type { NameType, ValueType } from "recharts/types/component/DefaultTooltipContent";
import Typography from "@mui/material/Typography"; import Typography from "@mui/material/Typography";
import prettyMilliseconds from "pretty-ms";
import { useSelector } from "react-redux"; import { useSelector } from "react-redux";
import { import {
@@ -68,12 +69,14 @@ const ResponseTimeToolTip = ({
if (!active) return null; if (!active) return null;
const format = tooltipDateFormatLookup(range); const format = tooltipDateFormatLookup(range);
console.log(payload);
const responseTime = Math.floor(payload[0]?.payload?.originalAvgResponseTime || 0); const responseTime = Math.floor(payload[0]?.payload?.originalAvgResponseTime || 0);
return ( return (
<BaseBox sx={{ py: theme.spacing(2), px: theme.spacing(4) }}> <BaseBox sx={{ py: theme.spacing(2), px: theme.spacing(4) }}>
<Typography>{formatDateWithTz(String(label), format, uiTimezone)}</Typography> <Typography>{formatDateWithTz(String(label), format, uiTimezone)}</Typography>
<Typography>Response time: {responseTime} ms</Typography> <Typography>
Response time:{" "}
{prettyMilliseconds(responseTime, { formatSubMilliseconds: true, compact: true })}
</Typography>
</BaseBox> </BaseBox>
); );
}; };
@@ -95,7 +98,7 @@ export const HistogramDetails = ({
strokeWidth={1.5} strokeWidth={1.5}
/> />
} }
title="Response times" title="Average response time (First byte)"
> >
<ResponsiveContainer <ResponsiveContainer
width="100%" width="100%"
@@ -1,10 +1,36 @@
import type { GroupedUptimeCheck } from "@/Types/Check"; import type { GroupedUptimeCheck } from "@/Types/Check";
import { Area, AreaChart, CartesianGrid, ResponsiveContainer, YAxis } from "recharts"; import type { TooltipProps } from "recharts";
import type { NameType, ValueType } from "recharts/types/component/DefaultTooltipContent";
import type { Theme } from "@mui/material/styles";
import type { RootState } from "@/Types/state";
import { Clock } from "lucide-react";
import prettyMilliseconds from "pretty-ms";
import Stack from "@mui/material/Stack";
import {
Area,
AreaChart,
CartesianGrid,
ResponsiveContainer,
YAxis,
XAxis,
Text,
Tooltip,
} from "recharts";
import { BaseBox, BaseChart, Dot } from "@/Components/design-elements";
import Typography from "@mui/material/Typography";
import {
formatDateWithTz,
tickDateFormatLookup,
tooltipDateFormatLookup,
} from "@/Utils/TimeUtils";
import { useSelector } from "react-redux";
import { useMemo } from "react"; import { useMemo } from "react";
import { useTheme } from "@mui/material"; import { useTheme } from "@mui/material";
import { SPACING } from "@/Utils/Theme/constants";
const PHASE_KEYS = [ export const PHASE_KEYS = [
"avgDns", "avgDns",
"avgTcp", "avgTcp",
"avgTls", "avgTls",
@@ -13,6 +39,94 @@ const PHASE_KEYS = [
"avgDownload", "avgDownload",
] as const; ] as const;
const PHASE_COLOR_KEYS = {
avgDns: "dns",
avgTcp: "tcp",
avgTls: "tls",
avgRequest: "request",
avgFirstByte: "firstByte",
avgDownload: "download",
} as const;
const PHASE_LABELS: Record<(typeof PHASE_KEYS)[number], string> = {
avgDns: "DNS",
avgTcp: "TCP",
avgTls: "TLS",
avgRequest: "Request",
avgFirstByte: "First byte",
avgDownload: "Download",
};
type TimingPhaseToolTipProps = TooltipProps<ValueType, NameType> & {
range: string;
theme: Theme;
uiTimezone: string;
};
type XTickProps = {
x: number;
y: number;
payload: { value: string };
range: 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>
);
};
export const XTick = ({ x, y, payload, range }: XTickProps) => {
const format = tickDateFormatLookup(range);
const theme = useTheme();
const uiTimezone = useSelector((state: RootState) => state.ui.timezone);
return (
<Text
x={x}
y={y + 10}
textAnchor="middle"
fill={theme.palette.text.secondary}
fontSize={11}
fontWeight={400}
>
{formatDateWithTz(payload?.value, format, uiTimezone)}
</Text>
);
};
export const HistogramDetailsStacked = ({ export const HistogramDetailsStacked = ({
checks = [], checks = [],
range, range,
@@ -21,83 +135,103 @@ export const HistogramDetailsStacked = ({
range: string; range: string;
}) => { }) => {
const theme = useTheme(); const theme = useTheme();
const uiTimezone = useSelector((state: RootState) => state.ui.timezone);
// Cap the y-domain at the p95 of bucket totals so a single timeout bucket // Cap the y domain at 2x the p95 of bucket totals, so typical stacks sit
// doesn't flatten every other bucket; spikes clip off the top instead. // around mid-height with headroom above
const yMax = useMemo(() => { const yMax = useMemo(() => {
const totals = checks const totals = checks
.map((check) => PHASE_KEYS.reduce((sum, key) => sum + check[key], 0)) .map((check) => PHASE_KEYS.reduce((sum, key) => sum + check[key], 0))
.sort((a, b) => a - b); .sort((a, b) => a - b);
if (totals.length < 2) return undefined; if (totals.length < 2) return undefined;
const p95 = totals[Math.min(totals.length - 1, Math.floor(totals.length * 0.95))]; const p95 = totals[Math.min(totals.length - 1, Math.floor(totals.length * 0.95))];
return p95 > 0 ? Math.ceil(p95) : undefined; return p95 > 0 ? Math.ceil(p95 * 2) : undefined;
}, [checks]); }, [checks]);
console.log(JSON.stringify(checks, null, 2));
return ( return (
<ResponsiveContainer <BaseChart
width="100%" icon={
height={300} <Clock
size={20}
strokeWidth={1.5}
/>
}
title="Timing phases"
> >
<AreaChart data={checks.slice()}> <ResponsiveContainer
<CartesianGrid width="100%"
stroke={theme.palette.divider} height={300}
strokeWidth={1} >
strokeOpacity={1} <AreaChart data={checks.slice()}>
fill="transparent" <CartesianGrid
vertical={false} stroke={theme.palette.divider}
/> strokeWidth={1}
<YAxis strokeOpacity={1}
hide fill="transparent"
domain={[0, yMax ?? "auto"]} vertical={false}
allowDataOverflow />
/> <defs>
{PHASE_KEYS.map((key) => (
<Area <linearGradient
type="monotone" key={key}
stackId={1} id={`phaseGradient-${key}`}
dataKey="avgDns" x1="0"
stroke={"blue"} y1="0"
fill="blue" x2="0"
/> y2="1"
<Area >
stackId={1} <stop
type="monotone" offset="0%"
dataKey="avgTcp" stopColor={theme.palette.chart.phases[PHASE_COLOR_KEYS[key]]}
stroke={"red"} stopOpacity={0.8}
fill="red" />
/> <stop
<Area offset="100%"
stackId={1} stopColor={theme.palette.chart.phases[PHASE_COLOR_KEYS[key]]}
type="monotone" stopOpacity={0.2}
dataKey="avgTls" />
stroke={"green"} </linearGradient>
fill="green" ))}
/> </defs>
<Area <YAxis
stackId={1} hide
type="monotone" // scale="sqrt"
dataKey="avgRequest" domain={[0, yMax ?? "auto"]}
stroke={"orange"} allowDataOverflow
fill="orange" />
/> <XAxis
<Area axisLine={false}
stackId={1} tickLine={false}
type="monotone" dataKey="bucketDate"
dataKey="avgFirstByte" tick={(props) => (
stroke={"purple"} <XTick
fill="purple" {...props}
/> range={range}
/>
<Area )}
stackId={1} />
type="monotone" <Tooltip
dataKey="avgDownload" content={(props) => (
stroke={"brown"} <TimingPhasesToolTip
fill="brown" {...props}
/> range={range}
</AreaChart> theme={theme}
</ResponsiveContainer> uiTimezone={uiTimezone}
/>
)}
/>
{PHASE_KEYS.map((key) => (
<Area
key={key}
type="monotone"
stackId={1}
dataKey={key}
stroke={theme.palette.chart.phases[PHASE_COLOR_KEYS[key]]}
fill={`url(#phaseGradient-${key})`}
/>
))}
</AreaChart>
</ResponsiveContainer>
</BaseChart>
); );
}; };
+18 -5
View File
@@ -31,7 +31,10 @@ import type { RootState } from "@/Types/state";
import { formatDateWithTz } from "@/Utils/TimeUtils"; import { formatDateWithTz } from "@/Utils/TimeUtils";
import { t } from "i18next"; import { t } from "i18next";
import { Typography } from "@mui/material"; import { Typography } from "@mui/material";
import { HistogramDetailsStacked } from "@/Components/monitors/charts/HistogramDetailsStacked"; import {
HistogramDetailsStacked,
PHASE_KEYS,
} from "@/Components/monitors/charts/HistogramDetailsStacked";
const certificateDateFormat = "MMM D, YYYY h A"; const certificateDateFormat = "MMM D, YYYY h A";
@@ -77,6 +80,14 @@ const UptimeDetailsPage = () => {
const monitor = monitorData?.monitor; const monitor = monitorData?.monitor;
const monitorStats = monitorDetailsData?.monitorStats ?? null; const monitorStats = monitorDetailsData?.monitorStats ?? null;
const hasTimingPhases = useMemo(
() =>
(monitorData?.groupedChecks ?? []).some((check) =>
PHASE_KEYS.some((key) => check[key] > 0)
),
[monitorData]
);
// Certificate fetch - only for HTTP monitors // Certificate fetch - only for HTTP monitors
const certificateUrl = useMemo(() => { const certificateUrl = useMemo(() => {
if (!monitorId || monitor?.type !== "http") { if (!monitorId || monitor?.type !== "http") {
@@ -225,13 +236,15 @@ const UptimeDetailsPage = () => {
/> />
</Stack> </Stack>
<HistogramDetails <HistogramDetails
checks={monitorData?.groupedChecks || []}
range={dateRange}
/>
<HistogramDetailsStacked
checks={monitorData.groupedChecks} checks={monitorData.groupedChecks}
range={dateRange} range={dateRange}
/> />
{hasTimingPhases && (
<HistogramDetailsStacked
checks={monitorData.groupedChecks}
range={dateRange}
/>
)}
<ChecksTable <ChecksTable
checks={checks} checks={checks}
count={checksCount} count={checksCount}