Repository navigation
feat(app): SLO detail page #3808
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,3 @@ | ||
| import SloDetailPage from '@/components/Slos/SloDetailPage'; | ||
|
|
||
| export default SloDetailPage; |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -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 ( | ||
| <div data-testid="slo-detail-page"> | ||
| <Head> | ||
| <title>{title}</title> | ||
| </Head> | ||
| <PageHeader | ||
| breadcrumbs={ | ||
| <Breadcrumbs fz="sm"> | ||
| <Anchor component={Link} href="/slos" fz="sm" c="dimmed"> | ||
| SLOs | ||
| </Anchor> | ||
| </Breadcrumbs> | ||
| } | ||
| leading={ | ||
| <Group gap="sm"> | ||
| <Text fw={500} data-testid="slo-detail-name"> | ||
| {slo.name} | ||
| </Text> | ||
| <SloStateBadge state={state} /> | ||
| </Group> | ||
| } | ||
| actions={ | ||
| <Group gap="xs" wrap="nowrap"> | ||
| <SloRowMenu slo={slo} onDeleted={() => router.push('/slos')} /> | ||
| </Group> | ||
| } | ||
| /> | ||
| <Container size="xl" py="md"> | ||
| <Stack gap="md"> | ||
| {slo.description && ( | ||
| <Text size="sm" c="dimmed"> | ||
| {slo.description} | ||
| </Text> | ||
| )} | ||
| <SloNumbers | ||
| slo={slo} | ||
| hasSource={source != null} | ||
| live={live} | ||
| liveError={liveError} | ||
| /> | ||
| </Stack> | ||
| </Container> | ||
| </div> | ||
| ); | ||
| } | ||
|
|
||
| /** 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 ( | ||
| <SloDetailView slo={slo} source={source} live={live} liveError={error} /> | ||
| ); | ||
| } | ||
|
|
||
| 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 ( | ||
| <Container size="xl" py="md"> | ||
| <Skeleton h={400} /> | ||
| </Container> | ||
| ); | ||
| } | ||
| if (isError || !slo) { | ||
| return ( | ||
| <Container size="xl" py="md"> | ||
| <Alert variant="danger" title="SLO not found"> | ||
| <Anchor component={Link} href="/slos"> | ||
| Back to SLOs | ||
| </Anchor> | ||
| </Alert> | ||
| </Container> | ||
| ); | ||
| } | ||
| // A deleted source leaves the stored numbers, which say what went wrong. | ||
| return source ? ( | ||
| <SloDetailLive slo={slo} source={source} /> | ||
| ) : ( | ||
| <SloDetailView slo={slo} /> | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 🔴 P1 — A failed background poll replaces a loaded SLO with "SLO not found"
Nothing blocks merge automatically, but a maintainer will expect this fixed if it is a real defect in code this PR changes. If it is about surrounding code, reply and say so instead of patching. Do not widen the PR. How to respond |
||
| ); | ||
| } | ||
|
|
||
| // 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; | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -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 ( | ||
| <Paper withBorder p="md" radius="sm" data-testid={testId}> | ||
| <Text size="xs" c="dimmed" mb={4}> | ||
| {label} | ||
| </Text> | ||
| <Text size="xl" fw={600} style={color ? { color } : undefined}> | ||
| {value} | ||
| </Text> | ||
| {detail != null && ( | ||
| <Text size="xs" c="dimmed" mt={4}> | ||
| {detail} | ||
| </Text> | ||
| )} | ||
| </Paper> | ||
| ); | ||
| } | ||
|
|
||
| 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 ( | ||
| <SimpleGrid cols={{ base: 2, md: 4 }} spacing="md"> | ||
| <Stat | ||
| testId="slo-stat-sli" | ||
| label={`SLI, last ${window}`} | ||
| value={formatSli(stats.sli)} | ||
| color={ | ||
| stats.sli == null | ||
| ? undefined | ||
| : stats.sli * 100 >= slo.target | ||
| ? 'var(--color-text-success)' | ||
| : 'var(--color-text-danger)' | ||
| } | ||
| detail={`Target ${formatTarget(slo.target)}`} | ||
| /> | ||
| <Stat | ||
| testId="slo-stat-budget" | ||
| label="Error budget left" | ||
| value={formatBudget(stats.budgetRemaining)} | ||
| color={budgetColorVar(stats.budgetRemaining)} | ||
| detail={ | ||
| stats.budgetRemainingEvents >= 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` | ||
| } | ||
| /> | ||
| <Stat | ||
| testId="slo-stat-burn" | ||
| label={`Burn rate over ${describeBurnWindow(hour.window)}`} | ||
| value={formatBurnRate(hour.burnRate)} | ||
| color={state === 'burning' ? 'var(--color-text-danger)' : undefined} | ||
| detail={`Fast burn at ${hour.threshold}x. Over ${describeBurnWindow(sixHours.window)}: ${formatBurnRate(sixHours.burnRate)} (slow burn at ${sixHours.threshold}x)`} | ||
| /> | ||
| <Stat | ||
| testId="slo-stat-events" | ||
| label={`Events, last ${window}`} | ||
| value={formatCount(stats.eligible)} | ||
| detail={`${formatCount(stats.good)} good, ${formatCount(stats.bad)} bad`} | ||
| /> | ||
| </SimpleGrid> | ||
| ); | ||
| } |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -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 <SloStatsGrid slo={slo} stats={live.stats} state={live.state} />; | ||
| } | ||
| // 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 ? ( | ||
| <Alert variant="danger" title="Could not compute the numbers"> | ||
| {liveError.message} | ||
| </Alert> | ||
| ) : ( | ||
| status?.state === 'error' && ( | ||
| <Alert variant="danger" title="The last update failed"> | ||
| {status.error} | ||
| </Alert> | ||
| ) | ||
| )} | ||
| {stored ? ( | ||
| <SloStatsGrid slo={slo} stats={stored} state={stored.state} /> | ||
| ) : hasSource && !liveError ? ( | ||
| <Skeleton h={110} /> | ||
| ) : ( | ||
| <Alert variant="info" title="No numbers yet"> | ||
| The SLO is updated in the background, within a few minutes of a save. | ||
| </Alert> | ||
| )} | ||
| </> | ||
| ); | ||
| } |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -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 ( | ||
| <Table.Tr data-testid="slo-row"> | ||
| <Table.Tr | ||
| data-testid="slo-row" | ||
| style={{ cursor: 'pointer' }} | ||
| onClick={() => router.push(`/slos/${slo.id}`)} | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 🟡 P2 — SLO rows navigate only on a plain click: no cmd/ctrl-click, middle-click or open in new tab
Advisory. Fix if it is a small defect in code this PR changes; otherwise reply in the thread. Do not widen the PR or touch files it did not already change. How to respond |
||
| > | ||
| <Table.Td> | ||
| <Stack gap={2}> | ||
| <Text size="sm" fw={500}> | ||
|
|
||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
🟡 P2 — Deleting from the detail page waits for the deleted SLO to 404 through its retries before navigating
useDeleteSlo's onSuccess invalidates['slos'], which includes the active['slos', id]query.mutateAsyncawaits that refetch, and it 404s through react-query's default 3 retries (about 7s, with no retry override in pages/_app.tsx). Only then do the toast andonDeletedrun, and "SLO not found" can flash before the push. InuseDeleteSlo,removeQueries({ queryKey: ['slos', id], exact: true })before invalidating, or navigate before invalidating.Advisory. Fix if it is a small defect in code this PR changes; otherwise reply in the thread. Do not widen the PR or touch files it did not already change. How to respond