diff --git a/.changeset/chart-search-series-where.md b/.changeset/chart-search-series-where.md new file mode 100644 index 0000000000..aeddaeb461 --- /dev/null +++ b/.changeset/chart-search-series-where.md @@ -0,0 +1,12 @@ +--- +'@hyperdx/app': patch +--- + +fix: carry the clicked series' WHERE condition into a chart's Search action + +A builder chart keeps each series' filter in its own `aggCondition`, not in the +statement-level `where`, so a drill-down built from chart-level state alone +spanned every series and its counts didn't reconcile with the clicked line. The +per-series Search now ANDs that series' condition onto the search, and "View all +events" carries the union of every series' condition (the scan scope the chart +itself used). diff --git a/packages/app/src/ChartUtils.tsx b/packages/app/src/ChartUtils.tsx index 10346ad7e0..30adf8a800 100644 --- a/packages/app/src/ChartUtils.tsx +++ b/packages/app/src/ChartUtils.tsx @@ -1289,12 +1289,20 @@ export function buildEventsSearchUrl({ dateRange, groupFilters, valueRangeFilter, + seriesCondition, }: { source: TSource; config: BuilderChartConfigWithDateRange; dateRange: [Date, Date]; groupFilters?: Array<{ column: string; value: any }>; valueRangeFilter?: { expression: string; value: number; threshold?: number }; + /** + * The WHERE the clicked series aggregated over, already variable-expanded by + * the caller (see `resolveSeriesConditionFilter`). A builder chart keeps each + * series' filter in its `aggCondition`, so without this the search spans + * every series on the chart. + */ + seriesCondition?: Filter; }): string | null { if (!source?.id) { return null; @@ -1336,6 +1344,10 @@ export function buildEventsSearchUrl({ let where = config.where; let whereLanguage = config.whereLanguage || 'lucene'; + // A lone series' condition reads better in the search bar than as a filter + // pill, and with an empty chart-level `where` there is nothing to AND it + // with. Every other shape keeps it as a filter below. + let promotedSingleSeries = false; if ( where.length === 0 && Array.isArray(config.select) && @@ -1343,10 +1355,15 @@ export function buildEventsSearchUrl({ ) { where = config.select[0].aggCondition ?? ''; whereLanguage = config.select[0].aggConditionLanguage ?? 'lucene'; + promotedSingleSeries = true; } const additionalFilters: Filter[] = []; + if (!promotedSingleSeries && seriesCondition != null) { + additionalFilters.push(seriesCondition); + } + // Add group-by column filters if (groupFilters && groupFilters.length > 0) { groupFilters.forEach(({ column, value }) => { diff --git a/packages/app/src/__tests__/eventsSearchUrl.test.ts b/packages/app/src/__tests__/eventsSearchUrl.test.ts index 57c6c41dc7..36fbb6111e 100644 --- a/packages/app/src/__tests__/eventsSearchUrl.test.ts +++ b/packages/app/src/__tests__/eventsSearchUrl.test.ts @@ -2,6 +2,7 @@ import { BuilderChartConfigWithDateRange, ChartVariable, DisplayType, + Filter, SourceKind, TLogSource, } from '@hyperdx/common-utils/dist/types'; @@ -190,6 +191,89 @@ describe('buildEventsSearchUrl variable expansion', () => { }); }); +describe('buildEventsSearchUrl series conditions', () => { + const series = (aggCondition: string) => ({ + ...builderConfig.select[0], + aggCondition, + }); + + const buildWithSeriesCondition = ( + overrides: Partial, + seriesCondition: Filter, + ) => + buildEventsSearchUrl({ + source: logSource, + config: { ...builderConfig, ...overrides }, + dateRange, + seriesCondition, + }); + + const filtersOf = (url: string | null) => + JSON.parse(searchParams(url).get('filters') ?? ''); + + it('applies the clicked series condition on a multi-series chart', () => { + const url = buildWithSeriesCondition( + { select: [series("ServiceName:'foo'"), series("ServiceName:'bar'")] }, + { type: 'lucene', condition: "ServiceName:'foo'" }, + ); + + expect(searchParams(url).get('where')).toBe(''); + expect(filtersOf(url)).toEqual([ + { type: 'lucene', condition: "ServiceName:'foo'" }, + ]); + }); + + it('ANDs the series condition with the chart-level where and filters', () => { + const url = buildWithSeriesCondition( + { + select: [series("ServiceName:'foo'"), series("ServiceName:'bar'")], + where: "Env = 'prod'", + whereLanguage: 'sql', + filters: [{ type: 'sql', condition: "Region = 'us-east-1'" }], + }, + { type: 'lucene', condition: "ServiceName:'foo'" }, + ); + + const params = searchParams(url); + expect(params.get('where')).toBe("Env = 'prod'"); + expect(params.get('whereLanguage')).toBe('sql'); + expect(filtersOf(url)).toEqual([ + { type: 'sql', condition: "Region = 'us-east-1'" }, + { type: 'lucene', condition: "ServiceName:'foo'" }, + ]); + }); + + // A single series' condition already lands in `where`; adding it again as a + // filter would show the user the same predicate twice. + it('does not duplicate a single series condition promoted into where', () => { + const url = buildWithSeriesCondition( + { select: [series("ServiceName:'foo'")] }, + { type: 'lucene', condition: "ServiceName:'foo'" }, + ); + + expect(searchParams(url).get('where')).toBe("ServiceName:'foo'"); + expect(filtersOf(url)).toEqual([]); + }); + + // Promotion is skipped when the chart has its own `where`, so the series + // condition would otherwise be dropped entirely. + it('keeps a single series condition that cannot be promoted', () => { + const url = buildWithSeriesCondition( + { + select: [series("ServiceName:'foo'")], + where: "Env = 'prod'", + whereLanguage: 'sql', + }, + { type: 'lucene', condition: "ServiceName:'foo'" }, + ); + + expect(searchParams(url).get('where')).toBe("Env = 'prod'"); + expect(filtersOf(url)).toEqual([ + { type: 'lucene', condition: "ServiceName:'foo'" }, + ]); + }); +}); + describe('buildTableRowSearchUrl variable expansion', () => { const tableConfig = { ...builderConfig, diff --git a/packages/app/src/components/DBTimeChart.tsx b/packages/app/src/components/DBTimeChart.tsx index a25412777c..96a8eed84e 100644 --- a/packages/app/src/components/DBTimeChart.tsx +++ b/packages/app/src/components/DBTimeChart.tsx @@ -8,6 +8,7 @@ import React, { useState, } from 'react'; import { add, differenceInSeconds } from 'date-fns'; +import { isRatioChartConfig } from '@hyperdx/common-utils/dist/core/renderChartConfig'; import { convertGranularityToSeconds, getAlignedDateRange, @@ -21,6 +22,7 @@ import { BuilderChartConfigWithDateRange, ChartConfigWithDateRange, DisplayType, + Filter, } from '@hyperdx/common-utils/dist/types'; import { Popover, Portal } from '@mantine/core'; import { @@ -42,7 +44,10 @@ import { useTimeChartSettings, } from '@/ChartUtils'; import { ChartAnnotation } from '@/components/charts/chartAnnotations'; -import { ChartSeriesTooltip } from '@/components/charts/ChartSeriesTooltip'; +import { + ChartSeriesTooltip, + type ClickedSeries, +} from '@/components/charts/ChartSeriesTooltip'; import { useChartTooltipZIndex } from '@/components/charts/ChartTooltip'; import { MAX_LOADABLE_TIME_CHART_SERIES, @@ -54,6 +59,7 @@ import { useQueriedChartConfig, } from '@/hooks/useChartConfig'; import { useMVOptimizationExplanation } from '@/hooks/useMVOptimizationExplanation'; +import { aggConditionScopeFilter } from '@/hooks/useReleaseAnnotations'; import { useChartNumberFormats, useSource } from '@/source'; import type { NumberFormat } from '@/types'; @@ -143,6 +149,62 @@ export function decodeSeriesGroupFilters({ return groupFilters; } +/** + * The WHERE the clicked series aggregated over, as a filter for the drill-down + * search. + * + * A builder time chart does not put a series' filter in the statement-level + * `where` — each series carries its own `aggCondition`, applied inside that + * series' aggregate. A search built from chart-level state alone therefore + * spans every series, and its counts don't reconcile with the clicked line. + * + * Value columns map positionally onto `select` (the contract + * `useChartNumberFormats` relies on), so the clicked series' result column + * names the series that produced it. + * + * When no single series can be attributed — a whole-bucket click, a merged + * ratio column, or a formula column — fall back to the union of every series' + * condition, which is the scan scope the chart itself used + * (`aggConditionScopeFilter` yields nothing once any series is unfiltered). + * Series mixing SQL and Lucene conditions also yield nothing: the search URL + * ANDs its filters and carries one `where` language, so it has no way to OR + * them, and an unscoped search is the safe superset. + */ +export function resolveSeriesConditionFilter({ + config, + resultColumns, + valueColumnName, +}: { + config: BuilderChartConfigWithDateRange; + /** Result column names in query order, i.e. `data.meta`. */ + resultColumns: string[]; + /** Result column behind the clicked series; absent for a whole-bucket click. */ + valueColumnName?: string; +}): Filter | undefined { + const select = config.select; + if (!Array.isArray(select) || select.length === 0) { + return undefined; + } + + // Ratio configs merge two series into one column, and a formula config with + // hidden operands projects only formula columns — neither lines up with + // `select` positionally (same guard as DBTableChart's per-column colors). + const columnsMapToSelect = + !isRatioChartConfig(select, config) && + !((config.formulas?.length ?? 0) > 0 && config.showOperandSeries === false); + + const index = + columnsMapToSelect && valueColumnName != null + ? resultColumns.indexOf(valueColumnName) + : -1; + const clickedSeries = + index >= 0 && index < select.length ? select[index] : undefined; + + return aggConditionScopeFilter( + clickedSeries != null ? [clickedSeries] : select, + ); +} + // The interactive PINNED tooltip, rendered over the chart in a body-portaled // Mantine Popover anchored at the clicked point. Hover uses the recharts tooltip // in MemoChart instead; this is only for the click-locked state. @@ -160,7 +222,7 @@ function ChartTooltipOverlay({ expanded, }: { payload: ActiveClickPayload | undefined; - buildSearchUrl: (key?: string, value?: number) => string | null; + buildSearchUrl: (series?: ClickedSeries) => string | null; onDismiss: () => void; /** Focus a series by its raw series key (dataKey) and display name. */ onFocusSeries: (payload: { dataKey?: string; name: string }) => void; @@ -727,8 +789,17 @@ function DBTimeChartComponent({ : undefined; }, [activeClickPayload]); + const resultColumns = useMemo( + () => (data?.meta ?? []).map(column => column.name), + [data?.meta], + ); + const buildSearchUrl = useCallback( - (seriesKey?: string, seriesValue?: number) => { + ({ + dataKey: seriesKey, + value: seriesValue, + valueColumnName, + }: ClickedSeries = {}) => { // Raw SQL charts are not supported for drill-down as we don't know the source which is being used. if ( clickedActiveLabelDate == null || @@ -848,6 +919,13 @@ function DBTimeChartComponent({ dateRange: [from, to], groupFilters, valueRangeFilter, + seriesCondition: isBuilderChartConfig(expandedConfig) + ? resolveSeriesConditionFilter({ + config: expandedConfig, + resultColumns, + valueColumnName, + }) + : undefined, }); }, [ @@ -856,6 +934,7 @@ function DBTimeChartComponent({ granularity, source, groupColumns, + resultColumns, valueColumns, isSingleValueColumn, ], diff --git a/packages/app/src/components/__tests__/DBTimeChart.test.tsx b/packages/app/src/components/__tests__/DBTimeChart.test.tsx index 3f7ab432b6..1851efaa9e 100644 --- a/packages/app/src/components/__tests__/DBTimeChart.test.tsx +++ b/packages/app/src/components/__tests__/DBTimeChart.test.tsx @@ -1,4 +1,5 @@ import React from 'react'; +import { BuilderChartConfigWithDateRange } from '@hyperdx/common-utils/dist/types'; import { screen, waitFor } from '@testing-library/react'; import userEvent from '@testing-library/user-event'; @@ -8,6 +9,7 @@ import DateRangeIndicator from '@/components/charts/DateRangeIndicator'; import { DBTimeChart, decodeSeriesGroupFilters, + resolveSeriesConditionFilter, } from '@/components/DBTimeChart'; import MVOptimizationIndicator from '@/components/MaterializedViews/MVOptimizationIndicator'; import { MAX_LOADABLE_TIME_CHART_SERIES } from '@/defaults'; @@ -704,6 +706,174 @@ describe('DBTimeChart', () => { }); }); + describe('resolveSeriesConditionFilter', () => { + const series = (aggCondition: string, alias?: string) => ({ + aggFn: 'count' as const, + aggCondition, + aggConditionLanguage: 'lucene' as const, + valueExpression: '', + ...(alias != null ? { alias } : {}), + }); + + const configWith = ( + overrides: Partial, + ): BuilderChartConfigWithDateRange => ({ + ...baseTestConfig, + ...overrides, + }); + + const twoSeriesConfig = configWith({ + select: [ + series("ServiceName:'foo'", 'foo'), + series("ServiceName:'bar'", 'bar'), + ], + }); + + it('resolves the clicked series by its value column', () => { + expect( + resolveSeriesConditionFilter({ + config: twoSeriesConfig, + resultColumns: ['foo', 'bar', '__hdx_time_bucket'], + valueColumnName: 'bar', + }), + ).toEqual({ type: 'lucene', condition: "ServiceName:'bar'" }); + }); + + it('unions every series condition for a whole-bucket click', () => { + expect( + resolveSeriesConditionFilter({ + config: twoSeriesConfig, + resultColumns: ['foo', 'bar', '__hdx_time_bucket'], + }), + ).toEqual({ + type: 'lucene', + condition: "(ServiceName:'foo') OR (ServiceName:'bar')", + }); + }); + + // The chart scans every row once a series is unfiltered, so there is no + // narrower scope to carry into the search. + it('returns nothing when a series has no condition', () => { + expect( + resolveSeriesConditionFilter({ + config: configWith({ + select: [series("ServiceName:'foo'", 'foo'), series('', 'all')], + }), + resultColumns: ['foo', 'all', '__hdx_time_bucket'], + }), + ).toBeUndefined(); + }); + + // The search URL can't OR a SQL condition with a Lucene one. + it('returns nothing when the series conditions mix languages', () => { + expect( + resolveSeriesConditionFilter({ + config: configWith({ + select: [ + series("ServiceName:'foo'", 'foo'), + { + ...series("ServiceName = 'bar'", 'bar'), + aggConditionLanguage: 'sql', + }, + ], + }), + resultColumns: ['foo', 'bar', '__hdx_time_bucket'], + }), + ).toBeUndefined(); + }); + + // A clicked series still resolves even when its siblings use another language. + it('resolves the clicked series in a mixed-language chart', () => { + expect( + resolveSeriesConditionFilter({ + config: configWith({ + select: [ + series("ServiceName:'foo'", 'foo'), + { + ...series("ServiceName = 'bar'", 'bar'), + aggConditionLanguage: 'sql', + }, + ], + }), + resultColumns: ['foo', 'bar', '__hdx_time_bucket'], + valueColumnName: 'bar', + }), + ).toEqual({ type: 'sql', condition: "ServiceName = 'bar'" }); + }); + + // Both series feed the single merged column, so neither can be singled out. + it('unions both series of a ratio config rather than attributing one', () => { + expect( + resolveSeriesConditionFilter({ + config: configWith({ + select: [ + series("ServiceName:'foo'", 'foo'), + series("ServiceName:'bar'", 'bar'), + ], + seriesReturnType: 'ratio', + }), + resultColumns: ['foo/bar', '__hdx_time_bucket'], + valueColumnName: 'foo/bar', + }), + ).toEqual({ + type: 'lucene', + condition: "(ServiceName:'foo') OR (ServiceName:'bar')", + }); + }); + + // With operands hidden the projection is formula columns only, so column 0 + // is not select[0]. + it('unions every series when a formula config hides its operands', () => { + expect( + resolveSeriesConditionFilter({ + config: configWith({ + select: [ + series("ServiceName:'foo'", 'foo'), + series("ServiceName:'bar'", 'bar'), + ], + formulas: [{ expression: 'A/B' }], + showOperandSeries: false, + }), + resultColumns: ['A/B', '__hdx_time_bucket'], + valueColumnName: 'A/B', + }), + ).toEqual({ + type: 'lucene', + condition: "(ServiceName:'foo') OR (ServiceName:'bar')", + }); + }); + + // A formula column sits past the operand columns and has no aggCondition. + it('falls back to the union when a formula column is clicked', () => { + expect( + resolveSeriesConditionFilter({ + config: configWith({ + select: [ + series("ServiceName:'foo'", 'foo'), + series("ServiceName:'bar'", 'bar'), + ], + formulas: [{ expression: 'A/B' }], + }), + resultColumns: ['foo', 'bar', 'A/B', '__hdx_time_bucket'], + valueColumnName: 'A/B', + }), + ).toEqual({ + type: 'lucene', + condition: "(ServiceName:'foo') OR (ServiceName:'bar')", + }); + }); + + it('returns nothing for a raw SQL select list', () => { + expect( + resolveSeriesConditionFilter({ + config: configWith({ select: 'count()' }), + resultColumns: ['count()'], + valueColumnName: 'count()', + }), + ).toBeUndefined(); + }); + }); + it('does not render DateRangeIndicator when MV optimization has no optimized date range and showDateRangeIndicator is false', () => { // Mock useMVOptimizationExplanation to return data without an optimized config jest.mocked(useMVOptimizationExplanation).mockReturnValue({ diff --git a/packages/app/src/components/charts/ChartSeriesTooltip.tsx b/packages/app/src/components/charts/ChartSeriesTooltip.tsx index eb31ec06c0..49f65bdb0f 100644 --- a/packages/app/src/components/charts/ChartSeriesTooltip.tsx +++ b/packages/app/src/components/charts/ChartSeriesTooltip.tsx @@ -155,6 +155,16 @@ function SeriesRow({ ); } +/** The series a drill-down action was invoked on. */ +export type ClickedSeries = { + /** Full series key, e.g. "count · error · api". */ + dataKey?: string; + /** The series' value at the clicked bucket. */ + value?: number; + /** Result column the series was plotted from; identifies its `select` entry. */ + valueColumnName?: string; +}; + export type ChartSeriesTooltipProps = { /** Bucket label (epoch seconds as a string) for the timestamp header. */ activeLabel: string; @@ -164,8 +174,8 @@ export type ChartSeriesTooltipProps = { /** Per-value-column number formats, keyed by result column name. */ numberFormatByKey: Map; previousPeriodOffsetSeconds?: number; - /** Drill-down URL for the whole bucket (no args) or one series (key + value). */ - buildSearchUrl?: (key?: string, value?: number) => string | null; + /** Drill-down URL for the whole bucket (no args) or for one clicked series. */ + buildSearchUrl?: (series?: ClickedSeries) => string | null; /** Dismiss the tooltip (used by links + focus). */ onDismiss?: () => void; /** Focus a series by its raw key + display name. */ @@ -326,10 +336,13 @@ export function ChartSeriesTooltip({ : undefined) ?? fallbackNumberFormat; const seriesUrl = showPerSeriesActions && canDrillDown - ? (buildSearchUrl!( - payload.dataKey, - Number.isFinite(payload.value) ? payload.value : undefined, - ) ?? bucketSearchUrl) + ? (buildSearchUrl!({ + dataKey: payload.dataKey, + value: Number.isFinite(payload.value) + ? payload.value + : undefined, + valueColumnName: payload.valueColumnName, + }) ?? bucketSearchUrl) : undefined; return ( { expect(onDismiss).toHaveBeenCalledTimes(1); }); + // The per-series WHERE lives on the series' `select` entry, which the caller + // can only find from the result column the series was plotted from. + it('identifies the clicked series by its value column when building its Search URL', () => { + const buildSearchUrl = jest.fn( + (series?: { dataKey?: string }) => `/search?series=${series?.dataKey}`, + ); + + renderWithMantine( + , + ); + + expect(buildSearchUrl).toHaveBeenCalledWith(); + expect(buildSearchUrl).toHaveBeenCalledWith({ + dataKey: 'error', + value: 90, + valueColumnName: 'error', + }); + expect(buildSearchUrl).toHaveBeenCalledWith({ + dataKey: 'warn', + value: 10, + valueColumnName: 'warn', + }); + expect(screen.getByTestId('chart-view-events-link-error')).toHaveAttribute( + 'href', + '/search?series=error', + ); + }); + it('renders no tooltip content when every series value is non-finite', () => { renderWithMantine(