@@ -5,7 +5,10 @@ import {
5
5
ResponseJSON ,
6
6
} from '@hyperdx/common-utils/dist/clickhouse' ;
7
7
import { 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' ;
9
12
import { format } from '@hyperdx/common-utils/dist/sqlFormatter' ;
10
13
import { ChartConfigWithOptDateRange } from '@hyperdx/common-utils/dist/types' ;
11
14
import {
@@ -14,7 +17,10 @@ import {
14
17
UseQueryOptions ,
15
18
} from '@tanstack/react-query' ;
16
19
17
- import { toStartOfInterval } from '@/ChartUtils' ;
20
+ import {
21
+ convertDateRangeToGranularityString ,
22
+ toStartOfInterval ,
23
+ } from '@/ChartUtils' ;
18
24
import { useClickhouseClient } from '@/clickhouse' ;
19
25
import { IS_MTVIEWS_ENABLED } from '@/config' ;
20
26
import { buildMTViewSelectQuery } from '@/hdxMTViews' ;
@@ -39,18 +45,25 @@ export const getGranularityAlignedTimeWindows = (
39
45
> ,
40
46
windowDurationsSeconds ?: number [ ] ,
41
47
) : 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.
43
49
// Date range is required for pagination, otherwise we'd have infinite pages, or some unbounded page(s).
44
50
if ( ! config . dateRange || ! config . granularity ) return [ undefined ] ;
45
51
46
52
const [ startDate , endDate ] = config . dateRange ;
47
- const granularity = config . granularity ;
48
53
const windowsUnaligned = generateTimeWindowsDescending (
49
54
startDate ,
50
55
endDate ,
51
56
windowDurationsSeconds ,
52
57
) ;
53
58
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
+
54
67
const windows = [ ] ;
55
68
for ( const [ index , window ] of windowsUnaligned . entries ( ) ) {
56
69
// Align windows to chart buckets
@@ -137,7 +150,7 @@ export function useQueriedChartConfig(
137
150
initialValue : { data : [ ] , meta : [ ] , rows : 0 } as TQueryFnData ,
138
151
reducer : ( acc , chunk ) => {
139
152
return {
140
- data : [ ...( acc ? .data || [ ] ) , ...( chunk . data || [ ] ) ] ,
153
+ data : [ ...( chunk . data || [ ] ) , ...( acc ? .data || [ ] ) ] ,
141
154
meta : chunk . meta ,
142
155
rows : ( acc ?. rows || 0 ) + ( chunk . rows || 0 ) ,
143
156
} ;
@@ -155,13 +168,8 @@ export function useQueriedChartConfig(
155
168
return query ;
156
169
}
157
170
158
- // TODO: Can we always search backwards or do we need to support forwards too?
159
171
// 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)
165
173
166
174
export function useRenderedSqlChartConfig (
167
175
config : ChartConfigWithOptDateRange ,
0 commit comments