Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 12 additions & 0 deletions .changeset/chart-search-series-where.md
Original file line number Diff line number Diff line change
@@ -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).
17 changes: 17 additions & 0 deletions packages/app/src/ChartUtils.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -1336,17 +1344,26 @@ 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) &&
config.select.length === 1
) {
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 }) => {
Expand Down
84 changes: 84 additions & 0 deletions packages/app/src/__tests__/eventsSearchUrl.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import {
BuilderChartConfigWithDateRange,
ChartVariable,
DisplayType,
Filter,
SourceKind,
TLogSource,
} from '@hyperdx/common-utils/dist/types';
Expand Down Expand Up @@ -190,6 +191,89 @@ describe('buildEventsSearchUrl variable expansion', () => {
});
});

describe('buildEventsSearchUrl series conditions', () => {
const series = (aggCondition: string) => ({
...builderConfig.select[0],
aggCondition,
});

const buildWithSeriesCondition = (
overrides: Partial<BuilderChartConfigWithDateRange>,
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,
Expand Down
85 changes: 82 additions & 3 deletions packages/app/src/components/DBTimeChart.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -21,6 +22,7 @@ import {
BuilderChartConfigWithDateRange,
ChartConfigWithDateRange,
DisplayType,
Filter,
} from '@hyperdx/common-utils/dist/types';
import { Popover, Portal } from '@mantine/core';
import {
Expand All @@ -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,
Expand All @@ -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';

Expand Down Expand Up @@ -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,
);
Comment on lines +203 to +205

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

P1 Mixed languages lose scope

When a whole-bucket, ratio, or formula action falls back to all series and those series use different condition languages, aggConditionScopeFilter returns undefined, so the Search URL drops every series condition. The chart query still renders each condition in its own language and ORs them together. The resulting search is therefore broader than the chart, and its results will not reconcile with the chart.

Fix in Claude Code Fix in Conductor Fix in Cursor Fix in Codex

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Accurate, but not something this PR introduced or can fix here. On main every drill-down drops all series conditions, so this case is no worse than before. The per-series Search is unaffected: a single clicked series has one language.

The search URL can't express the OR: it carries one where language and ANDs its filters. Turning Lucene into SQL needs async column metadata, and adding an OR mode to the search page's filters would also change how its sidebar filters combine. So the fallback leaves the search unscoped (a superset of the chart's rows) rather than scoped wrongly. I documented that in resolveSeriesConditionFilter and added tests for both the mixed-language fallback and a clicked series in a mixed-language chart (2ce2db9).

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Agreed. The mixed-language whole-bucket fallback is a pre-existing limitation of the search URL/filter model, and this change preserves the existing unscoped behavior when the union cannot be represented. The per-series path remains correctly scoped because it resolves a single condition and therefore a single filter language. The documentation and tests cover this tradeoff, so no change is needed for this PR.

}

// 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.
Expand All @@ -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;
Expand Down Expand Up @@ -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 ||
Expand Down Expand Up @@ -848,6 +919,13 @@ function DBTimeChartComponent({
dateRange: [from, to],
groupFilters,
valueRangeFilter,
seriesCondition: isBuilderChartConfig(expandedConfig)
? resolveSeriesConditionFilter({
config: expandedConfig,
resultColumns,
valueColumnName,
})
: undefined,
});
},
[
Expand All @@ -856,6 +934,7 @@ function DBTimeChartComponent({
granularity,
source,
groupColumns,
resultColumns,
valueColumns,
isSingleValueColumn,
],
Expand Down
Loading
Loading