From 2faf6d194beb72c480f98ad970d793c59cd38f39 Mon Sep 17 00:00:00 2001 From: Spencer Torres Date: Wed, 21 Oct 2026 13:55:15 -0700 Subject: [PATCH] feat: Severity filter for logs that considers SeverityNumber and SeverityText --- .changeset/severity-class-classifier.md | 5 +- .changeset/severity-class-filter.md | 12 + .changeset/severity-number-display.md | 6 +- .../__tests__/renderAlertTemplate.int.test.ts | 19 ++ .../api/src/tasks/checkAlerts/template.ts | 7 +- packages/app/src/DBSearchPage.tsx | 11 +- .../app/src/NamespaceDetailsSidePanel.tsx | 5 +- packages/app/src/NodeDetailsSidePanel.tsx | 5 +- packages/app/src/PodDetailsSidePanel.tsx | 7 +- .../app/src/__tests__/searchFilters.test.ts | 78 +++++ packages/app/src/__tests__/utils.test.ts | 17 +- .../app/src/components/ActiveFilterPills.tsx | 24 +- .../src/components/DBSearchPageFilters.tsx | 146 ++++++--- .../SeverityClassFilterGroup.tsx | 60 ++++ .../components/DBSearchPageFilters/utils.ts | 2 + .../Sources/SourceForm/ExpressionFormRow.tsx | 5 +- .../Sources/SourceForm/LogTableModelForm.tsx | 4 + .../Sources/SourceForm/constants.ts | 2 +- .../__tests__/ActiveFilterPills.test.tsx | 47 +++ .../__tests__/DBSearchPageFilters.test.tsx | 121 ++++++++ .../DBSearchPageFiltersSeverity.test.tsx | 139 +++++++++ .../__tests__/LogTableModelForm.test.tsx | 59 ++++ .../app/src/llm/__tests__/expressions.test.ts | 15 + packages/app/src/llm/lib/expressions.ts | 9 +- packages/app/src/searchFilters.tsx | 35 ++- .../src/utils/__tests__/severityClass.test.ts | 114 +++++++ packages/app/src/utils/severityClass.ts | 83 +++++ .../search/severity-class-filter.spec.ts | 85 ++++++ .../app/tests/e2e/fixtures/e2e-fixtures.json | 21 ++ .../app/tests/e2e/page-objects/SearchPage.ts | 8 + packages/app/tests/e2e/seed-clickhouse.ts | 61 ++++ packages/app/tests/e2e/utils/constants.ts | 7 + .../src/__tests__/filters.test.ts | 25 ++ .../src/__tests__/severity.int.test.ts | 287 ++++++++++++++++++ .../src/core/__tests__/severity.test.ts | 165 ++++++++-- packages/common-utils/src/core/severity.ts | 132 ++++++-- packages/common-utils/src/filters.ts | 27 ++ 37 files changed, 1719 insertions(+), 136 deletions(-) create mode 100644 .changeset/severity-class-filter.md create mode 100644 packages/app/src/components/DBSearchPageFilters/SeverityClassFilterGroup.tsx create mode 100644 packages/app/src/components/__tests__/DBSearchPageFiltersSeverity.test.tsx create mode 100644 packages/app/src/components/__tests__/LogTableModelForm.test.tsx create mode 100644 packages/app/src/utils/__tests__/severityClass.test.ts create mode 100644 packages/app/src/utils/severityClass.ts create mode 100644 packages/app/tests/e2e/features/search/severity-class-filter.spec.ts create mode 100644 packages/common-utils/src/__tests__/severity.int.test.ts diff --git a/.changeset/severity-class-classifier.md b/.changeset/severity-class-classifier.md index b68c374492..e034d42a84 100644 --- a/.changeset/severity-class-classifier.md +++ b/.changeset/severity-class-classifier.md @@ -3,5 +3,6 @@ --- Add `core/severity` with the OpenTelemetry log severity rules: -`getSeverityClass` (from SeverityNumber 1-24, falling back to a case-insensitive -severity text prefix), `getSeverityShortName` and `getSeverityLabel`. +`getSeverityClass` (from SeverityNumber 1 and up, falling back to a +case-insensitive severity text prefix), `getSeverityShortName` and +`getSeverityLabel`. diff --git a/.changeset/severity-class-filter.md b/.changeset/severity-class-filter.md new file mode 100644 index 0000000000..d193065c2f --- /dev/null +++ b/.changeset/severity-class-filter.md @@ -0,0 +1,12 @@ +--- +'@hyperdx/common-utils': minor +'@hyperdx/app': patch +'@hyperdx/api': patch +--- + +Log sources with a Severity Number Expression get a Severity filter with the +OpenTelemetry classes (TRACE to FATAL, plus OTHER), from SeverityNumber with a +severity text fallback. The severity text filter stays. Kubernetes "Errors" log +tables and LLM error counts match FATAL and ERROR, and alert notifications show +`Severity: ERROR` instead of the filter SQL. Materialized views keyed only on +severity text can't serve this filter; add SeverityNumber to their dimensions. diff --git a/.changeset/severity-number-display.md b/.changeset/severity-number-display.md index 609bfcc8ab..3b63698d2d 100644 --- a/.changeset/severity-number-display.md +++ b/.changeset/severity-number-display.md @@ -10,6 +10,6 @@ add the stored text in parentheses when it differs, as in `INFO (Informational)`, and take their color from the number. The search results and pattern table use the number only when the expression names a stored numeric column. Copy and filter actions still use the stored text. Rows without a number -in 1-24, and sources without the field, show the stored text as before. Level -labels and the waterfall flags also treat `panic`, `dpanic`, `ftl` and `severe` -as errors and `wrn` as a warning. +in 1-24, and sources without the field, show the stored text as before, and the +severity text fallback also recognizes `panic`, `severe` and Serilog short names +(`FTL`, `WRN`, `INF`, `DBG`, `TRC`, `VRB`). diff --git a/packages/api/src/tasks/checkAlerts/__tests__/renderAlertTemplate.int.test.ts b/packages/api/src/tasks/checkAlerts/__tests__/renderAlertTemplate.int.test.ts index c3e09091bc..b81c615006 100644 --- a/packages/api/src/tasks/checkAlerts/__tests__/renderAlertTemplate.int.test.ts +++ b/packages/api/src/tasks/checkAlerts/__tests__/renderAlertTemplate.int.test.ts @@ -1,3 +1,4 @@ +import { buildSeverityClassExpressionV1 } from '@hyperdx/common-utils/dist/core/severity'; import { AlertState, AlertThresholdType, @@ -721,6 +722,24 @@ describe('enriched message fields', () => { }); }); + it('reports a severity class filter by its classes', async () => { + const key = buildSeverityClassExpressionV1({ + numberExpr: 'SeverityNumber', + textExpr: 'SeverityText', + }); + const { dispatched } = await renderWithWebhook(AlertState.ALERT, { + where: '', + filters: [ + { type: 'sql', condition: `${key} IN ('ERROR')` }, + { type: 'sql', condition: "SeverityText IN ('error')" }, + ] satisfies Filter[], + }); + + expect(dispatched[0].message.sourceQuery).toBe( + "(Severity: ERROR) AND (SeverityText IN ('error'))", + ); + }); + it('applies the saved search pinned filters to the sample log query', async () => { const seenQueries: string[] = []; mockClickhouseQuery.mockImplementation(async ({ query }) => { diff --git a/packages/api/src/tasks/checkAlerts/template.ts b/packages/api/src/tasks/checkAlerts/template.ts index 9b31304d4d..f0c0654316 100644 --- a/packages/api/src/tasks/checkAlerts/template.ts +++ b/packages/api/src/tasks/checkAlerts/template.ts @@ -4,6 +4,7 @@ import { getPromqlSeries } from '@hyperdx/common-utils/dist/core/promql'; import { renderChartConfig } from '@hyperdx/common-utils/dist/core/renderChartConfig'; import { buildSearchChartConfig } from '@hyperdx/common-utils/dist/core/searchChartConfig'; import { formatDate, objectHash } from '@hyperdx/common-utils/dist/core/utils'; +import { describeFilter } from '@hyperdx/common-utils/dist/filters'; import { isPromqlSavedChartConfig, isRawSqlSavedChartConfig, @@ -13,7 +14,6 @@ import { AlertThresholdType, BuilderChartConfigWithOptDateRange, ChartConfigWithOptDateRange, - Filter, isRangeThresholdType, SavedChartConfig, SourceKind, @@ -113,11 +113,6 @@ const describeThreshold = (alert: AlertInput): string => { : `${alert.threshold}`; }; -const describeFilter = (filter: Filter): string => - filter.type === 'sql_ast' - ? `${filter.left} ${filter.operator} ${filter.right}` - : filter.condition; - const joinConditions = (...parts: (string | undefined)[]): string => { const present = parts .map(part => part?.trim()) diff --git a/packages/app/src/DBSearchPage.tsx b/packages/app/src/DBSearchPage.tsx index ef1bf3bebe..0cc2875ac6 100644 --- a/packages/app/src/DBSearchPage.tsx +++ b/packages/app/src/DBSearchPage.tsx @@ -38,6 +38,7 @@ import { isBrowser, splitAndTrimWithBracket, } from '@hyperdx/common-utils/dist/core/utils'; +import { describeFilter } from '@hyperdx/common-utils/dist/filters'; import { BuilderChartConfigWithDateRange, ChartConfigWithDateRange, @@ -177,6 +178,7 @@ import { parseAsSortingStateString, parseAsStringEncoded, } from './utils/queryParsers'; +import { getSeverityClassKey } from './utils/severityClass'; import { LOCAL_STORE_CONNECTIONS_KEY } from './connection'; import { DBSearchPageAlertModal } from './DBSearchPageAlertModal'; import { SearchConfig } from './types'; @@ -649,9 +651,7 @@ function SaveSearchModalComponent({ {searchedConfig.filters.map((filter, idx) => ( - {filter.type === 'sql_ast' - ? `${filter.left} ${filter.operator} ${filter.right}` - : filter.condition} + {describeFilter(filter)} ))} @@ -1547,7 +1547,10 @@ function DBSearchPageContent() { if (pendingFilterReconcileRef.current === watchedSource) { pendingFilterReconcileRef.current = null; - const dropped = searchFilters.retainFiltersByColumns(allowed); + const dropped = searchFilters.retainFiltersByColumns( + allowed, + getSeverityClassKey(watchedSourceObj) ?? null, + ); if (dropped.length > 0) { notifications.show({ color: 'yellow', diff --git a/packages/app/src/NamespaceDetailsSidePanel.tsx b/packages/app/src/NamespaceDetailsSidePanel.tsx index 5dbb278bd6..a43f202571 100644 --- a/packages/app/src/NamespaceDetailsSidePanel.tsx +++ b/packages/app/src/NamespaceDetailsSidePanel.tsx @@ -24,6 +24,7 @@ import { DrawerBody, DrawerHeader } from '@/components/DrawerUtils'; import { useQueriedChartConfig } from '@/hooks/useChartConfig'; import { InfraPodsStatusTable } from '@/KubernetesDashboardPage'; import { getEventBody } from '@/source'; +import { getLogsResultTypeQuery } from '@/utils/severityClass'; import { useZIndex, ZIndexContext } from '@/zIndex'; import DBSqlRowTableWithSideBar from './components/DBSqlRowTableWithSidebar'; @@ -126,8 +127,6 @@ function NamespaceLogs({ }) { const [resultType, setResultType] = React.useState<'all' | 'error'>('all'); - const _where = where + (resultType === 'error' ? ' Severity:err' : ''); - return ( @@ -166,7 +165,7 @@ function NamespaceLogs({ sourceId={logSource.id} config={{ ...logSource, - where: _where, + ...getLogsResultTypeQuery({ source: logSource, where, resultType }), whereLanguage: 'lucene', select: [ { diff --git a/packages/app/src/NodeDetailsSidePanel.tsx b/packages/app/src/NodeDetailsSidePanel.tsx index 20ab1ff272..4fb87b961b 100644 --- a/packages/app/src/NodeDetailsSidePanel.tsx +++ b/packages/app/src/NodeDetailsSidePanel.tsx @@ -24,6 +24,7 @@ import { DrawerBody, DrawerHeader } from '@/components/DrawerUtils'; import { InfraPodsStatusTable } from '@/KubernetesDashboardPage'; import { getEventBody } from '@/source'; import { formatUptime } from '@/utils'; +import { getLogsResultTypeQuery } from '@/utils/severityClass'; import { useZIndex, ZIndexContext } from '@/zIndex'; import DBSqlRowTableWithSideBar from './components/DBSqlRowTableWithSidebar'; @@ -139,8 +140,6 @@ function NodeLogs({ }) { const [resultType, setResultType] = React.useState<'all' | 'error'>('all'); - const _where = where + (resultType === 'error' ? ' Severity:err' : ''); - return ( @@ -179,7 +178,7 @@ function NodeLogs({ sourceId={logSource.id} config={{ ...logSource, - where: _where, + ...getLogsResultTypeQuery({ source: logSource, where, resultType }), whereLanguage: 'lucene', select: [ { diff --git a/packages/app/src/PodDetailsSidePanel.tsx b/packages/app/src/PodDetailsSidePanel.tsx index f0ec35707c..946ce817ad 100644 --- a/packages/app/src/PodDetailsSidePanel.tsx +++ b/packages/app/src/PodDetailsSidePanel.tsx @@ -25,6 +25,7 @@ import { DBTimeChart } from '@/components/DBTimeChart'; import { DrawerBody, DrawerHeader } from '@/components/DrawerUtils'; import { KubeTimeline, useV2LogBatch } from '@/components/KubeComponents'; import { WithClause } from '@/hooks/useRowWhere'; +import { getLogsResultTypeQuery } from '@/utils/severityClass'; import { useZIndex, ZIndexContext } from '@/zIndex'; import DBSqlRowTableWithSideBar from './components/DBSqlRowTableWithSidebar'; @@ -136,13 +137,11 @@ function PodLogs({ }) { const [resultType, setResultType] = React.useState<'all' | 'error'>('all'); - const _where = where + (resultType === 'error' ? ' Severity:err' : ''); - // Create a properly typed config object for DBSqlRowTable const tableConfig = React.useMemo(() => { return { from: logSource.from, - where: _where, + ...getLogsResultTypeQuery({ source: logSource, where, resultType }), whereLanguage: 'lucene' as const, timestampValueExpression: logSource.timestampValueExpression, implicitColumnExpression: logSource.implicitColumnExpression, @@ -175,7 +174,7 @@ function PodLogs({ limit: { limit: 200, offset: 0 }, dateRange, }; - }, [_where, dateRange, logSource]); + }, [where, resultType, dateRange, logSource]); return ( diff --git a/packages/app/src/__tests__/searchFilters.test.ts b/packages/app/src/__tests__/searchFilters.test.ts index a6eec0c225..6506a3fb13 100644 --- a/packages/app/src/__tests__/searchFilters.test.ts +++ b/packages/app/src/__tests__/searchFilters.test.ts @@ -1,4 +1,5 @@ import { enableMapSet } from 'immer'; +import { buildSeverityClassExpressionV1 } from '@hyperdx/common-utils/dist/core/severity'; import { filtersToQuery } from '@hyperdx/common-utils/dist/filters'; import { act, renderHook } from '@testing-library/react'; @@ -1252,6 +1253,83 @@ describe('searchFilters', () => { { type: 'sql', condition: `ServiceName IN ('app')` }, ]); }); + + describe('with a severity class key', () => { + const OLD = buildSeverityClassExpressionV1({ + numberExpr: 'OldSeverityNumber', + textExpr: 'SeverityText', + }); + const NEW = buildSeverityClassExpressionV1({ + numberExpr: 'SeverityNumber', + textExpr: 'SeverityText', + }); + const sql = (condition: string): ConditionFilter => ({ + type: 'sql', + condition, + }); + + it.each([ + [ + 'renames a stale key', + [sql(`${OLD} IN ('ERROR')`), sql(`ServiceName IN ('app')`)], + NEW, + [], + [sql(`${NEW} IN ('ERROR')`), sql(`ServiceName IN ('app')`)], + ], + [ + 'keeps the current key without a URL update', + [sql(`${NEW} IN ('ERROR')`)], + NEW, + [], + null, + ], + [ + 'drops class keys for a source without one', + [sql(`${OLD} IN ('ERROR')`), sql(`ServiceName IN ('app')`)], + null, + [OLD], + [sql(`ServiceName IN ('app')`)], + ], + [ + 'merges a stale key into the current one, the later key winning', + [ + sql(`${OLD} IN ('ERROR', 'WARN')`), + sql(`${NEW} IN ('FATAL')`), + sql(`${NEW} NOT IN ('ERROR')`), + ], + NEW, + [], + [ + sql(`${NEW} IN ('WARN', 'FATAL')`), + sql(`${NEW} NOT IN ('ERROR')`), + ], + ], + ])('%s', (_, searchQuery, classKey, expectedDropped, expectedUrl) => { + const onFilterChangeLocal = jest.fn(); + const { result } = renderHook(() => + useSearchPageFilterState({ + searchQuery, + onFilterChange: onFilterChangeLocal, + knownColumns: new Set(), + }), + ); + + let dropped: string[] = []; + act(() => { + dropped = result.current.retainFiltersByColumns( + new Set(['ServiceName']), + classKey, + ); + }); + + expect(dropped).toEqual(expectedDropped); + if (expectedUrl == null) { + expect(onFilterChangeLocal).not.toHaveBeenCalled(); + } else { + expect(onFilterChangeLocal).toHaveBeenLastCalledWith(expectedUrl); + } + }); + }); }); }); }); diff --git a/packages/app/src/__tests__/utils.test.ts b/packages/app/src/__tests__/utils.test.ts index 0287178aaf..875fb4753d 100644 --- a/packages/app/src/__tests__/utils.test.ts +++ b/packages/app/src/__tests__/utils.test.ts @@ -1553,8 +1553,11 @@ describe('getLogSeverityLevel', () => { [undefined, 'Warning', 'warn'], ['', 'fatal', 'error'], [undefined, 'info', undefined], - [25, 'warn', 'warn'], + [25, 'warn', 'error'], [0, 'panic', 'error'], + [0, 'SEVERE', 'error'], + [0, 'WRN', 'warn'], + [0, 'dbug', undefined], ])('maps %p and %p to %p', (severityNumber, severityText, expected) => { expect(getLogSeverityLevel({ severityNumber, severityText })).toBe( expected, @@ -1563,8 +1566,8 @@ describe('getLogSeverityLevel', () => { }); describe('getLogLevelClass', () => { - // Log-only vocabulary that trace StatusCode and chart colors don't take. - const LOG_ONLY_PREFIXES = [ + // getLogLevelClass also colors chart series and predates these logger spellings. + const NEWER_PREFIXES = [ 'panic', 'dpanic', 'ftl', @@ -1573,15 +1576,15 @@ describe('getLogLevelClass', () => { 'inf', 'dbg', 'trc', + 'vrb', ]; - it.each(TEXT_PREFIX_RULES.filter(r => !LOG_ONLY_PREFIXES.includes(r.prefix)))( + it.each(TEXT_PREFIX_RULES.filter(r => !NEWER_PREFIXES.includes(r.prefix)))( 'agrees with the log severity class on $prefix', ({ prefix }) => { - const severityClass = getSeverityClass({ severityText: prefix }); expect(getLogLevelClass(prefix)).toBe( - collapseSeverityClass(severityClass) ?? - (severityClass === 'UNKNOWN' ? undefined : 'info'), + collapseSeverityClass(getSeverityClass({ severityText: prefix })) ?? + 'info', ); }, ); diff --git a/packages/app/src/components/ActiveFilterPills.tsx b/packages/app/src/components/ActiveFilterPills.tsx index 86d8234951..b9f9c9e0c2 100644 --- a/packages/app/src/components/ActiveFilterPills.tsx +++ b/packages/app/src/components/ActiveFilterPills.tsx @@ -1,7 +1,12 @@ import { memo, useCallback, useEffect, useMemo, useRef, useState } from 'react'; +import { + isSeverityClassKeyV1, + SEVERITY_CLASS_FACET_VALUES, +} from '@hyperdx/common-utils/dist/core/severity'; import type { BuilderChartConfigWithDateRange } from '@hyperdx/common-utils/dist/types'; import { Flex, FlexProps, Text } from '@mantine/core'; +import { SEVERITY_CLASS_FACET_NAME } from '@/components/DBSearchPageFilters/utils'; import { FilterPill, FilterValueEditor } from '@/components/FilterPill'; import { useGetKeyValues } from '@/hooks/useMetadata'; import type { FilterStateHook } from '@/searchFilters'; @@ -15,6 +20,8 @@ const VALUE_EDIT_LIMIT = 50; // flattenFilters useMemo on every render. const EMPTY_DATE_TIME_COLUMNS: ReadonlyMap = new Map(); +const SEVERITY_CLASS_VALUE_OPTIONS = [...SEVERITY_CLASS_FACET_VALUES]; + type FormatTime = ReturnType; type PillItem = { @@ -94,6 +101,8 @@ function SearchFilterPill({ // (column missing on the active source) can only be removed. const isEditable = pill.type !== 'range' && !isInvalid; const [opened, setOpened] = useState(false); + // By shape, so stale class keys read as Severity too. + const isSeverityClass = isSeverityClassKeyV1(pill.field); // The picker lists values to switch this pill to, so it must not be scoped // by the active query or by the pill's own filter. Reusing chartConfig @@ -112,13 +121,20 @@ function SearchFilterPill({ keys: [pill.field], limit: VALUE_EDIT_LIMIT, }, - { enabled: opened && isEditable }, + { enabled: opened && isEditable && !isSeverityClass }, + ); + const valueOptions = useMemo( + () => + isSeverityClass + ? SEVERITY_CLASS_VALUE_OPTIONS + : (keyValues?.[0]?.value ?? []), + [isSeverityClass, keyValues], ); - const valueOptions = useMemo(() => keyValues?.[0]?.value ?? [], [keyValues]); + const field = isSeverityClass ? SEVERITY_CLASS_FACET_NAME : pill.field; return ( void; onClickOnly?: VoidFunction; onClickExclude?: VoidFunction; - onClickPin: VoidFunction; + onClickPin?: VoidFunction; onClickSharedPin?: VoidFunction; className?: string; percentage?: number; @@ -314,26 +317,30 @@ const FilterCheckbox = ({ data-testid={`${testIdPrefix}-exclude`} /> )} - + )} + + {onClickPin && ( + - - + )} ); }; @@ -414,7 +421,8 @@ export type FilterGroupProps = { onClearClick: VoidFunction; onOnlyClick: (value: string | boolean) => void; onExcludeClick: (value: string | boolean) => void; - valuePins: ValuePinHandlers; + /** Omit to hide value pin controls. */ + valuePins?: ValuePinHandlers; fieldPins?: FieldPinHandlers; onColumnToggle?: VoidFunction; isColumnDisplayed?: boolean; @@ -432,6 +440,8 @@ export type FilterGroupProps = { isLive?: boolean; onRangeChange?: (range: { min: number; max: number }) => void; distributionKey?: string; + /** Options are the complete, ordered list: keep their order, hide search. */ + fixedOptions?: boolean; }; /** @@ -460,6 +470,7 @@ const FilterGroupBody = ({ showDistributions, onDistributionError, onFetchingDistributionChange, + fixedOptions, }: { name: string; options: { value: string | boolean; label: string }[]; @@ -468,8 +479,8 @@ const FilterGroupBody = ({ onChange: (value: string | boolean) => void; onOnlyClick: (value: string | boolean) => void; onExcludeClick: (value: string | boolean) => void; - isPinned: (value: string | boolean) => boolean; - onPinClick: (value: string | boolean) => void; + isPinned?: (value: string | boolean) => boolean; + onPinClick?: (value: string | boolean) => void; isSharedPinned?: (value: string | boolean) => boolean; onSharedPinClick?: (value: string | boolean) => void; onLoadMore: (key: string) => void; @@ -481,6 +492,7 @@ const FilterGroupBody = ({ showDistributions: boolean; onDistributionError: () => void; onFetchingDistributionChange: (isFetching: boolean) => void; + fixedOptions?: boolean; }) => { const [search, setSearch] = useState(''); // "Show More" button when there's lots of options @@ -569,6 +581,8 @@ const FilterGroupBody = ({ // Options matching search, sorted appropriately const sortedMatchingOptions = useMemo(() => { + if (fixedOptions) return augmentedOptions; + // When searching, sort alphabetically if (search) { return augmentedOptions @@ -586,11 +600,11 @@ const FilterGroupBody = ({ // When not searching, sort by personal pinned, shared pinned, selected, // distribution, then alphabetically return augmentedOptions.toSorted((a, b) => { - const aPinned = isPinned(a.value); + const aPinned = isPinned?.(a.value) ?? false; const aShared = isSharedPinned?.(a.value) ?? false; const aIncluded = selectedValues.included.has(a.value); const aExcluded = selectedValues.excluded.has(a.value); - const bPinned = isPinned(b.value); + const bPinned = isPinned?.(b.value) ?? false; const bShared = isSharedPinned?.(b.value) ?? false; const bIncluded = selectedValues.included.has(b.value); const bExcluded = selectedValues.excluded.has(b.value); @@ -622,6 +636,7 @@ const FilterGroupBody = ({ return a.label.localeCompare(b.label, undefined, { numeric: true }); }); }, [ + fixedOptions, search, augmentedOptions, isPinned, @@ -684,7 +699,7 @@ const FilterGroupBody = ({ return ( - {augmentedOptions.length > FILTER_SEARCH_MIN_ITEMS && ( + {!fixedOptions && augmentedOptions.length > FILTER_SEARCH_MIN_ITEMS && ( handleChange(option.value)} onClickOnly={() => onOnlyClick(option.value)} onClickExclude={() => onExcludeClick(option.value)} - onClickPin={() => onPinClick(option.value)} + onClickPin={onPinClick ? () => onPinClick(option.value) : undefined} sharedPinned={isSharedPinned?.(option.value)} onClickSharedPin={ onSharedPinClick ? () => onSharedPinClick(option.value) : undefined @@ -809,7 +824,7 @@ type FilterGroupActionsProps = { totalAppliedFiltersSize: number; toggleShowDistributions: VoidFunction; onColumnToggle?: VoidFunction; - onFieldPinClick: VoidFunction; + onFieldPinClick?: VoidFunction; onToggleSharedFieldPin: VoidFunction; onClearClick: VoidFunction; }; @@ -886,12 +901,14 @@ function FilterGroupActions({ )} - + {onFieldPinClick && ( + + )} )} {totalAppliedFiltersSize > 0 && ( @@ -945,6 +962,7 @@ export const FilterGroup = ({ isLive, distributionKey, onRangeChange, + fixedOptions, }: FilterGroupProps) => { const [uncontrolledExpanded, setUncontrolledExpanded] = useState( isDefaultExpanded ?? false, @@ -1059,7 +1077,9 @@ export const FilterGroup = ({ isSharedFieldPinned={fieldPins?.isSharedFieldPinned ?? false} toggleShowDistributions={toggleShowDistributions} onColumnToggle={onColumnToggle} - onFieldPinClick={fieldPins?.onFieldPinClick ?? voidFunc} + onFieldPinClick={ + fieldPins && (fieldPins.onFieldPinClick ?? voidFunc) + } onToggleSharedFieldPin={ fieldPins?.onToggleSharedFieldPin ?? voidFunc } @@ -1090,10 +1110,10 @@ export const FilterGroup = ({ onChange={onChange} onOnlyClick={onOnlyClick} onExcludeClick={onExcludeClick} - isPinned={valuePins.isPinned} - onPinClick={valuePins.onPinClick} - isSharedPinned={valuePins.isSharedPinned} - onSharedPinClick={valuePins.onSharedPinClick} + isPinned={valuePins?.isPinned} + onPinClick={valuePins?.onPinClick} + isSharedPinned={valuePins?.isSharedPinned} + onSharedPinClick={valuePins?.onSharedPinClick} onLoadMore={onLoadMore} loadMoreLoading={loadMoreLoading} hasLoadedMore={hasLoadedMore} @@ -1103,6 +1123,7 @@ export const FilterGroup = ({ showDistributions={showDistributions} onDistributionError={onDistributionError} onFetchingDistributionChange={setIsFetchingDistribution} + fixedOptions={fixedOptions} /> ) )} @@ -1197,6 +1218,12 @@ const DBSearchPageFiltersComponent = ({ const { data: source } = useSource({ id: sourceId }); const sourceTableConnection = tcFromSource(source); + const severityClassKey = useMemo(() => getSeverityClassKey(source), [source]); + // Includes stale class keys from the source's earlier severity expressions. + const severityClassFilterKeys = useMemo( + () => Object.keys(filterState).filter(isSeverityClassKeyV1), + [filterState], + ); const { data: jsonColumns } = useJsonColumns(sourceTableConnection); // Special case for live tail @@ -1358,10 +1385,13 @@ const DBSearchPageFiltersComponent = ({ _facets.push(facet); } } - // get remaining filterState that are not in _facets + // get remaining filterState that are not in _facets. Class keys stay out + // so a stale one shows as a Severity pill, not a facet named by its SQL. const remainingFilterState = Object.keys(filterState).filter( key => - !_facets.some(facet => facet.key === key) && !sharedFilterKeys.has(key), + !isSeverityClassKeyV1(key) && + !_facets.some(facet => facet.key === key) && + !sharedFilterKeys.has(key), ); for (const key of remainingFilterState) { _facets.push({ @@ -1435,13 +1465,15 @@ const DBSearchPageFiltersComponent = ({ // Check if non-shared facets have active selections const showFiltersClearButton = useMemo( () => - shownFacets.some(facet => { - const f = filterState[facet.key]; - return ( - f && (f.included.size > 0 || f.excluded.size > 0 || f.range != null) - ); - }), - [shownFacets, filterState], + [...shownFacets.map(facet => facet.key), ...severityClassFilterKeys].some( + key => { + const f = filterState[key]; + return ( + f && (f.included.size > 0 || f.excluded.size > 0 || f.range != null) + ); + }, + ), + [shownFacets, filterState, severityClassFilterKeys], ); const clearSharedSelections = useCallback(() => { @@ -1454,7 +1486,10 @@ const DBSearchPageFiltersComponent = ({ for (const facet of shownFacets) { clearFilter(facet.key); } - }, [shownFacets, clearFilter]); + for (const key of severityClassFilterKeys) { + clearFilter(key); + } + }, [shownFacets, clearFilter, severityClassFilterKeys]); const parentSpanIdExpr = source?.kind === SourceKind.Trace @@ -1923,13 +1958,26 @@ const DBSearchPageFiltersComponent = ({ /> )} + {severityClassKey && ( + + )} {shownFacets.length === 0 && (isFacetsLoading ? ( ) : ( - No filters available + !severityClassKey && ( + No filters available + ) ))} {/* Filters show their own loader until their values arrive */} {renderFacetList(shownFacets)} diff --git a/packages/app/src/components/DBSearchPageFilters/SeverityClassFilterGroup.tsx b/packages/app/src/components/DBSearchPageFilters/SeverityClassFilterGroup.tsx new file mode 100644 index 0000000000..561d822c7b --- /dev/null +++ b/packages/app/src/components/DBSearchPageFilters/SeverityClassFilterGroup.tsx @@ -0,0 +1,60 @@ +import { SEVERITY_CLASS_FACET_VALUES } from '@hyperdx/common-utils/dist/core/severity'; +import type { FilterState } from '@hyperdx/common-utils/dist/filters'; +import type { BuilderChartConfigWithDateRange } from '@hyperdx/common-utils/dist/types'; + +import { FilterGroup } from '@/components/DBSearchPageFilters'; +import { SEVERITY_CLASS_FACET_NAME } from '@/components/DBSearchPageFilters/utils'; +import type { FilterStateHook } from '@/searchFilters'; + +const OPTIONS = SEVERITY_CLASS_FACET_VALUES.map(value => ({ + value, + label: value, +})); + +const noop = () => {}; + +/** The Severity facet: fixed class options, so it runs no values query. */ +export function SeverityClassFilterGroup({ + classKey, + filterState, + setFilterValue, + clearFilter, + chartConfig, + isLive, + showFilterCounts, +}: { + classKey: string; + filterState: FilterState; + setFilterValue: FilterStateHook['setFilterValue']; + clearFilter: FilterStateHook['clearFilter']; + chartConfig: BuilderChartConfigWithDateRange; + isLive?: boolean; + showFilterCounts?: boolean; +}) { + const selectedValues = filterState[classKey]; + const hasSelection = + selectedValues != null && + (selectedValues.included.size > 0 || selectedValues.excluded.size > 0); + + return ( + setFilterValue(classKey, value)} + onClearClick={() => clearFilter(classKey)} + onOnlyClick={value => setFilterValue(classKey, value, 'only')} + onExcludeClick={value => setFilterValue(classKey, value, 'exclude')} + onLoadMore={noop} + loadMoreLoading={false} + hasLoadedMore + isDefaultExpanded={hasSelection} + chartConfig={chartConfig} + isLive={isLive} + /> + ); +} diff --git a/packages/app/src/components/DBSearchPageFilters/utils.ts b/packages/app/src/components/DBSearchPageFilters/utils.ts index 6d7cb1d56d..634d60b0fd 100644 --- a/packages/app/src/components/DBSearchPageFilters/utils.ts +++ b/packages/app/src/components/DBSearchPageFilters/utils.ts @@ -10,6 +10,8 @@ import type { FilterState } from '@hyperdx/common-utils/dist/filters'; import type { Facet } from '@/hooks/useMetadata'; import { mergePath } from '@/utils'; +export const SEVERITY_CLASS_FACET_NAME = 'Severity'; + /** A filter key and its values, as the sidebar lists it. */ export type FacetEntry = { key: string; diff --git a/packages/app/src/components/Sources/SourceForm/ExpressionFormRow.tsx b/packages/app/src/components/Sources/SourceForm/ExpressionFormRow.tsx index e298d18312..d825f65f91 100644 --- a/packages/app/src/components/Sources/SourceForm/ExpressionFormRow.tsx +++ b/packages/app/src/components/Sources/SourceForm/ExpressionFormRow.tsx @@ -23,6 +23,7 @@ export function ExpressionFormRow({ columns, sourceKind, tableConnection, + validationExpression, }: { control: Control; setValue: UseFormSetValue; @@ -37,6 +38,8 @@ export function ExpressionFormRow({ tableName: string; connectionId: string; }; + /** Wraps the value before validating it, to check how it will be used. */ + validationExpression?: (expression: string) => string; }) { const currentValue = useWatch({ control, name }); const value = typeof currentValue === 'string' ? currentValue : ''; @@ -59,7 +62,7 @@ export function ExpressionFormRow({ /> {value.trim() ? ( ) : ( diff --git a/packages/app/src/components/Sources/SourceForm/LogTableModelForm.tsx b/packages/app/src/components/Sources/SourceForm/LogTableModelForm.tsx index 9dfb107266..3884992256 100644 --- a/packages/app/src/components/Sources/SourceForm/LogTableModelForm.tsx +++ b/packages/app/src/components/Sources/SourceForm/LogTableModelForm.tsx @@ -151,6 +151,10 @@ export function LogTableModelForm(props: TableModelProps) { columns={columns} sourceKind={SourceKind.Log} tableConnection={tableConnection} + // The Severity filter compares it with numbers, which ClickHouse + // rejects for a String; flag that here rather than on the LLM and + // K8s pages, which apply the filter automatically. + validationExpression={expression => `${expression} >= 1`} /> { ); }); }); + + describe('Severity class pills', () => { + const CLASS_KEY = buildSeverityClassExpressionV1({ + numberExpr: 'OldSeverityNumber', + textExpr: "LogAttributes['level']", + }); + + it('shows "Severity = ERROR" and picks from the fixed classes without a values query', async () => { + jest.useRealTimers(); + const user = userEvent.setup(); + const searchFilters = makeSearchFilters({ + [CLASS_KEY]: { + included: new Set(['ERROR']), + excluded: new Set(), + }, + }); + renderWithMantine( + , + ); + + const pill = screen.getByTestId('active-filter-pill-Severity'); + expect(pill).toHaveTextContent('Severity = ERROR'); + expect(screen.queryByText(CLASS_KEY)).not.toBeInTheDocument(); + + await user.click(pill); + await user.click(await screen.findByLabelText('Change filter value')); + + const options = await screen.findAllByRole('option', { hidden: true }); + expect(options.map(option => option.textContent).sort()).toEqual( + [...SEVERITY_CLASS_FACET_VALUES].sort(), + ); + const classQueries = mockedUseGetKeyValues.mock.calls.filter(([args]) => + args.keys.includes(CLASS_KEY), + ); + expect(classQueries.length).toBeGreaterThan(0); + expect(classQueries.map(([, options]) => options.enabled)).not.toContain( + true, + ); + }); + }); }); diff --git a/packages/app/src/components/__tests__/DBSearchPageFilters.test.tsx b/packages/app/src/components/__tests__/DBSearchPageFilters.test.tsx index 064f6a3b6d..498da1c44c 100644 --- a/packages/app/src/components/__tests__/DBSearchPageFilters.test.tsx +++ b/packages/app/src/components/__tests__/DBSearchPageFilters.test.tsx @@ -1,3 +1,4 @@ +import type { FilterState } from '@hyperdx/common-utils/dist/filters'; import { UseQueryResult } from '@tanstack/react-query'; import { screen, within } from '@testing-library/react'; import userEvent from '@testing-library/user-event'; @@ -8,6 +9,7 @@ import { type FilterGroupProps, } from '@/components/DBSearchPageFilters'; import { NestedFilterGroup } from '@/components/DBSearchPageFilters/NestedFilterGroup'; +import { SeverityClassFilterGroup } from '@/components/DBSearchPageFilters/SeverityClassFilterGroup'; import { cleanClickHouseExpression, groupFacetsByBaseName, @@ -853,6 +855,125 @@ describe('FilterGroup', () => { expect(screen.getByText('(empty)')).toBeInTheDocument(); }); + + describe('with fixedOptions', () => { + const fixedProps: FilterGroupProps = { + ...defaultProps, + name: 'Severity', + options: ['TRACE', 'DEBUG', 'INFO', 'WARN', 'ERROR', 'FATAL'].map( + value => ({ value, label: value }), + ), + fixedOptions: true, + valuePins: undefined, + }; + + it('keeps the given order over selections and distributions', async () => { + mockDistribution( + new Map([ + ['INFO', 80], + ['ERROR', 20], + ]), + ); + renderWithMantine( + , + ); + + await userEvent.click( + screen.getByTestId('toggle-distribution-button-Severity'), + ); + + expect( + screen + .getAllByTestId(/^filter-checkbox-Severity-[A-Z]+$/) + .map(row => row.dataset.testid), + ).toEqual( + ['TRACE', 'DEBUG', 'INFO', 'WARN', 'ERROR', 'FATAL'].map( + v => `filter-checkbox-Severity-${v}`, + ), + ); + expect( + screen.getByTestId('filter-distribution-Severity-INFO'), + ).toHaveTextContent('~80%'); + }); + + it('shows no value search or pin controls', () => { + renderWithMantine(); + + expect( + screen.queryByPlaceholderText('Search values...'), + ).not.toBeInTheDocument(); + expect(screen.queryByLabelText('Pin value')).not.toBeInTheDocument(); + expect(screen.queryByLabelText('Pin')).not.toBeInTheDocument(); + }); + }); +}); + +describe('SeverityClassFilterGroup', () => { + const classKey = "if(SeverityNumber > 0, 'INFO', 'OTHER')"; + const renderGroup = (filterState: FilterState, setFilterValue = jest.fn()) => + renderWithMantine( + , + ); + + it('starts collapsed without a selection', () => { + renderGroup({}); + + expect( + screen.queryByTestId('filter-checkbox-Severity-ERROR'), + ).not.toBeInTheDocument(); + }); + + it('opens on a selection and filters through the class key', async () => { + const setFilterValue = jest.fn(); + renderGroup( + { [classKey]: { included: new Set(['ERROR']), excluded: new Set() } }, + setFilterValue, + ); + + expect( + screen.getByTestId('filter-checkbox-Severity-ERROR-input'), + ).toBeChecked(); + expect( + screen.queryByTestId('filter-load-more-Severity'), + ).not.toBeInTheDocument(); + + await userEvent.click(screen.getByText('WARN')); + expect(setFilterValue).toHaveBeenLastCalledWith(classKey, 'WARN'); + + await userEvent.click( + screen.getByTestId('filter-checkbox-Severity-FATAL-exclude'), + ); + expect(setFilterValue).toHaveBeenLastCalledWith( + classKey, + 'FATAL', + 'exclude', + ); + + await userEvent.click( + screen.getByTestId('toggle-distribution-button-Severity'), + ); + expect(jest.mocked(useGetValuesDistribution)).toHaveBeenLastCalledWith( + expect.objectContaining({ key: classKey }), + expect.objectContaining({ enabled: true }), + ); + }); }); describe('NestedFilterGroup', () => { diff --git a/packages/app/src/components/__tests__/DBSearchPageFiltersSeverity.test.tsx b/packages/app/src/components/__tests__/DBSearchPageFiltersSeverity.test.tsx new file mode 100644 index 0000000000..dd3f193aa8 --- /dev/null +++ b/packages/app/src/components/__tests__/DBSearchPageFiltersSeverity.test.tsx @@ -0,0 +1,139 @@ +import { buildSeverityClassExpressionV1 } from '@hyperdx/common-utils/dist/core/severity'; +import type { FilterState } from '@hyperdx/common-utils/dist/filters'; +import { + type BuilderChartConfigWithDateRange, + SourceKind, + type TLogSource, +} from '@hyperdx/common-utils/dist/types'; +import { screen } from '@testing-library/react'; +import userEvent from '@testing-library/user-event'; + +import { DBSearchPageFilters } from '@/components/DBSearchPageFilters'; + +const logSource: TLogSource = { + id: 'source-id', + kind: SourceKind.Log, + name: 'logs', + connection: 'conn', + from: { databaseName: 'default', tableName: 'otel_logs' }, + timestampValueExpression: 'Timestamp', + defaultTableSelectExpression: 'Timestamp, Body', + severityTextExpression: 'SeverityText', + severityNumberExpression: 'SeverityNumber', +}; + +const CLASS_KEY = buildSeverityClassExpressionV1({ + numberExpr: 'SeverityNumber', + textExpr: 'SeverityText', +}); +// Built before the source's text expression was edited. +const STALE_CLASS_KEY = buildSeverityClassExpressionV1({ + numberExpr: 'SeverityNumber', + textExpr: "LogAttributes['level']", +}); + +jest.mock('@/source', () => ({ + ...jest.requireActual('@/source'), + useSource: () => ({ data: logSource }), +})); + +jest.mock('@/pinnedFilters', () => ({ + ...jest.requireActual('@/pinnedFilters'), + usePinnedFiltersApi: () => ({ data: undefined }), + useUpdatePinnedFilters: () => ({ mutate: jest.fn() }), +})); + +jest.mock('@/hooks/useMetadata', () => ({ + ...jest.requireActual('@/hooks/useMetadata'), + useColumns: () => ({ data: undefined }), + useJsonColumns: () => ({ data: undefined }), + useTableMetadata: () => ({ data: undefined }), + useGetValuesDistribution: () => ({ + data: undefined, + isFetching: false, + error: undefined, + }), +})); + +jest.mock('@/components/DBSearchPageFilters/hooks', () => ({ + ...jest.requireActual('@/components/DBSearchPageFilters/hooks'), + useFetchFacets: () => ({ + data: { + keyValues: [ + { key: 'ServiceName', value: ['api'] }, + { key: 'SeverityText', value: ['error'] }, + ], + pendingKeys: new Set(), + }, + isLoading: false, + isFetching: false, + error: null, + loadMoreFacetsForKey: jest.fn(), + loadMoreLoadingKeys: new Set(), + extraFacetKeys: new Set(), + }), +})); + +const chartConfig: BuilderChartConfigWithDateRange = { + connection: 'conn', + from: logSource.from, + select: '', + where: '', + timestampValueExpression: 'Timestamp', + dateRange: [new Date('2026-01-01'), new Date('2026-01-02')], +}; + +function renderFilters({ + filters = {}, + clearFilter = jest.fn(), +}: { filters?: FilterState; clearFilter?: jest.Mock } = {}) { + renderWithMantine( + , + ); +} + +const groupNames = () => + screen + .getAllByTestId('filter-group-control') + .map(el => el.textContent?.match(/^\w+/)?.[0]); + +describe('DBSearchPageFilters Severity facet', () => { + it('keeps class keys out of the raw facets, and clears them', async () => { + const user = userEvent.setup(); + const clearFilter = jest.fn(); + renderFilters({ + clearFilter, + filters: { + [CLASS_KEY]: { included: new Set(['ERROR']), excluded: new Set() }, + [STALE_CLASS_KEY]: { + included: new Set(['WARN']), + excluded: new Set(), + }, + }, + }); + + expect(groupNames()).toEqual(['Severity', 'ServiceName', 'SeverityText']); + + await user.click(screen.getByRole('button', { name: 'Clear Filters' })); + expect(clearFilter).toHaveBeenCalledWith(CLASS_KEY); + expect(clearFilter).toHaveBeenCalledWith(STALE_CLASS_KEY); + }); +}); diff --git a/packages/app/src/components/__tests__/LogTableModelForm.test.tsx b/packages/app/src/components/__tests__/LogTableModelForm.test.tsx new file mode 100644 index 0000000000..bf0fcd959d --- /dev/null +++ b/packages/app/src/components/__tests__/LogTableModelForm.test.tsx @@ -0,0 +1,59 @@ +import React from 'react'; +import { useForm } from 'react-hook-form'; +import { SourceKind, TSource } from '@hyperdx/common-utils/dist/types'; +import { screen } from '@testing-library/react'; + +import { LogTableModelForm } from '@/components/Sources/SourceForm/LogTableModelForm'; + +import '@testing-library/jest-dom'; + +jest.mock('@/components/Sources/SourceForm/ExpressionValidationStatus', () => ({ + ExpressionValidationStatus: ({ expression }: { expression: string }) => ( +
{expression}
+ ), +})); +jest.mock('@/components/SQLEditor/SQLInlineEditor', () => ({ + SQLInlineEditorControlled: () =>
, +})); +jest.mock('@/components/SourceSelect', () => ({ + SourceSelectControlled: () =>
, +})); +jest.mock('@/hooks/useMetadata', () => ({ + useColumns: () => ({ data: undefined }), +})); +jest.mock('@/theme/ThemeProvider', () => ({ + useBrandDisplayName: () => 'HyperDX', +})); +jest.mock('@/components/Sources/SourceForm/HighlightedAttributes', () => ({ + HighlightedAttributeExpressionsFormRow: () =>
, +})); +jest.mock('@/components/Sources/SourceForm/MaterializedViews', () => ({ + MaterializedViewsFormSection: () =>
, + MetadataMaterializedViewsFormSection: () =>
, +})); +jest.mock('@/components/Sources/SourceForm/OrderByFormRow', () => ({ + OrderByFormRow: () =>
, +})); +jest.mock('@/components/Sources/SourceForm/UseTextIndexFormRow', () => ({ + UseTextIndexFormRow: () =>
, +})); + +function Harness() { + const { control, setValue } = useForm({ + defaultValues: { + kind: SourceKind.Log, + connection: 'conn-1', + from: { databaseName: 'default', tableName: 'otel_logs' }, + severityNumberExpression: "LogAttributes['severity_number']", + }, + }); + return ; +} + +it('validates the Severity Number Expression as a number', () => { + renderWithMantine(); + + expect(screen.getByTestId('validated')).toHaveTextContent( + "LogAttributes['severity_number'] >= 1", + ); +}); diff --git a/packages/app/src/llm/__tests__/expressions.test.ts b/packages/app/src/llm/__tests__/expressions.test.ts index 6643b13965..e4d75aa6eb 100644 --- a/packages/app/src/llm/__tests__/expressions.test.ts +++ b/packages/app/src/llm/__tests__/expressions.test.ts @@ -1,3 +1,4 @@ +import { buildSeverityClassExpressionV1 } from '@hyperdx/common-utils/dist/core/severity'; import { TLogSource, TTraceSource } from '@hyperdx/common-utils/dist/types'; import { makeLogSource, makeTraceSource } from '@/llm/__fixtures__/sources'; @@ -380,4 +381,18 @@ describe('getLLMLogExpressions', () => { ); expect(expressions.sessionId).toContain("LogAttributes['session.id']"); }); + + it.each([ + ['severity text without', LOG_SOURCE, "lower(SeverityText) = 'error'"], + [ + 'the FATAL and ERROR classes with', + makeLogSource({ severityNumberExpression: 'SeverityNumber' }), + `${buildSeverityClassExpressionV1({ + numberExpr: 'SeverityNumber', + textExpr: 'SeverityText', + })} IN ('FATAL', 'ERROR')`, + ], + ])('matches errors by %s a severity number', (_, source, expected) => { + expect(getLLMLogExpressions(source, []).isError).toBe(expected); + }); }); diff --git a/packages/app/src/llm/lib/expressions.ts b/packages/app/src/llm/lib/expressions.ts index 27140cde54..9d24242929 100644 --- a/packages/app/src/llm/lib/expressions.ts +++ b/packages/app/src/llm/lib/expressions.ts @@ -1,6 +1,8 @@ import { errorStatusPredicateSql } from '@hyperdx/common-utils/dist/core/spanPredicates'; import { TLogSource, TTraceSource } from '@hyperdx/common-utils/dist/types'; +import { getSeverityClassErrorCondition } from '@/utils/severityClass'; + import { generateCostSqlExpression } from './cost'; import { buildAnyKeyExistsSql, buildLLMSpanSqlPredicate } from './detect'; @@ -487,9 +489,12 @@ export function getLLMLogExpressions( * predicate of its own rather than reusing the span-status one: the extra * status value that predicate matches cannot appear in a severity-text * column, and the values severity text does use for errors are a - * different set. + * different set. Sources with a Severity Number Expression match the + * FATAL and ERROR severity classes instead. */ - isError: `lower(${severityText}) = 'error'`, + isError: + getSeverityClassErrorCondition(source) ?? + `lower(${severityText}) = 'error'`, /** * Log events that belong to LLM activity: explicit LLM markers or any * session id (covers tool_result / lifecycle events that carry a diff --git a/packages/app/src/searchFilters.tsx b/packages/app/src/searchFilters.tsx index e9f1e50258..6ea75782f6 100644 --- a/packages/app/src/searchFilters.tsx +++ b/packages/app/src/searchFilters.tsx @@ -1,5 +1,6 @@ import { useCallback, useEffect, useMemo, useRef, useState } from 'react'; import produce from 'immer'; +import { isSeverityClassKeyV1 } from '@hyperdx/common-utils/dist/core/severity'; import { type FilterState, filtersToQuery, @@ -289,20 +290,50 @@ export const useSearchPageFilterState = ({ }, [updateFilterQuery]); // Keep only filters whose root column appears in `allowedColumnNames`. + // Severity class keys, matched by shape, move to `severityClassKey` or drop + // when it is null. // Returns the keys of the filters that were dropped so callers can surface // a notice when filter state was thrown away (e.g. on source change). const retainFiltersByColumns = useCallback( - (allowedColumnNames: Set): string[] => { + ( + allowedColumnNames: Set, + severityClassKey?: string | null, + ): string[] => { const dropped: string[] = []; const kept: FilterState = {}; + let renamed = false; for (const [key, value] of Object.entries(filters)) { + if (severityClassKey !== undefined && isSeverityClassKeyV1(key)) { + if (severityClassKey === null) { + dropped.push(key); + continue; + } + renamed ||= severityClassKey !== key; + // A stale class key and the current one merge here. The later filter + // wins a value both set, so it isn't both IN and NOT IN. + const existing = kept[severityClassKey]; + kept[severityClassKey] = existing + ? { + included: new Set([ + ...[...existing.included].filter(v => !value.excluded.has(v)), + ...value.included, + ]), + excluded: new Set([ + ...[...existing.excluded].filter(v => !value.included.has(v)), + ...value.excluded, + ]), + range: existing.range ?? value.range, + } + : value; + continue; + } if (isKeyCompatibleWithColumns(key, allowedColumnNames)) { kept[key] = value; } else { dropped.push(key); } } - if (dropped.length > 0) { + if (dropped.length > 0 || renamed) { setFilters(kept); updateFilterQuery(kept); } diff --git a/packages/app/src/utils/__tests__/severityClass.test.ts b/packages/app/src/utils/__tests__/severityClass.test.ts new file mode 100644 index 0000000000..2968e66520 --- /dev/null +++ b/packages/app/src/utils/__tests__/severityClass.test.ts @@ -0,0 +1,114 @@ +import { buildSeverityClassExpressionV1 } from '@hyperdx/common-utils/dist/core/severity'; +import type { TSource } from '@hyperdx/common-utils/dist/types'; + +import { + cleanClickHouseExpression, + toQuotedClickHouseKeyExpression, +} from '@/components/DBSearchPageFilters/utils'; +import { makeLogSource, makeTraceSource } from '@/llm/__fixtures__/sources'; +import { + getLogsResultTypeQuery, + getSeverityClassKey, +} from '@/utils/severityClass'; + +const logSource = makeLogSource({ severityNumberExpression: 'SeverityNumber' }); + +const otelKey = buildSeverityClassExpressionV1({ + numberExpr: 'SeverityNumber', + textExpr: 'SeverityText', +}); + +describe('getSeverityClassKey', () => { + it('builds the key from the severity expressions', () => { + expect(getSeverityClassKey(logSource)).toBe(otelKey); + expect( + getSeverityClassKey({ + ...logSource, + knownColumnsListExpression: 'Timestamp, SeverityText, SeverityNumber', + }), + ).toBe(otelKey); + }); + + it('leaves out the text when the source has no severity text', () => { + expect( + getSeverityClassKey({ ...logSource, severityTextExpression: ' ' }), + ).toBe(buildSeverityClassExpressionV1({ numberExpr: 'SeverityNumber' })); + }); + + it.each<[string, TSource | undefined]>([ + ['no source', undefined], + ['a trace source', makeTraceSource()], + [ + 'no severity number expression', + { ...logSource, severityNumberExpression: undefined }, + ], + [ + 'a known columns list without the number', + { ...logSource, knownColumnsListExpression: 'Timestamp, SeverityText' }, + ], + [ + 'a known columns list without the text', + { ...logSource, knownColumnsListExpression: 'Timestamp, SeverityNumber' }, + ], + [ + 'a backtick in an expression', + { ...logSource, severityTextExpression: '`SeverityText`' }, + ], + [ + 'an AND in an expression', + { + ...logSource, + severityNumberExpression: 'if(a AND b, SeverityNumber, 0)', + }, + ], + [ + 'an OR in an expression', + { + ...logSource, + severityTextExpression: "if(SeverityText = '' or x, 'info', '')", + }, + ], + ])('has no key for %s', (_, source) => { + expect(getSeverityClassKey(source)).toBeUndefined(); + }); + + it('survives the URL filter key cleanup unchanged', () => { + expect(cleanClickHouseExpression(otelKey)).toBe(otelKey); + expect( + toQuotedClickHouseKeyExpression( + otelKey, + new Set(['SeverityNumber', 'SeverityText']), + ), + ).toBe(otelKey); + }); +}); + +describe('getLogsResultTypeQuery', () => { + const where = 'k8s.pod.name:"api"'; + const noNumber = { ...logSource, severityNumberExpression: undefined }; + + it.each([ + [ + 'class filter for errors on sources with the field', + logSource, + 'error', + { + where, + filters: [ + { type: 'sql', condition: `${otelKey} IN ('FATAL', 'ERROR')` }, + ], + }, + ], + [ + 'Lucene term for errors on sources without it', + noNumber, + 'error', + { where: `${where} Severity:err` }, + ], + ['no severity scope for all', logSource, 'all', { where }], + ] as const)('%s', (_, source, resultType, expected) => { + expect(getLogsResultTypeQuery({ source, where, resultType })).toEqual( + expected, + ); + }); +}); diff --git a/packages/app/src/utils/severityClass.ts b/packages/app/src/utils/severityClass.ts new file mode 100644 index 0000000000..7d80508ea6 --- /dev/null +++ b/packages/app/src/utils/severityClass.ts @@ -0,0 +1,83 @@ +import { + buildSeverityClassExpressionV1, + SEVERITY_CLASS_ERROR_VALUES, +} from '@hyperdx/common-utils/dist/core/severity'; +import { splitAndTrimWithBracket } from '@hyperdx/common-utils/dist/core/utils'; +import { filtersToQuery } from '@hyperdx/common-utils/dist/filters'; +import { Filter, isLogSource, TSource } from '@hyperdx/common-utils/dist/types'; + +import { + getKnownColumnsList, + getSelectableSeverityNumberExpression, +} from '@/hooks/useMaterializedAliasColumnsOption'; + +// Backticks don't survive the URL filter round trip (cleanClickHouseExpression +// strips them), and the saved-search API rejects keys with a bare AND/OR/NOT. +function isUsableInFilterKey(expression: string): boolean { + return !expression.includes('`') && !/\b(AND|OR|NOT)\b/i.test(expression); +} + +/** + * The Severity facet's filter key for a log source, or undefined when the + * source has no usable Severity Number Expression. It reads only the saved + * source config, so it doesn't change while column metadata loads. + */ +export function getSeverityClassKey(source?: TSource): string | undefined { + if (source == null || !isLogSource(source)) return undefined; + + const numberExpr = getSelectableSeverityNumberExpression(source); + if (numberExpr == null) return undefined; + + const textExpr = source.severityTextExpression?.trim(); + const knownColumns = getKnownColumnsList(source); + // The key reads the text too, which a Distributed or Merge source restricted + // to its Known Columns List may not be able to select. + if ( + textExpr && + knownColumns != null && + !splitAndTrimWithBracket(knownColumns).includes(textExpr) + ) { + return undefined; + } + + if (![numberExpr, textExpr ?? ''].every(isUsableInFilterKey)) { + return undefined; + } + + return buildSeverityClassExpressionV1({ numberExpr, textExpr }); +} + +/** A SQL condition matching FATAL and ERROR rows, in the facet's format. */ +export function getSeverityClassErrorCondition( + source?: TSource, +): string | undefined { + const key = getSeverityClassKey(source); + if (key == null) return undefined; + + return filtersToQuery({ + [key]: { + included: new Set(SEVERITY_CLASS_ERROR_VALUES), + excluded: new Set(), + }, + })[0].condition; +} + +/** + * The where and filters for a K8s panel's logs table. Spread both into the + * config: on sources without the field, 'error' falls back to the Lucene term. + */ +export function getLogsResultTypeQuery({ + source, + where, + resultType, +}: { + source?: TSource; + where: string; + resultType: 'all' | 'error'; +}): { where: string; filters?: Filter[] } { + if (resultType !== 'error') return { where }; + const condition = getSeverityClassErrorCondition(source); + return condition == null + ? { where: `${where} Severity:err` } + : { where, filters: [{ type: 'sql', condition }] }; +} diff --git a/packages/app/tests/e2e/features/search/severity-class-filter.spec.ts b/packages/app/tests/e2e/features/search/severity-class-filter.spec.ts new file mode 100644 index 0000000000..56c52f14d9 --- /dev/null +++ b/packages/app/tests/e2e/features/search/severity-class-filter.spec.ts @@ -0,0 +1,85 @@ +import { SearchPage } from '../../page-objects/SearchPage'; +import { SEVERITY_CLASS_ROWS } from '../../seed-clickhouse'; +import { expect, test } from '../../utils/base-test'; +import { + DEFAULT_LOGS_SOURCE_NAME, + SEVERITY_CLASS_LOGS_SOURCE_NAME, +} from '../../utils/constants'; + +const FACET = 'Severity'; +const QUERY_TIMEOUT = 20_000; + +const errorRows = SEVERITY_CLASS_ROWS.filter(r => r.severityClass === 'ERROR'); +// SeverityText says error, but the SeverityNumber class wins. +const numberWinsRows = SEVERITY_CLASS_ROWS.filter( + r => r.severityClass !== 'ERROR' && r.text === 'error', +); + +test.describe('Severity class filter', { tag: ['@search'] }, () => { + test('filters by class and restores the filter from a saved search', async ({ + page, + }) => { + const searchPage = new SearchPage(page); + const rows = searchPage.table.getRows(); + let savedSearchUrl = ''; + + const expectErrorRows = async () => { + await expect(searchPage.getTableError()).toHaveCount(0); + await expect(rows).toHaveCount(errorRows.length, { + timeout: QUERY_TIMEOUT, + }); + for (const row of errorRows) { + await expect(rows.filter({ hasText: row.body })).toHaveCount(1); + } + for (const row of numberWinsRows) { + await expect(rows.filter({ hasText: row.body })).toHaveCount(0); + } + await expect(searchPage.getActiveFilterPill(FACET)).toHaveText( + 'Severity = ERROR', + ); + }; + + await test.step('Select the severity class source', async () => { + await searchPage.goto(); + await searchPage.selectSource(SEVERITY_CLASS_LOGS_SOURCE_NAME); + await searchPage.timePicker.selectRelativeTime('Last 1 days'); + await expect(rows).toHaveCount(SEVERITY_CLASS_ROWS.length, { + timeout: QUERY_TIMEOUT, + }); + }); + + await test.step('Selecting ERROR keeps number and text matches', async () => { + await searchPage.filters.openFilterGroup(FACET); + await searchPage.filters.applyFilter(FACET, 'ERROR'); + await searchPage.submitButton.click(); + await expectErrorRows(); + }); + + await test.step('Save the search', async () => { + await searchPage.openSaveSearchModal(); + await searchPage.savedSearchModal.saveSearchAndWaitForNavigation( + `Severity class filter ${Date.now()}`, + ); + savedSearchUrl = page.url().split('?')[0]; + }); + + await test.step('Reopening the saved search restores the filter', async () => { + await searchPage.goto(); + await page.goto(savedSearchUrl); + // The group opens itself when it has a selection; openFilterGroup toggles. + await expect( + searchPage.filters.getFilterCheckboxInput(FACET, 'ERROR'), + ).toBeChecked({ timeout: QUERY_TIMEOUT }); + await expectErrorRows(); + }); + + await test.step('A source without a severity number drops the filter', async () => { + await searchPage.selectSource(DEFAULT_LOGS_SOURCE_NAME); + await expect(searchPage.getDroppedFiltersToast()).toBeVisible({ + timeout: 10_000, + }); + await expect(searchPage.filters.getFilterGroup(FACET)).toHaveCount(0); + await expect(searchPage.getActiveFilterPill(FACET)).toHaveCount(0); + }); + }); +}); diff --git a/packages/app/tests/e2e/fixtures/e2e-fixtures.json b/packages/app/tests/e2e/fixtures/e2e-fixtures.json index fa6681e1dc..65a88d8f07 100644 --- a/packages/app/tests/e2e/fixtures/e2e-fixtures.json +++ b/packages/app/tests/e2e/fixtures/e2e-fixtures.json @@ -275,6 +275,27 @@ "tableName": "e2e_promql" }, "timestampValueExpression": "timestamp" + }, + { + "id": "E2E Severity Class Logs", + "kind": "log", + "name": "E2E Severity Class Logs", + "connection": "local", + "from": { + "databaseName": "default", + "tableName": "e2e_severity_class_logs" + }, + "timestampValueExpression": "Timestamp", + "defaultTableSelectExpression": "Timestamp, ServiceName, SeverityText, Body", + "serviceNameExpression": "ServiceName", + "severityTextExpression": "SeverityText", + "severityNumberExpression": "SeverityNumber", + "eventAttributesExpression": "LogAttributes", + "resourceAttributesExpression": "ResourceAttributes", + "traceIdExpression": "TraceId", + "spanIdExpression": "SpanId", + "implicitColumnExpression": "Body", + "displayedTimestampValueExpression": "Timestamp" } ] } diff --git a/packages/app/tests/e2e/page-objects/SearchPage.ts b/packages/app/tests/e2e/page-objects/SearchPage.ts index 5806833fdc..e53e16b8c9 100644 --- a/packages/app/tests/e2e/page-objects/SearchPage.ts +++ b/packages/app/tests/e2e/page-objects/SearchPage.ts @@ -661,6 +661,14 @@ export class SearchPage { return this.page.getByText(/filter.* didn't apply to this source/i); } + /** + * The active filter pill above the results for a field, by its displayed + * field label (e.g. 'SeverityText', or 'Severity' for the class facet). + */ + getActiveFilterPill(field: string) { + return this.page.getByTestId(`active-filter-pill-${field}`); + } + // Getters for assertions in spec files get form() { diff --git a/packages/app/tests/e2e/seed-clickhouse.ts b/packages/app/tests/e2e/seed-clickhouse.ts index 9736ffdaea..2a1cead073 100644 --- a/packages/app/tests/e2e/seed-clickhouse.ts +++ b/packages/app/tests/e2e/seed-clickhouse.ts @@ -26,6 +26,7 @@ import { E2E_PROMQL_METRIC_NAME, E2E_PROMQL_TABLE, E2E_SESSIONS_TABLE, + E2E_SEVERITY_CLASS_LOGS_TABLE, E2E_TRACES_MV_TABLE, E2E_TRACES_TABLE, } from './utils/constants'; @@ -164,6 +165,25 @@ export const METADATA_MV_ROWS = [ }, ] as const; +// Rows seeded into `e2e_severity_class_logs`, each with a distinct Body, and the +// class the Severity facet should give them. +export const SEVERITY_CLASS_ROWS = [ + { number: 17, text: '', severityClass: 'ERROR', body: 'class row number 17' }, + { number: 0, text: 'error', severityClass: 'ERROR', body: 'class row error' }, + { + number: 9, + text: 'error', + severityClass: 'INFO', + body: 'class row number 9 error', + }, + { + number: 0, + text: 'audit', + severityClass: 'OTHER', + body: 'class row audit', + }, +] as const; + // Service (`AppName`) values seeded into `e2e_custom_service_name`. export const CUSTOM_SERVICE_LOGS_APP_NAMES = [ 'checkout-app', @@ -817,6 +837,21 @@ function generateMetadataMvLogData(seedRef: number): string { }).join(',\n'); } +/** + * Build the VALUES tuples for `e2e_severity_class_logs`. Rows sit a few minutes + * before `seedRef`, inside a 'Last 1 days' search long after seeding. + */ +function generateSeverityClassLogData(seedRef: number): string { + return SEVERITY_CLASS_ROWS.map((row, i) => { + const timestampNs = (seedRef - (i + 1) * 60_000) * 1000000; + return ( + `('${timestampNs}', '', '', 0, '${row.text}', ${row.number}, ` + + `'severity-class-svc', '${row.body}', '', ` + + `{'service.name':'severity-class-svc'}, '', '', '', {}, {})` + ); + }).join(',\n'); +} + // CI can be slower, so use a longer timeout const CLICKHOUSE_READY_TIMEOUT_SECONDS = parseInt( process.env.E2E_CLICKHOUSE_READY_TIMEOUT || '60', @@ -900,6 +935,9 @@ async function clearTestData( await client.query( `TRUNCATE TABLE IF EXISTS ${E2E_CLICKHOUSE_DATABASE}.${E2E_CUSTOM_SERVICE_LOGS_TABLE}`, ); + await client.query( + `TRUNCATE TABLE IF EXISTS ${E2E_CLICKHOUSE_DATABASE}.${E2E_SEVERITY_CLASS_LOGS_TABLE}`, + ); console.log(' Existing data cleared'); } @@ -924,12 +962,23 @@ async function createAltMetricsDatabase( console.log(` ${E2E_ALT_METRICS_DATABASE} ready`); } +// Created here rather than in `init-db-e2e.sh` for the same reason as the +// alternate metrics database: that script only runs on a fresh volume. +async function createSeverityClassLogsTable( + client: ReturnType, +): Promise { + await client.query( + `CREATE TABLE IF NOT EXISTS ${E2E_CLICKHOUSE_DATABASE}.${E2E_SEVERITY_CLASS_LOGS_TABLE} AS ${E2E_CLICKHOUSE_DATABASE}.${E2E_LOGS_TABLE}`, + ); +} + export async function seedClickHouse(): Promise { console.log('Seeding ClickHouse with test data...'); const client = createClickHouseClient(); await waitForClickHouse(client); await createAltMetricsDatabase(client); + await createSeverityClassLogsTable(client); await clearTestData(client); const seedRef = Date.now(); @@ -1095,6 +1144,18 @@ export async function seedClickHouse(): Promise { `); console.log(` Inserted ${numDataPoints} generic log entries`); + console.log(' Inserting severity class logs...'); + await client.query(` + INSERT INTO ${E2E_CLICKHOUSE_DATABASE}.${E2E_SEVERITY_CLASS_LOGS_TABLE} ( + Timestamp, TraceId, SpanId, TraceFlags, SeverityText, SeverityNumber, + ServiceName, Body, ResourceSchemaUrl, ResourceAttributes, ScopeSchemaUrl, + ScopeName, ScopeVersion, ScopeAttributes, LogAttributes + ) VALUES ${generateSeverityClassLogData(seedRef)} + `); + console.log( + ` Inserted ${SEVERITY_CLASS_ROWS.length} severity class log entries`, + ); + // PromQL series: one per service, labelled with the same `ServiceName` values // the logs carry, so a dashboard filter on ServiceName selects values that // actually match a `service` label here. diff --git a/packages/app/tests/e2e/utils/constants.ts b/packages/app/tests/e2e/utils/constants.ts index 6e43341375..ad79156ba8 100644 --- a/packages/app/tests/e2e/utils/constants.ts +++ b/packages/app/tests/e2e/utils/constants.ts @@ -43,6 +43,10 @@ export const DEFAULT_TRACES_MV_SOURCE_NAME = 'E2E Traces MV'; export const DEFAULT_TRACES_MV_AUTOPOPULATE_SOURCE_NAME = 'E2E Traces MV AutoPopulate'; +// Log source with a Severity Number Expression, which E2E Logs lacks (its rows +// all carry SeverityNumber 0). +export const SEVERITY_CLASS_LOGS_SOURCE_NAME = 'E2E Severity Class Logs'; + // ClickHouse database/table names backing the default E2E sources. Must stay // in sync with `tests/e2e/fixtures/e2e-fixtures.json` and the CREATE TABLE // statements in `docker/clickhouse/local/init-db-e2e.sh`. @@ -87,3 +91,6 @@ export const E2E_METADATA_MV_KEY_ROLLUP_TABLE = 'e2e_otel_logs_key_rollup_15m'; // Table backing CUSTOM_SERVICE_LOGS_SOURCE_NAME. Created in // `docker/clickhouse/local/init-db-e2e.sh` and seeded in `seed-clickhouse.ts`. export const E2E_CUSTOM_SERVICE_LOGS_TABLE = 'e2e_custom_service_name'; +// Table backing SEVERITY_CLASS_LOGS_SOURCE_NAME. Created and seeded in +// `seed-clickhouse.ts`. +export const E2E_SEVERITY_CLASS_LOGS_TABLE = 'e2e_severity_class_logs'; diff --git a/packages/common-utils/src/__tests__/filters.test.ts b/packages/common-utils/src/__tests__/filters.test.ts index 5cfe4683cd..8b084d85e2 100644 --- a/packages/common-utils/src/__tests__/filters.test.ts +++ b/packages/common-utils/src/__tests__/filters.test.ts @@ -1,7 +1,9 @@ +import { buildSeverityClassExpressionV1 } from '@/core/severity'; import { adhocConditionsToFilters, adhocConditionsToPromqlFilters, deriveVariableName, + describeFilter, doesFilterApplyToSource, FilterState, filterStateToPredicate, @@ -1320,6 +1322,29 @@ describe('filters', () => { }); }); + describe('describeFilter', () => { + const key = buildSeverityClassExpressionV1({ + numberExpr: 'SeverityNumber', + textExpr: 'SeverityText', + }); + const sql = (condition: string): Filter => ({ type: 'sql', condition }); + + it.each([ + ['one value', `${key} IN ('ERROR')`, 'Severity: ERROR'], + ['two values', `${key} IN ('ERROR', 'FATAL')`, 'Severity: ERROR, FATAL'], + ['an exclusion', `${key} NOT IN ('OTHER')`, 'Severity: not OTHER'], + ])('describes %s', (_, condition, expected) => { + expect(describeFilter(sql(condition))).toBe(expected); + }); + + it.each([ + ['a severity text filter', "SeverityText IN ('error')"], + ['a compound filter', `${key} IN ('ERROR') AND ServiceName = 'api'`], + ])('prints %s as its condition', (_, condition) => { + expect(describeFilter(sql(condition))).toBe(condition); + }); + }); + // Dashboard filters can be defined by an arbitrary expression, not just a // bare column. The parser must treat the whole expression as the key and // ignore operators/keywords nested inside its parentheses, rather than diff --git a/packages/common-utils/src/__tests__/severity.int.test.ts b/packages/common-utils/src/__tests__/severity.int.test.ts new file mode 100644 index 0000000000..bf5474bdd9 --- /dev/null +++ b/packages/common-utils/src/__tests__/severity.int.test.ts @@ -0,0 +1,287 @@ +import type { ClickHouseClient } from '@clickhouse/client'; +import { createClient } from '@clickhouse/client'; + +import { parameterizedQueryToSql } from '@/clickhouse'; +import { ClickhouseClient as HdxClickhouseClient } from '@/clickhouse/node'; +import { Metadata, MetadataCache } from '@/core/metadata'; +import { renderChartConfig } from '@/core/renderChartConfig'; +import { + buildSeverityClassExpressionV1, + getSeverityClass, + SEVERITY_CLASS_FACET_VALUES, + TEXT_PREFIX_RULES, +} from '@/core/severity'; +import { filtersToQuery } from '@/filters'; +import { DisplayType } from '@/types'; + +type Row = { n: unknown; t: string | null; c: string; type: string }; + +const sqlLiteral = (value: string | number | null) => + value == null + ? 'NULL' + : typeof value === 'string' + ? `'${value.replace(/\\/g, '\\\\').replace(/'/g, "\\'")}'` + : String(value); + +const NUMBERS = [null, -1, ...Array.from({ length: 26 }, (_, i) => i), 255]; +const TEXTS = [ + null, + '', + 'Warning', + 'critical', + 'NOTICE', + 'Information', + 'information', + 'INF', + 'dpanic', + ' error', + '[error]', + 'log_error', + 'dbug', + 'ok', + 'audit', + 'ÉRROR', + 'İnfo', + 'verbo\u017fe', + ...TEXT_PREFIX_RULES.flatMap(r => [ + r.prefix, + r.prefix.toUpperCase(), + `${r.prefix[0].toUpperCase()}${r.prefix.slice(1)}`, + ]), +]; + +describe('buildSeverityClassExpressionV1 (integration)', () => { + let client: ClickHouseClient; + let hdxClient: HdxClickhouseClient; + + const DB = 'default'; + const TABLE = 'test_severity_class_logs'; + + async function classify({ + structure, + rows, + numberExpr, + textExpr, + }: { + structure: string; + rows: string[]; + numberExpr: string; + textExpr?: string; + }): Promise { + const key = buildSeverityClassExpressionV1({ numberExpr, textExpr }); + const result = await client.query({ + query: ` + SELECT ${numberExpr} AS n, ${textExpr ?? 'NULL'} AS t, ${key} AS c, + toTypeName(c) AS type + FROM values(${sqlLiteral(structure)}, ${rows.join(', ')}) + `, + format: 'JSONEachRow', + }); + return result.json(); + } + + const expectParity = (rows: Row[]) => { + expect(rows.length).toBeGreaterThan(0); + for (const row of rows) { + expect(row).toEqual({ + ...row, + c: getSeverityClass({ severityNumber: row.n, severityText: row.t }), + type: 'String', + }); + } + }; + + const cross = ( + numbers: (string | number | null)[], + texts: (string | null)[], + ) => + numbers.flatMap(n => + texts.map(t => `(${sqlLiteral(n)}, ${sqlLiteral(t)})`), + ); + + beforeAll(async () => { + const host = process.env.CLICKHOUSE_HOST || 'http://localhost:8123'; + const username = process.env.CLICKHOUSE_USER || 'default'; + const password = process.env.CLICKHOUSE_PASSWORD || ''; + + client = createClient({ url: host, username, password }); + hdxClient = new HdxClickhouseClient({ host, username, password }); + }); + + afterAll(async () => { + await client.command({ query: `DROP TABLE IF EXISTS ${DB}.${TABLE}` }); + await hdxClient.close(); + await client.close(); + }); + + // Int64 and UInt64 come back as digit strings, as they do in the app. + it.each([ + ['Nullable(Int64)', 'Nullable(String)', NUMBERS, TEXTS], + [ + 'UInt8', + 'LowCardinality(String)', + NUMBERS.filter(n => n != null && n >= 0), + TEXTS.filter(t => t != null), + ], + ['UInt64', 'String', [0, 17, '18446744073709551615'], ['warn']], + ['Float64', 'String', [17, 17.5, 0.5, 1.5, 24.9, 25, -1, NaN], ['warn']], + [ + 'Int32', + "Enum8('error' = 1, 'Warning' = 2, 'audit' = 3)", + [-1, 0, 9, 17], + ['error', 'Warning', 'audit'], + ], + ] as const)( + 'matches getSeverityClass for %s numbers and %s text', + async (numberType, textType, numbers, texts) => { + expectParity( + await classify({ + structure: `n ${numberType}, t ${textType}`, + rows: cross([...numbers], [...texts]), + numberExpr: 'n', + textExpr: 't', + }), + ); + }, + ); + + it('matches getSeverityClass without a text expression', async () => { + expectParity( + await classify({ + structure: 'n Nullable(UInt8)', + rows: NUMBERS.filter(n => n == null || n >= 0).map(sqlLiteral), + numberExpr: 'n', + }), + ); + }); + + it('matches getSeverityClass for JSON attributes', async () => { + expectParity( + await classify({ + structure: 'j JSON', + rows: [ + `'{"severity_number": 17, "level": "info"}'`, + `'{"severity_number": 9.5, "level": "warn"}'`, + `'{"severity_number": 300, "level": "err"}'`, + `'{"severity_number": 0, "level": "Crit"}'`, + `'{"level": "debug"}'`, + ], + numberExpr: 'j.severity_number', + textExpr: 'j.level', + }), + ); + }); + + describe('as a stored filter', () => { + const ROWS: [number, string][] = [ + [17, ''], + [0, 'error'], + [0, 'critical'], + [9, 'error'], + [21, ''], + [13, 'warning'], + [0, 'audit'], + [18, 'info'], + [0, 'Err'], + [0, '[warn]'], + [3, 'debug'], + [25, ''], + [0, 'panic'], + [0, 'INF'], + [0, 'dbug'], + ]; + + const otelKey = buildSeverityClassExpressionV1({ + numberExpr: 'SeverityNumber', + textExpr: 'SeverityText', + }); + // LogAttributes['level'] has a MATERIALIZED column, so renderChartConfig + // rewrites the filter through node-sql-parser. + const mapKey = buildSeverityClassExpressionV1({ + numberExpr: 'SeverityNumber', + textExpr: "LogAttributes['level']", + }); + + beforeAll(async () => { + await client.command({ + query: ` + CREATE OR REPLACE TABLE ${DB}.${TABLE} ( + Timestamp DateTime64(9), + SeverityNumber UInt8, + SeverityText LowCardinality(String), + LogAttributes Map(LowCardinality(String), String), + Level String MATERIALIZED LogAttributes['level'] + ) + ENGINE = MergeTree() + ORDER BY (Timestamp) + `, + }); + await client.command({ + query: ` + INSERT INTO ${DB}.${TABLE} + (Timestamp, SeverityNumber, SeverityText, LogAttributes) + VALUES ${ROWS.map( + ([n, t]) => + `('2025-06-01 00:00:00', ${n}, ${sqlLiteral(t)}, map('level', ${sqlLiteral(t)}))`, + ).join(', ')} + `, + }); + }); + + async function count( + key: string, + included: string[], + excluded: string[] = [], + ) { + const sql = parameterizedQueryToSql( + await renderChartConfig( + { + displayType: DisplayType.Table, + connection: 'test-connection', + from: { databaseName: DB, tableName: TABLE }, + select: [{ aggFn: 'count', valueExpression: '', alias: 'count' }], + where: '', + whereLanguage: 'sql', + timestampValueExpression: 'Timestamp', + dateRange: [new Date('2025-01-01'), new Date('2025-12-31')], + filters: filtersToQuery({ + [key]: { + included: new Set(included), + excluded: new Set(excluded), + }, + }), + }, + new Metadata(hdxClient, new MetadataCache()), + undefined, + ), + ); + const result = await client.query({ query: sql, format: 'JSONEachRow' }); + const [row] = await result.json<{ count: string }>(); + return { sql, count: Number(row.count) }; + } + + const jsCount = (matches: (severityClass: string) => boolean) => + ROWS.filter(([severityNumber, severityText]) => + matches(getSeverityClass({ severityNumber, severityText })), + ).length; + + it.each([ + ['OTel columns', otelKey], + ['a map attribute', mapKey], + ])('counts each class like getSeverityClass on %s', async (_, key) => { + for (const severityClass of SEVERITY_CLASS_FACET_VALUES) { + expect((await count(key, [severityClass])).count).toBe( + jsCount(c => c === severityClass), + ); + } + expect((await count(key, [], ['OTHER'])).count).toBe( + jsCount(c => c !== 'OTHER'), + ); + }); + + it('runs the map key against the materialized column', async () => { + const { sql } = await count(mapKey, ['ERROR']); + expect(sql).toContain("Level ILIKE 'err%'"); + }); + }); +}); diff --git a/packages/common-utils/src/core/__tests__/severity.test.ts b/packages/common-utils/src/core/__tests__/severity.test.ts index 381bf5ac49..3ae8285c0f 100644 --- a/packages/common-utils/src/core/__tests__/severity.test.ts +++ b/packages/common-utils/src/core/__tests__/severity.test.ts @@ -1,11 +1,22 @@ +import * as SQLParser from 'node-sql-parser'; + import { + buildSeverityClassExpressionV1, collapseSeverityClass, getSeverityClass, getSeverityLabel, getSeverityShortName, + isSeverityClassKeyV1, NUMBER_RANGES, + SEVERITY_CLASS_FACET_VALUES, TEXT_PREFIX_RULES, } from '@/core/severity'; +import { + filtersToQuery, + isRenderablePinnedFilter, + isValidFilterCondition, + parseQuery, +} from '@/filters'; describe('getSeverityClass', () => { it.each([ @@ -21,9 +32,14 @@ describe('getSeverityClass', () => { [20, 'ERROR'], [21, 'FATAL'], [24, 'FATAL'], + [25, 'FATAL'], + [255, 'FATAL'], + [17.5, 'ERROR'], + [1.5, 'TRACE'], + [Infinity, 'FATAL'], ['17', 'ERROR'], - ['+17', 'ERROR'], ['017', 'ERROR'], + ['18446744073709551615', 'FATAL'], ])('classifies number %p as %p', (severityNumber, expected) => { expect(getSeverityClass({ severityNumber })).toBe(expected); }); @@ -34,7 +50,7 @@ describe('getSeverityClass', () => { ); }); - it.each([0, 25, 255, -1, 17.5, ' 17', '17 ', '0x11', '', null, undefined])( + it.each([0, 0.5, -1, NaN, ' 17', '17.5', '-1', '', null, undefined, true])( 'falls back to the text for number %p', severityNumber => { expect(getSeverityClass({ severityNumber, severityText: 'Warn' })).toBe( @@ -55,23 +71,39 @@ describe('getSeverityClass', () => { ); it.each([ - ['panic', 'FATAL'], + ['information', 'INFO'], + ['INF', 'INFO'], + ['critical', 'ERROR'], + ['SEVERE', 'ERROR'], ['dpanic', 'FATAL'], - ['ftl', 'FATAL'], - ['severe', 'ERROR'], - ['wrn', 'WARN'], - ])('classifies %p as %p', (severityText, expected) => { - expect(getSeverityClass({ severityText })).toBe(expected); - }); - - it.each(['', ' error', 'E', 'log', 'Normal', null, undefined])( - 'classifies unmatched text %p as unknown', - severityText => { - expect(getSeverityClass({ severityNumber: 0, severityText })).toBe( - 'UNKNOWN', - ); - }, - ); + ['Panic', 'FATAL'], + ['FTL', 'FATAL'], + ['WRN', 'WARN'], + ['DBG', 'DEBUG'], + ['TRC', 'TRACE'], + ['VRB', 'TRACE'], + ])('classifies text %p as %p', (severityText, expected) => { + expect(getSeverityClass({ severityNumber: 0, severityText })).toBe( + expected, + ); + }); + + it.each([ + '', + 'E', + 'audit', + '[error]', + ' error', + 'log_error', + 'dbug', + 'ok', + 'unset', + 'ÉRROR', + null, + undefined, + ])('classifies unmatched text %p as other', severityText => { + expect(getSeverityClass({ severityNumber: 0, severityText })).toBe('OTHER'); + }); it('keeps the ranges contiguous and four wide and the prefixes order-independent', () => { NUMBER_RANGES.forEach((range, i) => { @@ -95,7 +127,7 @@ describe('collapseSeverityClass', () => { ['INFO', undefined], ['DEBUG', undefined], ['TRACE', undefined], - ['UNKNOWN', undefined], + ['OTHER', undefined], ] as const)('collapses %p to %p', (severityClass, expected) => { expect(collapseSeverityClass(severityClass)).toBe(expected); }); @@ -133,3 +165,98 @@ describe('getSeverityLabel', () => { expect(getSeverityLabel({ severityNumber, severityText })).toBe(expected); }); }); + +describe('buildSeverityClassExpressionV1', () => { + const sqlParser = new SQLParser.Parser(); + const OTEL_KEY = + "multiIf(SeverityNumber >= 21, 'FATAL', SeverityNumber >= 17, 'ERROR', SeverityNumber >= 13, 'WARN', SeverityNumber >= 9, 'INFO', SeverityNumber >= 5, 'DEBUG', SeverityNumber >= 1, 'TRACE', SeverityText ILIKE 'fatal%', 'FATAL', SeverityText ILIKE 'emerg%', 'FATAL', SeverityText ILIKE 'panic%', 'FATAL', SeverityText ILIKE 'dpanic%', 'FATAL', SeverityText ILIKE 'ftl%', 'FATAL', SeverityText ILIKE 'err%', 'ERROR', SeverityText ILIKE 'crit%', 'ERROR', SeverityText ILIKE 'alert%', 'ERROR', SeverityText ILIKE 'severe%', 'ERROR', SeverityText ILIKE 'warn%', 'WARN', SeverityText ILIKE 'wrn%', 'WARN', SeverityText ILIKE 'inf%', 'INFO', SeverityText ILIKE 'notice%', 'INFO', SeverityText ILIKE 'debug%', 'DEBUG', SeverityText ILIKE 'dbg%', 'DEBUG', SeverityText ILIKE 'trace%', 'TRACE', SeverityText ILIKE 'trc%', 'TRACE', SeverityText ILIKE 'verbose%', 'TRACE', SeverityText ILIKE 'vrb%', 'TRACE', 'OTHER')"; + + const otelKey = buildSeverityClassExpressionV1({ + numberExpr: 'SeverityNumber', + textExpr: 'SeverityText', + }); + const numberOnlyKey = buildSeverityClassExpressionV1({ + numberExpr: 'SeverityNumber', + }); + const mapKey = buildSeverityClassExpressionV1({ + numberExpr: "toUInt8OrZero(LogAttributes['severity_number'])", + textExpr: "LogAttributes['level']", + }); + + // Stored filters hold this exact string; changing it needs a V2 builder. + it('builds the frozen V1 keys', () => { + expect(otelKey).toBe(OTEL_KEY); + expect(numberOnlyKey).toBe( + "multiIf(SeverityNumber >= 21, 'FATAL', SeverityNumber >= 17, 'ERROR', SeverityNumber >= 13, 'WARN', SeverityNumber >= 9, 'INFO', SeverityNumber >= 5, 'DEBUG', SeverityNumber >= 1, 'TRACE', 'OTHER')", + ); + expect(mapKey).toBe( + OTEL_KEY.replaceAll( + 'SeverityNumber', + "toUInt8OrZero(LogAttributes['severity_number'])", + ).replaceAll('SeverityText', "LogAttributes['level']"), + ); + }); + + it.each([ + ['the OTel source', otelKey], + ['a number-only source', numberOnlyKey], + ['map attributes', mapKey], + ])('recognizes and round-trips the key for %s', (_, key) => { + expect(isSeverityClassKeyV1(key)).toBe(true); + + const [included, excluded] = filtersToQuery({ + [key]: { + included: new Set(['ERROR', 'FATAL']), + excluded: new Set(['OTHER']), + }, + }); + + expect(parseQuery([included, excluded]).filters).toEqual({ + [key]: { + included: new Set(['ERROR', 'FATAL']), + excluded: new Set(['OTHER']), + }, + }); + for (const filter of [included, excluded]) { + expect(filter.type).toBe('sql'); + expect(isValidFilterCondition(filter.condition, 'sql')).toBe(true); + expect(isRenderablePinnedFilter(filter)).toBe(true); + // renderChartConfig rewrites conditions through node-sql-parser when the + // table has materialized columns. + const sql = `SELECT * FROM \`t\` WHERE ${filter.condition}`; + expect( + sqlParser.sqlify(sqlParser.astify(sql, { database: 'Postgresql' })), + ).toBe(sql); + } + }); + + it('recognizes a number expression that contains the first branch', () => { + const key = buildSeverityClassExpressionV1({ + numberExpr: "if(x >= 21, 'FATAL', 1)", + textExpr: 'SeverityText', + }); + expect(isSeverityClassKeyV1(key)).toBe(true); + }); + + it.each([ + 'SeverityText', + OTEL_KEY.slice(0, -1), + OTEL_KEY.replace('SeverityNumber', 'SeverityNumber2'), + OTEL_KEY.replace('SeverityText', 'SeverityText2'), + OTEL_KEY.replace(" SeverityText ILIKE 'verbose%', 'TRACE',", ''), + ])('rejects other key %#', key => { + expect(isSeverityClassKeyV1(key)).toBe(false); + }); + + it('lists the facet values in severity order', () => { + expect(SEVERITY_CLASS_FACET_VALUES).toEqual([ + 'TRACE', + 'DEBUG', + 'INFO', + 'WARN', + 'ERROR', + 'FATAL', + 'OTHER', + ]); + }); +}); diff --git a/packages/common-utils/src/core/severity.ts b/packages/common-utils/src/core/severity.ts index dff0814208..7cca11bed1 100644 --- a/packages/common-utils/src/core/severity.ts +++ b/packages/common-utils/src/core/severity.ts @@ -1,8 +1,7 @@ /** * OpenTelemetry log severity classes (uppercase, as in the spec), from the - * SeverityNumber with a severity text fallback. Parsing mirrors ClickHouse - * `accurateCastOrNull(n, 'UInt8')` and `lower()` so SQL built from these - * tables classifies rows the same way. + * SeverityNumber with a severity text fallback. The class SQL is built from + * the same tables, and its comparisons are mirrored here. * https://github.com/open-telemetry/opentelemetry-specification/blob/v1.61.0/specification/logs/data-model.md#field-severitynumber */ @@ -13,7 +12,7 @@ export const SEVERITY_CLASSES = [ 'INFO', 'DEBUG', 'TRACE', - 'UNKNOWN', + 'OTHER', ] as const; export type SeverityClass = (typeof SEVERITY_CLASSES)[number]; @@ -60,8 +59,9 @@ export function getSeverityShortName( : `${range.severityClass}${position}`; } -// Matched against the start of the lowercased text. No prefix starts another -// prefix of a different class, so rule order never changes the result. +// Matched against the start of the text, case-insensitively, in this order. +// No prefix starts another prefix of a different class, so the order never +// changes the result. export const TEXT_PREFIX_RULES: readonly { prefix: string; severityClass: SeverityClass; @@ -77,30 +77,30 @@ export const TEXT_PREFIX_RULES: readonly { { prefix: 'severe', severityClass: 'ERROR' }, { prefix: 'warn', severityClass: 'WARN' }, { prefix: 'wrn', severityClass: 'WARN' }, - { prefix: 'info', severityClass: 'INFO' }, { prefix: 'inf', severityClass: 'INFO' }, { prefix: 'notice', severityClass: 'INFO' }, - { prefix: 'ok', severityClass: 'INFO' }, - { prefix: 'unset', severityClass: 'INFO' }, { prefix: 'debug', severityClass: 'DEBUG' }, { prefix: 'dbg', severityClass: 'DEBUG' }, { prefix: 'trace', severityClass: 'TRACE' }, { prefix: 'trc', severityClass: 'TRACE' }, { prefix: 'verbose', severityClass: 'TRACE' }, + { prefix: 'vrb', severityClass: 'TRACE' }, ]; -/** - * The SeverityNumber as an integer in 1-24, or undefined. ClickHouse quotes - * 64-bit integers in JSON, so digit strings count too; they are not trimmed - * because accurateCastOrNull rejects surrounding spaces. - */ +// Highest first, so the first range whose min a number reaches is its class. +// FATAL has no upper bound, to keep the class SQL to plain comparisons. +const NUMBER_RANGES_DESC = [...NUMBER_RANGES].reverse(); + +// ClickHouse quotes 64-bit integers in JSON, so digit strings count too. +function toNumber(value: unknown): number | undefined { + if (typeof value === 'number') return value; + if (typeof value === 'string' && /^\+?\d+$/.test(value)) return Number(value); + return undefined; +} + +/** The SeverityNumber as an integer in 1-24, or undefined. */ export function parseSeverityNumber(value: unknown): number | undefined { - const number = - typeof value === 'number' - ? value - : typeof value === 'string' && /^\+?\d+$/.test(value) - ? Number(value) - : undefined; + const number = toNumber(value); return number != null && Number.isInteger(number) && @@ -110,14 +110,14 @@ export function parseSeverityNumber(value: unknown): number | undefined { : undefined; } -// ClickHouse lower() only folds ASCII. +// The prefixes and short names are ASCII, so only ASCII needs folding. function lowerAscii(text: string): string { return text.replace(/[A-Z]/g, c => c.toLowerCase()); } /** - * The severity class of a log row. A SeverityNumber in 1-24 wins because the - * SDK normalizes it; anything else (0 means unspecified) falls back to a + * The severity class of a log row. A SeverityNumber of 1 or more decides + * (21 and up is FATAL); anything else (0 means unspecified) falls back to a * case-insensitive prefix match on the text. */ export function getSeverityClass({ @@ -127,14 +127,16 @@ export function getSeverityClass({ severityNumber?: unknown; severityText?: string | null; }): SeverityClass { - const number = parseSeverityNumber(severityNumber); - if (number != null) return getNumberRange(number).severityClass; + const number = toNumber(severityNumber); + const range = + number == null ? undefined : NUMBER_RANGES_DESC.find(r => number >= r.min); + if (range != null) return range.severityClass; const text = lowerAscii(severityText ?? ''); return ( TEXT_PREFIX_RULES.find(r => text.startsWith(r.prefix))?.severityClass ?? - 'UNKNOWN' + 'OTHER' ); } @@ -167,3 +169,81 @@ export function collapseSeverityClass( return severityClass === 'WARN' ? 'warn' : undefined; } + +/** The Severity facet's values, in severity order. */ +export const SEVERITY_CLASS_FACET_VALUES: readonly SeverityClass[] = [ + ...NUMBER_RANGES.map(r => r.severityClass), + 'OTHER', +]; + +const numberBranch = ( + numberExpr: string, + { min, severityClass }: { min: number; severityClass: SeverityClass }, +) => `${numberExpr} >= ${min}, '${severityClass}', `; + +const numberBranches = (numberExpr: string) => + NUMBER_RANGES_DESC.map(r => numberBranch(numberExpr, r)).join(''); + +// ILIKE folds a few non-ASCII letters to ASCII (the Kelvin sign to k), but +// none in these prefixes, so here it matches lowerAscii. +const textBranches = (textExpr: string) => + TEXT_PREFIX_RULES.map( + r => `${textExpr} ILIKE '${r.prefix}%', '${r.severityClass}', `, + ).join(''); + +const V1_START = 'multiIf('; +const V1_END = "'OTHER')"; + +/** + * A ClickHouse String expression that classifies rows like getSeverityClass, + * used as the key of stored Severity filters (` IN ('ERROR')`). Without a + * text expression the text branches are left out. + * + * V1 is a stored contract: saved searches, alerts and URLs hold the exact + * string. Change the rules or template only in a V2 builder, keep V1 on a + * frozen copy of these tables, and keep recognizing V1 keys. + * + * The number expression must be numeric: ClickHouse rejects comparing a String + * column with a number. A Bool (or a JSON/Dynamic value holding true) counts as + * 1, TRACE, while getSeverityClass ignores booleans and falls back to the text. + */ +export function buildSeverityClassExpressionV1({ + numberExpr, + textExpr, +}: { + numberExpr: string; + textExpr?: string; +}): string { + return `${V1_START}${numberBranches(numberExpr)}${ + textExpr ? textBranches(textExpr) : '' + }${V1_END}`; +} + +/** Whether a filter key is a V1 severity class expression, for any source. */ +export function isSeverityClassKeyV1(key: string): boolean { + if (!key.startsWith(V1_START) || !key.endsWith(V1_END)) return false; + + // The number expression ends at one of these; try each, and size the text + // expression from what's left, since every branch repeats it. + const numberEnd = numberBranch('', NUMBER_RANGES_DESC[0]); + const textTemplateLength = textBranches('').length; + for ( + let end = key.indexOf(numberEnd, V1_START.length + 1); + end !== -1; + end = key.indexOf(numberEnd, end + 1) + ) { + const numberExpr = key.slice(V1_START.length, end); + const rest = key.slice( + V1_START.length + numberBranches(numberExpr).length, + -V1_END.length, + ); + const textExpr = rest.slice( + 0, + (rest.length - textTemplateLength) / TEXT_PREFIX_RULES.length, + ); + if (buildSeverityClassExpressionV1({ numberExpr, textExpr }) === key) { + return true; + } + } + return false; +} diff --git a/packages/common-utils/src/filters.ts b/packages/common-utils/src/filters.ts index 78e3d09c66..cd77b735b0 100644 --- a/packages/common-utils/src/filters.ts +++ b/packages/common-utils/src/filters.ts @@ -1,5 +1,6 @@ import * as SQLParser from 'node-sql-parser'; +import { isSeverityClassKeyV1 } from '@/core/severity'; import { escapeSqlString, replaceJsonExpressions } from '@/core/utils'; import { parse } from '@/queryParser'; import { @@ -648,6 +649,32 @@ export function isRenderablePinnedFilter(filter: Filter): boolean { return countTopLevelAnd(filter.condition) === expectedAnds; } +/** + * "Severity: ERROR" for a Severity facet filter, whose SQL runs to ~600 + * characters; undefined otherwise. + */ +function describeSeverityClassFilter(filter: Filter): string | undefined { + if (!isRenderablePinnedFilter(filter)) return undefined; + + const [[key, { included, excluded }]] = Object.entries( + parseQuery([filter]).filters, + ); + if (!isSeverityClassKeyV1(key)) return undefined; + + if (included.size > 0) return `Severity: ${[...included].join(', ')}`; + if (excluded.size > 0) return `Severity: not ${[...excluded].join(', ')}`; + return undefined; +} + +export function describeFilter(filter: Filter): string { + return ( + describeSeverityClassFilter(filter) ?? + (filter.type === 'sql_ast' + ? `${filter.left} ${filter.operator} ${filter.right}` + : filter.condition) + ); +} + export type SavedFilterValueIssue = { /** Index of the offending value within the input array */ index: number;