@@ -5,7 +5,10 @@ import {
55 ResponseJSON ,
66} from '@hyperdx/common-utils/dist/clickhouse' ;
77import { ClickhouseClient } from '@hyperdx/common-utils/dist/clickhouse/browser' ;
8- import { renderChartConfig } from '@hyperdx/common-utils/dist/renderChartConfig' ;
8+ import {
9+ DEFAULT_AUTO_GRANULARITY_MAX_BUCKETS ,
10+ renderChartConfig ,
11+ } from '@hyperdx/common-utils/dist/renderChartConfig' ;
912import { format } from '@hyperdx/common-utils/dist/sqlFormatter' ;
1013import { ChartConfigWithOptDateRange } from '@hyperdx/common-utils/dist/types' ;
1114import {
@@ -14,7 +17,10 @@ import {
1417 UseQueryOptions ,
1518} from '@tanstack/react-query' ;
1619
17- import { toStartOfInterval } from '@/ChartUtils' ;
20+ import {
21+ convertDateRangeToGranularityString ,
22+ toStartOfInterval ,
23+ } from '@/ChartUtils' ;
1824import { useClickhouseClient } from '@/clickhouse' ;
1925import { IS_MTVIEWS_ENABLED } from '@/config' ;
2026import { buildMTViewSelectQuery } from '@/hdxMTViews' ;
@@ -39,18 +45,25 @@ export const getGranularityAlignedTimeWindows = (
3945 > ,
4046 windowDurationsSeconds ?: number [ ] ,
4147) : TimeWindow [ ] | [ undefined ] => {
42- // Granularity is required for pagination, otherwise we could break other group-bys
48+ // Granularity is required for pagination, otherwise we could break other group-bys.
4349 // Date range is required for pagination, otherwise we'd have infinite pages, or some unbounded page(s).
4450 if ( ! config . dateRange || ! config . granularity ) return [ undefined ] ;
4551
4652 const [ startDate , endDate ] = config . dateRange ;
47- const granularity = config . granularity ;
4853 const windowsUnaligned = generateTimeWindowsDescending (
4954 startDate ,
5055 endDate ,
5156 windowDurationsSeconds ,
5257 ) ;
5358
59+ const granularity =
60+ config . granularity === 'auto' // TODO test this
61+ ? convertDateRangeToGranularityString (
62+ config . dateRange ,
63+ DEFAULT_AUTO_GRANULARITY_MAX_BUCKETS ,
64+ )
65+ : config . granularity ;
66+
5467 const windows = [ ] ;
5568 for ( const [ index , window ] of windowsUnaligned . entries ( ) ) {
5669 // Align windows to chart buckets
@@ -137,7 +150,7 @@ export function useQueriedChartConfig(
137150 initialValue : { data : [ ] , meta : [ ] , rows : 0 } as TQueryFnData ,
138151 reducer : ( acc , chunk ) => {
139152 return {
140- data : [ ...( acc ? .data || [ ] ) , ...( chunk . data || [ ] ) ] ,
153+ data : [ ...( chunk . data || [ ] ) , ...( acc ? .data || [ ] ) ] ,
141154 meta : chunk . meta ,
142155 rows : ( acc ?. rows || 0 ) + ( chunk . rows || 0 ) ,
143156 } ;
@@ -155,13 +168,8 @@ export function useQueriedChartConfig(
155168 return query ;
156169}
157170
158- // TODO: Can we always search backwards or do we need to support forwards too?
159171// TODO: Check if this is impacted by timezones or DST changes --> Everything UTC? Should be fine.
160- // TODO: See if we can combine this with the useOffsetPaginatedQuery stuff
161- // TODO: How does live mode affect this?
162- // TODO: Can we remove the IS_MTVIEWS_ENABLED stuff?
163- // TODO: How is caching working?
164- // Test that this works as expected with no pagination in place (eg. no granularity or no date range)
172+ // TODO: Test that this works as expected with no pagination in place (eg. no granularity or no date range)
165173
166174export function useRenderedSqlChartConfig (
167175 config : ChartConfigWithOptDateRange ,
0 commit comments