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/new.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
import SloEditorPage from '@/components/Slos/SloEditorPage';

export default SloEditorPage;
99 changes: 99 additions & 0 deletions packages/app/src/components/Slos/SloDefinitionFields.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,99 @@
import { useEffect, useRef } from 'react';
import {
type Control,
type UseFormGetValues,
type UseFormSetValue,
useFormState,
useWatch,
} from 'react-hook-form';
import { tcFromSource } from '@hyperdx/common-utils/dist/core/metadata';
import { buildSloAvailabilityCondition } from '@hyperdx/common-utils/dist/core/slo';
import { SourceKind, type TSource } from '@hyperdx/common-utils/dist/types';
import { Input } from '@mantine/core';

import SearchWhereInput from '@/components/SearchInput/SearchWhereInput';
import { SourceSelectControlled } from '@/components/SourceSelect';
import { SQLInlineEditorControlled } from '@/components/SQLEditor/SQLInlineEditor';

import type { SloFormValues } from './sloForm';
import { SloFormRow } from './SloFormLayout';

const SLO_SOURCE_KINDS = [SourceKind.Trace, SourceKind.Log];

/** Which events count, and which of them are good. */
export function SloIndicatorFields({
control,
getValues,
setValue,
source,
}: {
control: Control<SloFormValues>;
getValues: UseFormGetValues<SloFormValues>;
setValue: UseFormSetValue<SloFormValues>;
source?: TSource;
}) {
const sourceId = useWatch({ control, name: 'source' });
const { errors } = useFormState({ control, name: 'indicator.good' });
const tableConnection = tcFromSource(source);

// The availability condition starts from the source's status or severity
// column. Switching sources replaces a seeded condition, never one the
// user wrote.
const seededGoodRef = useRef<string | undefined>(undefined);
useEffect(() => {
if (!source) return;

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 — The effect that seeds and replaces the good condition has no test

The effect fills in the condition from the source and replaces it on a source switch only if the user hasn't edited it. That is the builder's main new logic, but sloForm.test.ts only covers the resolver and toSloInput. Add a component test: pick source A, check the seeded condition, switch to source B and check it is replaced; then edit the condition, switch again and check the edit is kept.

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

const current = getValues('indicator.good').trim();
const condition = buildSloAvailabilityCondition(source);
if (current === '' || current === seededGoodRef.current) {
setValue('indicator.good', condition ?? '', {
shouldValidate: condition != null,
});
}
seededGoodRef.current = condition;
}, [source, getValues, setValue]);

return (
<>
<SloFormRow label="Source">
{/* The select shows its own error. */}
<div data-testid="slo-source">
<SourceSelectControlled
control={control}
name="source"
allowedSourceKinds={SLO_SOURCE_KINDS}
size="sm"
/>
</div>
</SloFormRow>
<SloFormRow
label="Eligible events"
helpText="The events the SLI counts. Leave empty to count every event in the source."
>
<SearchWhereInput
tableConnection={tableConnection}
control={control}
name="indicator.eligible.where"
sourceId={sourceId}
size="sm"
sqlPlaceholder="For example ServiceName = 'checkout'"
lucenePlaceholder="For example ServiceName:checkout"
data-testid="slo-eligible-input"

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-eligible-input test id never reaches the input in SQL mode, which is the form's default

SearchWhereInput (packages/app/src/components/SearchInput/SearchWhereInput.tsx:229-256) only forwards data-testid to SearchInputV2, the Lucene input. The SQLInlineEditorControlled branch drops it. This form defaults eligible.whereLanguage to 'sql', so the id is never rendered. Either forward data-testid to the SQL editor in SearchWhereInput, or put the id on a wrapper element here.

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

/>
</SloFormRow>
<SloFormRow
label="Good events"
helpText="The SQL condition a good event matches. An event where it evaluates to NULL counts as bad."
>
<SQLInlineEditorControlled
tableConnection={tableConnection}
sourceId={sourceId}
control={control}
name="indicator.good"
placeholder="For example StatusCode != 'Error'"
size="sm"
/>
<Input.Error mt={4}>{errors.indicator?.good?.message}</Input.Error>
</SloFormRow>
</>
);
}
150 changes: 150 additions & 0 deletions packages/app/src/components/Slos/SloEditorPage.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,150 @@
import dynamic from 'next/dynamic';
import Head from 'next/head';
import Link from 'next/link';
import { useRouter } from 'next/router';
import { useForm, useFormState, useWatch } from 'react-hook-form';
import {
Anchor,
Box,
Breadcrumbs,
Button,
Container,
Flex,
Grid,
Group,
Stack,
Text,
} from '@mantine/core';
import { notifications } from '@mantine/notifications';

import { usePageTitle } from '@/brand';
import { InlineNameInputControlled } from '@/components/InlineNameInput/InlineNameInput';
import { PageHeader } from '@/components/PageHeader';
import { withAppNavForSurface } from '@/layout';
import { useSaveSlo, useSlosEnabledGuard } from '@/slo';
import { useSource } from '@/source';
import { getApiErrorMessage } from '@/utils/apiErrors';

import { SloIndicatorFields } from './SloDefinitionFields';
import {
DEFAULT_SLO_FORM_VALUES,
sloFormResolver,
type SloFormValues,
toSloInput,
} from './sloForm';
import { SloFormSection } from './SloFormLayout';

function SloEditorPage() {
const enabled = useSlosEnabledGuard();
const router = useRouter();
const title = usePageTitle('New SLO');

const { control, handleSubmit, setValue, getValues } = useForm<SloFormValues>(
{
defaultValues: DEFAULT_SLO_FORM_VALUES,
resolver: sloFormResolver,
},
);
const { errors } = useFormState({ control, name: 'name' });
const sourceId = useWatch({ control, name: 'source' });
const { data: source } = useSource({ id: sourceId || undefined });

const save = useSaveSlo();
const onSave = handleSubmit(async values => {
try {
const saved = await save.mutateAsync({
slo: toSloInput(values, source),
});
notifications.show({ color: 'green', message: `Saved ${saved.name}` });
router.push('/slos');
} catch (e) {
notifications.show({
color: 'red',
title: 'Could not save the SLO',
message: await getApiErrorMessage(e, 'Something went wrong'),
});
}
});

if (!enabled) return null;

return (
<div data-testid="slo-editor-page">
<Head>
<title>{title}</title>
</Head>
<PageHeader
breadcrumbs={
<Breadcrumbs fz="sm">
<Anchor component={Link} href="/slos" fz="sm" c="dimmed">
SLOs
</Anchor>
</Breadcrumbs>
}
>
{/* The page title, like a dashboard's or a saved search's name. */}
<Flex justify="space-between" align="flex-start" gap="sm">
{/* Takes the free width, which the name grows into. */}
<Box flex={1} miw={0}>
<InlineNameInputControlled
control={control}
name="name"
size="md"
headingLevel={3}
placeholder="Untitled SLO"
aria-label="SLO name"
data-testid="slo-name-input"
/>
{errors.name?.message && (
<Text size="xs" variant="danger" data-testid="slo-name-error">
{errors.name.message}
</Text>
)}
</Box>
<Group gap="xs" wrap="nowrap">
<Button component={Link} href="/slos" variant="secondary">
Cancel
</Button>
<Button
variant="primary"
onClick={onSave}
loading={save.isPending}
data-testid="slo-save"
>
Create SLO
</Button>
</Group>
</Flex>
</PageHeader>
{/* Keeps the form's controls under the sticky header when scrolled. */}
<Container size="xl" py="md" style={{ isolation: 'isolate' }}>
<Grid>
<Grid.Col span={{ base: 12, lg: 8 }}>
<Stack gap="md">
<SloFormSection
title="Indicator"
description="Which events count, and which of them are good. Good divided by eligible is the SLI."
>
<SloIndicatorFields
control={control}
getValues={getValues}
setValue={setValue}
source={source}
/>
</SloFormSection>
</Stack>
</Grid.Col>
</Grid>
</Container>
</div>
);
}

// Client only, like Search and the chart explorer: the SQL editors and the
// themed charts render differently on the server.
const SloEditorPageDynamic = dynamic(async () => SloEditorPage, { ssr: false });

// @ts-expect-error next/dynamic component type does not include the getLayout static
SloEditorPageDynamic.getLayout = withAppNavForSurface('slo');

export default SloEditorPageDynamic;
60 changes: 60 additions & 0 deletions packages/app/src/components/Slos/SloFormLayout.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
import { Flex, Paper, Stack, Text } from '@mantine/core';

import { EDITOR_INPUT_HEIGHTS } from '@/components/editorInputHeights';
import { FormRow } from '@/components/Sources/SourceForm/FormRow';

/** A titled group of rows in the SLO builder. */
export function SloFormSection({
id,
title,
description,
children,
}: {
id?: string;
title: string;
description?: string;
children: React.ReactNode;
}) {
return (
<Paper withBorder p="md" radius="sm" id={id}>
<Text fw={500}>{title}</Text>
{description && (
<Text size="xs" c="dimmed">
{description}
</Text>
)}
<Stack gap="md" mt="md">
{children}
</Stack>
</Paper>
);
}

/**
* A row of the SLO builder: the Source form's label column, with the label
* kept in sentence case. `inline` centers a control shorter than an input,
* such as a radio group or a switch, on the label.
*/
export function SloFormRow({
label,
helpText,
inline,
children,
}: {
label: string;
helpText?: string;
inline?: boolean;
children: React.ReactNode;
}) {
return (
<FormRow label={<Text size="sm">{label}</Text>} helpText={helpText}>
{inline ? (
<Flex mih={EDITOR_INPUT_HEIGHTS.sm} align="center">
{children}
</Flex>
) : (
children
)}
</FormRow>
);
}
24 changes: 21 additions & 3 deletions packages/app/src/components/Slos/SlosListPage.tsx
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import { useMemo } from 'react';
import Head from 'next/head';
import Link from 'next/link';
import {
parseAsArrayOf,
parseAsString,
Expand All @@ -16,7 +17,7 @@ import {
Skeleton,
Text,
} from '@mantine/core';
import { IconTargetArrow } from '@tabler/icons-react';
import { IconPlus, IconTargetArrow } from '@tabler/icons-react';

import { usePageTitle } from '@/brand';
import EmptyState from '@/components/EmptyState';
Expand Down Expand Up @@ -116,15 +117,32 @@ export default function SlosListPage() {
<Head>
<title>{title}</title>
</Head>
<PageHeader title="SLOs" />
<PageHeader
title="SLOs"
actions={
<Button
component={Link}
href="/slos/new"
variant="primary"
leftSection={<IconPlus size={16} />}
data-testid="new-slo-button"
>
New SLO
</Button>
}
/>
<div style={{ flex: 1, display: 'flex', flexDirection: 'column' }}>
{hasNone ? (
<Flex align="center" justify="center" style={{ flex: 1 }}>
<EmptyState
icon={<IconTargetArrow size={32} />}
title="No SLOs yet"
description="Define a target for the share of good events in your logs or traces, and get alerted when the error budget burns too fast."
/>
>
<Button component={Link} href="/slos/new" variant="primary">
Create an SLO
</Button>
</EmptyState>
</Flex>
) : (
<Container maw={1500} w="100%" py="md">
Expand Down
Loading
Loading