diff --git a/.changeset/histogram-skip-column-refetch.md b/.changeset/histogram-skip-column-refetch.md new file mode 100644 index 000000000..54b1ea46e --- /dev/null +++ b/.changeset/histogram-skip-column-refetch.md @@ -0,0 +1,5 @@ +--- +'@hyperdx/app': patch +--- + +fix: Don't refetch the search histogram when a table column is added or removed diff --git a/packages/app/src/ChartUtils.tsx b/packages/app/src/ChartUtils.tsx index 98689c44b..5eb4663e3 100644 --- a/packages/app/src/ChartUtils.tsx +++ b/packages/app/src/ChartUtils.tsx @@ -214,6 +214,9 @@ export function convertToTimeChartConfig( // Overwrite (not conditionally spread) so a cleared `null` from the // source config is normalized to undefined rather than carried over. seriesLimit, + // Only feeds "View events" links (read from the unconverted config); + // leaving it in would refetch the chart whenever a table column changes. + eventTableSelect: undefined, } : { ...config, diff --git a/packages/app/src/DBSearchPage.tsx b/packages/app/src/DBSearchPage.tsx index e9b867885..e75e0bde8 100644 --- a/packages/app/src/DBSearchPage.tsx +++ b/packages/app/src/DBSearchPage.tsx @@ -166,6 +166,7 @@ import DBSqlRowTableWithSideBar from './components/DBSqlRowTableWithSidebar'; import PatternTable from './components/PatternTable'; import { DBSearchHeatmapChart } from './components/Search/DBSearchHeatmapChart'; import DirectTraceSidePanel from './components/Search/DirectTraceSidePanel'; +import { getReferencedWithClauses } from './components/Search/histogramWith'; import { TRACE_CHART_MODES, TraceRedMetricsChart, @@ -1304,13 +1305,22 @@ function DBSearchPageContent() { const [displayedTimeInputValue, setDisplayedTimeInputValue] = useState('Live Tail'); + // The last time input the time query wrote, i.e. the applied range. User + // typing goes through setDisplayedTimeInputValue, so it differs once edited. + const [appliedTimeInputValue, setAppliedTimeInputValue] = + useState('Live Tail'); + const setAppliedTimeInput = useCallback((value: string) => { + setDisplayedTimeInputValue(value); + setAppliedTimeInputValue(value); + }, []); + const isTimeInputEdited = displayedTimeInputValue !== appliedTimeInputValue; const { isReady, searchedTimeRange, onSearch, onTimeRangeSelect } = useNewTimeQuery({ initialDisplayValue: 'Live Tail', initialTimeRange: defaultTimeRange, showRelativeInterval: isLive ?? true, - setDisplayedTimeInputValue, + setDisplayedTimeInputValue: setAppliedTimeInput, updateInput: !isLive, }); @@ -1420,8 +1430,13 @@ function DBSearchPageContent() { // completes "Explore your data". An explicit arg, not a shared ref, so // concurrent submits can't consume each other's suppression. const onSubmit = useCallback( - ({ recordExploration = true }: { recordExploration?: boolean } = {}) => { - onSearch(displayedTimeInputValue); + ({ + recordExploration = true, + resolveTimeRange = true, + }: { recordExploration?: boolean; resolveTimeRange?: boolean } = {}) => { + if (resolveTimeRange) { + onSearch(displayedTimeInputValue); + } handleSubmit( ({ select, where, whereLanguage, source, filters, orderBy }) => { setSearchedConfig({ @@ -1858,12 +1873,15 @@ function DBSearchPageContent() { parseAsInteger.withDefault(DEFAULT_REFRESH_FREQUENCY), ); - const updateRelativeTimeInputValue = useCallback((interval: number) => { - const label = getRelativeTimeOptionLabel(interval); - if (label) { - setDisplayedTimeInputValue(label); - } - }, []); + const updateRelativeTimeInputValue = useCallback( + (interval: number) => { + const label = getRelativeTimeOptionLabel(interval); + if (label) { + setAppliedTimeInput(label); + } + }, + [setAppliedTimeInput], + ); useEffect(() => { if (isReady && isLive) { @@ -1995,7 +2013,11 @@ function DBSearchPageContent() { setValue('orderBy', nextOrderBy); } } - onSubmit(); + // Keep the current time range unless the user typed a new one: + // re-resolving a relative range would refetch the histogram for a + // column change. Live tail refetches on every tick anyway, so it always + // re-resolves. + onSubmit({ resolveTimeRange: !!isLive || isTimeInputEdited }); }, [ displayedColumns, @@ -2004,6 +2026,8 @@ function DBSearchPageContent() { getValues, defaultSearchConfig.orderBy, onSubmit, + isLive, + isTimeInputEdited, ], ); @@ -2141,6 +2165,7 @@ function DBSearchPageContent() { return undefined; } + const groupBy = resolveSearchGroupBy(groupByParam, searchedSource); return { ...chartConfig, select: [ @@ -2154,8 +2179,14 @@ function DBSearchPageContent() { granularity: 'auto', dateRange: searchedTimeRange, displayType: DisplayType.StackedBar, - with: aliasWith, - // Preserve the original table select string for "View Events" links + // Everything but the table's select and sort can reference an alias, + // including the body, timestamp and sample-weight expressions. + with: getReferencedWithClauses(aliasWith, [ + JSON.stringify({ ...chartConfig, select: '', orderBy: '' }), + groupBy ?? '', + ]), + // Preserve the original table select string for "View Events" links. + // convertToTimeChartConfig drops it from the query key. eventTableSelect: searchedConfig.select ?? undefined, // Never align to granularity boundaries on the search page: the histogram // and total count must reflect the user's exact selected range so they @@ -2166,7 +2197,7 @@ function DBSearchPageContent() { alignDateRangeToGranularity: false, // Make sure the end date is inclusive so that the histogram and table counts match dateRangeEndInclusive: true, - groupBy: resolveSearchGroupBy(groupByParam, searchedSource), + groupBy, } satisfies BuilderChartConfigWithDateRange; }, [ chartConfig, diff --git a/packages/app/src/__tests__/ChartUtils.test.ts b/packages/app/src/__tests__/ChartUtils.test.ts index 612f026a4..e570ae3b4 100644 --- a/packages/app/src/__tests__/ChartUtils.test.ts +++ b/packages/app/src/__tests__/ChartUtils.test.ts @@ -1382,6 +1382,26 @@ describe('ChartUtils', () => { expect(granularityFromFunction).toBe('5 minute'); }); + it('drops eventTableSelect so table column changes do not change the query key', () => { + const config: BuilderChartConfigWithDateRange = { + select: 'count()', + from: { databaseName: 'test', tableName: 'logs' }, + where: '', + timestampValueExpression: 'timestamp', + connection: 'test-connection', + granularity: '5 minute', + dateRange: [ + new Date('2025-11-26T00:00:00Z'), + new Date('2025-11-27T00:00:00Z'), + ], + eventTableSelect: 'Timestamp, Body', + }; + + expect(convertToTimeChartConfig(config)).toMatchObject({ + eventTableSelect: undefined, + }); + }); + // seriesLimit lives on the builder member of the ChartConfigWithDateRange // union, so narrow the result before reading it. The per-tile value is // read from the config itself (no team override anymore). diff --git a/packages/app/src/components/Search/__tests__/histogramWith.test.ts b/packages/app/src/components/Search/__tests__/histogramWith.test.ts new file mode 100644 index 000000000..4b2d118e3 --- /dev/null +++ b/packages/app/src/components/Search/__tests__/histogramWith.test.ts @@ -0,0 +1,39 @@ +import { getReferencedWithClauses } from '@/components/Search/histogramWith'; + +const clause = (name: string, sql: string) => ({ + name, + sql: { sql, params: {} }, + isSubquery: false, +}); + +describe('getReferencedWithClauses', () => { + const withClauses = [ + clause('service', 'ServiceName'), + clause('body', 'toString(Body)'), + clause('shout', 'upper(body)'), + ]; + + it('returns undefined when nothing is referenced', () => { + expect( + getReferencedWithClauses(withClauses, ['level:error', '[]']), + ).toBeUndefined(); + }); + + it('keeps clauses referenced by the where, filters or group by', () => { + expect( + getReferencedWithClauses(withClauses, ["service = 'api'", '[]', ''])?.map( + c => c.name, + ), + ).toEqual(['service']); + }); + + it('keeps aliases that a referenced alias depends on', () => { + expect( + getReferencedWithClauses(withClauses, ['shout:ERROR'])?.map(c => c.name), + ).toEqual(['body', 'shout']); + }); + + it('handles missing clauses', () => { + expect(getReferencedWithClauses(undefined, ['service'])).toBeUndefined(); + }); +}); diff --git a/packages/app/src/components/Search/histogramWith.ts b/packages/app/src/components/Search/histogramWith.ts new file mode 100644 index 000000000..1fb301b55 --- /dev/null +++ b/packages/app/src/components/Search/histogramWith.ts @@ -0,0 +1,33 @@ +import { BuilderChartConfig } from '@hyperdx/common-utils/dist/types'; + +/** + * Keeps only the alias WITH clauses that `texts` (the query outside the + * table's select) reference, directly or through another kept alias. The histogram + * doesn't select the table's columns, so the rest only change its query key + * and refetch it whenever a column is added or removed. + */ +export function getReferencedWithClauses( + withClauses: BuilderChartConfig['with'], + texts: string[], +): BuilderChartConfig['with'] { + if (!withClauses) return undefined; + + const kept = new Set(); + let haystack = texts.join('\n'); + let added = true; + while (added) { + added = false; + for (const clause of withClauses) { + // Substring match over-includes (e.g. `svc` in `svc_name`), which only + // costs a refetch; under-including would break the query. + if (!kept.has(clause.name) && haystack.includes(clause.name)) { + kept.add(clause.name); + haystack += `\n${clause.sql?.sql ?? ''}`; + added = true; + } + } + } + + const referenced = withClauses.filter(clause => kept.has(clause.name)); + return referenced.length > 0 ? referenced : undefined; +}