From 53d40cf03aef98b9d9ac7413fe5bd0fabb52d254 Mon Sep 17 00:00:00 2001 From: Alex Fedotyev <61838744+alex-fedotyev@users.noreply.github.com> Date: Tue, 24 Nov 2026 15:47:14 +0000 Subject: [PATCH] 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 --- packages/app/src/DBSearchPage.tsx | 12 ++++- .../src/components/Slos/CreateSloButton.tsx | 52 +++++++++++++++++++ .../app/src/components/Slos/SloEditorPage.tsx | 45 +++++++++++++++- .../app/src/components/Slos/SloRowMenu.tsx | 11 +++- .../components/Slos/__tests__/sloForm.test.ts | 35 +++++++++++++ packages/app/src/components/Slos/sloForm.ts | 38 ++++++++++++++ packages/app/src/slo.ts | 4 +- 7 files changed, 191 insertions(+), 6 deletions(-) create mode 100644 packages/app/src/components/Slos/CreateSloButton.tsx diff --git a/packages/app/src/DBSearchPage.tsx b/packages/app/src/DBSearchPage.tsx index c22ba2701..120d92c8f 100644 --- a/packages/app/src/DBSearchPage.tsx +++ b/packages/app/src/DBSearchPage.tsx @@ -124,12 +124,13 @@ import SearchWhereInput, { } from '@/components/SearchInput/SearchWhereInput'; import SearchPageActionBar from '@/components/SearchPageActionBar'; import SearchTotalCountChart from '@/components/SearchTotalCountChart'; +import { CreateSloButton } from '@/components/Slos/CreateSloButton'; import { TableSourceForm } from '@/components/Sources/SourceForm'; import { SourceSelectControlled } from '@/components/SourceSelect'; import { SQLInlineEditorControlled } from '@/components/SQLEditor/SQLInlineEditor'; import { Tags } from '@/components/Tags'; import { TimePicker } from '@/components/TimePicker'; -import { IS_LOCAL_MODE } from '@/config'; +import { IS_LOCAL_MODE, IS_SLOS_ENABLED } from '@/config'; import { useAliasMapFromChartConfig } from '@/hooks/useChartConfig'; import { useExplainQuery } from '@/hooks/useExplainQuery'; import { useResolvedSourceParam } from '@/hooks/useResolvedSourceParam'; @@ -2743,6 +2744,15 @@ function DBSearchPageContent() { )} + {!IS_LOCAL_MODE && IS_SLOS_ENABLED && ( + + )} {isCompact && ( + + ); +} diff --git a/packages/app/src/components/Slos/SloEditorPage.tsx b/packages/app/src/components/Slos/SloEditorPage.tsx index f64c83898..b0578d5b2 100644 --- a/packages/app/src/components/Slos/SloEditorPage.tsx +++ b/packages/app/src/components/Slos/SloEditorPage.tsx @@ -3,12 +3,14 @@ import dynamic from 'next/dynamic'; import Head from 'next/head'; import Link from 'next/link'; import { useRouter } from 'next/router'; +import { useQueryStates } from 'nuqs'; import { type FieldPath, useForm, useFormState, useWatch, } from 'react-hook-form'; +import type { Filter } from '@hyperdx/common-utils/dist/types'; import { Anchor, Box, @@ -30,6 +32,7 @@ import { withAppNavForSurface } from '@/layout'; import { useSaveSlo, useSlo, useSlosEnabledGuard } from '@/slo'; import { useSource } from '@/source'; import { getApiErrorMessage } from '@/utils/apiErrors'; +import { parseAsJsonEncoded, parseAsStringEncoded } from '@/utils/queryParsers'; import { SloDetailsFields, @@ -40,8 +43,10 @@ import { buildSloPreviewRequest, DEFAULT_SLO_FORM_VALUES, getSloPreviewKey, + sloCopyFormValues, sloFormResolver, type SloFormValues, + sloFormValuesFromSearch, type SloPreviewRequest, sloToFormValues, toSloInput, @@ -57,12 +62,28 @@ const PREVIEW_FIELDS: FieldPath[] = [ 'window', ]; +// Carried over from Search by "Create SLO from this search". +const prefillParsers = { + source: parseAsStringEncoded, + where: parseAsStringEncoded, + whereLanguage: parseAsStringEncoded, + filters: parseAsJsonEncoded(), + name: parseAsStringEncoded, +}; + function SloEditorPage() { const enabled = useSlosEnabledGuard(); const router = useRouter(); const editId = typeof router.query.sloId === 'string' ? router.query.sloId : undefined; + // "Duplicate" on an SLO links here with ?duplicate=. + const duplicateId = + !editId && typeof router.query.duplicate === 'string' + ? router.query.duplicate + : undefined; + const [prefill] = useQueryStates(prefillParsers); const { data: existing } = useSlo(editId); + const { data: original } = useSlo(duplicateId); const title = usePageTitle(editId ? 'Edit SLO' : 'New SLO'); const { control, handleSubmit, reset, setValue, getValues, trigger } = @@ -77,16 +98,21 @@ function SloEditorPage() { }); const { data: source } = useSource({ id: sourceId || undefined }); - // Load the SLO being edited, once. + // Load the SLO being edited or copied, or the search it starts from, once. const initializedRef = useRef(false); useEffect(() => { if (initializedRef.current || !router.isReady) return; if (editId) { if (!existing) return; reset(sloToFormValues(existing)); + } else if (duplicateId) { + if (!original) return; + reset(sloCopyFormValues(original)); + } else if (prefill.source) { + reset(sloFormValuesFromSearch({ ...prefill, source: prefill.source })); } initializedRef.current = true; - }, [router.isReady, editId, existing, reset]); + }, [router.isReady, editId, existing, duplicateId, original, prefill, reset]); const [preview, setPreview] = useState(null); const runPreview = async () => { @@ -100,6 +126,21 @@ function SloEditorPage() { source, ) !== preview.key; + // A definition carried over from Search previews itself once its source + // has loaded. + const autoPreviewedRef = useRef(false); + useEffect(() => { + if (autoPreviewedRef.current || !initializedRef.current || !source) return; + if (editId || !prefill.source) return; + autoPreviewedRef.current = true; + void trigger(PREVIEW_FIELDS).then(valid => { + if (!valid) return; + setPreview( + buildSloPreviewRequest(getValues(), source, Date.now()) ?? null, + ); + }); + }); + const save = useSaveSlo(); const onSave = handleSubmit(async values => { try { diff --git a/packages/app/src/components/Slos/SloRowMenu.tsx b/packages/app/src/components/Slos/SloRowMenu.tsx index 550a621f4..d06f85684 100644 --- a/packages/app/src/components/Slos/SloRowMenu.tsx +++ b/packages/app/src/components/Slos/SloRowMenu.tsx @@ -2,7 +2,7 @@ import Link from 'next/link'; import type { SloApiResponse } from '@hyperdx/common-utils/dist/slo'; import { ActionIcon, Menu } from '@mantine/core'; import { notifications } from '@mantine/notifications'; -import { IconDots, IconPencil, IconTrash } from '@tabler/icons-react'; +import { IconCopy, IconDots, IconPencil, IconTrash } from '@tabler/icons-react'; import { useDeleteSlo } from '@/slo'; import { useConfirm } from '@/useConfirm'; @@ -59,6 +59,15 @@ export function SloRowMenu({ slo }: { slo: SloApiResponse }) { > Edit + {/* A copy starts without its alerts, as a duplicated tile does. */} + } + data-testid="slo-duplicate" + > + Duplicate + { ); }); + it('copies an SLO under a new name and without its alerts', () => { + const copy = sloCopyFormValues(savedSlo); + expect(copy).toEqual({ + ...sloToFormValues(savedSlo), + name: 'Copy of Checkout availability', + alerts: undefined, + }); + }); + + it('starts an SLO from a search', () => { + expect( + sloFormValuesFromSearch({ + source: 'source-1', + name: 'Checkout errors', + where: 'ServiceName:checkout', + whereLanguage: 'lucene', + filters: null, + }), + ).toEqual({ + ...DEFAULT_SLO_FORM_VALUES, + name: 'Checkout errors', + source: 'source-1', + indicator: { + ...DEFAULT_SLO_FORM_VALUES.indicator, + eligible: { + where: 'ServiceName:checkout', + whereLanguage: 'lucene', + filters: [], + }, + }, + }); + }); + it('marks a preview stale when its counts would change, not the target', () => { const key = getSloPreviewKey(valid, traceSource); const retargeted: SloFormValues = { ...valid, target: 95 }; diff --git a/packages/app/src/components/Slos/sloForm.ts b/packages/app/src/components/Slos/sloForm.ts index 45bcffec7..ddcec82f6 100644 --- a/packages/app/src/components/Slos/sloForm.ts +++ b/packages/app/src/components/Slos/sloForm.ts @@ -15,6 +15,7 @@ import { } from '@hyperdx/common-utils/dist/slo'; import type { BuilderChartConfigWithDateRange, + Filter, TSource, } from '@hyperdx/common-utils/dist/types'; @@ -76,6 +77,43 @@ export function sloToFormValues(slo: SloApiResponse): SloFormValues { }; } +/** A copy of an SLO, named as such, without its alerts. */ +export function sloCopyFormValues(slo: SloApiResponse): SloFormValues { + return { + ...sloToFormValues(slo), + name: `Copy of ${slo.name}`.slice(0, 256), + alerts: DEFAULT_SLO_FORM_VALUES.alerts, + }; +} + +/** A search carried over by "Create SLO from this search". */ +export type SloSearchPrefill = { + source: string; + name?: string | null; + where?: string | null; + whereLanguage?: string | null; + filters?: Filter[] | null; +}; + +/** A new SLO counting the events of a search. */ +export function sloFormValuesFromSearch( + prefill: SloSearchPrefill, +): SloFormValues { + return { + ...DEFAULT_SLO_FORM_VALUES, + name: prefill.name ?? '', + source: prefill.source, + indicator: { + ...DEFAULT_SLO_FORM_VALUES.indicator, + eligible: { + where: prefill.where ?? '', + whereLanguage: prefill.whereLanguage === 'lucene' ? 'lucene' : 'sql', + filters: prefill.filters ?? [], + }, + }, + }; +} + /** * The source's duration column and its unit, when the expression is that * column. It needs no cast and its unit is the source's duration precision. diff --git a/packages/app/src/slo.ts b/packages/app/src/slo.ts index 0c776482b..1c79eb825 100644 --- a/packages/app/src/slo.ts +++ b/packages/app/src/slo.ts @@ -14,8 +14,8 @@ const SLO_POLL_MS = 60_000; const SLO_FIRST_SNAPSHOT_POLL_MS = 10_000; /** - * Direct-URL guard while the feature bakes: the nav only links to SLOs when - * the flag is on, but the routes must bounce too. + * Direct-URL guard while the feature bakes: the nav and Search only link to + * SLOs when the flag is on, but the routes must bounce too. */ export function useSlosEnabledGuard(): boolean { const router = useRouter();