diff --git a/packages/app/src/components/Slos/SloDefinitionFields.tsx b/packages/app/src/components/Slos/SloDefinitionFields.tsx index e2effd953..9636e225c 100644 --- a/packages/app/src/components/Slos/SloDefinitionFields.tsx +++ b/packages/app/src/components/Slos/SloDefinitionFields.tsx @@ -9,7 +9,10 @@ import { } from 'react-hook-form'; import { tcFromSource } from '@hyperdx/common-utils/dist/core/metadata'; import { buildSloAvailabilityCondition } from '@hyperdx/common-utils/dist/core/slo'; -import { SloWindowSchema } from '@hyperdx/common-utils/dist/slo'; +import { + SloTemplateSchema, + SloWindowSchema, +} from '@hyperdx/common-utils/dist/slo'; import { SourceKind, type TSource } from '@hyperdx/common-utils/dist/types'; import { Button, @@ -26,8 +29,10 @@ import SearchWhereInput from '@/components/SearchInput/SearchWhereInput'; import { SourceSelectControlled } from '@/components/SourceSelect'; import { SQLInlineEditorControlled } from '@/components/SQLEditor/SQLInlineEditor'; +import { SloEligibleFilters } from './SloEligibleFilters'; import type { SloFormValues } from './sloForm'; import { SloFormRow } from './SloFormLayout'; +import { SloLatencyFields } from './SloLatencyFields'; import { formatSloWindow } from './sloUtils'; const SLO_SOURCE_KINDS = [SourceKind.Trace, SourceKind.Log]; @@ -45,7 +50,10 @@ export function SloIndicatorFields({ setValue: UseFormSetValue; source?: TSource; }) { - const sourceId = useWatch({ control, name: 'source' }); + const [sourceId, template] = useWatch({ + control, + name: ['source', 'indicator.template'], + }); const { errors } = useFormState({ control, name: 'indicator.good' }); const tableConnection = tcFromSource(source); @@ -67,6 +75,32 @@ export function SloIndicatorFields({ return ( <> + + ( + { + const next = SloTemplateSchema.safeParse(value); + if (next.success) field.onChange(next.data); + }} + aria-label="Type" + data-testid="slo-template" + > + + + + + + )} + /> + {/* The select shows its own error. */}
@@ -92,21 +126,35 @@ export function SloIndicatorFields({ lucenePlaceholder="For example ServiceName:checkout" data-testid="slo-eligible-input" /> - - - - {errors.indicator?.good?.message} + {template === 'latency' ? ( + + ) : ( + + + {errors.indicator?.good?.message} + + )} ); } diff --git a/packages/app/src/components/Slos/SloEligibleFilters.tsx b/packages/app/src/components/Slos/SloEligibleFilters.tsx new file mode 100644 index 000000000..493fc992b --- /dev/null +++ b/packages/app/src/components/Slos/SloEligibleFilters.tsx @@ -0,0 +1,134 @@ +import { useCallback, useMemo } from 'react'; +import { Control, UseFormSetValue, useWatch } from 'react-hook-form'; +import { + filterColumnMetaByType, + JSDataType, +} from '@hyperdx/common-utils/dist/clickhouse'; +import { buildSearchChartConfig } from '@hyperdx/common-utils/dist/core/searchChartConfig'; +import { describeFilter, parseQuery } from '@hyperdx/common-utils/dist/filters'; +import type { + BuilderChartConfigWithDateRange, + Filter, + TSource, +} from '@hyperdx/common-utils/dist/types'; +import { CloseButton, Code, Group, Text } from '@mantine/core'; + +import { ActiveFilterPills } from '@/components/ActiveFilterPills'; +import { NOW } from '@/config'; +import { useColumns } from '@/hooks/useMetadata'; +import { useSearchPageFilterState } from '@/searchFilters'; + +import type { SloFormValues } from './sloForm'; + +const VALUE_PICKER_RANGE_MS = 60 * 60 * 1000; +// Stable, so a missing list does not hand the filter state a new array on +// every render. +const NO_FILTERS: Filter[] = []; + +/** Filters the facet pills cannot show, such as raw SQL, kept as written. */ +function isRawSloFilter(filter: Filter): boolean { + try { + return Object.keys(parseQuery([filter]).filters).length === 0; + } catch { + return true; + } +} + +/** + * The eligible-event filters, shown and edited with Search's own pills, so + * filters carried over from Search stay visible in the SLO definition. + */ +export function SloEligibleFilters({ + control, + setValue, + source, +}: { + control: Control; + setValue: UseFormSetValue; + source?: TSource; +}) { + const filters = + useWatch({ control, name: 'indicator.eligible.filters' }) ?? NO_FILTERS; + const { data: columns } = useColumns( + { + databaseName: source?.from.databaseName ?? '', + tableName: source?.from.tableName ?? '', + connectionId: source?.connection ?? '', + }, + { enabled: !!source }, + ); + const knownColumns = useMemo( + () => new Set((columns ?? []).map(c => c.name)), + [columns], + ); + const jsonColumns = useMemo( + () => + new Set( + (filterColumnMetaByType(columns ?? [], [JSDataType.JSON]) ?? []).map( + c => c.name, + ), + ), + [columns], + ); + const onFilterChange = useCallback( + (next: Filter[]) => + setValue('indicator.eligible.filters', next, { shouldDirty: true }), + [setValue], + ); + const searchFilters = useSearchPageFilterState({ + searchQuery: filters, + onFilterChange, + knownColumns, + jsonColumns, + isPreservedFilter: isRawSloFilter, + }); + + // Only used by the pill value picker to list a field's recent values. + const chartConfig = useMemo( + () => + source + ? { + ...buildSearchChartConfig(source, { + where: '', + whereLanguage: 'sql', + }), + dateRange: [new Date(NOW - VALUE_PICKER_RANGE_MS), new Date(NOW)], + } + : undefined, + [source], + ); + + const rawFilters = filters.filter(isRawSloFilter); + if (filters.length === 0) return null; + + return ( +
+ {chartConfig && ( + + )} + {rawFilters.length > 0 && ( + + {rawFilters.map(filter => ( + + {describeFilter(filter)} + + onFilterChange(filters.filter(f => f !== filter)) + } + /> + + ))} + + )} + + Filters narrow the eligible events together with the where clause. + +
+ ); +} diff --git a/packages/app/src/components/Slos/SloLatencyFields.tsx b/packages/app/src/components/Slos/SloLatencyFields.tsx new file mode 100644 index 000000000..90bc7412b --- /dev/null +++ b/packages/app/src/components/Slos/SloLatencyFields.tsx @@ -0,0 +1,187 @@ +import { useEffect, useRef } from 'react'; +import { + type Control, + Controller, + type UseFormGetValues, + type UseFormSetValue, + useFormState, + useWatch, +} from 'react-hook-form'; +import { tcFromSource } from '@hyperdx/common-utils/dist/core/metadata'; +import { + buildSloLatencyGoodSql, + getSloDefaultDuration, +} from '@hyperdx/common-utils/dist/core/sloLatency'; +import { SloDurationUnitSchema } from '@hyperdx/common-utils/dist/slo'; +import type { TSource } from '@hyperdx/common-utils/dist/types'; +import { + Checkbox, + Group, + Input, + NumberInput, + Select, + Text, +} from '@mantine/core'; + +import { SQLInlineEditorControlled } from '@/components/SQLEditor/SQLInlineEditor'; + +import { getNativeDuration, type SloFormValues, toSloLatency } from './sloForm'; +import { SloFormRow } from './SloFormLayout'; + +const UNIT_OPTIONS = [ + { value: 'ns', label: 'Nanoseconds' }, + { value: 'us', label: 'Microseconds' }, + { value: 'ms', label: 'Milliseconds' }, + { value: 's', label: 'Seconds' }, +]; + +/** + * Where the duration comes from and the threshold it is held to. Trace + * sources start on their duration column, in the unit of the source's + * duration precision; any source can point at another column or attribute, + * such as a log attribute holding milliseconds, and say which unit it holds. + */ +export function SloLatencyFields({ + control, + getValues, + setValue, + source, +}: { + control: Control; + getValues: UseFormGetValues; + setValue: UseFormSetValue; + source?: TSource; +}) { + const latency = useWatch({ control, name: 'indicator.latency' }); + const { errors } = useFormState({ + control, + name: 'indicator.latency.expression', + }); + const expression = latency.expression.trim(); + const native = getNativeDuration(expression, source) != null; + // A native duration is in the source's unit, whatever the form holds. + const saved = toSloLatency({ ...latency, expression }, source); + + // Switching sources replaces the source's duration column, never an + // expression the user typed. + const nativeDurationRef = useRef(undefined); + useEffect(() => { + if (!source) return; + const sourceDuration = getSloDefaultDuration(source); + const current = getValues('indicator.latency.expression').trim(); + if (current === '' || current === nativeDurationRef.current) { + setValue( + 'indicator.latency.expression', + sourceDuration?.expression ?? '', + ); + if (sourceDuration) { + setValue('indicator.latency.unit', sourceDuration.unit); + } + } + nativeDurationRef.current = sourceDuration?.expression; + }, [source, getValues, setValue]); + + return ( + <> + + + + {errors.indicator?.latency?.expression?.message} + + {!native && expression !== '' && ( + ( + field.onChange(e.currentTarget.checked)} + /> + )} + /> + )} + + + + ( +