diff --git a/.changeset/carry-where-aggcondition-on-chart-type-switch.md b/.changeset/carry-where-aggcondition-on-chart-type-switch.md new file mode 100644 index 0000000000..b4181e364b --- /dev/null +++ b/.changeset/carry-where-aggcondition-on-chart-type-switch.md @@ -0,0 +1,5 @@ +--- +'@hyperdx/app': patch +--- + +fix: Carry the Search WHERE filter over to the series aggregation condition (and back) when switching chart types, so a filter no longer resets on switching between Search and other chart types diff --git a/packages/app/src/components/DBEditTimeChartForm/EditTimeChartForm.tsx b/packages/app/src/components/DBEditTimeChartForm/EditTimeChartForm.tsx index fdb798e7e5..9adcb5d69d 100644 --- a/packages/app/src/components/DBEditTimeChartForm/EditTimeChartForm.tsx +++ b/packages/app/src/components/DBEditTimeChartForm/EditTimeChartForm.tsx @@ -767,6 +767,23 @@ export default function EditTimeChartForm({ isStringSelectDisplayType(displayType) && typeof select !== 'string' ) { + // Carry a single builder series' aggCondition back into `where` so a + // filter set in the builder survives the switch to Search/EventPatterns + // (which only edit `where`). Only with exactly one series and an empty + // `where`, so a filter isn't guessed across series or allowed to + // clobber an existing `where`. See HDX-5602. + const currentSeries = getValues('series'); + if (Array.isArray(currentSeries) && currentSeries.length === 1) { + const condition = currentSeries[0]?.aggCondition ?? ''; + const currentWhere = getValues('where') ?? ''; + if (condition && !currentWhere) { + setValue('where', condition); + const language = currentSeries[0]?.aggConditionLanguage; + if (language) { + setValue('whereLanguage', language); + } + } + } setValue('select', ''); setValue('series', []); } else if (displayType === DisplayType.Heatmap) { @@ -786,11 +803,17 @@ export default function EditTimeChartForm({ } applyHeatmapDefaults(setValue, defaultValue); } else if (!Array.isArray(select)) { + // Carry a Search/EventPatterns `where` filter into the new single + // series' aggCondition so a filter set on those tabs survives the + // switch to a builder chart type, which filters per-series rather than + // via the (hidden) chart-level `where`. See HDX-5602. + const carriedWhere = getValues('where') ?? ''; const defaultSeries: SavedChartConfigWithSelectArray['select'] = [ { aggFn: 'count', - aggCondition: '', - aggConditionLanguage: getStoredLanguage() ?? 'lucene', + aggCondition: carriedWhere, + aggConditionLanguage: + getValues('whereLanguage') ?? getStoredLanguage() ?? 'lucene', valueExpression: '', }, ]; @@ -806,7 +829,15 @@ export default function EditTimeChartForm({ onSubmit(true); } } - }, [displayType, select, setValue, onSubmit, configType, tableSource]); + }, [ + displayType, + select, + setValue, + getValues, + onSubmit, + configType, + tableSource, + ]); // Handle auto-submitting and form state updates when the source changes. useEffect(() => { diff --git a/packages/app/tests/e2e/components/ChartEditorComponent.ts b/packages/app/tests/e2e/components/ChartEditorComponent.ts index 213d02594a..1982e6bbbe 100644 --- a/packages/app/tests/e2e/components/ChartEditorComponent.ts +++ b/packages/app/tests/e2e/components/ChartEditorComponent.ts @@ -160,6 +160,16 @@ export class ChartEditorComponent { await this.whereInput(scope).typeLucene(text); } + /** + * Read the current text of a WHERE input in Lucene mode, where it renders as + * a plain textarea. Switches the input to Lucene first (the mode is sticky in + * localStorage, so a prior spec can leave it on SQL). + */ + async getLuceneWhere(scope: 'chart' | 'series' = 'chart'): Promise { + await this.setWhereLanguage('Lucene', scope); + return this.whereInput(scope).luceneInput.inputValue(); + } + /** * Type `prefix` into a SQL WHERE input to open its autocomplete popup, and * report what it offers plus the help panel of the highlighted suggestion. diff --git a/packages/app/tests/e2e/features/chart-explorer.spec.ts b/packages/app/tests/e2e/features/chart-explorer.spec.ts index acb05b582c..2e462fd2b1 100644 --- a/packages/app/tests/e2e/features/chart-explorer.spec.ts +++ b/packages/app/tests/e2e/features/chart-explorer.spec.ts @@ -359,4 +359,73 @@ test.describe('Chart Explorer Functionality', { tag: ['@charts'] }, () => { }); }, ); + + // HDX-5602: a filter set on the Search tab lives in the chart-level `where`, + // while the builder chart types filter per-series via `aggCondition`. The + // editor carries the value across so the filter does not silently reset when + // the user switches chart types. + test( + 'should carry a Search WHERE filter into the series when switching to Time Series', + { tag: '@full-stack' }, + async () => { + const filter = 'ServiceName:"frontend"'; + + await test.step('Wait for the chart editor data to load', async () => { + await chartExplorerPage.chartEditor.waitForDataToLoad(); + }); + + await test.step('Select the E2E Logs source', async () => { + await chartExplorerPage.chartEditor.selectSource( + DEFAULT_LOGS_SOURCE_NAME, + ); + }); + + await test.step('Switch to the Search chart type and enter a filter', async () => { + await chartExplorerPage.chartEditor.setChartType(DisplayType.Search); + await chartExplorerPage.chartEditor.setWhereLanguage('Lucene', 'chart'); + await chartExplorerPage.chartEditor.typeLuceneWhere(filter, 'chart'); + }); + + await test.step('Switch to Time Series and verify the filter carried into the series WHERE', async () => { + await chartExplorerPage.chartEditor.setChartType(DisplayType.Line); + await expect + .poll(() => chartExplorerPage.chartEditor.getLuceneWhere('series')) + .toBe(filter); + }); + }, + ); + + test( + 'should carry a single series WHERE filter into the Search WHERE when switching to Search', + { tag: '@full-stack' }, + async () => { + const filter = 'ServiceName:"frontend"'; + + await test.step('Wait for the chart editor data to load', async () => { + await chartExplorerPage.chartEditor.waitForDataToLoad(); + }); + + await test.step('Select the E2E Logs source', async () => { + await chartExplorerPage.chartEditor.selectSource( + DEFAULT_LOGS_SOURCE_NAME, + ); + }); + + await test.step('On Time Series, enter a filter in the series WHERE', async () => { + await chartExplorerPage.chartEditor.setChartType(DisplayType.Line); + await chartExplorerPage.chartEditor.setWhereLanguage( + 'Lucene', + 'series', + ); + await chartExplorerPage.chartEditor.typeLuceneWhere(filter, 'series'); + }); + + await test.step('Switch to Search and verify the filter carried into the chart WHERE', async () => { + await chartExplorerPage.chartEditor.setChartType(DisplayType.Search); + await expect + .poll(() => chartExplorerPage.chartEditor.getLuceneWhere('chart')) + .toBe(filter); + }); + }, + ); });