Skip to content

Commit 400a06b

Browse files
alex-fedotyevclaude
andcommitted
feat(app): create an SLO from a search, and duplicate an SLO
Search gets an SLO button that opens the SLO builder with the current search as the eligible events: its source, query and filters are copied, and a saved search's name becomes the SLO's name. The builder previews such a definition as soon as its source loads. The list's row menu can also duplicate an SLO, which opens the builder with its definition under a "Copy of" name and without its alerts. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
1 parent 3131671 commit 400a06b

7 files changed

Lines changed: 191 additions & 6 deletions

File tree

‎packages/app/src/DBSearchPage.tsx‎

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -124,12 +124,13 @@ import SearchWhereInput, {
124124
} from '@/components/SearchInput/SearchWhereInput';
125125
import SearchPageActionBar from '@/components/SearchPageActionBar';
126126
import SearchTotalCountChart from '@/components/SearchTotalCountChart';
127+
import { CreateSloButton } from '@/components/Slos/CreateSloButton';
127128
import { TableSourceForm } from '@/components/Sources/SourceForm';
128129
import { SourceSelectControlled } from '@/components/SourceSelect';
129130
import { SQLInlineEditorControlled } from '@/components/SQLEditor/SQLInlineEditor';
130131
import { Tags } from '@/components/Tags';
131132
import { TimePicker } from '@/components/TimePicker';
132-
import { IS_LOCAL_MODE } from '@/config';
133+
import { IS_LOCAL_MODE, IS_SLOS_ENABLED } from '@/config';
133134
import { useAliasMapFromChartConfig } from '@/hooks/useChartConfig';
134135
import { useExplainQuery } from '@/hooks/useExplainQuery';
135136
import { useResolvedSourceParam } from '@/hooks/useResolvedSourceParam';
@@ -2743,6 +2744,15 @@ function DBSearchPageContent() {
27432744
</Group>
27442745
</Button>
27452746
)}
2747+
{!IS_LOCAL_MODE && IS_SLOS_ENABLED && (
2748+
<CreateSloButton
2749+
source={searchedConfig.source}
2750+
where={searchedConfig.where}
2751+
whereLanguage={searchedConfig.whereLanguage}
2752+
filters={searchedConfig.filters}
2753+
savedSearchName={savedSearch?.name}
2754+
/>
2755+
)}
27462756
{isCompact && (
27472757
<Button
27482758
variant="subtle"
Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
import Link from 'next/link';
2+
import type { Filter } from '@hyperdx/common-utils/dist/types';
3+
import { Button, Tooltip } from '@mantine/core';
4+
import { IconTargetArrow } from '@tabler/icons-react';
5+
6+
import {
7+
encodeJsonUrlParamValue,
8+
encodeUrlParamValue,
9+
} from '@/utils/urlParamEncoding';
10+
11+
/**
12+
* Opens the SLO builder with the current search as the eligible events. The
13+
* query and filters are copied, so later edits to the search leave the SLO
14+
* alone.
15+
*/
16+
export function CreateSloButton({
17+
source,
18+
where,
19+
whereLanguage,
20+
filters,
21+
savedSearchName,
22+
}: {
23+
source?: string | null;
24+
where?: string | null;
25+
whereLanguage?: string | null;
26+
filters?: Filter[] | null;
27+
savedSearchName?: string | null;
28+
}) {
29+
if (!source) return null;
30+
const params = new URLSearchParams({
31+
source: encodeUrlParamValue(source),
32+
where: encodeUrlParamValue(where ?? ''),
33+
whereLanguage: encodeUrlParamValue(whereLanguage ?? 'lucene'),
34+
filters: encodeJsonUrlParamValue(filters ?? []),
35+
});
36+
if (savedSearchName) params.set('name', encodeUrlParamValue(savedSearchName));
37+
return (
38+
<Tooltip label="Create an SLO from this search" withArrow openDelay={300}>
39+
<Button
40+
component={Link}
41+
href={`/slos/new?${params.toString()}`}
42+
variant="secondary"
43+
size="xs"
44+
leftSection={<IconTargetArrow size={14} />}
45+
style={{ flexShrink: 0 }}
46+
data-testid="create-slo-button"
47+
>
48+
SLO
49+
</Button>
50+
</Tooltip>
51+
);
52+
}

‎packages/app/src/components/Slos/SloEditorPage.tsx‎

Lines changed: 43 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,12 +3,14 @@ import dynamic from 'next/dynamic';
33
import Head from 'next/head';
44
import Link from 'next/link';
55
import { useRouter } from 'next/router';
6+
import { useQueryStates } from 'nuqs';
67
import {
78
type FieldPath,
89
useForm,
910
useFormState,
1011
useWatch,
1112
} from 'react-hook-form';
13+
import type { Filter } from '@hyperdx/common-utils/dist/types';
1214
import {
1315
Anchor,
1416
Box,
@@ -30,6 +32,7 @@ import { withAppNavForSurface } from '@/layout';
3032
import { useSaveSlo, useSlo, useSlosEnabledGuard } from '@/slo';
3133
import { useSource } from '@/source';
3234
import { getApiErrorMessage } from '@/utils/apiErrors';
35+
import { parseAsJsonEncoded, parseAsStringEncoded } from '@/utils/queryParsers';
3336

3437
import {
3538
SloDetailsFields,
@@ -40,8 +43,10 @@ import {
4043
buildSloPreviewRequest,
4144
DEFAULT_SLO_FORM_VALUES,
4245
getSloPreviewKey,
46+
sloCopyFormValues,
4347
sloFormResolver,
4448
type SloFormValues,
49+
sloFormValuesFromSearch,
4550
type SloPreviewRequest,
4651
sloToFormValues,
4752
toSloInput,
@@ -57,12 +62,28 @@ const PREVIEW_FIELDS: FieldPath<SloFormValues>[] = [
5762
'window',
5863
];
5964

65+
// Carried over from Search by "Create SLO from this search".
66+
const prefillParsers = {
67+
source: parseAsStringEncoded,
68+
where: parseAsStringEncoded,
69+
whereLanguage: parseAsStringEncoded,
70+
filters: parseAsJsonEncoded<Filter[]>(),
71+
name: parseAsStringEncoded,
72+
};
73+
6074
function SloEditorPage() {
6175
const enabled = useSlosEnabledGuard();
6276
const router = useRouter();
6377
const editId =
6478
typeof router.query.sloId === 'string' ? router.query.sloId : undefined;
79+
// "Duplicate" on an SLO links here with ?duplicate=<id>.
80+
const duplicateId =
81+
!editId && typeof router.query.duplicate === 'string'
82+
? router.query.duplicate
83+
: undefined;
84+
const [prefill] = useQueryStates(prefillParsers);
6585
const { data: existing } = useSlo(editId);
86+
const { data: original } = useSlo(duplicateId);
6687
const title = usePageTitle(editId ? 'Edit SLO' : 'New SLO');
6788

6889
const { control, handleSubmit, reset, setValue, getValues, trigger } =
@@ -77,16 +98,21 @@ function SloEditorPage() {
7798
});
7899
const { data: source } = useSource({ id: sourceId || undefined });
79100

80-
// Load the SLO being edited, once.
101+
// Load the SLO being edited or copied, or the search it starts from, once.
81102
const initializedRef = useRef(false);
82103
useEffect(() => {
83104
if (initializedRef.current || !router.isReady) return;
84105
if (editId) {
85106
if (!existing) return;
86107
reset(sloToFormValues(existing));
108+
} else if (duplicateId) {
109+
if (!original) return;
110+
reset(sloCopyFormValues(original));
111+
} else if (prefill.source) {
112+
reset(sloFormValuesFromSearch({ ...prefill, source: prefill.source }));
87113
}
88114
initializedRef.current = true;
89-
}, [router.isReady, editId, existing, reset]);
115+
}, [router.isReady, editId, existing, duplicateId, original, prefill, reset]);
90116

91117
const [preview, setPreview] = useState<SloPreviewRequest | null>(null);
92118
const runPreview = async () => {
@@ -100,6 +126,21 @@ function SloEditorPage() {
100126
source,
101127
) !== preview.key;
102128

129+
// A definition carried over from Search previews itself once its source
130+
// has loaded.
131+
const autoPreviewedRef = useRef(false);
132+
useEffect(() => {
133+
if (autoPreviewedRef.current || !initializedRef.current || !source) return;
134+
if (editId || !prefill.source) return;
135+
autoPreviewedRef.current = true;
136+
void trigger(PREVIEW_FIELDS).then(valid => {
137+
if (!valid) return;
138+
setPreview(
139+
buildSloPreviewRequest(getValues(), source, Date.now()) ?? null,
140+
);
141+
});
142+
});
143+
103144
const save = useSaveSlo();
104145
const onSave = handleSubmit(async values => {
105146
try {

‎packages/app/src/components/Slos/SloRowMenu.tsx‎

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ import Link from 'next/link';
22
import type { SloApiResponse } from '@hyperdx/common-utils/dist/slo';
33
import { ActionIcon, Menu } from '@mantine/core';
44
import { notifications } from '@mantine/notifications';
5-
import { IconDots, IconPencil, IconTrash } from '@tabler/icons-react';
5+
import { IconCopy, IconDots, IconPencil, IconTrash } from '@tabler/icons-react';
66

77
import { useDeleteSlo } from '@/slo';
88
import { useConfirm } from '@/useConfirm';
@@ -59,6 +59,15 @@ export function SloRowMenu({ slo }: { slo: SloApiResponse }) {
5959
>
6060
Edit
6161
</Menu.Item>
62+
{/* A copy starts without its alerts, as a duplicated tile does. */}
63+
<Menu.Item
64+
component={Link}
65+
href={`/slos/new?duplicate=${slo.id}`}
66+
leftSection={<IconCopy size={14} />}
67+
data-testid="slo-duplicate"
68+
>
69+
Duplicate
70+
</Menu.Item>
6271
<Menu.Divider />
6372
<Menu.Item
6473
color="red"

‎packages/app/src/components/Slos/__tests__/sloForm.test.ts‎

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,10 @@ import {
66
buildSloPreviewRequest,
77
DEFAULT_SLO_FORM_VALUES,
88
getSloPreviewKey,
9+
sloCopyFormValues,
910
sloFormResolver,
1011
type SloFormValues,
12+
sloFormValuesFromSearch,
1113
sloToFormValues,
1214
toSloInput,
1315
} from '@/components/Slos/sloForm';
@@ -170,6 +172,39 @@ describe('SLO form', () => {
170172
);
171173
});
172174

175+
it('copies an SLO under a new name and without its alerts', () => {
176+
const copy = sloCopyFormValues(savedSlo);
177+
expect(copy).toEqual({
178+
...sloToFormValues(savedSlo),
179+
name: 'Copy of Checkout availability',
180+
alerts: undefined,
181+
});
182+
});
183+
184+
it('starts an SLO from a search', () => {
185+
expect(
186+
sloFormValuesFromSearch({
187+
source: 'source-1',
188+
name: 'Checkout errors',
189+
where: 'ServiceName:checkout',
190+
whereLanguage: 'lucene',
191+
filters: null,
192+
}),
193+
).toEqual({
194+
...DEFAULT_SLO_FORM_VALUES,
195+
name: 'Checkout errors',
196+
source: 'source-1',
197+
indicator: {
198+
...DEFAULT_SLO_FORM_VALUES.indicator,
199+
eligible: {
200+
where: 'ServiceName:checkout',
201+
whereLanguage: 'lucene',
202+
filters: [],
203+
},
204+
},
205+
});
206+
});
207+
173208
it('marks a preview stale when its counts would change, not the target', () => {
174209
const key = getSloPreviewKey(valid, traceSource);
175210
const retargeted: SloFormValues = { ...valid, target: 95 };

‎packages/app/src/components/Slos/sloForm.ts‎

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ import {
1515
} from '@hyperdx/common-utils/dist/slo';
1616
import type {
1717
BuilderChartConfigWithDateRange,
18+
Filter,
1819
TSource,
1920
} from '@hyperdx/common-utils/dist/types';
2021

@@ -76,6 +77,43 @@ export function sloToFormValues(slo: SloApiResponse): SloFormValues {
7677
};
7778
}
7879

80+
/** A copy of an SLO, named as such, without its alerts. */
81+
export function sloCopyFormValues(slo: SloApiResponse): SloFormValues {
82+
return {
83+
...sloToFormValues(slo),
84+
name: `Copy of ${slo.name}`.slice(0, 256),
85+
alerts: DEFAULT_SLO_FORM_VALUES.alerts,
86+
};
87+
}
88+
89+
/** A search carried over by "Create SLO from this search". */
90+
export type SloSearchPrefill = {
91+
source: string;
92+
name?: string | null;
93+
where?: string | null;
94+
whereLanguage?: string | null;
95+
filters?: Filter[] | null;
96+
};
97+
98+
/** A new SLO counting the events of a search. */
99+
export function sloFormValuesFromSearch(
100+
prefill: SloSearchPrefill,
101+
): SloFormValues {
102+
return {
103+
...DEFAULT_SLO_FORM_VALUES,
104+
name: prefill.name ?? '',
105+
source: prefill.source,
106+
indicator: {
107+
...DEFAULT_SLO_FORM_VALUES.indicator,
108+
eligible: {
109+
where: prefill.where ?? '',
110+
whereLanguage: prefill.whereLanguage === 'lucene' ? 'lucene' : 'sql',
111+
filters: prefill.filters ?? [],
112+
},
113+
},
114+
};
115+
}
116+
79117
/**
80118
* The source's duration column and its unit, when the expression is that
81119
* column. It needs no cast and its unit is the source's duration precision.

‎packages/app/src/slo.ts‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,8 +14,8 @@ const SLO_POLL_MS = 60_000;
1414
const SLO_FIRST_SNAPSHOT_POLL_MS = 10_000;
1515

1616
/**
17-
* Direct-URL guard while the feature bakes: the nav only links to SLOs when
18-
* the flag is on, but the routes must bounce too.
17+
* Direct-URL guard while the feature bakes: the nav and Search only link to
18+
* SLOs when the flag is on, but the routes must bounce too.
1919
*/
2020
export function useSlosEnabledGuard(): boolean {
2121
const router = useRouter();

0 commit comments

Comments
 (0)