Skip to content

Commit 63fcf14

Browse files
authored
fix: optimize query key for aliasMap to prevent jitter (#1351)
Fixes: HDX-2787 During live tail, the date range changes every few seconds (e.g., from 9:00-9:15 to 9:02-9:17, etc...). The original aliasMap query key included the entire config object, which contains the dateRange property. Every date range change triggered a refetch of the alias map, even though aliases are derived from the SELECT statement and not from the date range. While refetching, react-query sets aliasMap to undefined. This caused column IDs to change. React-table uses column IDs as keys to track resize state, so when the ID changes, it loses the stored width and resets to the default size, causing the visible jitter. Now we have a consistent aliasMap with the added benefit of less network requests.
1 parent 64b5673 commit 63fcf14

File tree

3 files changed

+30
-6
lines changed

3 files changed

+30
-6
lines changed

.changeset/pretty-sloths-admire.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
"@hyperdx/app": patch
3+
---
4+
5+
fix: optimize query key for aliasMap to prevent jitter

packages/app/src/DBSearchPage.tsx

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,7 @@ import {
5757
useDocumentVisibility,
5858
} from '@mantine/hooks';
5959
import { notifications } from '@mantine/notifications';
60-
import { keepPreviousData, useIsFetching } from '@tanstack/react-query';
60+
import { useIsFetching } from '@tanstack/react-query';
6161
import { SortingState } from '@tanstack/react-table';
6262
import CodeMirror from '@uiw/react-codemirror';
6363

@@ -1147,10 +1147,7 @@ function DBSearchPage() {
11471147
}
11481148
}, [isReady, queryReady, isChartConfigLoading, onSearch]);
11491149

1150-
const { data: aliasMap } = useAliasMapFromChartConfig(dbSqlRowTableConfig, {
1151-
placeholderData: keepPreviousData,
1152-
queryKey: ['aliasMap', dbSqlRowTableConfig, 'withPlaceholder'],
1153-
});
1150+
const { data: aliasMap } = useAliasMapFromChartConfig(dbSqlRowTableConfig);
11541151

11551152
const aliasWith = useMemo(
11561153
() =>

packages/app/src/hooks/useChartConfig.tsx

Lines changed: 23 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -286,8 +286,30 @@ export function useAliasMapFromChartConfig(
286286
config: ChartConfigWithOptDateRange | undefined,
287287
options?: UseQueryOptions<Record<string, string>>,
288288
) {
289+
// For granularity: 'auto', the bucket size depends on dateRange duration (not absolute times).
290+
// Include duration in key to detect when bucket size changes, but omit absolute times
291+
// to prevent refetches when the time window just slides forward (e.g., live tail).
292+
const dateRangeDuration =
293+
config?.dateRange && isUsingGranularity(config)
294+
? config.dateRange[1].getTime() - config.dateRange[0].getTime()
295+
: undefined;
296+
289297
return useQuery<Record<string, string>>({
290-
queryKey: ['aliasMap', config],
298+
// Only include config properties that affect SELECT structure and aliases.
299+
// When adding new ChartConfig fields, check renderChartConfig.ts to see if they
300+
// affect the SELECT clause. If yes, add them here to avoid stale alias maps.
301+
queryKey: [
302+
'aliasMap',
303+
config?.select,
304+
config?.from,
305+
config?.connection,
306+
config?.with,
307+
config?.groupBy,
308+
config?.selectGroupBy,
309+
config?.granularity,
310+
config?.seriesReturnType,
311+
dateRangeDuration,
312+
],
291313
queryFn: async () => {
292314
if (config == null) {
293315
return {};

0 commit comments

Comments
 (0)