From 521cad5387da249599609138de1d1e2ea1f0e840 Mon Sep 17 00:00:00 2001 From: Alex Fedotyev <61838744+alex-fedotyev@users.noreply.github.com> Date: Tue, 24 Nov 2026 04:22:46 +0000 Subject: [PATCH] feat(app): search, filter, sort and delete SLOs in the list Adds a toolbar to the SLO list: a search over name, description and tags, a tag filter (every picked tag must match, as on the dashboards list), a source filter when the SLOs use more than one source, and a sort by worst first, name, lowest SLI, least budget left or fastest burn. SLOs without numbers yet sort last. The filters and the sort live in the URL. Each row gets a menu to delete the SLO, which deletes its burn alerts too. Co-Authored-By: Claude Opus 5.5 --- .../src/components/Slos/SloListToolbar.tsx | 86 +++++++++++++++++++ .../app/src/components/Slos/SloRowMenu.tsx | 65 ++++++++++++++ packages/app/src/components/Slos/SloTable.tsx | 5 ++ .../app/src/components/Slos/SlosListPage.tsx | 84 +++++++++++++++--- .../components/Slos/__tests__/sloList.test.ts | 77 +++++++++++++++++ .../app/src/components/Slos/sloListSort.ts | 52 +++++++++++ packages/app/src/slo.ts | 11 ++- 7 files changed, 369 insertions(+), 11 deletions(-) create mode 100644 packages/app/src/components/Slos/SloListToolbar.tsx create mode 100644 packages/app/src/components/Slos/SloRowMenu.tsx create mode 100644 packages/app/src/components/Slos/__tests__/sloList.test.ts create mode 100644 packages/app/src/components/Slos/sloListSort.ts diff --git a/packages/app/src/components/Slos/SloListToolbar.tsx b/packages/app/src/components/Slos/SloListToolbar.tsx new file mode 100644 index 000000000..de86616b9 --- /dev/null +++ b/packages/app/src/components/Slos/SloListToolbar.tsx @@ -0,0 +1,86 @@ +import { Group, MultiSelect, Select, TextInput } from '@mantine/core'; +import { IconSearch } from '@tabler/icons-react'; + +import { + DEFAULT_SLO_SORT, + isSloSort, + SLO_SORT_OPTIONS, + type SloSort, +} from './sloListSort'; + +/** Search, tag and source filters and the sort of the SLO list. */ +export function SloListToolbar({ + search, + onSearchChange, + tags, + tagFilter, + onTagFilterChange, + sources, + sourceFilter, + onSourceFilterChange, + sort, + onSortChange, +}: { + search: string; + onSearchChange: (search: string) => void; + /** The tags the team's SLOs carry. */ + tags: string[]; + tagFilter: string[]; + onTagFilterChange: (tags: string[]) => void; + /** The sources the team's SLOs are defined on. */ + sources: { value: string; label: string }[]; + sourceFilter: string | null; + onSourceFilterChange: (source: string | null) => void; + sort: SloSort; + onSortChange: (sort: SloSort) => void; +}) { + return ( + + } + value={search} + onChange={e => onSearchChange(e.currentTarget.value)} + w={320} + data-testid="slo-search" + /> + {/* Stay while a filter is set, so a stale link can still be cleared. */} + {(tags.length > 0 || tagFilter.length > 0) && ( + 0 ? undefined : 'All tags'} + data={[...new Set([...tags, ...tagFilter])]} + value={tagFilter} + onChange={onTagFilterChange} + searchable + clearable + w={220} + data-testid="slo-tag-filter" + /> + )} + {(sources.length > 1 || sourceFilter != null) && ( + ({ ...o }))} + value={sort} + onChange={value => + onSortChange(isSloSort(value) ? value : DEFAULT_SLO_SORT) + } + allowDeselect={false} + w={180} + data-testid="slo-sort" + /> + + ); +} diff --git a/packages/app/src/components/Slos/SloRowMenu.tsx b/packages/app/src/components/Slos/SloRowMenu.tsx new file mode 100644 index 000000000..60e2db950 --- /dev/null +++ b/packages/app/src/components/Slos/SloRowMenu.tsx @@ -0,0 +1,65 @@ +import type { SloApiResponse } from '@hyperdx/common-utils/dist/slo'; +import { ActionIcon, Menu } from '@mantine/core'; +import { notifications } from '@mantine/notifications'; +import { IconDots, IconTrash } from '@tabler/icons-react'; + +import { useDeleteSlo } from '@/slo'; +import { useConfirm } from '@/useConfirm'; +import { getApiErrorMessage } from '@/utils/apiErrors'; + +export function SloRowMenu({ slo }: { slo: SloApiResponse }) { + const confirm = useConfirm(); + const deleteSlo = useDeleteSlo(); + + const handleDelete = async () => { + if ( + !(await confirm( + <> + Delete {slo.name}? + {slo.alerts.fastBurn || slo.alerts.slowBurn + ? ' Its burn alerts are deleted too.' + : ''}{' '} + This is not reversible. + , + 'Delete', + { variant: 'danger' }, + )) + ) { + return; + } + try { + await deleteSlo.mutateAsync(slo.id); + notifications.show({ color: 'green', message: `Deleted ${slo.name}` }); + } catch (e) { + notifications.show({ + color: 'red', + message: await getApiErrorMessage(e, 'Could not delete the SLO'), + }); + } + }; + + return ( + + + e.stopPropagation()} + > + + + + e.stopPropagation()}> + } + onClick={handleDelete} + data-testid="slo-delete" + > + Delete + + + + ); +} diff --git a/packages/app/src/components/Slos/SloTable.tsx b/packages/app/src/components/Slos/SloTable.tsx index 58610dac8..2ad9dd092 100644 --- a/packages/app/src/components/Slos/SloTable.tsx +++ b/packages/app/src/components/Slos/SloTable.tsx @@ -8,6 +8,7 @@ import { Group, Stack, Table, Text, Tooltip } from '@mantine/core'; import { useSources } from '@/source'; +import { SloRowMenu } from './SloRowMenu'; import { SloStateBadge } from './SloStateBadge'; import { budgetColorVar, @@ -114,6 +115,9 @@ function SloRow({ + + + ); } @@ -141,6 +145,7 @@ export function SloTable({ slos }: { slos: SloApiResponse[] }) { help="How fast the budget is being spent: 1x spends it exactly over the window." /> Target + diff --git a/packages/app/src/components/Slos/SlosListPage.tsx b/packages/app/src/components/Slos/SlosListPage.tsx index a0e2e3e7b..12b0d3025 100644 --- a/packages/app/src/components/Slos/SlosListPage.tsx +++ b/packages/app/src/components/Slos/SlosListPage.tsx @@ -1,7 +1,12 @@ import { useMemo } from 'react'; import Head from 'next/head'; -import { parseAsStringEnum, useQueryState } from 'nuqs'; -import { compareSlosWorstFirst } from '@hyperdx/common-utils/dist/core/sloStats'; +import { + parseAsArrayOf, + parseAsString, + parseAsStringEnum, + useQueryState, +} from 'nuqs'; +import type { SloApiResponse } from '@hyperdx/common-utils/dist/slo'; import { Alert, Button, @@ -18,7 +23,15 @@ import EmptyState from '@/components/EmptyState'; import { PageHeader } from '@/components/PageHeader'; import { withAppNavForSurface } from '@/layout'; import { useSlos, useSlosEnabledGuard } from '@/slo'; +import { useSources } from '@/source'; +import { + compareSlos, + DEFAULT_SLO_SORT, + SLO_SORT_VALUES, + type SloSort, +} from './sloListSort'; +import { SloListToolbar } from './SloListToolbar'; import { SloTable } from './SloTable'; import { getSloDisplayState, @@ -27,24 +40,61 @@ import { sloStateLabel, } from './sloUtils'; +function matchesSearch(slo: SloApiResponse, term: string) { + return [slo.name, slo.description ?? '', ...slo.tags] + .join(' ') + .toLowerCase() + .includes(term.toLowerCase()); +} + export default function SlosListPage() { const enabled = useSlosEnabledGuard(); const title = usePageTitle('SLOs'); const { data: slos, isPending, isError, refetch } = useSlos(); + const { data: allSources } = useSources(); + const [search, setSearch] = useQueryState('search'); const [stateFilter, setStateFilter] = useQueryState( 'state', parseAsStringEnum(SLO_DISPLAY_STATES), ); - - // Worst first, so what needs attention is on top. - const filtered = useMemo( - () => - (slos ?? []) - .filter(slo => !stateFilter || getSloDisplayState(slo) === stateFilter) - .sort(compareSlosWorstFirst), - [slos, stateFilter], + const [tagFilter, setTagFilter] = useQueryState( + 'tag', + parseAsArrayOf(parseAsString).withDefault([]), ); + const [sourceFilter, setSourceFilter] = useQueryState('source'); + // Worst first by default, so what needs attention is on top. + const [sort, setSort] = useQueryState( + 'sort', + parseAsStringEnum(SLO_SORT_VALUES).withDefault(DEFAULT_SLO_SORT), + ); + + const filtered = useMemo(() => { + const term = search?.trim() ?? ''; + const tags = tagFilter.map(tag => tag.toLowerCase()); + return (slos ?? []) + .filter( + slo => + (!term || matchesSearch(slo, term)) && + (!stateFilter || getSloDisplayState(slo) === stateFilter) && + // Every tag picked, as the dashboards list filters. + tags.every(tag => slo.tags.some(t => t.toLowerCase() === tag)) && + (!sourceFilter || slo.source === sourceFilter), + ) + .sort(compareSlos(sort)); + }, [slos, search, stateFilter, tagFilter, sourceFilter, sort]); + + // The tags and sources the team's SLOs use, to filter by. + const { tagOptions, sourceOptions } = useMemo(() => { + const sourceIds = [...new Set((slos ?? []).map(slo => slo.source))]; + return { + tagOptions: [...new Set((slos ?? []).flatMap(slo => slo.tags))].sort(), + sourceOptions: sourceIds.map(id => ({ + value: id, + label: allSources?.find(s => s.id === id)?.name ?? 'Unknown source', + })), + }; + }, [slos, allSources]); const counts = useMemo(() => { const result = new Map(); @@ -96,6 +146,20 @@ export default function SlosListPage() { ))} + setSearch(value || null)} + tags={tagOptions} + tagFilter={tagFilter} + onTagFilterChange={value => + setTagFilter(value.length > 0 ? value : null) + } + sources={sourceOptions} + sourceFilter={sourceFilter} + onSourceFilterChange={setSourceFilter} + sort={sort} + onSortChange={setSort} + /> {isPending ? ( ) : isError ? ( diff --git a/packages/app/src/components/Slos/__tests__/sloList.test.ts b/packages/app/src/components/Slos/__tests__/sloList.test.ts new file mode 100644 index 000000000..60f7da3e2 --- /dev/null +++ b/packages/app/src/components/Slos/__tests__/sloList.test.ts @@ -0,0 +1,77 @@ +import type { SloApiResponse } from '@hyperdx/common-utils/dist/slo'; + +import { compareSlos } from '@/components/Slos/sloListSort'; + +/** An SLO with only what the sort reads; no status means no update yet. */ +const slo = ( + name: string, + status?: { sli: number; budgetRemaining: number; burnRate: number }, +): SloApiResponse => ({ + id: name, + version: 1, + name, + tags: [], + source: 'source-1', + indicator: { + template: 'availability', + eligible: { where: '', whereLanguage: 'sql' }, + good: "StatusCode != 'Error'", + }, + target: 99, + window: '7d', + alerts: {}, + alertStates: {}, + createdAt: '2026-11-01T00:00:00.000Z', + updatedAt: '2026-11-01T00:00:00.000Z', + status: status && { + eligible: 1000, + good: Math.round(status.sli * 1000), + bad: 1000 - Math.round(status.sli * 1000), + sli: status.sli, + errorBudget: 10, + budgetRemaining: status.budgetRemaining, + budgetRemainingEvents: 0, + burn: [ + { + rule: 'fastBurn', + window: '1h', + shortWindow: '5m', + budgetSpent: 0.02, + minEvents: 20, + threshold: 3.36, + eligible: 100, + bad: 1, + burnRate: status.burnRate, + shortBurnRate: status.burnRate, + }, + ], + state: status.budgetRemaining <= 0 ? 'breached' : 'healthy', + evaluatedAt: '2026-11-01T00:00:00.000Z', + windowStart: '2026-10-25T00:00:00.000Z', + windowEnd: '2026-11-01T00:00:00.000Z', + durationMs: 0, + rowsRead: 0, + }, +}); + +describe('SLO list sort', () => { + const slos = [ + slo('b', { sli: 0.999, budgetRemaining: 0.9, burnRate: 0.5 }), + slo('pending'), + slo('a', { sli: 0.95, budgetRemaining: -1, burnRate: 30 }), + slo('c', { sli: 0.998, budgetRemaining: 0.4, burnRate: 4 }), + ]; + const sorted = (sort: Parameters[0]) => + [...slos].sort(compareSlos(sort)).map(s => s.name); + + it('puts SLOs without numbers last', () => { + expect(sorted('sli')).toEqual(['a', 'c', 'b', 'pending']); + expect(sorted('budget')).toEqual(['a', 'c', 'b', 'pending']); + expect(sorted('burn')).toEqual(['a', 'c', 'b', 'pending']); + }); + + it('sorts by name, or worst first', () => { + expect(sorted('name')).toEqual(['a', 'b', 'c', 'pending']); + expect(sorted('worst')[0]).toBe('a'); + }); +}); diff --git a/packages/app/src/components/Slos/sloListSort.ts b/packages/app/src/components/Slos/sloListSort.ts new file mode 100644 index 000000000..f2352eda0 --- /dev/null +++ b/packages/app/src/components/Slos/sloListSort.ts @@ -0,0 +1,52 @@ +import { compareSlosWorstFirst } from '@hyperdx/common-utils/dist/core/sloStats'; +import type { SloApiResponse } from '@hyperdx/common-utils/dist/slo'; + +export const SLO_SORT_OPTIONS = [ + { value: 'worst', label: 'Worst first' }, + { value: 'name', label: 'Name' }, + { value: 'sli', label: 'Lowest SLI' }, + { value: 'budget', label: 'Least budget left' }, + { value: 'burn', label: 'Fastest burn' }, +] as const; + +export type SloSort = (typeof SLO_SORT_OPTIONS)[number]['value']; + +export const SLO_SORT_VALUES = SLO_SORT_OPTIONS.map(o => o.value); + +export const DEFAULT_SLO_SORT: SloSort = 'worst'; + +export function isSloSort(value: string | null): value is SloSort { + return SLO_SORT_VALUES.some(sort => sort === value); +} + +type SloCompare = (a: SloApiResponse, b: SloApiResponse) => number; + +/** Ascending by a number; SLOs without one (no update yet) come last. */ +const byNumber = + (value: (slo: SloApiResponse) => number | null | undefined): SloCompare => + (a, b) => { + const x = value(a); + const y = value(b); + if (x == null || y == null) + return (x == null ? 1 : 0) - (y == null ? 1 : 0); + return x - y; + }; + +export function compareSlos(sort: SloSort): SloCompare { + switch (sort) { + case 'worst': + return compareSlosWorstFirst; + case 'name': + return (a, b) => a.name.localeCompare(b.name); + case 'sli': + return byNumber(slo => slo.status?.sli); + case 'budget': + return byNumber(slo => slo.status?.budgetRemaining); + case 'burn': + // Fastest first: the 1h condition's rate, negated. + return byNumber(slo => { + const rate = slo.status?.burn[0]?.burnRate; + return rate == null ? null : -rate; + }); + } +} diff --git a/packages/app/src/slo.ts b/packages/app/src/slo.ts index c381d052e..949b23b8a 100644 --- a/packages/app/src/slo.ts +++ b/packages/app/src/slo.ts @@ -1,7 +1,7 @@ import { useEffect } from 'react'; import { useRouter } from 'next/router'; import type { SloApiResponse } from '@hyperdx/common-utils/dist/slo'; -import { useQuery } from '@tanstack/react-query'; +import { useMutation, useQuery, useQueryClient } from '@tanstack/react-query'; import { hdxServer } from './api'; import { IS_SLOS_ENABLED } from './config'; @@ -31,3 +31,12 @@ export function useSlos() { refetchInterval: SLO_POLL_MS, }); } + +export function useDeleteSlo() { + const queryClient = useQueryClient(); + return useMutation({ + mutationFn: (id: string) => hdxServer(`slos/${id}`, { method: 'DELETE' }), + onSuccess: () => + queryClient.invalidateQueries({ queryKey: SLOS_QUERY_KEY }), + }); +}