@@ -48,7 +48,9 @@ type TimeWindow = {
4848 dateRangeEndInclusive ?: boolean ;
4949} ;
5050
51- type TQueryFnData = Pick < ResponseJSON < any > , 'data' | 'meta' | 'rows' > & { } ;
51+ type TQueryFnData = Pick < ResponseJSON < any > , 'data' | 'meta' | 'rows' > & {
52+ isComplete : boolean ;
53+ } ;
5254
5355const shouldUseChunking = (
5456 config : ChartConfigWithOptDateRange ,
@@ -125,7 +127,6 @@ async function* fetchDataInChunks(
125127 ? getGranularityAlignedTimeWindows ( config )
126128 : ( [ undefined ] as const ) ;
127129
128- let query = null ;
129130 if ( IS_MTVIEWS_ENABLED ) {
130131 const { dataTableDDL, mtViewDDL, renderMTViewConfig } =
131132 await buildMTViewSelectQuery ( config ) ;
@@ -134,10 +135,12 @@ async function* fetchDataInChunks(
134135 console . log ( 'dataTableDDL:' , dataTableDDL ) ;
135136 // eslint-disable-next-line no-console
136137 console . log ( 'mtViewDDL:' , mtViewDDL ) ;
137- query = await renderMTViewConfig ( ) ;
138+ await renderMTViewConfig ( ) ;
138139 }
139140
140- for ( const window of windows ) {
141+ for ( let i = 0 ; i < windows . length ; i ++ ) {
142+ const window = windows [ i ] ;
143+
141144 const windowedConfig = {
142145 ...config ,
143146 ...( window ?? { } ) ,
@@ -151,7 +154,7 @@ async function* fetchDataInChunks(
151154 } ,
152155 } ) ;
153156
154- yield result ;
157+ yield { chunk : result , isComplete : i === windows . length - 1 } ;
155158 }
156159}
157160
@@ -164,13 +167,17 @@ async function* fetchDataInChunks(
164167 *
165168 * For chunked queries, note the following:
166169 * - `config.limit`, if provided, is applied to each chunk, so the total number
167- * of rows returned may be up to `limit * number_of_chunks`.
170+ * of rows returned may be up to `limit * number_of_chunks`.
168171 * - The returned data will be ordered within each chunk, and chunks will
169- * be ordered oldest-first, by the `timestampValueExpression`.
172+ * be ordered oldest-first, by the `timestampValueExpression`.
173+ * - `isPending` is true until the first chunk is fetched. Once the first chunk
174+ * is available, `isPending` will be false and `isSuccess` will be true.
175+ * `isFetching` will be true until all chunks have been fetched.
176+ * - `data.isComplete` indicates whether all chunks have been fetched.
170177 */
171178export function useQueriedChartConfig (
172179 config : ChartConfigWithOptDateRange ,
173- options ?: Partial < UseQueryOptions < ResponseJSON < any > > > &
180+ options ?: Partial < UseQueryOptions < TQueryFnData > > &
174181 AdditionalUseQueriedChartConfigOptions ,
175182) {
176183 const clickhouseClient = useClickhouseClient ( ) ;
@@ -194,12 +201,18 @@ export function useQueriedChartConfig(
194201 * in flickering or render loops.
195202 */
196203 refetchMode : 'replace' ,
197- initialValue : { data : [ ] , meta : [ ] , rows : 0 } as TQueryFnData ,
198- reducer : ( acc , chunk ) => {
204+ initialValue : {
205+ data : [ ] ,
206+ meta : [ ] ,
207+ rows : 0 ,
208+ isComplete : false ,
209+ } as TQueryFnData ,
210+ reducer : ( acc , { chunk, isComplete } ) => {
199211 return {
200212 data : [ ...( chunk . data || [ ] ) , ...( acc ?. data || [ ] ) ] ,
201213 meta : chunk . meta ,
202214 rows : ( acc ?. rows || 0 ) + ( chunk . rows || 0 ) ,
215+ isComplete,
203216 } ;
204217 } ,
205218 } ) ,
0 commit comments