Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions packages/app/pages/slos/[sloId]/index.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
import SloDetailPage from '@/components/Slos/SloDetailPage';

export default SloDetailPage;
155 changes: 155 additions & 0 deletions packages/app/src/components/Slos/SloDetailPage.tsx
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}

Copy link
Copy Markdown
Contributor

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. mutateAsync awaits 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 and onDeleted run, and "SLO not found" can flash before the push. In useDeleteSlo, 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

/>
</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} />

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The 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"

useSlo (packages/app/src/slo.ts) refetches every 60s. If one refetch fails after its retries, for example from a 5xx or a network blip, isError becomes true while slo still holds the last good data, and this branch swaps the whole page for "SLO not found". Show the error state only when there is no data, e.g. if (!slo), and pick the message from the error (404 or not) instead of always saying 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;
106 changes: 106 additions & 0 deletions packages/app/src/components/Slos/SloDetailPanels.tsx
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>
);
}
2 changes: 1 addition & 1 deletion packages/app/src/components/Slos/SloEditorPage.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -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',
Expand Down
55 changes: 55 additions & 0 deletions packages/app/src/components/Slos/SloNumbers.tsx
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>
)}
</>
);
}
9 changes: 8 additions & 1 deletion packages/app/src/components/Slos/SloRowMenu.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -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();

Expand All @@ -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',
Expand Down
8 changes: 7 additions & 1 deletion packages/app/src/components/Slos/SloTable.tsx
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 {
Expand Down Expand Up @@ -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}`)}

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The 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

ListingRow (packages/app/src/components/ListingListRow.tsx:31-45) already handles metaKey/ctrlKey and onAuxClick with window.open. Reuse that pattern, or render the name as a Link, so the row also works with the keyboard and modifier clicks.

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}>
Expand Down
Loading
Loading