From 7eafc9e90252386b3b7a0cda9562b845c5fc2848 Mon Sep 17 00:00:00 2001 From: Drew Davis Date: Thu, 15 Oct 2026 12:36:08 -0400 Subject: [PATCH 1/4] feat: Support click-to-adhoc-filter on search tiles --- .changeset/adhoc-filter-click-search.md | 6 + packages/app/src/DBDashboardPage.tsx | 51 +++- .../AdhocDashboardFilter.tsx | 20 +- .../AdhocDashboardFilter/notifications.ts | 29 ++ packages/app/src/components/DBRowTable.tsx | 32 ++- .../components/DBSqlRowTableWithSidebar.tsx | 3 + .../__tests__/useDashboardFilters.test.tsx | 65 +++++ .../usePresetDashboardFilters.test.tsx | 1 + .../app/src/hooks/useDashboardFilters.tsx | 88 +++++- packages/app/src/types.ts | 13 + .../e2e/components/ChartEditorComponent.ts | 9 + .../e2e/components/SidePanelComponent.ts | 11 + .../dashboard-adhoc-click-filters.spec.ts | 251 ++++++++++++++++++ .../tests/e2e/page-objects/DashboardPage.ts | 48 ++++ .../__tests__/dashboardFilterValues.test.ts | 80 ++++++ .../src/__tests__/filters.test.ts | 97 +++++++ .../common-utils/src/dashboardFilterValues.ts | 40 ++- packages/common-utils/src/filters.ts | 53 ++++ 18 files changed, 865 insertions(+), 32 deletions(-) create mode 100644 .changeset/adhoc-filter-click-search.md create mode 100644 packages/app/src/components/AdhocDashboardFilter/notifications.ts create mode 100644 packages/app/tests/e2e/features/dashboard-adhoc-click-filters.spec.ts 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..b93e05d1c4 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, @@ -148,6 +151,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 +181,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 +452,7 @@ const Tile = ({ showReleaseAnnotations, isLive, readOnly, + onFilter, // Properties forwarded by grid layout className, @@ -482,6 +488,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 +667,22 @@ 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 && (isLogSource(source) || isTraceSource(source)) + ? { + onPropertyAddClick: (key, value, action) => + onFilter( + [{ key, value: String(value) }], + action === 'exclude' ? 'exclude' : 'include', + ), + source, + } + : undefined, + [onFilter, source], + ); const promqlFilters = useMemo( () => (filters ? getPromqlFilters(filters) : undefined), [filters], @@ -1379,6 +1403,8 @@ const Tile = ({ queryKeyPrefix={'search'} variant="default" errorVariant="collapsible" + context={searchRowContext} + filterOnAliasedExpressions /> )} @@ -1460,6 +1486,7 @@ const Tile = ({ readOnly, tileVariables, variableNames, + searchRowContext, ], ); @@ -1910,6 +1937,7 @@ function DBDashboardPage({ setFilterValue, adhocConditionsByFilterId, setAdhocConditions, + applyAdhocAction, setFilterValueEntries, filterValueEntries, ignoredFilterExpressions, @@ -2348,6 +2376,25 @@ function DBDashboardPage({ [where, whereLanguage, getFilterQueriesForSource], ); + const getTileFilterHandler = useCallback( + ( + chart: Tile, + sourceId: string | undefined, + ): TileFilterHandler | undefined => { + // The filter bar is hidden in kiosk mode, so a click would add conditions + // the viewer can't see or remove. + if (isKioskMode) return undefined; + const targetIds = getAdhocFilterTargetsForTile( + filters, + chart.config, + sourceId, + ).map(filter => filter.id); + if (targetIds.length === 0) return undefined; + return (entries, action) => applyAdhocAction(targetIds, entries, action); + }, + [isKioskMode, filters, applyAdhocAction], + ); + const renderTileComponent = useCallback( (chart: Tile) => { // Resolve the tile's source ID so per-source-scoped filters can be @@ -2367,6 +2414,7 @@ function DBDashboardPage({ isRefreshEnabled ? granularityOverride : (granularity ?? undefined) } filters={getTileFilters(tileSourceId)} + onFilter={getTileFilterHandler(chart, tileSourceId)} variables={variables} unsatisfiedRequiredFilters={unsatisfiedRequiredFilters} onTimeRangeSelect={onTimeRangeSelect} @@ -2469,6 +2517,7 @@ function DBDashboardPage({ showAlertAnnotations, showReleaseAnnotations, getTileFilters, + getTileFilterHandler, 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/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/DBRowTable.tsx b/packages/app/src/components/DBRowTable.tsx index afa13b7dbe..d2093fe2e0 100644 --- a/packages/app/src/components/DBRowTable.tsx +++ b/packages/app/src/components/DBRowTable.tsx @@ -29,6 +29,7 @@ import { isJSDataTypeJSONStringifiable, JSDataType, } from '@hyperdx/common-utils/dist/clickhouse'; +import { quoteResultColumnNameIfNeeded } from '@hyperdx/common-utils/dist/core/metadata'; import { splitAndTrimWithBracket } from '@hyperdx/common-utils/dist/core/utils'; import { DENOISE_NOISE_THRESHOLD, @@ -384,6 +385,7 @@ export const RawLogTable = memo( showExpandButton = true, enableRowSelection = false, selectionResetKey, + filterOnAliasedExpressions, onSelectedRowsChange, getRowWhere, variant = 'default', @@ -404,7 +406,7 @@ export const RawLogTable = memo( tableId?: string; columnNameMap?: Record; dedupRows?: boolean; - columnTypeMap: Map; + columnTypeMap: Map; isError?: boolean; error?: ClickHouseQueryError | Error; @@ -419,6 +421,11 @@ export const RawLogTable = memo( showExpandButton?: boolean; enableRowSelection?: boolean; selectionResetKey?: string; + /** + * Pass the expression behind an aliased column as the cell's filter key, + * for filters applied outside this query, where the alias isn't defined. + */ + filterOnAliasedExpressions?: boolean; onSelectedRowsChange?: (hasSelectedRows: boolean) => void; renderRowDetails?: (row: { id: string; @@ -521,6 +528,19 @@ export const RawLogTable = memo( // Get the alias map from the config so we resolve correct column ids const { data: aliasMap } = useAliasMapFromChartConfig(config); + const getCellFilterKey = useCallback( + (column: string | undefined) => { + if (!filterOnAliasedExpressions || column == null) return column; + // Cells hold ISO UTC timestamps, which never equal toString() of a + // DateTime (space-separated, in the column's timezone). + if (/\bDateTime/.test(columnTypeMap.get(column)?.type ?? '')) { + return undefined; + } + return aliasMap?.[column] ?? quoteResultColumnNameIfNeeded(column); + }, + [filterOnAliasedExpressions, aliasMap, columnTypeMap], + ); + // Reset scroll when live tail is enabled for the first time const prevIsLive = usePrevious(isLive); useEffect(() => { @@ -1291,10 +1311,10 @@ export const RawLogTable = memo( cellValue={cellValue} wrapLinesEnabled={wrapLinesEnabled} tableContainerRef={tableContainerRef} - columnName={ + columnName={getCellFilterKey( (cell.column.columnDef.meta as any) - ?.column - } + ?.column, + )} isChart={ (cell.column.columnDef.meta as any) ?.column === '__hdx_pattern_trend' @@ -1607,6 +1627,7 @@ function selectColumnMapWithoutAdditionalKeys( string, { _type: JSDataType | null; + type: string; } > { if (selectMeta == null || additionalKeysLength == null) { @@ -1644,6 +1665,7 @@ function DBSqlRowTableComponent({ showExpandButton = true, enableRowSelection = false, selectionResetKey, + filterOnAliasedExpressions, onSelectedRowsChange, renderRowDetails, onSortingChange, @@ -1678,6 +1700,7 @@ function DBSqlRowTableComponent({ showExpandButton?: boolean; enableRowSelection?: boolean; selectionResetKey?: string; + filterOnAliasedExpressions?: boolean; onSelectedRowsChange?: (hasSelectedRows: boolean) => void; initialSortBy?: SortingState; onSortingChange?: (v: SortingState | null) => void; @@ -2001,6 +2024,7 @@ function DBSqlRowTableComponent({ showExpandButton={showExpandButton} enableRowSelection={enableRowSelection} selectionResetKey={selectionResetKey} + filterOnAliasedExpressions={filterOnAliasedExpressions} onSelectedRowsChange={onSelectedRowsChange} enableSorting={true} onSortingChange={_onSortingChange} diff --git a/packages/app/src/components/DBSqlRowTableWithSidebar.tsx b/packages/app/src/components/DBSqlRowTableWithSidebar.tsx index c683786711..eed26cf924 100644 --- a/packages/app/src/components/DBSqlRowTableWithSidebar.tsx +++ b/packages/app/src/components/DBSqlRowTableWithSidebar.tsx @@ -54,6 +54,7 @@ interface Props { // it. Enabled by default; pass `false` to opt out. closeOnClickOutside?: boolean; keepOpenSelector?: string; + filterOnAliasedExpressions?: boolean; } // Clicking the results table (selecting/switching rows, scrolling) keeps the @@ -84,6 +85,7 @@ export default function DBSqlRowTableWithSideBar({ onResolvedColumnsChange, closeOnClickOutside = true, keepOpenSelector = DEFAULT_KEEP_OPEN_SELECTOR, + filterOnAliasedExpressions, }: Props) { const { data: sourceData } = useSource({ id: sourceId }); const [rowId, setRowId] = useQueryState('rowWhere', parseAsStringEncoded); @@ -163,6 +165,7 @@ export default function DBSqlRowTableWithSideBar({ selectionResetKey={selectionResetKey} onSelectedRowsChange={onSelectedRowsChange} onResolvedColumnsChange={onResolvedColumnsChange} + filterOnAliasedExpressions={filterOnAliasedExpressions} /> ); 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..304dfcbdc6 --- /dev/null +++ b/packages/app/tests/e2e/features/dashboard-adhoc-click-filters.spec.ts @@ -0,0 +1,251 @@ +/** + * 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_TRACES_SOURCE_NAME, +} from '../utils/constants'; +import { + 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('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] }, + ]); + + await new TableComponent(page).clickFirstRow(); + await new SidePanelComponent( + page, + 'row-side-panel', + ).addParsedFieldToFilter('ServiceName'); + await expect + .poll(() => filtersParam(page)) + .toEqual([ + { + type: 'adhoc', + name: 'Conditions', + conditions: [ + { key: 'ServiceName', operator: '=', value: expect.any(String) }, + ], + }, + ]); + }); + + 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); + }); + }); + }, +); 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/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..2b6b7ee25d 100644 --- a/packages/common-utils/src/__tests__/filters.test.ts +++ b/packages/common-utils/src/__tests__/filters.test.ts @@ -23,6 +23,7 @@ import { isFilterVariableEnabled, isQueryExpressionFilter, isRenderablePinnedFilter, + mergeAdhocConditions, parseQuery, resolveFilterValuesWhere, resolvePromqlLabelFilterMatch, @@ -34,6 +35,7 @@ import { } from '@/filters'; import type { AdhocDashboardFilter, + AdhocFilterCondition, AdhocFilterOperator, AdhocFilterSourceType, ChartVariable, @@ -1814,6 +1816,101 @@ describe('filters', () => { }); }); + 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('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..7b786d5162 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, @@ -993,6 +994,58 @@ export function adhocConditionsToFilters( ); } +/** How a value clicked in a tile updates an ad hoc filter. */ +export type AdhocConditionAction = 'include' | 'exclude'; + +/** 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. + */ +export function mergeAdhocConditions( + existingConditions: AdhocFilterCondition[], + newConditions: AdhocConditionEntry[], + action: AdhocConditionAction, +): AdhocFilterCondition[] { + const operator: AdhocFilterOperator = action === 'include' ? '=' : '!='; + 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) { + return existingConditions.filter( + condition => + !newConditions.some(entry => matches(condition, entry, operator)), + ); + } + + const opposite: AdhocFilterOperator = action === 'include' ? '!=' : '='; + 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 = ({ From 3a96640a910b55685f1ab9c8f730939d89f8e0de Mon Sep 17 00:00:00 2001 From: Drew Davis Date: Thu, 15 Oct 2026 17:34:03 -0400 Subject: [PATCH 2/4] review: Filter only scalar and stringifiable columns --- packages/app/src/DBDashboardPage.tsx | 2 +- .../__tests__/useAdhocFilterOptions.test.tsx | 30 ++++++ .../useAdhocFilterOptions.ts | 17 ++- packages/app/src/components/DBRowTable.tsx | 44 ++++---- .../components/DBSqlRowTableWithSidebar.tsx | 6 +- .../DBTable/DBRowTableFieldWithPopover.tsx | 15 +-- .../__tests__/adhocCellCondition.test.ts | 100 ++++++++++++++++++ .../components/DBTable/adhocCellCondition.ts | 52 +++++++++ .../dashboard-adhoc-click-filters.spec.ts | 74 +++++++++++++ .../src/__tests__/filters.test.ts | 20 ++++ packages/common-utils/src/filters.ts | 9 ++ 11 files changed, 337 insertions(+), 32 deletions(-) create mode 100644 packages/app/src/components/DBTable/__tests__/adhocCellCondition.test.ts create mode 100644 packages/app/src/components/DBTable/adhocCellCondition.ts diff --git a/packages/app/src/DBDashboardPage.tsx b/packages/app/src/DBDashboardPage.tsx index b93e05d1c4..a4b1bdd7dc 100644 --- a/packages/app/src/DBDashboardPage.tsx +++ b/packages/app/src/DBDashboardPage.tsx @@ -1404,7 +1404,7 @@ const Tile = ({ variant="default" errorVariant="collapsible" context={searchRowContext} - filterOnAliasedExpressions + isAdhocFilterTarget /> )} 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/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/DBRowTable.tsx b/packages/app/src/components/DBRowTable.tsx index d2093fe2e0..cdd90b2c7f 100644 --- a/packages/app/src/components/DBRowTable.tsx +++ b/packages/app/src/components/DBRowTable.tsx @@ -29,7 +29,6 @@ import { isJSDataTypeJSONStringifiable, JSDataType, } from '@hyperdx/common-utils/dist/clickhouse'; -import { quoteResultColumnNameIfNeeded } from '@hyperdx/common-utils/dist/core/metadata'; import { splitAndTrimWithBracket } from '@hyperdx/common-utils/dist/core/utils'; import { DENOISE_NOISE_THRESHOLD, @@ -114,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 { @@ -385,7 +385,7 @@ export const RawLogTable = memo( showExpandButton = true, enableRowSelection = false, selectionResetKey, - filterOnAliasedExpressions, + isAdhocFilterTarget, onSelectedRowsChange, getRowWhere, variant = 'default', @@ -422,10 +422,11 @@ export const RawLogTable = memo( enableRowSelection?: boolean; selectionResetKey?: string; /** - * Pass the expression behind an aliased column as the cell's filter key, - * for filters applied outside this query, where the alias isn't defined. + * Cell include / exclude actions update ad hoc dashboard filters, which + * apply outside this query and compare `toString(key)` with the value. See + * getAdhocCellCondition for how that shapes each cell's key and value. */ - filterOnAliasedExpressions?: boolean; + isAdhocFilterTarget?: boolean; onSelectedRowsChange?: (hasSelectedRows: boolean) => void; renderRowDetails?: (row: { id: string; @@ -528,17 +529,23 @@ export const RawLogTable = memo( // Get the alias map from the config so we resolve correct column ids const { data: aliasMap } = useAliasMapFromChartConfig(config); - const getCellFilterKey = useCallback( - (column: string | undefined) => { - if (!filterOnAliasedExpressions || column == null) return column; - // Cells hold ISO UTC timestamps, which never equal toString() of a - // DateTime (space-separated, in the column's timezone). - if (/\bDateTime/.test(columnTypeMap.get(column)?.type ?? '')) { - return undefined; + const getCellFilter = useCallback( + ( + column: string | undefined, + cellValue: unknown, + ): { columnName?: string; filterValue?: string } => { + if (!isAdhocFilterTarget || column == null) { + return { columnName: column }; } - return aliasMap?.[column] ?? quoteResultColumnNameIfNeeded(column); + const condition = getAdhocCellCondition( + column, + columnTypeMap.get(column), + cellValue, + aliasMap, + ); + return { columnName: condition?.key, filterValue: condition?.value }; }, - [filterOnAliasedExpressions, aliasMap, columnTypeMap], + [isAdhocFilterTarget, aliasMap, columnTypeMap], ); // Reset scroll when live tail is enabled for the first time @@ -1311,9 +1318,10 @@ export const RawLogTable = memo( cellValue={cellValue} wrapLinesEnabled={wrapLinesEnabled} tableContainerRef={tableContainerRef} - columnName={getCellFilterKey( + {...getCellFilter( (cell.column.columnDef.meta as any) ?.column, + cellValue, )} isChart={ (cell.column.columnDef.meta as any) @@ -1665,7 +1673,7 @@ function DBSqlRowTableComponent({ showExpandButton = true, enableRowSelection = false, selectionResetKey, - filterOnAliasedExpressions, + isAdhocFilterTarget, onSelectedRowsChange, renderRowDetails, onSortingChange, @@ -1700,7 +1708,7 @@ function DBSqlRowTableComponent({ showExpandButton?: boolean; enableRowSelection?: boolean; selectionResetKey?: string; - filterOnAliasedExpressions?: boolean; + isAdhocFilterTarget?: boolean; onSelectedRowsChange?: (hasSelectedRows: boolean) => void; initialSortBy?: SortingState; onSortingChange?: (v: SortingState | null) => void; @@ -2024,7 +2032,7 @@ function DBSqlRowTableComponent({ showExpandButton={showExpandButton} enableRowSelection={enableRowSelection} selectionResetKey={selectionResetKey} - filterOnAliasedExpressions={filterOnAliasedExpressions} + isAdhocFilterTarget={isAdhocFilterTarget} onSelectedRowsChange={onSelectedRowsChange} enableSorting={true} onSortingChange={_onSortingChange} diff --git a/packages/app/src/components/DBSqlRowTableWithSidebar.tsx b/packages/app/src/components/DBSqlRowTableWithSidebar.tsx index eed26cf924..402d324842 100644 --- a/packages/app/src/components/DBSqlRowTableWithSidebar.tsx +++ b/packages/app/src/components/DBSqlRowTableWithSidebar.tsx @@ -54,7 +54,7 @@ interface Props { // it. Enabled by default; pass `false` to opt out. closeOnClickOutside?: boolean; keepOpenSelector?: string; - filterOnAliasedExpressions?: boolean; + isAdhocFilterTarget?: boolean; } // Clicking the results table (selecting/switching rows, scrolling) keeps the @@ -85,7 +85,7 @@ export default function DBSqlRowTableWithSideBar({ onResolvedColumnsChange, closeOnClickOutside = true, keepOpenSelector = DEFAULT_KEEP_OPEN_SELECTOR, - filterOnAliasedExpressions, + isAdhocFilterTarget, }: Props) { const { data: sourceData } = useSource({ id: sourceId }); const [rowId, setRowId] = useQueryState('rowWhere', parseAsStringEncoded); @@ -165,7 +165,7 @@ export default function DBSqlRowTableWithSideBar({ selectionResetKey={selectionResetKey} onSelectedRowsChange={onSelectedRowsChange} onResolvedColumnsChange={onResolvedColumnsChange} - filterOnAliasedExpressions={filterOnAliasedExpressions} + isAdhocFilterTarget={isAdhocFilterTarget} /> ); 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/tests/e2e/features/dashboard-adhoc-click-filters.spec.ts b/packages/app/tests/e2e/features/dashboard-adhoc-click-filters.spec.ts index 304dfcbdc6..8d7e78c44c 100644 --- a/packages/app/tests/e2e/features/dashboard-adhoc-click-filters.spec.ts +++ b/packages/app/tests/e2e/features/dashboard-adhoc-click-filters.spec.ts @@ -10,6 +10,7 @@ import { expect, test } from '../utils/base-test'; import { DEFAULT_LOGS_SOURCE_NAME, DEFAULT_TRACES_SOURCE_NAME, + INTERESTING_FILTER_KEYS_SOURCE_NAME, } from '../utils/constants'; import { expectFiltersParam, @@ -171,6 +172,79 @@ test.describe( }); }); + 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({ diff --git a/packages/common-utils/src/__tests__/filters.test.ts b/packages/common-utils/src/__tests__/filters.test.ts index 2b6b7ee25d..4e8379d0c8 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, @@ -1816,6 +1817,25 @@ 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, diff --git a/packages/common-utils/src/filters.ts b/packages/common-utils/src/filters.ts index 7b786d5162..66552e666f 100644 --- a/packages/common-utils/src/filters.ts +++ b/packages/common-utils/src/filters.ts @@ -954,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, From c43c7f6ea8f038f9bcbbe1745b1d44387723215f Mon Sep 17 00:00:00 2001 From: Drew Davis Date: Thu, 15 Oct 2026 18:08:09 -0400 Subject: [PATCH 3/4] review: Fix side panel filtering --- packages/app/src/DBDashboardPage.tsx | 14 ++- .../app/src/components/DBRowSidePanel.tsx | 7 +- packages/app/src/components/DBRowTable.tsx | 12 +- .../components/DBSqlRowTableWithSidebar.tsx | 3 - .../dashboard-adhoc-click-filters.spec.ts | 107 +++++++++++++++--- packages/app/tests/e2e/utils/filters-param.ts | 7 ++ .../src/__tests__/filters.test.ts | 17 +++ packages/common-utils/src/filters.ts | 9 +- 8 files changed, 139 insertions(+), 37 deletions(-) diff --git a/packages/app/src/DBDashboardPage.tsx b/packages/app/src/DBDashboardPage.tsx index a4b1bdd7dc..e201285e68 100644 --- a/packages/app/src/DBDashboardPage.tsx +++ b/packages/app/src/DBDashboardPage.tsx @@ -130,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 { @@ -671,14 +672,22 @@ const Tile = ({ // `source` keeps the handler off rows from other sources, e.g. a linked trace const searchRowContext = useMemo( () => - onFilter && source && (isLogSource(source) || isTraceSource(source)) + 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 === 'exclude' ? 'exclude' : 'include', + action === 'include' || action === 'exclude' ? action : 'add', ), source, + isAdhocFilterTarget: true, } : undefined, [onFilter, source], @@ -1404,7 +1413,6 @@ const Tile = ({ variant="default" errorVariant="collapsible" context={searchRowContext} - isAdhocFilterTarget /> )} 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 cdd90b2c7f..29c5016588 100644 --- a/packages/app/src/components/DBRowTable.tsx +++ b/packages/app/src/components/DBRowTable.tsx @@ -385,7 +385,6 @@ export const RawLogTable = memo( showExpandButton = true, enableRowSelection = false, selectionResetKey, - isAdhocFilterTarget, onSelectedRowsChange, getRowWhere, variant = 'default', @@ -421,12 +420,6 @@ export const RawLogTable = memo( showExpandButton?: boolean; enableRowSelection?: boolean; selectionResetKey?: string; - /** - * Cell include / exclude actions update ad hoc dashboard filters, which - * apply outside this query and compare `toString(key)` with the value. See - * getAdhocCellCondition for how that shapes each cell's key and value. - */ - isAdhocFilterTarget?: boolean; onSelectedRowsChange?: (hasSelectedRows: boolean) => void; renderRowDetails?: (row: { id: string; @@ -529,6 +522,8 @@ 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, @@ -1673,7 +1668,6 @@ function DBSqlRowTableComponent({ showExpandButton = true, enableRowSelection = false, selectionResetKey, - isAdhocFilterTarget, onSelectedRowsChange, renderRowDetails, onSortingChange, @@ -1708,7 +1702,6 @@ function DBSqlRowTableComponent({ showExpandButton?: boolean; enableRowSelection?: boolean; selectionResetKey?: string; - isAdhocFilterTarget?: boolean; onSelectedRowsChange?: (hasSelectedRows: boolean) => void; initialSortBy?: SortingState; onSortingChange?: (v: SortingState | null) => void; @@ -2032,7 +2025,6 @@ function DBSqlRowTableComponent({ showExpandButton={showExpandButton} enableRowSelection={enableRowSelection} selectionResetKey={selectionResetKey} - isAdhocFilterTarget={isAdhocFilterTarget} onSelectedRowsChange={onSelectedRowsChange} enableSorting={true} onSortingChange={_onSortingChange} diff --git a/packages/app/src/components/DBSqlRowTableWithSidebar.tsx b/packages/app/src/components/DBSqlRowTableWithSidebar.tsx index 402d324842..c683786711 100644 --- a/packages/app/src/components/DBSqlRowTableWithSidebar.tsx +++ b/packages/app/src/components/DBSqlRowTableWithSidebar.tsx @@ -54,7 +54,6 @@ interface Props { // it. Enabled by default; pass `false` to opt out. closeOnClickOutside?: boolean; keepOpenSelector?: string; - isAdhocFilterTarget?: boolean; } // Clicking the results table (selecting/switching rows, scrolling) keeps the @@ -85,7 +84,6 @@ export default function DBSqlRowTableWithSideBar({ onResolvedColumnsChange, closeOnClickOutside = true, keepOpenSelector = DEFAULT_KEEP_OPEN_SELECTOR, - isAdhocFilterTarget, }: Props) { const { data: sourceData } = useSource({ id: sourceId }); const [rowId, setRowId] = useQueryState('rowWhere', parseAsStringEncoded); @@ -165,7 +163,6 @@ export default function DBSqlRowTableWithSideBar({ selectionResetKey={selectionResetKey} onSelectedRowsChange={onSelectedRowsChange} onResolvedColumnsChange={onResolvedColumnsChange} - isAdhocFilterTarget={isAdhocFilterTarget} /> ); 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 index 8d7e78c44c..92d4288b87 100644 --- a/packages/app/tests/e2e/features/dashboard-adhoc-click-filters.spec.ts +++ b/packages/app/tests/e2e/features/dashboard-adhoc-click-filters.spec.ts @@ -9,10 +9,12 @@ 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, @@ -256,22 +258,60 @@ test.describe( { name: 'Conditions', sources: [DEFAULT_LOGS_SOURCE_NAME] }, ]); - await new TableComponent(page).clickFirstRow(); - await new SidePanelComponent( - page, - 'row-side-panel', - ).addParsedFieldToFilter('ServiceName'); - await expect - .poll(() => filtersParam(page)) - .toEqual([ - { - type: 'adhoc', - name: 'Conditions', - conditions: [ - { key: 'ServiceName', operator: '=', value: expect.any(String) }, - ], - }, - ]); + 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 () => { @@ -321,5 +361,40 @@ test.describe( 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/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__/filters.test.ts b/packages/common-utils/src/__tests__/filters.test.ts index 4e8379d0c8..5cfe4683cd 100644 --- a/packages/common-utils/src/__tests__/filters.test.ts +++ b/packages/common-utils/src/__tests__/filters.test.ts @@ -1884,6 +1884,23 @@ describe('filters', () => { ).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( diff --git a/packages/common-utils/src/filters.ts b/packages/common-utils/src/filters.ts index 66552e666f..78e3d09c66 100644 --- a/packages/common-utils/src/filters.ts +++ b/packages/common-utils/src/filters.ts @@ -1004,7 +1004,7 @@ export function adhocConditionsToFilters( } /** How a value clicked in a tile updates an ad hoc filter. */ -export type AdhocConditionAction = 'include' | 'exclude'; +export type AdhocConditionAction = 'include' | 'exclude' | 'add'; /** A clicked `key = value` pair, before an action gives it an operator. */ export type AdhocConditionEntry = Pick; @@ -1016,13 +1016,14 @@ export type AdhocConditionEntry = Pick; * `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 === 'include' ? '=' : '!='; + const operator: AdhocFilterOperator = action === 'exclude' ? '!=' : '='; const matches = ( condition: AdhocFilterCondition, entry: AdhocConditionEntry, @@ -1035,14 +1036,14 @@ export function mergeAdhocConditions( const isApplied = newConditions.every(entry => existingConditions.some(condition => matches(condition, entry, operator)), ); - if (isApplied) { + if (isApplied && action !== 'add') { return existingConditions.filter( condition => !newConditions.some(entry => matches(condition, entry, operator)), ); } - const opposite: AdhocFilterOperator = action === 'include' ? '!=' : '='; + const opposite: AdhocFilterOperator = action === 'exclude' ? '=' : '!='; const kept = existingConditions.filter( condition => !newConditions.some(entry => matches(condition, entry, opposite)), From 82fb40b7260953f316bf7b31ba224bf0662c1dd9 Mon Sep 17 00:00:00 2001 From: Drew Davis Date: Fri, 16 Oct 2026 14:54:13 -0400 Subject: [PATCH 4/4] fix: Fix tile onClick handler stability --- packages/app/src/DBDashboardPage.tsx | 37 ++++++++++++++++------------ 1 file changed, 21 insertions(+), 16 deletions(-) diff --git a/packages/app/src/DBDashboardPage.tsx b/packages/app/src/DBDashboardPage.tsx index e201285e68..3f32c8adab 100644 --- a/packages/app/src/DBDashboardPage.tsx +++ b/packages/app/src/DBDashboardPage.tsx @@ -2384,24 +2384,29 @@ function DBDashboardPage({ [where, whereLanguage, getFilterQueriesForSource], ); - const getTileFilterHandler = useCallback( - ( - chart: Tile, - sourceId: string | undefined, - ): TileFilterHandler | undefined => { - // The filter bar is hidden in kiosk mode, so a click would add conditions - // the viewer can't see or remove. - if (isKioskMode) return undefined; + // 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, - chart.config, + tile.config, sourceId, ).map(filter => filter.id); - if (targetIds.length === 0) return undefined; - return (entries, action) => applyAdhocAction(targetIds, entries, action); - }, - [isKioskMode, filters, applyAdhocAction], - ); + 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) => { @@ -2422,7 +2427,7 @@ function DBDashboardPage({ isRefreshEnabled ? granularityOverride : (granularity ?? undefined) } filters={getTileFilters(tileSourceId)} - onFilter={getTileFilterHandler(chart, tileSourceId)} + onFilter={tileFilterHandlers.get(chart.id)} variables={variables} unsatisfiedRequiredFilters={unsatisfiedRequiredFilters} onTimeRangeSelect={onTimeRangeSelect} @@ -2525,7 +2530,7 @@ function DBDashboardPage({ showAlertAnnotations, showReleaseAnnotations, getTileFilters, - getTileFilterHandler, + tileFilterHandlers, variables, unsatisfiedRequiredFilters, moveTargetContainers,