diff --git a/client/src/Components/design-elements/BaseChart.tsx b/client/src/Components/design-elements/BaseChart.tsx index ddfd82a3e..46abe5d39 100644 --- a/client/src/Components/design-elements/BaseChart.tsx +++ b/client/src/Components/design-elements/BaseChart.tsx @@ -13,6 +13,7 @@ type BaseChartProps = React.PropsWithChildren<{ width?: number | string; maxWidth?: number | string; padding?: number | string | ResponsiveStyleValue; + onClick?: () => void; }>; export const BaseChart = ({ @@ -21,16 +22,22 @@ export const BaseChart = ({ width = "100%", maxWidth = "100%", padding, + onClick, }: BaseChartProps) => { const theme = useTheme(); return ( { + const sorted = values.slice().sort((a, b) => a - b); + if (sorted.length < 2) return undefined; + const p95 = sorted[Math.min(sorted.length - 1, Math.floor(sorted.length * 0.95))]; + return p95 > 0 ? Math.ceil(p95 * 2) : undefined; +}; + +export const PHASE_KEYS = [ + "avgDns", + "avgTcp", + "avgTls", + "avgRequest", + "avgFirstByte", + "avgDownload", +] as const; + +export const PHASE_COLOR_KEYS = { + avgDns: "dns", + avgTcp: "tcp", + avgTls: "tls", + avgRequest: "request", + avgFirstByte: "firstByte", + avgDownload: "download", +} as const; + +export const PHASE_LABELS: Record<(typeof PHASE_KEYS)[number], string> = { + avgDns: "DNS", + avgTcp: "TCP", + avgTls: "TLS", + avgRequest: "Request", + avgFirstByte: "First byte", + avgDownload: "Download", +}; diff --git a/client/src/Components/monitors/charts/ChartUtils.tsx b/client/src/Components/monitors/charts/ChartUtils.tsx deleted file mode 100644 index 27c36d38b..000000000 --- a/client/src/Components/monitors/charts/ChartUtils.tsx +++ /dev/null @@ -1,6 +0,0 @@ -export const computeYAxisCap = (values: number[]): number | undefined => { - const sorted = values.slice().sort((a, b) => a - b); - if (sorted.length < 2) return undefined; - const p95 = sorted[Math.min(sorted.length - 1, Math.floor(sorted.length * 0.95))]; - return p95 > 0 ? Math.ceil(p95 * 2) : undefined; -}; diff --git a/client/src/Components/monitors/charts/HistogramDetails.tsx b/client/src/Components/monitors/charts/HistogramDetails.tsx index 3ad0e03b3..5c3970d3c 100644 --- a/client/src/Components/monitors/charts/HistogramDetails.tsx +++ b/client/src/Components/monitors/charts/HistogramDetails.tsx @@ -1,5 +1,7 @@ +import Stack from "@mui/material/Stack"; import { BaseChart, BaseBox } from "@/Components/design-elements"; import { Clock } from "lucide-react"; +import { Dot } from "@/Components/design-elements"; import { AreaChart, Area, @@ -18,11 +20,21 @@ import { useSelector } from "react-redux"; import { formatDateWithTz, tooltipDateFormatLookup } from "@/Utils/TimeUtils"; import { useTheme } from "@mui/material/styles"; import type { Theme } from "@mui/material/styles"; -import type { GroupedCheck } from "@/Types/Check"; +import type { GroupedCheck, GroupedUptimeCheck } from "@/Types/Check"; import type { RootState } from "@/Types/state"; -import { useMemo } from "react"; import { computeYAxisCap } from "@/Components/monitors/charts/ChartUtils"; +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>; + type ResponseTimeToolTipProps = TooltipProps & { range: string; theme: Theme; @@ -54,19 +66,71 @@ const ResponseTimeToolTip = ({ ); }; +type TimingPhaseToolTipProps = TooltipProps & { + 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 ( + + {formatDateWithTz(bucket.bucketDate, format, uiTimezone)} + Total: {prettyMilliseconds(total)} + {PHASE_KEYS.map((key) => ( + + + + {PHASE_LABELS[key]}:{" "} + {prettyMilliseconds(bucket[key], { + formatSubMilliseconds: true, + compact: true, + })} + + + ))} + + ); +}; + export const HistogramDetails = ({ checks, range, }: { - checks: GroupedCheck[]; + checks: DetailsCheck[]; range: string; }) => { + const [showTimingPhases, setShowTimingPhases] = useState(false); const theme = useTheme(); const uiTimezone = useSelector((state: RootState) => state.ui.timezone); const yMax = useMemo(() => { const totals = checks.map((check) => check.avgResponseTime || 0); return computeYAxisCap(totals); }, [checks]); + + const hasTimingPhases = useMemo( + () => (checks ?? []).some((check) => PHASE_KEYS.some((key) => (check[key] ?? 0) > 0)), + [checks] + ); + return ( } - title="Average response time (First byte)" + title={showTimingPhases ? "Timing Phases" : "Response Time"} + onClick={() => { + if (!hasTimingPhases) return; + setShowTimingPhases((prev) => !prev); + }} > + {PHASE_KEYS.map((key) => ( + + + + + ))} - ( - - )} - /> - + {showTimingPhases ? ( + ( + + )} + /> + ) : ( + ( + + )} + /> + )} + {!showTimingPhases && ( + + )} + {hasTimingPhases && + showTimingPhases && + PHASE_KEYS.map((key) => { + return ( + + ); + })} diff --git a/client/src/Components/monitors/charts/HistogramDetailsStacked.tsx b/client/src/Components/monitors/charts/HistogramDetailsStacked.tsx deleted file mode 100644 index 2ef74f56b..000000000 --- a/client/src/Components/monitors/charts/HistogramDetailsStacked.tsx +++ /dev/null @@ -1,207 +0,0 @@ -import type { GroupedUptimeCheck } from "@/Types/Check"; -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, - Tooltip, -} from "recharts"; -import { BaseBox, BaseChart, Dot } from "@/Components/design-elements"; -import Typography from "@mui/material/Typography"; -import { XTick } from "@/Components/monitors/charts/XTick"; - -import { formatDateWithTz, tooltipDateFormatLookup } from "@/Utils/TimeUtils"; -import { useSelector } from "react-redux"; -import { useMemo } from "react"; -import { useTheme } from "@mui/material"; -import { SPACING } from "@/Utils/Theme/constants"; -import { computeYAxisCap } from "@/Components/monitors/charts/ChartUtils"; - -export const PHASE_KEYS = [ - "avgDns", - "avgTcp", - "avgTls", - "avgRequest", - "avgFirstByte", - "avgDownload", -] 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 & { - 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 ( - - {formatDateWithTz(bucket.bucketDate, format, uiTimezone)} - Total: {prettyMilliseconds(total)} - {PHASE_KEYS.map((key) => ( - - - - {PHASE_LABELS[key]}:{" "} - {prettyMilliseconds(bucket[key], { - formatSubMilliseconds: true, - compact: true, - })} - - - ))} - - ); -}; - -export const HistogramDetailsStacked = ({ - checks = [], - range, -}: { - checks: GroupedUptimeCheck[]; - range: string; -}) => { - const theme = useTheme(); - const uiTimezone = useSelector((state: RootState) => state.ui.timezone); - - // Cap the y domain at 2x the p95 of bucket totals, so typical stacks sit - // around mid-height with headroom above - const yMax = useMemo(() => { - const totals = checks.map((check) => - PHASE_KEYS.reduce((sum, key) => sum + check[key], 0) - ); - return computeYAxisCap(totals); - }, [checks]); - - return ( - - } - title="Timing phases" - > - - - - - {PHASE_KEYS.map((key) => ( - - - - - ))} - - - ( - - )} - /> - ( - - )} - /> - {PHASE_KEYS.map((key) => ( - - ))} - - - - ); -}; diff --git a/client/src/Pages/Uptime/Details/index.tsx b/client/src/Pages/Uptime/Details/index.tsx index c4128e5e4..56724259a 100644 --- a/client/src/Pages/Uptime/Details/index.tsx +++ b/client/src/Pages/Uptime/Details/index.tsx @@ -31,10 +31,6 @@ import type { RootState } from "@/Types/state"; import { formatDateWithTz } from "@/Utils/TimeUtils"; import { t } from "i18next"; import { Typography } from "@mui/material"; -import { - HistogramDetailsStacked, - PHASE_KEYS, -} from "@/Components/monitors/charts/HistogramDetailsStacked"; const certificateDateFormat = "MMM D, YYYY h A"; @@ -79,14 +75,6 @@ const UptimeDetailsPage = () => { const monitor = monitorData?.monitor; 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 const certificateUrl = useMemo(() => { if (!monitorId || monitor?.type !== "http") { @@ -238,12 +226,6 @@ const UptimeDetailsPage = () => { checks={monitorData.groupedChecks} range={dateRange} /> - {hasTimingPhases && ( - - )}