From c4d3f25303ab2e8413c88508a000e08e3ff728a8 Mon Sep 17 00:00:00 2001 From: Alex Fedotyev <61838744+alex-fedotyev@users.noreply.github.com> Date: Tue, 24 Nov 2026 15:47:38 +0000 Subject: [PATCH] feat(app): SLO detail page Adds /slos/, which the list's rows and the builder's save now open. The page computes the SLO's numbers when it opens, from the browser like its charts: the SLI and error budget from the window's counts in buckets, and the fast and slow burn rates from a 6-hour query in 5-minute buckets, moved forward every 5 minutes. The state badge and four stats (SLI, budget left, burn rate, events) follow those numbers. Until they load, or when the source is gone, the page shows the stored status, and it says when the last background update failed. Deleting the SLO from its menu goes back to the list. Co-Authored-By: Claude Opus 5.5 --- packages/app/pages/slos/[sloId]/index.tsx | 3 + .../app/src/components/Slos/SloDetailPage.tsx | 155 ++++++++++++++++++ .../src/components/Slos/SloDetailPanels.tsx | 106 ++++++++++++ .../app/src/components/Slos/SloEditorPage.tsx | 2 +- .../app/src/components/Slos/SloNumbers.tsx | 55 +++++++ .../app/src/components/Slos/SloRowMenu.tsx | 9 +- packages/app/src/components/Slos/SloTable.tsx | 8 +- packages/app/src/components/Slos/sloUtils.ts | 20 +++ .../src/components/Slos/useSloLiveStatus.ts | 101 ++++++++++++ 9 files changed, 456 insertions(+), 3 deletions(-) create mode 100644 packages/app/pages/slos/[sloId]/index.tsx create mode 100644 packages/app/src/components/Slos/SloDetailPage.tsx create mode 100644 packages/app/src/components/Slos/SloDetailPanels.tsx create mode 100644 packages/app/src/components/Slos/SloNumbers.tsx create mode 100644 packages/app/src/components/Slos/useSloLiveStatus.ts diff --git a/packages/app/pages/slos/[sloId]/index.tsx b/packages/app/pages/slos/[sloId]/index.tsx new file mode 100644 index 000000000..d1d6b4bf7 --- /dev/null +++ b/packages/app/pages/slos/[sloId]/index.tsx @@ -0,0 +1,3 @@ +import SloDetailPage from '@/components/Slos/SloDetailPage'; + +export default SloDetailPage; diff --git a/packages/app/src/components/Slos/SloDetailPage.tsx b/packages/app/src/components/Slos/SloDetailPage.tsx new file mode 100644 index 000000000..7f7fec7cc --- /dev/null +++ b/packages/app/src/components/Slos/SloDetailPage.tsx @@ -0,0 +1,155 @@ +import dynamic from 'next/dynamic'; +import Head from 'next/head'; +import Link from 'next/link'; +import { useRouter } from 'next/router'; +import type { SloApiResponse } from '@hyperdx/common-utils/dist/slo'; +import type { TSource } from '@hyperdx/common-utils/dist/types'; +import { + Alert, + Anchor, + Breadcrumbs, + Container, + Group, + Skeleton, + Stack, + Text, +} from '@mantine/core'; + +import { usePageTitle } from '@/brand'; +import { PageHeader } from '@/components/PageHeader'; +import { withAppNavForSurface } from '@/layout'; +import { useSlo, useSlosEnabledGuard } from '@/slo'; +import { useSource } from '@/source'; + +import { SloNumbers } from './SloNumbers'; +import { SloRowMenu } from './SloRowMenu'; +import { SloStateBadge } from './SloStateBadge'; +import { getSloDisplayState } from './sloUtils'; +import { + type SloLiveStatus, + useSloLiveStatus, + useSloNow, +} from './useSloLiveStatus'; + +function SloDetailView({ + slo, + source, + live, + liveError, +}: { + slo: SloApiResponse; + source?: TSource; + live?: SloLiveStatus; + liveError?: Error | null; +}) { + const router = useRouter(); + // Named with its section, as the alert detail page is. + const title = usePageTitle(`${slo.name} - SLOs`); + const state = live?.state ?? getSloDisplayState(slo); + + return ( +
+ + {title} + + + + SLOs + + + } + leading={ + + + {slo.name} + + + + } + actions={ + + router.push('/slos')} /> + + } + /> + + + {slo.description && ( + + {slo.description} + + )} + + + +
+ ); +} + +/** One SLO is computed when it is looked at, as every chart is. */ +function SloDetailLive({ + slo, + source, +}: { + slo: SloApiResponse; + source: TSource; +}) { + const now = useSloNow(); + const { live, error } = useSloLiveStatus(slo, source, now); + return ( + + ); +} + +function SloDetailPage() { + const enabled = useSlosEnabledGuard(); + const router = useRouter(); + const sloId = + typeof router.query.sloId === 'string' ? router.query.sloId : undefined; + const { data: slo, isPending, isError } = useSlo(sloId); + const { data: source, isPending: isSourcePending } = useSource({ + id: slo?.source, + }); + + if (!enabled) return null; + if (isPending || (slo != null && isSourcePending)) { + return ( + + + + ); + } + if (isError || !slo) { + return ( + + + + Back to SLOs + + + + ); + } + // A deleted source leaves the stored numbers, which say what went wrong. + return source ? ( + + ) : ( + + ); +} + +// Client only, like Search and the chart explorer: the SQL editors and the +// themed charts render differently on the server. +const SloDetailPageDynamic = dynamic(async () => SloDetailPage, { ssr: false }); + +// @ts-expect-error next/dynamic component type does not include the getLayout static +SloDetailPageDynamic.getLayout = withAppNavForSurface('slo'); + +export default SloDetailPageDynamic; diff --git a/packages/app/src/components/Slos/SloDetailPanels.tsx b/packages/app/src/components/Slos/SloDetailPanels.tsx new file mode 100644 index 000000000..272eea8ea --- /dev/null +++ b/packages/app/src/components/Slos/SloDetailPanels.tsx @@ -0,0 +1,106 @@ +import { getSloBurnRule } from '@hyperdx/common-utils/dist/core/slo'; +import type { + SloApiResponse, + SloState, + SloStats, +} from '@hyperdx/common-utils/dist/slo'; +import { Paper, SimpleGrid, Text } from '@mantine/core'; + +import { + budgetColorVar, + describeBurnWindow, + formatBudget, + formatBurnRate, + formatCount, + formatSli, + formatSloWindow, + formatTarget, +} from './sloUtils'; + +function Stat({ + label, + value, + detail, + color, + testId, +}: { + label: string; + value: string; + detail?: React.ReactNode; + color?: string; + testId?: string; +}) { + return ( + + + {label} + + + {value} + + {detail != null && ( + + {detail} + + )} + + ); +} + +export function SloStatsGrid({ + slo, + stats, + state, +}: { + slo: SloApiResponse; + stats: SloStats; + state: SloState; +}) { + // The rule's conditions with their numbers, the 1h one first. + const [hour, sixHours] = getSloBurnRule(slo.window).map((condition, i) => ({ + ...condition, + ...stats.burn[i], + })); + const window = formatSloWindow(slo.window); + return ( + + = slo.target + ? 'var(--color-text-success)' + : 'var(--color-text-danger)' + } + detail={`Target ${formatTarget(slo.target)}`} + /> + = 0 + ? `${formatCount(Math.round(stats.budgetRemainingEvents))} of ${formatCount(Math.round(stats.errorBudget))} allowed bad events left` + : `Overspent by ${formatCount(Math.round(-stats.budgetRemainingEvents))} bad events` + } + /> + + + + ); +} diff --git a/packages/app/src/components/Slos/SloEditorPage.tsx b/packages/app/src/components/Slos/SloEditorPage.tsx index b0578d5b2..bccc7399e 100644 --- a/packages/app/src/components/Slos/SloEditorPage.tsx +++ b/packages/app/src/components/Slos/SloEditorPage.tsx @@ -149,7 +149,7 @@ function SloEditorPage() { slo: toSloInput(values, source), }); notifications.show({ color: 'green', message: `Saved ${saved.name}` }); - router.push('/slos'); + router.push(`/slos/${saved.id}`); } catch (e) { notifications.show({ color: 'red', diff --git a/packages/app/src/components/Slos/SloNumbers.tsx b/packages/app/src/components/Slos/SloNumbers.tsx new file mode 100644 index 000000000..aed91e04a --- /dev/null +++ b/packages/app/src/components/Slos/SloNumbers.tsx @@ -0,0 +1,55 @@ +import type { SloApiResponse } from '@hyperdx/common-utils/dist/slo'; +import { Alert, Skeleton } from '@mantine/core'; + +import { SloStatsGrid } from './SloDetailPanels'; +import type { SloLiveStatus } from './useSloLiveStatus'; + +/** + * The numbers on the page: computed now when the source is there (live), or + * the stored snapshot the list uses when it is not, or until live ones load. + */ +export function SloNumbers({ + slo, + hasSource, + live, + liveError, +}: { + slo: SloApiResponse; + hasSource: boolean; + live?: SloLiveStatus; + liveError?: Error | null; +}) { + const { status } = slo; + if (live) { + return ; + } + // An SLO that never updated successfully has no stored numbers to show. + const stored = + status != null && (status.state !== 'error' || status.lastSuccessAt) + ? status + : null; + return ( + <> + {liveError ? ( + + {liveError.message} + + ) : ( + status?.state === 'error' && ( + + {status.error} + + ) + )} + {stored ? ( + + ) : hasSource && !liveError ? ( + + ) : ( + + The SLO is updated in the background, within a few minutes of a save. + + )} + + ); +} diff --git a/packages/app/src/components/Slos/SloRowMenu.tsx b/packages/app/src/components/Slos/SloRowMenu.tsx index d06f85684..e3041e962 100644 --- a/packages/app/src/components/Slos/SloRowMenu.tsx +++ b/packages/app/src/components/Slos/SloRowMenu.tsx @@ -8,7 +8,13 @@ import { useDeleteSlo } from '@/slo'; import { useConfirm } from '@/useConfirm'; import { getApiErrorMessage } from '@/utils/apiErrors'; -export function SloRowMenu({ slo }: { slo: SloApiResponse }) { +export function SloRowMenu({ + slo, + onDeleted, +}: { + slo: SloApiResponse; + onDeleted?: () => void; +}) { const confirm = useConfirm(); const deleteSlo = useDeleteSlo(); @@ -31,6 +37,7 @@ export function SloRowMenu({ slo }: { slo: SloApiResponse }) { try { await deleteSlo.mutateAsync(slo.id); notifications.show({ color: 'green', message: `Deleted ${slo.name}` }); + onDeleted?.(); } catch (e) { notifications.show({ color: 'red', diff --git a/packages/app/src/components/Slos/SloTable.tsx b/packages/app/src/components/Slos/SloTable.tsx index 2ad9dd092..8b1543fad 100644 --- a/packages/app/src/components/Slos/SloTable.tsx +++ b/packages/app/src/components/Slos/SloTable.tsx @@ -1,3 +1,4 @@ +import { useRouter } from 'next/router'; import { formatDistanceToNowStrict } from 'date-fns'; import { Badge } from '@clickhouse/click-ui'; import type { @@ -66,9 +67,14 @@ function SloRow({ slo: SloApiResponse; sourceName?: string; }) { + const router = useRouter(); const { status } = slo; return ( - + router.push(`/slos/${slo.id}`)} + > diff --git a/packages/app/src/components/Slos/sloUtils.ts b/packages/app/src/components/Slos/sloUtils.ts index 985719a07..e740529a1 100644 --- a/packages/app/src/components/Slos/sloUtils.ts +++ b/packages/app/src/components/Slos/sloUtils.ts @@ -1,5 +1,6 @@ import type { SloApiResponse, + SloBurnWindow, SloState, SloWindow, } from '@hyperdx/common-utils/dist/slo'; @@ -55,6 +56,25 @@ export function sloStateDescription(state: SloDisplayState): string { } } +/** "hour" or "5 minutes", as in "the last hour". */ +function formatBurnWindow(window: SloBurnWindow): string { + switch (window) { + case '5m': + return '5 minutes'; + case '30m': + return '30 minutes'; + case '1h': + return 'hour'; + case '6h': + return '6 hours'; + } +} + +/** "the last hour" or "the last 5 minutes", for sentences about a window. */ +export function describeBurnWindow(window: SloBurnWindow): string { + return `the last ${formatBurnWindow(window)}`; +} + /** SLIs near 100% need more decimals to tell 99.9% from 99.99%. */ export function formatSli(sli: number | null | undefined): string { if (sli == null) return '-'; diff --git a/packages/app/src/components/Slos/useSloLiveStatus.ts b/packages/app/src/components/Slos/useSloLiveStatus.ts new file mode 100644 index 000000000..b99f553c4 --- /dev/null +++ b/packages/app/src/components/Slos/useSloLiveStatus.ts @@ -0,0 +1,101 @@ +import { useEffect, useMemo, useState } from 'react'; +import { + buildSloBurnChartConfig, + buildSloCountsChartConfig, + getSloHistoryGranularity, + getSloWindowRange, + parseSloHistoryRows, + sumSloBuckets, +} from '@hyperdx/common-utils/dist/core/slo'; +import { + computeSloBurnFromRows, + computeSloStats, + deriveSloState, +} from '@hyperdx/common-utils/dist/core/sloStats'; +import type { + SloApiResponse, + SloState, + SloStats, +} from '@hyperdx/common-utils/dist/slo'; +import type { TSource } from '@hyperdx/common-utils/dist/types'; + +import { useQueriedChartConfig } from '@/hooks/useChartConfig'; + +/** How often an open SLO page moves its window forward. */ +const SLO_LIVE_REFRESH_MS = 5 * 60 * 1000; + +/** The current time, moved forward every few minutes. */ +export function useSloNow(): Date { + const [now, setNow] = useState(() => new Date()); + useEffect(() => { + const id = setInterval(() => setNow(new Date()), SLO_LIVE_REFRESH_MS); + return () => clearInterval(id); + }, []); + return now; +} + +/** + * Eligible and good events per bucket over the SLO window that ends at + * `end`. + */ +function useSloHistory(slo: SloApiResponse, source: TSource, end: Date) { + const granularity = getSloHistoryGranularity(slo.window); + const config = useMemo( + () => + buildSloCountsChartConfig({ + slo, + source, + dateRange: getSloWindowRange(slo.window, end), + granularity, + }), + [slo, source, end, granularity], + ); + const { data, isLoading, error } = useQueriedChartConfig(config, { + queryKeyPrefix: 'slo-history', + refetchOnWindowFocus: false, + retry: 1, + }); + const buckets = useMemo(() => parseSloHistoryRows(data?.data ?? []), [data]); + return { + config, + granularity, + buckets, + hasData: data != null, + isLoading: isLoading && data == null, + error, + }; +} + +export type SloLiveStatus = { stats: SloStats; state: SloState }; + +/** + * The SLO's numbers as of `now`, computed in the browser like its charts: + * the window from the history buckets, the burn windows from a 6h query in + * 5-minute buckets. Undefined until both are in. + */ +export function useSloLiveStatus( + slo: SloApiResponse, + source: TSource, + now: Date, +): { live?: SloLiveStatus; error: Error | null } { + const history = useSloHistory(slo, source, now); + const burnConfig = useMemo( + () => buildSloBurnChartConfig({ slo, source, now }), + [slo, source, now], + ); + const burn = useQueriedChartConfig(burnConfig, { + queryKeyPrefix: 'slo-burn', + refetchOnWindowFocus: false, + retry: 1, + }); + const live = useMemo(() => { + if (!history.hasData || burn.data == null) return undefined; + const stats = computeSloStats( + sumSloBuckets(history.buckets), + computeSloBurnFromRows(burn.data.data, slo, now), + slo.target, + ); + return { stats, state: deriveSloState(stats) }; + }, [history.hasData, history.buckets, burn.data, slo, now]); + return { live, error: history.error ?? burn.error ?? null }; +}