14
14
15
15
import m from 'mithril' ;
16
16
import { ColorScheme } from '../../base/color_scheme' ;
17
- import { assertTrue } from '../../base/logging' ;
18
17
import { Time } from '../../base/time' ;
19
18
import { TrackEventDetailsPanel } from '../../public/details_panel' ;
20
19
import { TrackEventDetails , TrackEventSelection } from '../../public/selection' ;
@@ -96,7 +95,7 @@ export interface DatasetSliceTrackAttrs<T extends DatasetSchema> {
96
95
* different layers will be mipmapped independency of each other, and the
97
96
* buckets of higher layers will be rendered on top of lower layers.
98
97
*/
99
- readonly dataset : SourceDataset < T > ;
98
+ readonly dataset : SourceDataset < T > | ( ( ) => SourceDataset < T > ) ;
100
99
101
100
/**
102
101
* An optional initial estimate for the maximum depth value. Helps minimize
@@ -182,41 +181,39 @@ export type ROW_SCHEMA = typeof rowSchema;
182
181
// resolved properly.
183
182
type SliceWithRow < T > = Slice & { row : T } ;
184
183
184
+ function getDataset < T extends DatasetSchema > (
185
+ attrs : DatasetSliceTrackAttrs < T > ,
186
+ ) : SourceDataset < T > {
187
+ const dataset = attrs . dataset ;
188
+ return typeof dataset === 'function' ? dataset ( ) : dataset ;
189
+ }
190
+
185
191
export class DatasetSliceTrack < T extends ROW_SCHEMA > extends BaseSliceTrack <
186
192
SliceWithRow < T > ,
187
193
BaseRow & T
188
194
> {
189
- protected readonly sqlSource : string ;
190
195
readonly rootTableName ?: string ;
191
196
192
197
constructor ( private readonly attrs : DatasetSliceTrackAttrs < T > ) {
198
+ const dataset = getDataset ( attrs ) ;
193
199
super (
194
200
attrs . trace ,
195
201
attrs . uri ,
196
- { ...BASE_ROW , ...attrs . dataset . schema } ,
202
+ { ...BASE_ROW , ...dataset . schema } ,
197
203
attrs . sliceLayout ,
198
204
attrs . initialMaxDepth ,
199
205
attrs . instantStyle ?. width ,
200
206
) ;
201
- const { dataset} = attrs ;
202
-
203
- // This is the minimum viable implementation that the source dataset must
204
- // implement for the track to work properly. Typescript should enforce this
205
- // now, but typescript can be worked around, and checking it is cheap.
206
- // Better to error out early.
207
- assertTrue ( this . attrs . dataset . implements ( rowSchema ) ) ;
208
-
209
- this . sqlSource = this . generateRenderQuery ( dataset ) ;
210
207
this . rootTableName = attrs . rootTableName ;
211
208
}
212
209
213
210
override rowToSlice ( row : BaseRow & T ) : SliceWithRow < T > {
214
211
const slice = this . rowToSliceBase ( row ) ;
215
212
const title = this . getTitle ( row ) ;
216
213
const color = this . getColor ( row , title ) ;
217
-
214
+ const dataset = getDataset ( this . attrs ) ;
218
215
// Take a copy of the row, only copying the keys listed in the schema.
219
- const cols = Object . keys ( this . attrs . dataset . schema ) ;
216
+ const cols = Object . keys ( dataset . schema ) ;
220
217
const clonedRow = Object . fromEntries (
221
218
Object . entries ( row ) . filter ( ( [ key ] ) => cols . includes ( key ) ) ,
222
219
) as T ;
@@ -276,11 +273,15 @@ export class DatasetSliceTrack<T extends ROW_SCHEMA> extends BaseSliceTrack<
276
273
}
277
274
278
275
override getSqlSource ( ) : string {
279
- return this . sqlSource ;
276
+ const dataset =
277
+ typeof this . attrs . dataset === 'function'
278
+ ? this . attrs . dataset ( )
279
+ : this . attrs . dataset ;
280
+ return this . generateRenderQuery ( dataset ) ;
280
281
}
281
282
282
283
getDataset ( ) {
283
- return this . attrs . dataset ;
284
+ return getDataset ( this . attrs ) ;
284
285
}
285
286
286
287
detailsPanel ( sel : TrackEventSelection ) : TrackEventDetailsPanel | undefined {
@@ -301,7 +302,8 @@ export class DatasetSliceTrack<T extends ROW_SCHEMA> extends BaseSliceTrack<
301
302
async getSelectionDetails (
302
303
id : number ,
303
304
) : Promise < TrackEventDetails | undefined > {
304
- const { trace, dataset} = this . attrs ;
305
+ const { trace} = this . attrs ;
306
+ const dataset = getDataset ( this . attrs ) ;
305
307
const result = await trace . engine . query ( `
306
308
SELECT *
307
309
FROM (${ dataset . query ( ) } )
0 commit comments