diff --git a/.changeset/adhoc-filter-click-search.md b/.changeset/adhoc-filter-click-search.md new file mode 100644 index 0000000000..0a9e3f635f --- /dev/null +++ b/.changeset/adhoc-filter-click-search.md @@ -0,0 +1,6 @@ +--- +'@hyperdx/common-utils': patch +'@hyperdx/app': patch +--- + +feat: Filter search tile cells into adhoc dashboard filters diff --git a/packages/app/src/DBDashboardPage.tsx b/packages/app/src/DBDashboardPage.tsx index fb2fa510b7..3f32c8adab 100644 --- a/packages/app/src/DBDashboardPage.tsx +++ b/packages/app/src/DBDashboardPage.tsx @@ -34,7 +34,10 @@ import { Granularity, isTimeSeriesDisplayType, } from '@hyperdx/common-utils/dist/core/utils'; -import { getBlockingRequiredFilterNames } from '@hyperdx/common-utils/dist/dashboardFilterValues'; +import { + getAdhocFilterTargetsForTile, + getBlockingRequiredFilterNames, +} from '@hyperdx/common-utils/dist/dashboardFilterValues'; import { getPromqlFilters, getSqlFilters, @@ -127,6 +130,7 @@ import { } from '@tabler/icons-react'; import { IsolatedChartSyncProvider } from '@/chartSync'; +import { getAllowedSourceKinds } from '@/components/ChartEditor/utils'; import { mergeAnnotations } from '@/components/charts/chartAnnotations'; import { ContactSupportText } from '@/components/ContactSupportText'; import { @@ -148,6 +152,7 @@ import { DashboardKioskHeader } from '@/components/DashboardKioskHeader'; import DashboardTableOfContents from '@/components/DashboardTableOfContents'; import EditTimeChartForm from '@/components/DBEditTimeChartForm'; import DBNumberChart from '@/components/DBNumberChart'; +import type { RowSidePanelContextProps } from '@/components/DBRowSidePanel'; import DBTableChart from '@/components/DBTableChart'; import { DBTimeChart } from '@/components/DBTimeChart'; import { FavoriteButton } from '@/components/FavoriteButton'; @@ -177,6 +182,7 @@ import useDashboardContainers, { import { useDashboardKioskMode } from '@/hooks/useDashboardKioskMode'; import { useReleaseAnnotations } from '@/hooks/useReleaseAnnotations'; import { QueryAttributionProvider } from '@/queryAttribution'; +import type { TileFilterHandler } from '@/types'; import { calculateNextTilePosition, makeId } from '@/utils/tilePositioning'; import ChartContainer, { @@ -447,6 +453,7 @@ const Tile = ({ showReleaseAnnotations, isLive, readOnly, + onFilter, // Properties forwarded by grid layout className, @@ -482,6 +489,8 @@ const Tile = ({ showReleaseAnnotations?: boolean; isLive?: boolean; readOnly?: boolean; + /** Set when an ad hoc filter broadcasts to this tile; clicked values update it. */ + onFilter?: TileFilterHandler; // Properties forwarded by grid layout className?: string; @@ -659,6 +668,30 @@ const Tile = ({ () => (filters ? getSqlFilters(filters) : undefined), [filters], ); + + // `source` keeps the handler off rows from other sources, e.g. a linked trace + const searchRowContext = useMemo( + () => + onFilter && + source && + getAllowedSourceKinds({ + configType: 'builder', + displayType: DisplayType.Search, + }).includes(source.kind) + ? { + // Only the cell popover passes an action; the side panel's + // "Add to filters" passes none and shouldn't toggle a condition off. + onPropertyAddClick: (key, value, action) => + onFilter( + [{ key, value: String(value) }], + action === 'include' || action === 'exclude' ? action : 'add', + ), + source, + isAdhocFilterTarget: true, + } + : undefined, + [onFilter, source], + ); const promqlFilters = useMemo( () => (filters ? getPromqlFilters(filters) : undefined), [filters], @@ -1379,6 +1412,7 @@ const Tile = ({ queryKeyPrefix={'search'} variant="default" errorVariant="collapsible" + context={searchRowContext} /> )} @@ -1460,6 +1494,7 @@ const Tile = ({ readOnly, tileVariables, variableNames, + searchRowContext, ], ); @@ -1910,6 +1945,7 @@ function DBDashboardPage({ setFilterValue, adhocConditionsByFilterId, setAdhocConditions, + applyAdhocAction, setFilterValueEntries, filterValueEntries, ignoredFilterExpressions, @@ -2348,6 +2384,30 @@ function DBDashboardPage({ [where, whereLanguage, getFilterQueriesForSource], ); + // Built once per tile list so each tile's `onFilter` keeps its identity + // across renders; a fresh closure would rebuild the tile's row-panel context + // and re-render every cell in its search table. + const tileFilterHandlers = useMemo(() => { + const handlers = new Map(); + // The filter bar is hidden in kiosk mode, so a click would add conditions + // the viewer can't see or remove. + if (isKioskMode) return handlers; + for (const tile of dashboard?.tiles ?? []) { + const sourceId = 'source' in tile.config ? tile.config.source : undefined; + const targetIds = getAdhocFilterTargetsForTile( + filters, + tile.config, + sourceId, + ).map(filter => filter.id); + if (targetIds.length > 0) { + handlers.set(tile.id, (entries, action) => + applyAdhocAction(targetIds, entries, action), + ); + } + } + return handlers; + }, [isKioskMode, dashboard?.tiles, filters, applyAdhocAction]); + const renderTileComponent = useCallback( (chart: Tile) => { // Resolve the tile's source ID so per-source-scoped filters can be @@ -2367,6 +2427,7 @@ function DBDashboardPage({ isRefreshEnabled ? granularityOverride : (granularity ?? undefined) } filters={getTileFilters(tileSourceId)} + onFilter={tileFilterHandlers.get(chart.id)} variables={variables} unsatisfiedRequiredFilters={unsatisfiedRequiredFilters} onTimeRangeSelect={onTimeRangeSelect} @@ -2469,6 +2530,7 @@ function DBDashboardPage({ showAlertAnnotations, showReleaseAnnotations, getTileFilters, + tileFilterHandlers, variables, unsatisfiedRequiredFilters, moveTargetContainers, diff --git a/packages/app/src/components/AdhocDashboardFilter/AdhocDashboardFilter.tsx b/packages/app/src/components/AdhocDashboardFilter/AdhocDashboardFilter.tsx index 959f1631e2..2bc0eaae61 100644 --- a/packages/app/src/components/AdhocDashboardFilter/AdhocDashboardFilter.tsx +++ b/packages/app/src/components/AdhocDashboardFilter/AdhocDashboardFilter.tsx @@ -15,7 +15,6 @@ import { Text, Tooltip, } from '@mantine/core'; -import { notifications } from '@mantine/notifications'; import { IconPlus } from '@tabler/icons-react'; import { @@ -25,6 +24,10 @@ import { import { FilterPill, getFilterOperatorDisplay } from '@/components/FilterPill'; import { AdhocConditionEditor } from './AdhocConditionEditor'; +import { + notifyAdhocConditionTooLong, + notifyAdhocFilterFull, +} from './notifications'; type AdhocDashboardFilterProps = { filter: AdhocDashboardFilterType; @@ -47,12 +50,7 @@ export function AdhocDashboardFilter({ const toggleAdding = () => { if (isAtLimit) { - notifications.show({ - id: `adhoc-filter-full-${filter.id}`, - color: 'yellow', - title: `${filter.name} is full`, - message: `A filter holds up to ${ADHOC_FILTER_MAX_CONDITIONS} conditions. Remove one to add another.`, - }); + notifyAdhocFilterFull(filter); return; } setIsAdding(!isAdding); @@ -61,13 +59,7 @@ export function AdhocDashboardFilter({ // lose the filter's others on reload. Keep the editor open to fix it instead. const isStorable = (condition: AdhocFilterCondition) => { if (AdhocFilterConditionSchema.safeParse(condition).success) return true; - const maxLength = AdhocFilterConditionSchema.shape.value.maxLength; - notifications.show({ - id: `adhoc-filter-too-long-${filter.id}`, - color: 'yellow', - title: 'Condition is too long', - message: `Keys and values can be up to ${maxLength?.toLocaleString()} characters.`, - }); + notifyAdhocConditionTooLong(filter); return false; }; const replace = (index: number, condition: AdhocFilterCondition) => diff --git a/packages/app/src/components/AdhocDashboardFilter/__tests__/useAdhocFilterOptions.test.tsx b/packages/app/src/components/AdhocDashboardFilter/__tests__/useAdhocFilterOptions.test.tsx index 8d0af7d422..e652ed61b4 100644 --- a/packages/app/src/components/AdhocDashboardFilter/__tests__/useAdhocFilterOptions.test.tsx +++ b/packages/app/src/components/AdhocDashboardFilter/__tests__/useAdhocFilterOptions.test.tsx @@ -75,6 +75,27 @@ describe('getSqlSourceKeys', () => { ), ).toEqual(['ServiceName', "LogAttributes['1']", 'Body.`user`.`id`']); }); + + it('leaves out DateTime keys, whose values never match toString()', () => { + expect( + getSqlSourceKeys( + [ + { + path: ['Timestamp'], + type: 'DateTime64(9)', + jsType: JSDataType.Date, + }, + { path: ['Day'], type: 'Date', jsType: JSDataType.Date }, + { path: ['Body', 'ts'], type: 'DateTime', jsType: JSDataType.Date }, + ], + [ + column('Timestamp', 'DateTime64(9)'), + column('Day', 'Date'), + column('Body', 'JSON'), + ], + ), + ).toEqual(['Day']); + }); }); describe('getSqlSourceColumnKeys', () => { @@ -87,6 +108,15 @@ describe('getSqlSourceColumnKeys', () => { ).toEqual(['ServiceName']); }); + it('leaves out DateTime columns', () => { + expect( + getSqlSourceColumnKeys([ + column('Timestamp', "DateTime64(9, 'UTC')"), + column('Day', 'Date'), + ]), + ).toEqual(['Day']); + }); + it('quotes column names that are not bare identifiers', () => { expect(getSqlSourceColumnKeys([column('service-name', 'String')])).toEqual([ '`service-name`', diff --git a/packages/app/src/components/AdhocDashboardFilter/notifications.ts b/packages/app/src/components/AdhocDashboardFilter/notifications.ts new file mode 100644 index 0000000000..8bb3087a37 --- /dev/null +++ b/packages/app/src/components/AdhocDashboardFilter/notifications.ts @@ -0,0 +1,29 @@ +import { + ADHOC_FILTER_MAX_CONDITIONS, + AdhocDashboardFilter, + AdhocFilterConditionSchema, +} from '@hyperdx/common-utils/dist/types'; +import { notifications } from '@mantine/notifications'; + +export function notifyAdhocFilterFull( + filter: Pick, +) { + notifications.show({ + id: `adhoc-filter-full-${filter.id}`, + color: 'yellow', + title: `${filter.name} is full`, + message: `A filter holds up to ${ADHOC_FILTER_MAX_CONDITIONS} conditions. Remove one to add another.`, + }); +} + +export function notifyAdhocConditionTooLong( + filter: Pick, +) { + const maxLength = AdhocFilterConditionSchema.shape.value.maxLength; + notifications.show({ + id: `adhoc-filter-too-long-${filter.id}`, + color: 'yellow', + title: 'Condition is too long', + message: `Keys and values can be up to ${maxLength?.toLocaleString()} characters.`, + }); +} diff --git a/packages/app/src/components/AdhocDashboardFilter/useAdhocFilterOptions.ts b/packages/app/src/components/AdhocDashboardFilter/useAdhocFilterOptions.ts index fbb2aa54c4..06a73ada02 100644 --- a/packages/app/src/components/AdhocDashboardFilter/useAdhocFilterOptions.ts +++ b/packages/app/src/components/AdhocDashboardFilter/useAdhocFilterOptions.ts @@ -8,7 +8,10 @@ import { QueryAttribution, } from '@hyperdx/common-utils/dist/clickhouse'; import { Field, tcFromSource } from '@hyperdx/common-utils/dist/core/metadata'; -import { getAdhocFilterSourceKinds } from '@hyperdx/common-utils/dist/filters'; +import { + getAdhocFilterSourceKinds, + isAdhocComparableDataType, +} from '@hyperdx/common-utils/dist/filters'; import { AdhocDashboardFilter, TSource, @@ -63,7 +66,11 @@ export function getSqlSourceKeys(fields: Field[], columns: ColumnMeta[]) { const jsonColumns = columnNames([JSDataType.JSON]); const mapColumns = columnNames([JSDataType.Map]); const keys = fields - .filter(field => field.path.length > 1 || isJSDataTypeScalar(field.jsType)) + .filter( + field => + (field.path.length > 1 || isJSDataTypeScalar(field.jsType)) && + isAdhocComparableDataType(field.type), + ) .map(field => mergePath(field.path, jsonColumns, mapColumns)); return toQuotedKeys(keys, columns); } @@ -71,8 +78,10 @@ export function getSqlSourceKeys(fields: Field[], columns: ColumnMeta[]) { /** A SQL source's scalar columns, the only keys known without field metadata. */ export function getSqlSourceColumnKeys(columns: ColumnMeta[]) { const keys = columns - .filter(column => - isJSDataTypeScalar(convertCHDataTypeToJSType(column.type)), + .filter( + column => + isJSDataTypeScalar(convertCHDataTypeToJSType(column.type)) && + isAdhocComparableDataType(column.type), ) .map(column => column.name); return toQuotedKeys(keys, columns); diff --git a/packages/app/src/components/DBRowSidePanel.tsx b/packages/app/src/components/DBRowSidePanel.tsx index 17f3412748..5fa0c2e465 100644 --- a/packages/app/src/components/DBRowSidePanel.tsx +++ b/packages/app/src/components/DBRowSidePanel.tsx @@ -111,8 +111,13 @@ export type RowSidePanelContextProps = { dbSqlRowTableConfig?: BuilderChartConfigWithDateRange; isChildModalOpen?: boolean; setChildModalOpen?: (open: boolean) => void; - source?: TLogSource | TTraceSource; + source?: TSource; onOpenLinkedTrace?: (link: SpanLinkData) => void; + /** + * onPropertyAddClick updates ad hoc dashboard filters. Tables shape + * each cell's key and value for that with getAdhocCellCondition. + */ + isAdhocFilterTarget?: boolean; }; export const RowSidePanelContext = createContext({}); diff --git a/packages/app/src/components/DBRowTable.tsx b/packages/app/src/components/DBRowTable.tsx index afa13b7dbe..29c5016588 100644 --- a/packages/app/src/components/DBRowTable.tsx +++ b/packages/app/src/components/DBRowTable.tsx @@ -113,6 +113,7 @@ import { csvExportFilename } from '@/utils/csv'; import ChartErrorState, { ChartErrorStateVariant, } from './charts/ChartErrorState'; +import { getAdhocCellCondition } from './DBTable/adhocCellCondition'; import DBRowTableFieldWithPopover from './DBTable/DBRowTableFieldWithPopover'; import DBRowTableRowButtons from './DBTable/DBRowTableRowButtons'; import { @@ -404,7 +405,7 @@ export const RawLogTable = memo( tableId?: string; columnNameMap?: Record; dedupRows?: boolean; - columnTypeMap: Map; + columnTypeMap: Map; isError?: boolean; error?: ClickHouseQueryError | Error; @@ -521,6 +522,27 @@ export const RawLogTable = memo( // Get the alias map from the config so we resolve correct column ids const { data: aliasMap } = useAliasMapFromChartConfig(config); + const { isAdhocFilterTarget } = use(RowSidePanelContext); + + const getCellFilter = useCallback( + ( + column: string | undefined, + cellValue: unknown, + ): { columnName?: string; filterValue?: string } => { + if (!isAdhocFilterTarget || column == null) { + return { columnName: column }; + } + const condition = getAdhocCellCondition( + column, + columnTypeMap.get(column), + cellValue, + aliasMap, + ); + return { columnName: condition?.key, filterValue: condition?.value }; + }, + [isAdhocFilterTarget, aliasMap, columnTypeMap], + ); + // Reset scroll when live tail is enabled for the first time const prevIsLive = usePrevious(isLive); useEffect(() => { @@ -1291,10 +1313,11 @@ export const RawLogTable = memo( cellValue={cellValue} wrapLinesEnabled={wrapLinesEnabled} tableContainerRef={tableContainerRef} - columnName={ + {...getCellFilter( (cell.column.columnDef.meta as any) - ?.column - } + ?.column, + cellValue, + )} isChart={ (cell.column.columnDef.meta as any) ?.column === '__hdx_pattern_trend' @@ -1607,6 +1630,7 @@ function selectColumnMapWithoutAdditionalKeys( string, { _type: JSDataType | null; + type: string; } > { if (selectMeta == null || additionalKeysLength == null) { diff --git a/packages/app/src/components/DBTable/DBRowTableFieldWithPopover.tsx b/packages/app/src/components/DBTable/DBRowTableFieldWithPopover.tsx index f8a966e2ed..5180754ebb 100644 --- a/packages/app/src/components/DBTable/DBRowTableFieldWithPopover.tsx +++ b/packages/app/src/components/DBTable/DBRowTableFieldWithPopover.tsx @@ -20,6 +20,8 @@ interface DBRowTableFieldWithPopoverProps { cellValue: unknown; wrapLinesEnabled: boolean; columnName?: string; + /** The value include / exclude filter on, when it differs from the cell's. */ + filterValue?: string; tableContainerRef: HTMLDivElement | null; isChart?: boolean; } @@ -30,6 +32,7 @@ const DBRowTableFieldWithPopover = ({ tableContainerRef, wrapLinesEnabled, columnName, + filterValue, isChart = false, }: DBRowTableFieldWithPopoverProps) => { const [opened, { close, open }] = useDisclosure(false); @@ -102,20 +105,20 @@ const DBRowTableFieldWithPopover = ({ setTimeout(() => setIsCopied(false), 2000); }; + const getFilterValue = () => + filterValue ?? + (typeof cellValue === 'string' ? cellValue : String(cellValue ?? '')); + const addFilter = () => { if (canFilter) { - const value = - typeof cellValue === 'string' ? cellValue : String(cellValue ?? ''); - onPropertyAddClick(columnName, value, 'include'); + onPropertyAddClick(columnName, getFilterValue(), 'include'); handleClick(); // Close the popover } }; const excludeFilter = () => { if (canFilter) { - const value = - typeof cellValue === 'string' ? cellValue : String(cellValue ?? ''); - onPropertyAddClick(columnName, value, 'exclude'); + onPropertyAddClick(columnName, getFilterValue(), 'exclude'); handleClick(); // Close the popover } }; diff --git a/packages/app/src/components/DBTable/__tests__/adhocCellCondition.test.ts b/packages/app/src/components/DBTable/__tests__/adhocCellCondition.test.ts new file mode 100644 index 0000000000..783e1c2a62 --- /dev/null +++ b/packages/app/src/components/DBTable/__tests__/adhocCellCondition.test.ts @@ -0,0 +1,100 @@ +import { + convertCHDataTypeToJSType, + JSDataType, +} from '@hyperdx/common-utils/dist/clickhouse'; + +import { getAdhocCellCondition } from '@/components/DBTable/adhocCellCondition'; + +const columnType = (type: string) => ({ + _type: convertCHDataTypeToJSType(type), + type, +}); + +describe('getAdhocCellCondition', () => { + it('filters scalar cells on the column', () => { + expect( + getAdhocCellCondition('ServiceName', columnType('String'), 'api', {}), + ).toEqual({ key: 'ServiceName', value: 'api' }); + expect( + getAdhocCellCondition('Duration', columnType('UInt64'), '42', {}), + ).toEqual({ key: 'Duration', value: '42' }); + expect( + getAdhocCellCondition('Day', columnType('Date'), '2026-10-15', {}), + ).toEqual({ key: 'Day', value: '2026-10-15' }); + }); + + it('filters on the expression behind an alias', () => { + expect( + getAdhocCellCondition('svc', columnType('String'), 'api', { + svc: 'ServiceName', + }), + ).toEqual({ key: 'ServiceName', value: 'api' }); + }); + + it('quotes column names that are not bare identifiers', () => { + expect( + getAdhocCellCondition('x-host-header', columnType('String'), 'h', {}), + ).toEqual({ key: '`x-host-header`', value: 'h' }); + }); + + it('unwraps Dynamic scalars from their JSON text', () => { + const dynamic = columnType('Dynamic'); + expect(getAdhocCellCondition('json.service', dynamic, '"api"', {})).toEqual( + { key: 'json.service', value: 'api' }, + ); + expect(getAdhocCellCondition('json.n', dynamic, '"5"', {})).toEqual({ + key: 'json.n', + value: '5', + }); + expect(getAdhocCellCondition('json.ok', dynamic, 'true', {})).toEqual({ + key: 'json.ok', + value: 'true', + }); + }); + + it('offers no condition for Dynamic objects, arrays, or nulls', () => { + const dynamic = columnType('Dynamic'); + expect(getAdhocCellCondition('json.o', dynamic, '{"a":1}', {})).toBe( + undefined, + ); + expect(getAdhocCellCondition('json.a', dynamic, '["a"]', {})).toBe( + undefined, + ); + expect(getAdhocCellCondition('json.x', dynamic, 'null', {})).toBe( + undefined, + ); + }); + + it.each([ + 'Array(String)', + 'Map(LowCardinality(String), String)', + 'Tuple(String, UInt8)', + 'JSON', + ])('offers no condition for %s cells', type => { + expect(getAdhocCellCondition('c', columnType(type), '["a"]', {})).toBe( + undefined, + ); + }); + + it.each([ + 'DateTime', + "DateTime64(9, 'UTC')", + 'Nullable(DateTime64(3))', + 'LowCardinality(Nullable(DateTime))', + ])('offers no condition for %s cells', type => { + expect( + getAdhocCellCondition( + 'Timestamp', + { _type: JSDataType.Date, type }, + '2026-10-15T12:00:00Z', + {}, + ), + ).toBe(undefined); + }); + + it('offers no condition for null cells', () => { + expect( + getAdhocCellCondition('ServiceName', columnType('String'), null, {}), + ).toBe(undefined); + }); +}); diff --git a/packages/app/src/components/DBTable/adhocCellCondition.ts b/packages/app/src/components/DBTable/adhocCellCondition.ts new file mode 100644 index 0000000000..ef2f3ef0f1 --- /dev/null +++ b/packages/app/src/components/DBTable/adhocCellCondition.ts @@ -0,0 +1,52 @@ +import { + isJSDataTypeScalar, + JSDataType, +} from '@hyperdx/common-utils/dist/clickhouse'; +import { quoteResultColumnNameIfNeeded } from '@hyperdx/common-utils/dist/core/metadata'; +import { isAdhocComparableDataType } from '@hyperdx/common-utils/dist/filters'; + +type ColumnType = { _type: JSDataType | null; type?: string }; + +// The table renders Dynamic values as JSON text, e.g. `"api"` for api. +function parseDynamicScalar(cellValue: unknown): string | undefined { + if (typeof cellValue !== 'string') return undefined; + try { + const parsed: unknown = JSON.parse(cellValue); + return parsed != null && typeof parsed !== 'object' + ? String(parsed) + : undefined; + } catch { + return undefined; + } +} + +/** + * The `key = value` that clicking a search tile cell adds to an ad hoc filter, + * which compares the value with `toString(key)`. Undefined when the cell can't + * match that: structured values, which the table renders as JSON, and + * DateTimes, which it renders as ISO UTC rather than in the column's timezone. + */ +export function getAdhocCellCondition( + column: string, + columnType: ColumnType | undefined, + cellValue: unknown, + aliasMap: Record | undefined, +): { key: string; value: string } | undefined { + const jsType = columnType?._type ?? null; + if ( + cellValue == null || + !isJSDataTypeScalar(jsType) || + !isAdhocComparableDataType(columnType?.type ?? '') + ) { + return undefined; + } + const value = + jsType === JSDataType.Dynamic + ? parseDynamicScalar(cellValue) + : String(cellValue); + if (value == null) return undefined; + return { + key: aliasMap?.[column] ?? quoteResultColumnNameIfNeeded(column), + value, + }; +} diff --git a/packages/app/src/hooks/__tests__/useDashboardFilters.test.tsx b/packages/app/src/hooks/__tests__/useDashboardFilters.test.tsx index a84b0ef080..41f9223420 100644 --- a/packages/app/src/hooks/__tests__/useDashboardFilters.test.tsx +++ b/packages/app/src/hooks/__tests__/useDashboardFilters.test.tsx @@ -1106,6 +1106,71 @@ describe('useDashboardFilters', () => { ]); }); + it('applies a clicked value to every target filter', () => { + const other: AdhocDashboardFilter = { + ...adhocFilter, + id: 'adhoc2', + name: 'More', + variableName: 'more', + }; + mockState = [{ type: 'adhoc', name: 'conds', conditions: [condition] }]; + const { result } = renderHook(() => + useDashboardFilters([adhocFilter, other]), + ); + + act(() => { + result.current.applyAdhocAction( + ['adhoc1', 'adhoc2'], + [{ key: 'ServiceName', value: 'api' }], + 'include', + ); + }); + + const included = { key: 'ServiceName', operator: '=', value: 'api' }; + expect(mockState).toEqual([ + { type: 'adhoc', name: 'conds', conditions: [included] }, + { type: 'adhoc', name: 'more', conditions: [included] }, + ]); + }); + + it('leaves filters unchanged when a clicked value is too long', () => { + mockState = [{ type: 'adhoc', name: 'conds', conditions: [condition] }]; + const { result } = renderHook(() => useDashboardFilters([adhocFilter])); + + act(() => { + result.current.applyAdhocAction( + ['adhoc1'], + [{ key: 'ServiceName', value: 'x'.repeat(10001) }], + 'include', + ); + }); + + expect(mockState).toEqual([ + { type: 'adhoc', name: 'conds', conditions: [condition] }, + ]); + }); + + it('leaves a filter that would exceed its limit unchanged', () => { + const full = Array.from( + { length: ADHOC_FILTER_MAX_CONDITIONS }, + (_, i) => ({ ...condition, value: `${i}` }), + ); + mockState = [{ type: 'adhoc', name: 'conds', conditions: full }]; + const { result } = renderHook(() => useDashboardFilters([adhocFilter])); + + act(() => { + result.current.applyAdhocAction( + ['adhoc1'], + [{ key: 'ServiceName', value: 'web' }], + 'include', + ); + }); + + expect(mockState).toEqual([ + { type: 'adhoc', name: 'conds', conditions: full }, + ]); + }); + it('keeps conditions across a write to another filter', () => { mockState = [{ type: 'adhoc', name: 'conds', conditions: [condition] }]; const filters = [mockFilters[0], adhocFilter]; diff --git a/packages/app/src/hooks/__tests__/usePresetDashboardFilters.test.tsx b/packages/app/src/hooks/__tests__/usePresetDashboardFilters.test.tsx index 5c47539263..e982aa476b 100644 --- a/packages/app/src/hooks/__tests__/usePresetDashboardFilters.test.tsx +++ b/packages/app/src/hooks/__tests__/usePresetDashboardFilters.test.tsx @@ -113,6 +113,7 @@ describe('usePresetDashboardFilters', () => { setFilterValue: mockSetFilterValue, adhocConditionsByFilterId: new Map(), setAdhocConditions: jest.fn(), + applyAdhocAction: jest.fn(), broadcastedFilters: mockFilterQueries, setFilterValueEntries: jest.fn(), filterValueEntries: null, diff --git a/packages/app/src/hooks/useDashboardFilters.tsx b/packages/app/src/hooks/useDashboardFilters.tsx index 1e381a7ad6..d556fa094e 100644 --- a/packages/app/src/hooks/useDashboardFilters.tsx +++ b/packages/app/src/hooks/useDashboardFilters.tsx @@ -12,6 +12,8 @@ import { VariableEntryValue, } from '@hyperdx/common-utils/dist/dashboardFilterValues'; import { + AdhocConditionAction, + AdhocConditionEntry, adhocConditionsToFilters, adhocConditionsToPromqlFilters, doesFilterApplyToSource, @@ -24,15 +26,23 @@ import { isAdhocFilter, isFilterBroadcastEnabled, isQueryExpressionFilter, + mergeAdhocConditions, } from '@hyperdx/common-utils/dist/filters'; import { + ADHOC_FILTER_MAX_CONDITIONS, + AdhocDashboardFilter, AdhocFilterCondition, + AdhocFilterConditionSchema, AdhocFilterValueSchema, BroadcastFilter, ChartVariable, DashboardFilter, } from '@hyperdx/common-utils/dist/types'; +import { + notifyAdhocConditionTooLong, + notifyAdhocFilterFull, +} from '@/components/AdhocDashboardFilter/notifications'; import { dashboardFilterValuesParser } from '@/utils/queryParsers'; const hasSelection = (selection: FilterSelection): boolean => @@ -154,25 +164,35 @@ const useDashboardFilters = (filters: DashboardFilter[]) => { [setFilterValueEntries, filters], ); - const setAdhocConditions = useCallback( - (filterId: string, conditions: AdhocFilterCondition[]) => { + /** Replace the conditions of several ad hoc filters in one update. */ + const setAdhocConditionsByFilterId = useCallback( + (conditionsByFilterId: ReadonlyMap) => { setFilterValueEntries(prev => { - const target = filters.find(f => f.id === filterId); - const name = target && getFilterVariableName(target); - // The URL parser drops an invalid entry whole, losing every condition - const unique = uniqConditions(conditions); - const isStorable = - AdhocFilterValueSchema.shape.conditions.safeParse(unique).success; - if (!name || !isStorable) return prev; - const parsed = parseDashboardFilterValues(prev ?? []); - parsed.byVariable.set(name, { type: 'adhoc', conditions: unique }); - return rebuildEntries(filters, parsed); + let changed = false; + for (const [filterId, conditions] of conditionsByFilterId) { + const target = filters.find(f => f.id === filterId); + const name = target && getFilterVariableName(target); + // The URL parser drops an invalid entry whole, losing every condition + const unique = uniqConditions(conditions); + const isStorable = + AdhocFilterValueSchema.shape.conditions.safeParse(unique).success; + if (!name || !isStorable) continue; + parsed.byVariable.set(name, { type: 'adhoc', conditions: unique }); + changed = true; + } + return changed ? rebuildEntries(filters, parsed) : prev; }); }, [setFilterValueEntries, filters], ); + const setAdhocConditions = useCallback( + (filterId: string, conditions: AdhocFilterCondition[]) => + setAdhocConditionsByFilterId(new Map([[filterId, conditions]])), + [setAdhocConditionsByFilterId], + ); + const { selectionByFilterId, adhocConditionsByFilterId, @@ -301,6 +321,48 @@ const useDashboardFilters = (filters: DashboardFilter[]) => { [filters, selectionByFilterId, adhocConditionsByFilterId], ); + /** + * Apply a value clicked in a tile to each of the given ad hoc filters. A + * filter that would exceed its condition limit is left unchanged. + */ + const applyAdhocAction = useCallback( + ( + filterIds: string[], + entries: AdhocConditionEntry[], + action: AdhocConditionAction, + ) => { + const targets = filters.filter( + (filter): filter is AdhocDashboardFilter => + isAdhocFilter(filter) && filterIds.includes(filter.id), + ); + const isStorable = entries.every( + ({ key, value }) => + AdhocFilterConditionSchema.safeParse({ key, operator: '=', value }) + .success, + ); + if (!isStorable) { + if (targets[0]) notifyAdhocConditionTooLong(targets[0]); + return; + } + + const next = new Map(); + for (const filter of targets) { + const merged = mergeAdhocConditions( + adhocConditionsByFilterId.get(filter.id) ?? [], + entries, + action, + ); + if (merged.length > ADHOC_FILTER_MAX_CONDITIONS) { + notifyAdhocFilterFull(filter); + continue; + } + next.set(filter.id, merged); + } + if (next.size > 0) setAdhocConditionsByFilterId(next); + }, + [filters, adhocConditionsByFilterId, setAdhocConditionsByFilterId], + ); + // Its consumers apply it as SQL; PromQL conditions reach tiles by source only. const broadcastedFilters = useMemo( () => getSqlFilters(getFiltersQueriesFor(isFilterBroadcastEnabled)), @@ -333,6 +395,8 @@ const useDashboardFilters = (filters: DashboardFilter[]) => { adhocConditionsByFilterId, /** Replace the conditions of an ad hoc filter, by its ID */ setAdhocConditions, + /** Apply a value clicked in a tile to the given ad hoc filters, by ID */ + applyAdhocAction, /** Set the raw filter value state */ setFilterValueEntries, /** The raw persisted entries, as they appear in the URL param. */ diff --git a/packages/app/src/types.ts b/packages/app/src/types.ts index 07bcceaff8..b56ebc0e7d 100644 --- a/packages/app/src/types.ts +++ b/packages/app/src/types.ts @@ -1,4 +1,8 @@ import { ResponseJSON } from '@hyperdx/common-utils/dist/clickhouse'; +import type { + AdhocConditionAction, + AdhocConditionEntry, +} from '@hyperdx/common-utils/dist/filters'; import { Alert, AlertsPageItem as _AlertsPageItem, @@ -21,6 +25,15 @@ export type ChartQueryResult = Pick< isComplete: boolean; }; +/** + * Applies a value clicked in a dashboard tile to the ad hoc filters that + * broadcast to the tile. A chart series passes one entry per group-by value. + */ +export type TileFilterHandler = ( + entries: AdhocConditionEntry[], + action: AdhocConditionAction, +) => void; + export type AlertsPageItem = _AlertsPageItem; export type AlertWithCreatedBy = Alert & { diff --git a/packages/app/tests/e2e/components/ChartEditorComponent.ts b/packages/app/tests/e2e/components/ChartEditorComponent.ts index c2d8b22096..2edff80654 100644 --- a/packages/app/tests/e2e/components/ChartEditorComponent.ts +++ b/packages/app/tests/e2e/components/ChartEditorComponent.ts @@ -58,6 +58,15 @@ export class ChartEditorComponent { await this.chartNameInput.fill(name); } + /** Replace a Search tile's SELECT list, the first editor in its form. */ + async setSearchSelect(select: string) { + await replaceEditorText( + this.page, + this.tileEditor.locator('.cm-editor .cm-content').first(), + select, + ); + } + /** * Set chart type */ diff --git a/packages/app/tests/e2e/components/SidePanelComponent.ts b/packages/app/tests/e2e/components/SidePanelComponent.ts index d36ca993bc..b8caefe762 100644 --- a/packages/app/tests/e2e/components/SidePanelComponent.ts +++ b/packages/app/tests/e2e/components/SidePanelComponent.ts @@ -121,6 +121,17 @@ export class SidePanelComponent { .click({ timeout: 10_000 }); } + /** Click the "Add to Filters" line action on a top-level field of the parsed row. */ + async addParsedFieldToFilter(field: string) { + await this.clickTab('parsed'); + const line = this.panelContainer + .getByTestId('json-viewer-line') + .filter({ has: this.page.getByText(field, { exact: true }) }) + .first(); + await line.hover(); + await line.getByTitle(/add to filters/i).click(); + } + /** * The cross-source "View Trace" action rendered in a log panel's metadata row * when the log has trace context (TraceId + a configured trace source). diff --git a/packages/app/tests/e2e/features/dashboard-adhoc-click-filters.spec.ts b/packages/app/tests/e2e/features/dashboard-adhoc-click-filters.spec.ts new file mode 100644 index 0000000000..92d4288b87 --- /dev/null +++ b/packages/app/tests/e2e/features/dashboard-adhoc-click-filters.spec.ts @@ -0,0 +1,400 @@ +/** + * Values clicked in a dashboard tile update the ad hoc filters that broadcast + * to it: include / exclude toggle `key = value` / `key != value` on every such + * filter. Tiles no ad hoc filter reaches offer no filter actions. + */ +import { SidePanelComponent } from '../components/SidePanelComponent'; +import { TableComponent } from '../components/TableComponent'; +import { DashboardPage } from '../page-objects/DashboardPage'; +import { expect, test } from '../utils/base-test'; +import { + DEFAULT_LOGS_SOURCE_NAME, + DEFAULT_SESSIONS_SOURCE_NAME, + DEFAULT_TRACES_SOURCE_NAME, + INTERESTING_FILTER_KEYS_SOURCE_NAME, +} from '../utils/constants'; +import { + adhocConditionsParam, + expectFiltersParam, + type FilterEntry, + filtersParam, +} from '../utils/filters-param'; + +test.describe( + 'Ad hoc dashboard filters: click to filter', + { tag: ['@dashboard', '@full-stack'] }, + () => { + let dashboardPage: DashboardPage; + + test.beforeEach(async ({ page }) => { + dashboardPage = new DashboardPage(page); + await dashboardPage.goto(); + }); + + const addAdhocFilters = async ( + filters: { name: string; sources: string[] }[], + ) => { + await dashboardPage.openEditFiltersModal(); + for (const { name, sources } of filters) { + await dashboardPage.addAdhocFilterToDashboard(name, sources); + } + await dashboardPage.closeFiltersModal(); + }; + + test('search tile cells update every ad hoc filter on the source', async ({ + page, + }) => { + const tile = dashboardPage.getTile(0); + const conditionsIn = ( + conditions: { key: string; operator: string; value: string }[], + ): FilterEntry[] => [ + { type: 'adhoc', name: 'Conditions', conditions }, + { type: 'adhoc', name: 'More', conditions }, + ]; + + await test.step('Add a search tile with an aliased column and two ad hoc filters', async () => { + await dashboardPage.createNewDashboard(); + await dashboardPage.addSearchTile({ + chartName: 'Logs', + sourceName: DEFAULT_LOGS_SOURCE_NAME, + select: 'Timestamp, ServiceName AS svc, Body', + }); + await expect(dashboardPage.getTiles()).toHaveCount(1); + await addAdhocFilters([ + { name: 'Conditions', sources: [DEFAULT_LOGS_SOURCE_NAME] }, + { name: 'More', sources: [DEFAULT_LOGS_SOURCE_NAME] }, + ]); + await expect( + tile.getByText('frontend', { exact: true }).first(), + ).toBeVisible({ timeout: 20000 }); + }); + + await test.step('Including a value adds key = value on the expression behind the alias', async () => { + await dashboardPage.filterTileCell(tile, 'accounting', 'include'); + await expectFiltersParam( + page, + conditionsIn([ + { key: 'ServiceName', operator: '=', value: 'accounting' }, + ]), + ); + await expect(tile.getByText('frontend', { exact: true })).toHaveCount( + 0, + ); + }); + + await test.step('Excluding the value replaces the condition', async () => { + await dashboardPage.filterTileCell(tile, 'accounting', 'exclude'); + await expectFiltersParam( + page, + conditionsIn([ + { key: 'ServiceName', operator: '!=', value: 'accounting' }, + ]), + ); + await expect( + tile.getByText('frontend', { exact: true }).first(), + ).toBeVisible(); + await expect(tile.getByText('accounting', { exact: true })).toHaveCount( + 0, + ); + }); + + await test.step('Including a value twice toggles it off', async () => { + await dashboardPage.filterTileCell(tile, 'frontend', 'include'); + await expectFiltersParam( + page, + conditionsIn([ + { key: 'ServiceName', operator: '!=', value: 'accounting' }, + { key: 'ServiceName', operator: '=', value: 'frontend' }, + ]), + ); + await dashboardPage.filterTileCell(tile, 'frontend', 'include'); + await expectFiltersParam( + page, + conditionsIn([ + { key: 'ServiceName', operator: '!=', value: 'accounting' }, + ]), + ); + }); + }); + + test('search tile cells filter on the expressions behind complex columns', async ({ + page, + }) => { + const tile = dashboardPage.getTile(0); + const conditionsIn = ( + conditions: { key: string; operator: string; value: string }[], + ): FilterEntry[] => [{ type: 'adhoc', name: 'Conditions', conditions }]; + + await test.step('Add a search tile with an aliased and an unaliased expression', async () => { + await dashboardPage.createNewDashboard(); + await dashboardPage.addSearchTile({ + chartName: 'Logs', + sourceName: DEFAULT_LOGS_SOURCE_NAME, + select: + 'Timestamp, upper(ServiceName) AS svc, lower(ServiceName), Body', + }); + await expect(dashboardPage.getTiles()).toHaveCount(1); + await addAdhocFilters([ + { name: 'Conditions', sources: [DEFAULT_LOGS_SOURCE_NAME] }, + ]); + await expect( + tile.getByText('FRONTEND', { exact: true }).first(), + ).toBeVisible({ timeout: 20000 }); + }); + + await test.step('An aliased expression filters on the expression', async () => { + await dashboardPage.filterTileCell(tile, 'ACCOUNTING', 'include'); + await expectFiltersParam( + page, + conditionsIn([ + { key: 'upper(ServiceName)', operator: '=', value: 'ACCOUNTING' }, + ]), + ); + await expect(tile.getByText('FRONTEND', { exact: true })).toHaveCount( + 0, + ); + await dashboardPage.filterTileCell(tile, 'ACCOUNTING', 'include'); + await expect.poll(() => filtersParam(page) ?? []).toEqual([]); + }); + + await test.step('An unaliased expression filters on the expression', async () => { + await dashboardPage.filterTileCell(tile, 'frontend', 'exclude'); + await expectFiltersParam( + page, + conditionsIn([ + { key: 'lower(ServiceName)', operator: '!=', value: 'frontend' }, + ]), + ); + await expect(tile.getByText('FRONTEND', { exact: true })).toHaveCount( + 0, + ); + await expect( + tile.getByText('ACCOUNTING', { exact: true }).first(), + ).toBeVisible(); + }); + }); + + test('search tile cells filter only where toString() matches the cell', async ({ + page, + }) => { + const tile = dashboardPage.getTile(0); + const conditionsIn = ( + conditions: { key: string; operator: string; value: string }[], + ): FilterEntry[] => [{ type: 'adhoc', name: 'Conditions', conditions }]; + + await test.step('Add a search tile with map, JSON path, and hyphenated columns', async () => { + await dashboardPage.createNewDashboard(); + await dashboardPage.addSearchTile({ + chartName: 'Logs', + sourceName: INTERESTING_FILTER_KEYS_SOURCE_NAME, + select: + 'Timestamp, ResourceAttributes, ResourceAttributesJSON.key.subKey.subSubKey, `service-name`', + }); + await expect(dashboardPage.getTiles()).toHaveCount(1); + await addAdhocFilters([ + { + name: 'Conditions', + sources: [INTERESTING_FILTER_KEYS_SOURCE_NAME], + }, + ]); + await expect( + tile.getByText('svc-one', { exact: true }).first(), + ).toBeVisible({ timeout: 20000 }); + }); + + await test.step('Map cells offer no filter actions', async () => { + const mapCell = /^\{"key\.subKey\.subSubKey":"value1"\}$/; + await expect( + await dashboardPage.getTileCellAction(tile, mapCell, 'copy'), + ).toBeVisible(); + await expect( + await dashboardPage.getTileCellAction(tile, mapCell, 'include'), + ).toHaveCount(0); + }); + + await test.step('A JSON path filters on its unwrapped value', async () => { + await dashboardPage.filterTileCell(tile, '"value2"', 'include'); + await expectFiltersParam( + page, + conditionsIn([ + { + key: 'ResourceAttributesJSON.key.subKey.subSubKey', + operator: '=', + value: 'value2', + }, + ]), + ); + await expect(tile.getByText('svc-one', { exact: true })).toHaveCount(0); + await expect( + tile.getByText('svc-two', { exact: true }).first(), + ).toBeVisible(); + await dashboardPage.filterTileCell(tile, '"value2"', 'include'); + await expect.poll(() => filtersParam(page) ?? []).toEqual([]); + }); + + await test.step('A hyphenated column filters on its quoted name', async () => { + await dashboardPage.filterTileCell(tile, 'svc-one', 'exclude'); + await expectFiltersParam( + page, + conditionsIn([ + { key: '`service-name`', operator: '!=', value: 'svc-one' }, + ]), + ); + await expect(tile.getByText('svc-one', { exact: true })).toHaveCount(0); + await expect( + tile.getByText('svc-two', { exact: true }).first(), + ).toBeVisible(); + }); + }); + + test('row side panel fields update the ad hoc filter', async ({ page }) => { + await dashboardPage.createNewDashboard(); + await dashboardPage.addSearchTile({ + chartName: 'Logs', + sourceName: DEFAULT_LOGS_SOURCE_NAME, + }); + await expect(dashboardPage.getTiles()).toHaveCount(1); + await addAdhocFilters([ + { name: 'Conditions', sources: [DEFAULT_LOGS_SOURCE_NAME] }, + ]); + + const sidePanel = new SidePanelComponent(page, 'row-side-panel'); + const serviceCondition = { + key: 'ServiceName', + operator: '=', + value: expect.any(String), + }; + const expectServiceCondition = () => + expect + .poll(() => filtersParam(page)) + .toEqual([ + { + type: 'adhoc', + name: 'Conditions', + conditions: [serviceCondition], + }, + ]); + + await test.step('Add to filters adds a condition', async () => { + await new TableComponent(page).clickFirstRow(); + await sidePanel.addParsedFieldToFilter('ServiceName'); + await expectServiceCondition(); + }); + + await test.step('Adding it again keeps the condition', async () => { + await sidePanel.addParsedFieldToFilter('ServiceName'); + await expectServiceCondition(); + }); + + await test.step('Surrounding context cells are shaped like tile cells', async () => { + const [{ value: serviceName }] = adhocConditionsParam( + page, + 'Conditions', + ); + await sidePanel.clickTab('context'); + const contextTable = sidePanel.contextTab.getByTestId( + 'search-results-table', + ); + const includeAction = contextTable.getByRole('button', { + name: 'Toggle filter for this value', + }); + await contextTable + .getByText(serviceName, { exact: true }) + .first() + .hover(); + await expect(includeAction.last()).toBeVisible(); + await contextTable + .getByText(/\d:\d{2}:\d{2}\.\d{3}/) + .first() + .hover(); + await expect( + contextTable.getByRole('button', { name: 'Copy field value' }).last(), + ).toBeVisible(); + await expect(includeAction).toHaveCount(0); + }); + }); + + test('offers filter actions only where an ad hoc filter applies', async () => { + const tile = dashboardPage.getTile(0); + const expectFilterActions = async ( + count: number, + value: string | RegExp = 'frontend', + ) => { + await expect( + await dashboardPage.getTileCellAction(tile, value, 'copy'), + ).toBeVisible(); + await expect( + await dashboardPage.getTileCellAction(tile, value, 'include'), + ).toHaveCount(count); + }; + + await test.step('A filter on another source offers none', async () => { + await dashboardPage.createNewDashboard(); + await dashboardPage.addSearchTile({ + chartName: 'Logs', + sourceName: DEFAULT_LOGS_SOURCE_NAME, + select: 'Timestamp, ServiceName, Body', + }); + await expect(dashboardPage.getTiles()).toHaveCount(1); + await addAdhocFilters([ + { name: 'Traces only', sources: [DEFAULT_TRACES_SOURCE_NAME] }, + ]); + await expect( + tile.getByText('frontend', { exact: true }).first(), + ).toBeVisible({ timeout: 20000 }); + await expectFilterActions(0); + }); + + await test.step('A filter on the tile source offers them', async () => { + await addAdhocFilters([ + { name: 'Logs', sources: [DEFAULT_LOGS_SOURCE_NAME] }, + ]); + await expectFilterActions(1); + }); + + await test.step('DateTime cells offer none', async () => { + await expectFilterActions(0, /\d:\d{2}:\d{2}\.\d{3}/); + }); + + await test.step('Kiosk mode offers none', async () => { + await dashboardPage.enterKioskMode(); + await expectFilterActions(0); + }); + }); + + test('session search tile cells update the ad hoc filter', async ({ + page, + }) => { + const tile = dashboardPage.getTile(0); + + await test.step('Add a session search tile and an ad hoc filter on its source', async () => { + await dashboardPage.createNewDashboard(); + await dashboardPage.addSearchTile({ + chartName: 'Sessions', + sourceName: DEFAULT_SESSIONS_SOURCE_NAME, + select: 'Timestamp, ServiceName, Body', + }); + await expect(dashboardPage.getTiles()).toHaveCount(1); + await addAdhocFilters([ + { name: 'Sessions', sources: [DEFAULT_SESSIONS_SOURCE_NAME] }, + ]); + await expect( + tile.getByText('browser', { exact: true }).first(), + ).toBeVisible({ timeout: 20000 }); + }); + + await test.step('Including a value adds key = value', async () => { + await dashboardPage.filterTileCell(tile, 'browser', 'include'); + await expectFiltersParam(page, [ + { + type: 'adhoc', + name: 'Sessions', + conditions: [ + { key: 'ServiceName', operator: '=', value: 'browser' }, + ], + }, + ]); + }); + }); + }, +); diff --git a/packages/app/tests/e2e/page-objects/DashboardPage.ts b/packages/app/tests/e2e/page-objects/DashboardPage.ts index b47de94465..9785e80f4b 100644 --- a/packages/app/tests/e2e/page-objects/DashboardPage.ts +++ b/packages/app/tests/e2e/page-objects/DashboardPage.ts @@ -1632,6 +1632,54 @@ export class DashboardPage { return this.getAdhocFilter(filterName).getByTestId('adhoc-condition-pill'); } + /** Add a Search tile and save it. */ + async addSearchTile({ + chartName, + sourceName, + select, + }: { + chartName: string; + sourceName: string; + select?: string; + }) { + await this.addTile(); + await expect(this.chartEditor.nameInput).toBeVisible(); + await this.chartEditor.waitForDataToLoad(); + await this.chartEditor.setChartType(DisplayType.Search); + await this.chartEditor.setChartName(chartName); + await this.chartEditor.selectSource(sourceName); + if (select) await this.chartEditor.setSearchSelect(select); + await this.chartEditor.save(); + } + + /** + * The hover action on a tile cell showing `value`: `include` / `exclude` + * filter on it, `copy` copies it. Hovers the cell so the actions render. + */ + async getTileCellAction( + tile: Locator, + value: string | RegExp, + action: 'include' | 'exclude' | 'copy', + ): Promise { + await tile.getByText(value, { exact: true }).first().hover(); + const name = { + include: 'Toggle filter for this value', + exclude: 'Exclude this value', + copy: 'Copy field value', + }[action]; + // The previous cell's popover can still be closing + return tile.getByRole('button', { name }).last(); + } + + /** Hover a tile cell showing `value` and click its include / exclude action. */ + async filterTileCell( + tile: Locator, + value: string, + action: 'include' | 'exclude', + ) { + await (await this.getTileCellAction(tile, value, action)).click(); + } + /** The open condition editor of an ad hoc filter. */ getAdhocConditionEditor(filterName: string): Locator { return this.page.getByTestId(`adhoc-condition-editor-${filterName}`); diff --git a/packages/app/tests/e2e/utils/filters-param.ts b/packages/app/tests/e2e/utils/filters-param.ts index a131150672..6aa8e91e0f 100644 --- a/packages/app/tests/e2e/utils/filters-param.ts +++ b/packages/app/tests/e2e/utils/filters-param.ts @@ -26,6 +26,13 @@ export const filtersParam = (page: Page): FilterEntry[] | null => { return JSON.parse(decodeURIComponent(raw)); }; +/** The conditions of ad hoc filter `name` in the `filters=` param. */ +export const adhocConditionsParam = (page: Page, name: string) => + filtersParam(page)?.find( + (entry): entry is Extract => + entry.type === 'adhoc' && entry.name === name, + )?.conditions ?? []; + /** Wait for the param to settle on `expected` — nuqs writes it asynchronously. */ export const expectFiltersParam = async ( page: Page, diff --git a/packages/common-utils/src/__tests__/dashboardFilterValues.test.ts b/packages/common-utils/src/__tests__/dashboardFilterValues.test.ts index 669c190fa4..0a3a177dd1 100644 --- a/packages/common-utils/src/__tests__/dashboardFilterValues.test.ts +++ b/packages/common-utils/src/__tests__/dashboardFilterValues.test.ts @@ -1,6 +1,7 @@ import { configConsumesBroadcastFilters, filterSelectionKey, + getAdhocFilterTargetsForTile, getBlockingRequiredFilterNames, getBlockingRequiredFilters, getUnsatisfiedRequiredFilters, @@ -11,12 +12,14 @@ import { } from '@/dashboardFilterValues'; import { FilterState, filtersToQuery } from '@/filters'; import type { + AdhocDashboardFilter, AdhocFilterCondition, ChartConfigWithOptDateRange, DashboardFilter, DashboardFilterValue, PromqlLabelDashboardFilter, QueryExpressionDashboardFilter, + SavedChartConfig, StaticListDashboardFilter, } from '@/types'; @@ -883,6 +886,83 @@ describe('dashboardFilterValues', () => { }); }); + describe('getAdhocFilterTargetsForTile', () => { + const builderTile: SavedChartConfig = { + source: 'logs', + select: 'count()', + where: '', + }; + const rawSqlTile = (sqlTemplate: string): SavedChartConfig => ({ + configType: 'sql', + sqlTemplate, + connection: 'local', + }); + const promqlTile: SavedChartConfig = { + configType: 'promql', + promqlExpression: 'up', + source: 'logs', + }; + const adhoc = ( + overrides: Partial = {}, + ): AdhocDashboardFilter => ({ + id: 'a1', + type: 'ADHOC', + name: 'Conditions', + sourceType: 'sql', + sources: ['logs'], + isVariableEnabled: true, + ...overrides, + }); + const ids = ( + filters: DashboardFilter[], + config: SavedChartConfig, + sourceId: string | undefined, + ) => getAdhocFilterTargetsForTile(filters, config, sourceId).map(f => f.id); + + it('returns every ad hoc filter covering the tile source', () => { + const filters = [ + adhoc(), + adhoc({ id: 'a2', sources: ['traces'], appliesToSourceIds: ['logs'] }), + adhoc({ id: 'a3', sources: ['traces'] }), + filter(), + ]; + expect(ids(filters, builderTile, 'logs')).toEqual(['a1', 'a2']); + }); + + it('skips filters that do not broadcast', () => { + expect( + ids([adhoc({ isBroadcastEnabled: false })], builderTile, 'logs'), + ).toEqual([]); + }); + + it('matches the filter language to the tile', () => { + const filters = [ + adhoc(), + adhoc({ id: 'p1', sourceType: 'promql', sources: ['logs'] }), + ]; + expect(ids(filters, builderTile, 'logs')).toEqual(['a1']); + expect(ids(filters, promqlTile, 'logs')).toEqual(['p1']); + }); + + it('requires $__filters and a source on raw SQL tiles', () => { + const filters = [adhoc()]; + expect( + ids(filters, rawSqlTile('SELECT 1 FROM t WHERE $__filters'), 'logs'), + ).toEqual(['a1']); + expect( + ids(filters, rawSqlTile('SELECT 1 FROM t WHERE $__filters'), undefined), + ).toEqual([]); + expect(ids(filters, rawSqlTile('SELECT 1 FROM t'), 'logs')).toEqual([]); + expect( + ids( + filters, + rawSqlTile('SELECT 1 FROM t WHERE $__filter(ServiceName, $svc)'), + 'logs', + ), + ).toEqual([]); + }); + }); + describe('getBlockingRequiredFilterNames', () => { const builderTile: ChartConfigWithOptDateRange = { select: 'count()', diff --git a/packages/common-utils/src/__tests__/filters.test.ts b/packages/common-utils/src/__tests__/filters.test.ts index b3ea3e004f..5cfe4683cd 100644 --- a/packages/common-utils/src/__tests__/filters.test.ts +++ b/packages/common-utils/src/__tests__/filters.test.ts @@ -16,6 +16,7 @@ import { getPromqlFilters, getSqlFilters, hasFilterEffect, + isAdhocComparableDataType, isAdhocConditionSupported, isFilterBroadcastEnabled, isFilterGlobalRequirement, @@ -23,6 +24,7 @@ import { isFilterVariableEnabled, isQueryExpressionFilter, isRenderablePinnedFilter, + mergeAdhocConditions, parseQuery, resolveFilterValuesWhere, resolvePromqlLabelFilterMatch, @@ -34,6 +36,7 @@ import { } from '@/filters'; import type { AdhocDashboardFilter, + AdhocFilterCondition, AdhocFilterOperator, AdhocFilterSourceType, ChartVariable, @@ -1814,6 +1817,137 @@ describe('filters', () => { }); }); + describe('isAdhocComparableDataType', () => { + it.each([ + 'DateTime', + "DateTime('UTC')", + 'DateTime64(9)', + 'Nullable(DateTime64(3))', + 'LowCardinality(Nullable(DateTime))', + ])('rejects %s', type => { + expect(isAdhocComparableDataType(type)).toBe(false); + }); + + it.each(['Date', 'Date32', 'String', 'UInt64', 'Dynamic'])( + 'accepts %s', + type => { + expect(isAdhocComparableDataType(type)).toBe(true); + }, + ); + }); + + describe('mergeAdhocConditions', () => { + const svc = ( + operator: AdhocFilterOperator, + value = 'api', + ): AdhocFilterCondition => ({ key: 'ServiceName', operator, value }); + const level: AdhocFilterCondition = { + key: 'SeverityText', + operator: '=', + value: 'error', + }; + + it('includes a value', () => { + expect( + mergeAdhocConditions( + [level], + [{ key: 'ServiceName', value: 'api' }], + 'include', + ), + ).toEqual([level, svc('=')]); + }); + + it('toggles an included value off', () => { + expect( + mergeAdhocConditions( + [svc('='), level], + [{ key: 'ServiceName', value: 'api' }], + 'include', + ), + ).toEqual([level]); + }); + + it('replaces the opposite condition on the same value', () => { + expect( + mergeAdhocConditions( + [svc('=')], + [{ key: 'ServiceName', value: 'api' }], + 'exclude', + ), + ).toEqual([svc('!=')]); + expect( + mergeAdhocConditions( + [svc('!=')], + [{ key: 'ServiceName', value: 'api' }], + 'include', + ), + ).toEqual([svc('=')]); + }); + + it('adds a value without toggling an existing condition off', () => { + expect( + mergeAdhocConditions( + [svc('='), level], + [{ key: 'ServiceName', value: 'api' }], + 'add', + ), + ).toEqual([svc('='), level]); + expect( + mergeAdhocConditions( + [svc('!=')], + [{ key: 'ServiceName', value: 'api' }], + 'add', + ), + ).toEqual([svc('=')]); + }); + + it('toggles an excluded value off', () => { + expect( + mergeAdhocConditions( + [svc('!=')], + [{ key: 'ServiceName', value: 'api' }], + 'exclude', + ), + ).toEqual([]); + }); + + it('appends a second value on the same key', () => { + expect( + mergeAdhocConditions( + [svc('=')], + [{ key: 'ServiceName', value: 'web' }], + 'include', + ), + ).toEqual([svc('='), svc('=', 'web')]); + }); + + it('adds the entries that are missing when some already apply', () => { + expect( + mergeAdhocConditions( + [svc('=')], + [ + { key: 'ServiceName', value: 'api' }, + { key: 'SeverityText', value: 'error' }, + ], + 'include', + ), + ).toEqual([svc('='), level]); + }); + + it('removes every entry when all already apply', () => { + expect( + mergeAdhocConditions( + [svc('='), level], + [ + { key: 'ServiceName', value: 'api' }, + { key: 'SeverityText', value: 'error' }, + ], + 'include', + ), + ).toEqual([]); + }); + }); + describe('adhocConditionsToPromqlFilters', () => { const matcherFor = ( operator: AdhocFilterOperator, diff --git a/packages/common-utils/src/dashboardFilterValues.ts b/packages/common-utils/src/dashboardFilterValues.ts index 33b5f6180f..2ea039b02b 100644 --- a/packages/common-utils/src/dashboardFilterValues.ts +++ b/packages/common-utils/src/dashboardFilterValues.ts @@ -5,14 +5,17 @@ import { getDashboardVariableFilters, getFilterExpression, getFilterVariableName, + isAdhocFilter, isAdhocFilterValue, isFilterGlobalRequirement, isFilterRequired, isFilterVariableEnabled, parseQuery, } from '@/filters'; -import { isMissingFiltersMacro } from '@/macros'; +import { isPromqlSavedChartConfig, isRawSqlSavedChartConfig } from '@/guards'; +import { hasMacro, isMissingFiltersMacro } from '@/macros'; import { + AdhocDashboardFilter, AdhocFilterValue, ChartConfigWithOptDateRange, DashboardFilter, @@ -210,6 +213,14 @@ export function getUnsatisfiedRequiredFilters< ); } +const hasFiltersMacro = (sqlTemplate: string): boolean => { + try { + return hasMacro(sqlTemplate, 'filters'); + } catch { + return false; + } +}; + /** Whether a dashboard's broadcast filters reach a tile with the given config. */ export function configConsumesBroadcastFilters( config: SavedChartConfig | ChartConfigWithOptDateRange, @@ -223,6 +234,33 @@ export function configConsumesBroadcastFilters( return true; } +/** + * The ad hoc filters whose conditions reach a tile, i.e. the filters that + * clicking a value in the tile should update. PromQL tiles take PromQL filters; + * builder and raw SQL tiles take SQL filters. + */ +export function getAdhocFilterTargetsForTile( + filters: DashboardFilter[], + config: SavedChartConfig, + sourceId: string | undefined, +): AdhocDashboardFilter[] { + // Ad hoc conditions reach raw SQL only through `$__filters`, which requires a sourceId. + if ( + isRawSqlSavedChartConfig(config) && + !(sourceId && hasFiltersMacro(config.sqlTemplate)) + ) { + return []; + } + const isPromql = isPromqlSavedChartConfig(config); + return filters + .filter(isAdhocFilter) + .filter( + filter => + filter.sourceType === (isPromql ? 'promql' : 'sql') && + doesFilterApplyToSource(filter, sourceId), + ); +} + /** * The subset of `unsatisfiedRequiredFilters` that blocks the given tile. * diff --git a/packages/common-utils/src/filters.ts b/packages/common-utils/src/filters.ts index 123033a762..78e3d09c66 100644 --- a/packages/common-utils/src/filters.ts +++ b/packages/common-utils/src/filters.ts @@ -5,6 +5,7 @@ import { parse } from '@/queryParser'; import { AdhocDashboardFilter, AdhocFilterCondition, + AdhocFilterOperator, AdhocFilterSourceType, AdhocFilterValue, BroadcastFilter, @@ -953,6 +954,15 @@ export function isAdhocFilterValue( return value.type === 'adhoc'; } +/** + * Whether a SQL ad hoc condition on a value of this ClickHouse type can match. + * Conditions compare `toString(key)` with the value as queries return it, and + * queries return DateTimes as ISO UTC, unlike toString's space-separated form + * in the column's timezone. + */ +export const isAdhocComparableDataType = (type: string): boolean => + !/\bDateTime/.test(type); + const renderAdhocConditionSql = ({ key, operator, @@ -993,6 +1003,59 @@ export function adhocConditionsToFilters( ); } +/** How a value clicked in a tile updates an ad hoc filter. */ +export type AdhocConditionAction = 'include' | 'exclude' | 'add'; + +/** A clicked `key = value` pair, before an action gives it an operator. */ +export type AdhocConditionEntry = Pick; + +/** + * Apply a click-to-filter action for `entries` (one per key, e.g. each group-by + * value of a chart series) to an ad hoc filter's conditions. + * + * `include` / `exclude` add a condition per entry, replacing the opposite + * condition on the same key and value. When every entry's condition already + * exists, they are removed instead, so a second click undoes the first. + * `add` is `include` without the undo, for actions labeled as adding. + */ +export function mergeAdhocConditions( + existingConditions: AdhocFilterCondition[], + newConditions: AdhocConditionEntry[], + action: AdhocConditionAction, +): AdhocFilterCondition[] { + const operator: AdhocFilterOperator = action === 'exclude' ? '!=' : '='; + const matches = ( + condition: AdhocFilterCondition, + entry: AdhocConditionEntry, + op: AdhocFilterOperator, + ) => + condition.key === entry.key && + condition.value === entry.value && + condition.operator === op; + + const isApplied = newConditions.every(entry => + existingConditions.some(condition => matches(condition, entry, operator)), + ); + if (isApplied && action !== 'add') { + return existingConditions.filter( + condition => + !newConditions.some(entry => matches(condition, entry, operator)), + ); + } + + const opposite: AdhocFilterOperator = action === 'exclude' ? '=' : '!='; + const kept = existingConditions.filter( + condition => + !newConditions.some(entry => matches(condition, entry, opposite)), + ); + const added = newConditions + .filter( + entry => !kept.some(condition => matches(condition, entry, operator)), + ) + .map((entry): AdhocFilterCondition => ({ ...entry, operator })); + return [...kept, ...added]; +} + const LEGACY_LABEL_NAME = /^[a-zA-Z_][a-zA-Z0-9_]*$/; const renderAdhocMatcherPromql = ({