-
Notifications
You must be signed in to change notification settings - Fork 166
Expand file tree
/
Copy pathpivot-data-store.ts
More file actions
733 lines (673 loc) · 24.6 KB
/
pivot-data-store.ts
File metadata and controls
733 lines (673 loc) · 24.6 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
import { getDimensionFilterWithSearch } from "@rilldata/web-common/features/dashboards/dimension-table/dimension-table-utils";
import {
calculateEffectiveRowLimit,
MAX_ROW_EXPANSION_LIMIT,
SHOW_MORE_BUTTON,
} from "@rilldata/web-common/features/dashboards/pivot/pivot-constants";
import { mergeFilters } from "@rilldata/web-common/features/dashboards/pivot/pivot-merge-filters";
import { memoizeMetricsStore } from "@rilldata/web-common/features/dashboards/state-managers/memoize-metrics-store";
import type { StateManagers } from "@rilldata/web-common/features/dashboards/state-managers/state-managers";
import { createAndExpression } from "@rilldata/web-common/features/dashboards/stores/filter-utils";
import type { TimeRangeString } from "@rilldata/web-common/lib/time/types";
import type {
V1Expression,
V1MetricsViewAggregationResponse,
V1MetricsViewAggregationSort,
} from "@rilldata/web-common/runtime-client";
import type { HTTPError } from "@rilldata/web-common/runtime-client/fetchWrapper";
import type { CreateQueryResult } from "@tanstack/svelte-query";
import type { ColumnDef } from "@tanstack/svelte-table";
import { type Readable, derived, readable } from "svelte/store";
import { getColumnDefForPivot } from "./pivot-column-definition";
import { getPivotConfig } from "./pivot-data-config";
import {
addExpandedDataToPivot,
getExpandedQueryErrors,
queryExpandedRowMeasureValues,
} from "./pivot-expansion";
import {
NUM_ROWS_PER_PAGE,
sliceColumnAxesDataForDef,
} from "./pivot-infinite-scroll";
import {
createPivotAggregationRowQuery,
getAxisForDimensions,
getAxisQueryForMeasureTotals,
getTotalsRowQuery,
} from "./pivot-queries";
import {
getTotalsRow,
getTotalsRowSkeleton,
mergeRowTotalsInOrder,
prepareNestedPivotData,
reduceTableCellDataIntoRows,
} from "./pivot-table-transformations";
import {
getErrorFromResponses,
getErrorState,
getFilterForPivotTable,
getFiltersForCell,
getPivotConfigKey,
getSortFilteredMeasureBody,
getSortForAccessor,
getTimeForQuery,
getTimeGrainFromDimension,
getTotalColumnCount,
isTimeDimension,
splitPivotChips,
} from "./pivot-utils";
import {
type PivotAxesData,
type PivotDashboardContext,
type PivotDataRow,
type PivotDataStore,
type PivotDataStoreConfig,
type PivotFilter,
} from "./types";
/**
* Returns a query for cell data for the initial table.
* The table cell is sorted by the anchor dimension irrespective
* of the sort config. The dimension axes values are sorted using
* the config and values from this query are used to create the
* table.
*/
export function createTableCellQuery(
ctx: PivotDashboardContext,
config: PivotDataStoreConfig,
columnDimensionAxesData: Record<string, string[]> | undefined,
totalsRow: PivotDataRow,
rowDimensionValues: string[],
limit = "5000",
offset = "0",
) {
const {
rowDimensionNames,
colDimensionNames,
measureNames,
isFlat,
time,
whereFilter,
} = config;
const anchorDimension: string | undefined = rowDimensionNames?.[0];
const rowPage = config.pivot.rowPage;
if (!isFlat && rowDimensionValues.length === 0 && rowPage > 1)
return readable(null);
let allDimensions = colDimensionNames;
if (isFlat) {
allDimensions = colDimensionNames.concat(rowDimensionNames);
} else if (anchorDimension) {
allDimensions = colDimensionNames.concat([anchorDimension]);
}
const dimensionBody = allDimensions.map((dimension) => {
if (isTimeDimension(dimension, time.timeDimension)) {
return {
name: time.timeDimension,
timeGrain: getTimeGrainFromDimension(dimension),
timeZone: time.timeZone,
alias: dimension,
};
} else return { name: dimension };
});
const measureBody = measureNames.map((m) => ({ name: m }));
const { filters: filterForInitialTable, timeFilters } =
getFilterForPivotTable(
config,
columnDimensionAxesData,
totalsRow,
rowDimensionValues,
anchorDimension,
);
const timeRange: TimeRangeString = getTimeForQuery(time, timeFilters);
const mergedFilter =
mergeFilters(filterForInitialTable, whereFilter) ?? createAndExpression([]);
let sortBy: V1MetricsViewAggregationSort[] = [];
if (isFlat) {
const sortConfig = config.pivot.sorting?.[0];
if (sortConfig) {
sortBy = [
{
desc: sortConfig.desc,
name: sortConfig.id, // For flat tables, sort ID is directly the measure or dimension name
},
];
} else {
// Default sort if no sort config provided
sortBy = [
{
desc: measureNames[0] ? true : false,
name: measureNames[0] || allDimensions[0],
},
];
}
} else {
sortBy = [
{
desc: false,
name: anchorDimension || measureNames[0],
},
];
}
return createPivotAggregationRowQuery(
ctx,
config,
measureBody,
dimensionBody,
mergedFilter,
sortBy,
limit,
offset,
timeRange,
);
}
/**
* Stores the last pivot data and column def to be used when there is no data
* to be displayed. This is to avoid the table from flickering when there is no
* data to be displayed.
*/
let lastPivotData: PivotDataRow[] = [];
let lastPivotColumnDef: ColumnDef<PivotDataRow>[] = [];
let lastTotalColumns: number = 0;
/**
* The expanded table has to iterate over itself to find nested dimension values
* which are being expanded. Since the expanded values are added in one go, the previously
* expanded values are not available in the table data. This map stores the expanded table
* data for each pivot config. This is cleared when the pivot config changes.
*/
let expandedTableMap: Record<string, PivotDataRow[]> = {};
/**
* Main store for pivot table data
*
* At a high-level, we make the following queries in the order below:
*
* Input pivot config
* |
* | (Column headers)
* v
* Create table headers by querying axes values for each column dimension
* |
* | (Row headers and sort order)
* v
* Create skeleton table data by querying axes values for row dimension.
* Also fetch column wise totals to determine columns to render
* |
* | (Cell Data)
* v
* For the visible axes values, query the data for each cell
* |
* | (Expanded)
* v
* For each expanded row, query the data for each cell
* |
* | (Assemble)
* v
* Table data and column definitions
*/
export function createPivotDataStore(
ctx: PivotDashboardContext,
configStore: Readable<PivotDataStoreConfig>,
): PivotDataStore {
/**
* Derive a store using pivot config
*/
return derived(configStore, (config, configSet) => {
const { rowDimensionNames, colDimensionNames, measureNames, isFlat } =
config;
if (
(!rowDimensionNames.length && !measureNames.length) ||
(colDimensionNames.length && !measureNames.length)
) {
const { dimension: colDimensions, measure: colMeasures } =
splitPivotChips(config.pivot.columns);
const isFetching =
colMeasures.length > 0 ||
(config.pivot.rows.length > 0 && !colDimensions.length);
return configSet({
isFetching: isFetching,
data: [],
columnDef: [],
assembled: false,
totalColumns: 0,
});
}
const measureBody = measureNames.map((m) => ({ name: m }));
const columnDimensionAxesQuery = getAxisForDimensions(
ctx,
config,
colDimensionNames,
measureBody,
config.whereFilter,
[],
);
return derived(
columnDimensionAxesQuery,
(columnDimensionAxes, columnSet) => {
if (columnDimensionAxes?.isFetching) {
return columnSet({
isFetching: true,
data: [],
columnDef: [],
assembled: false,
totalColumns: 0,
});
}
if (columnDimensionAxes?.error && columnDimensionAxes?.error.length) {
return columnSet(getErrorState(columnDimensionAxes.error));
}
const anchorDimension = rowDimensionNames[0];
const rowPage = config.pivot.rowPage;
const rowOffset = (rowPage - 1) * NUM_ROWS_PER_PAGE;
let whereFilter: V1Expression = config.whereFilter;
if (config.searchText) {
whereFilter =
getDimensionFilterWithSearch(
whereFilter,
config.searchText,
anchorDimension,
) || config.whereFilter;
}
const {
where: measureWhere,
sortPivotBy,
timeRange,
} = getSortForAccessor(
anchorDimension,
config,
columnDimensionAxes?.data,
);
const { sortFilteredMeasureBody, isMeasureSortAccessor, sortAccessor } =
getSortFilteredMeasureBody(measureBody, sortPivotBy, measureWhere);
let rowDimensionAxisQuery: Readable<PivotAxesData | null> =
readable(null);
if (!isFlat) {
// Use outermostRowLimit if set, otherwise fall back to rowLimit
const effectiveOutermostLimit =
config.pivot.outermostRowLimit ?? config.pivot.rowLimit;
// Calculate the effective limit based on outermostRowLimit, offset, and page size
// When outermostRowLimit is explicitly set, don't constrain by page size
const isExplicitOutermostLimit =
config.pivot.outermostRowLimit !== undefined;
const limitToApply = calculateEffectiveRowLimit(
effectiveOutermostLimit,
rowOffset,
NUM_ROWS_PER_PAGE,
!isExplicitOutermostLimit, // Don't respect page size for explicit outermost limit
);
// Query for limit + 1 to detect if there's more data
const limitToQuery =
effectiveOutermostLimit !== undefined
? (parseInt(limitToApply) + 1).toString()
: limitToApply;
// Get sort order for the anchor dimension
rowDimensionAxisQuery = getAxisForDimensions(
ctx,
config,
rowDimensionNames.slice(0, 1),
sortFilteredMeasureBody,
whereFilter,
sortPivotBy,
timeRange,
limitToQuery,
rowOffset.toString(),
);
}
let globalTotalsQuery:
| Readable<null>
| CreateQueryResult<V1MetricsViewAggregationResponse, HTTPError> =
readable(null);
let totalsRowQuery:
| Readable<null>
| CreateQueryResult<V1MetricsViewAggregationResponse, HTTPError> =
readable(null);
if (rowDimensionNames.length && measureNames.length) {
globalTotalsQuery = createPivotAggregationRowQuery(
ctx,
config,
config.measureNames.map((m) => ({ name: m })),
[],
config.whereFilter,
[],
"5000", // Using 5000 for cache hit
);
}
const displayTotalsRow = Boolean(
rowDimensionNames.length && measureNames.length,
);
if (
(rowDimensionNames.length || colDimensionNames.length) &&
measureNames.length &&
!isFlat
) {
totalsRowQuery = getTotalsRowQuery(
ctx,
config,
columnDimensionAxes?.data,
);
}
/**
* Derive a store from axes queries
*/
return derived(
[rowDimensionAxisQuery, globalTotalsQuery, totalsRowQuery],
(
[rowDimensionAxes, globalTotalsResponse, totalsRowResponse],
axesSet,
) => {
if (
(globalTotalsResponse !== null &&
globalTotalsResponse?.isFetching) ||
(totalsRowResponse !== null && totalsRowResponse?.isFetching) ||
rowDimensionAxes?.isFetching
) {
const skeletonTotalsRowData = getTotalsRowSkeleton(
config,
columnDimensionAxes?.data,
);
return axesSet({
isFetching: true,
data: lastPivotData,
columnDef: lastPivotColumnDef,
assembled: false,
totalColumns: lastTotalColumns,
totalsRowData: displayTotalsRow
? skeletonTotalsRowData
: undefined,
});
}
// check for errors in the responses
const totalErrors = getErrorFromResponses([
globalTotalsResponse,
totalsRowResponse,
]);
if (totalErrors.length || rowDimensionAxes?.error?.length) {
const allErrors = totalErrors.concat(
rowDimensionAxes?.error || [],
);
return axesSet(getErrorState(allErrors));
}
/**
* If there are no axes values, return an empty table
*/
if (
(rowDimensionAxes?.data?.[anchorDimension]?.length === 0 ||
totalsRowResponse?.data?.data?.length === 0) &&
rowPage === 1
) {
return axesSet({
isFetching: false,
data: [],
columnDef: [],
assembled: true,
totalColumns: 0,
totalsRowData: displayTotalsRow ? [] : undefined,
});
}
const totalsRowData = getTotalsRow(
config,
columnDimensionAxes?.data,
totalsRowResponse?.data?.data,
globalTotalsResponse?.data?.data,
);
let rowDimensionValues =
rowDimensionAxes?.data?.[anchorDimension] || [];
let axesRowTotals =
rowDimensionAxes?.totals?.[anchorDimension] || [];
// Detect if there's more data for the outermost dimension
// and trim to the actual limit
let hasMoreRows = false;
const effectiveOutermostLimit =
config.pivot.outermostRowLimit ?? config.pivot.rowLimit;
if (!isFlat && effectiveOutermostLimit !== undefined) {
const isExplicitOutermostLimit =
config.pivot.outermostRowLimit !== undefined;
const limitToApply = calculateEffectiveRowLimit(
effectiveOutermostLimit,
rowOffset,
NUM_ROWS_PER_PAGE,
!isExplicitOutermostLimit, // Don't respect page size for explicit outermost limit
);
const actualLimit = parseInt(limitToApply);
if (rowDimensionValues.length > actualLimit) {
hasMoreRows = true;
rowDimensionValues = rowDimensionValues.slice(0, actualLimit);
axesRowTotals = axesRowTotals.slice(0, actualLimit);
}
}
const totalColumns = getTotalColumnCount(totalsRowData);
const rowAxesQueryForMeasureTotals = getAxisQueryForMeasureTotals(
ctx,
config,
isMeasureSortAccessor,
sortAccessor,
anchorDimension,
rowDimensionValues,
timeRange,
);
let tableCellQuery:
| Readable<null>
| CreateQueryResult<V1MetricsViewAggregationResponse, HTTPError> =
readable(null);
let columnDef: ColumnDef<PivotDataRow>[] = [];
if (
isFlat ||
colDimensionNames.length ||
!rowDimensionNames.length
) {
const slicedAxesDataForDef = sliceColumnAxesDataForDef(
config,
columnDimensionAxes?.data,
totalsRowData,
);
columnDef = getColumnDefForPivot(
config,
slicedAxesDataForDef,
totalsRowData,
);
tableCellQuery = createTableCellQuery(
ctx,
config,
columnDimensionAxes?.data,
totalsRowData,
rowDimensionValues,
isFlat ? NUM_ROWS_PER_PAGE.toString() : "5000",
isFlat ? rowOffset.toString() : "0",
);
} else {
columnDef = getColumnDefForPivot(
config,
columnDimensionAxes?.data,
totalsRowData,
);
}
/**
* Derive a store from table cell data query
*/
return derived(
[rowAxesQueryForMeasureTotals, tableCellQuery],
([rowMeasureTotalsAxesQuery, tableCellData], cellSet) => {
if (rowMeasureTotalsAxesQuery?.isFetching) {
return cellSet({
isFetching: true,
data: lastPivotData ? lastPivotData : axesRowTotals,
columnDef,
assembled: false,
totalColumns,
totalsRowData: displayTotalsRow ? totalsRowData : undefined,
});
}
const tableCellQueryError = getErrorFromResponses([
tableCellData,
]);
if (
tableCellQueryError.length ||
rowMeasureTotalsAxesQuery?.error?.length
) {
const allErrors = tableCellQueryError.concat(
rowMeasureTotalsAxesQuery?.error || [],
);
return cellSet(getErrorState(allErrors));
}
const mergedRowTotals = mergeRowTotalsInOrder(
rowDimensionValues,
axesRowTotals,
rowMeasureTotalsAxesQuery?.data?.[anchorDimension] || [],
rowMeasureTotalsAxesQuery?.totals?.[anchorDimension] || [],
);
let pivotSkeleton = mergedRowTotals;
if (!isFlat && rowPage > 1) {
pivotSkeleton = [...lastPivotData, ...mergedRowTotals];
}
let pivotData: PivotDataRow[] = [];
let cellData: PivotDataRow[] = [];
let isCellDataEmpty = false;
if (getPivotConfigKey(config) in expandedTableMap) {
pivotData = expandedTableMap[getPivotConfigKey(config)];
} else {
if (tableCellData === null) {
cellData = pivotSkeleton as PivotDataRow[];
} else {
if (tableCellData.isFetching) {
return cellSet({
isFetching: true,
data: isFlat ? lastPivotData : pivotSkeleton,
columnDef,
assembled: false,
totalColumns,
totalsRowData: displayTotalsRow
? totalsRowData
: undefined,
});
}
cellData = (tableCellData.data?.data ||
[]) as PivotDataRow[];
isCellDataEmpty = cellData.length === 0;
}
let tableDataWithCells: PivotDataRow[] = [];
if (isFlat) {
if (rowPage > 1) {
tableDataWithCells = [...lastPivotData, ...cellData];
} else {
tableDataWithCells = cellData;
}
} else {
tableDataWithCells = reduceTableCellDataIntoRows(
config,
anchorDimension,
rowDimensionValues || [],
columnDimensionAxes?.data || {},
pivotSkeleton as PivotDataRow[],
cellData,
);
}
pivotData = structuredClone(tableDataWithCells);
}
const expandedSubTableCellQuery = queryExpandedRowMeasureValues(
ctx,
config,
pivotData,
columnDimensionAxes?.data,
totalsRowData,
);
/**
* Derive a store based on expanded rows and totals
*/
return derived(
[expandedSubTableCellQuery],
([expandedRowMeasureValues]) => {
prepareNestedPivotData(pivotData, rowDimensionNames);
let tableDataExpanded: PivotDataRow[] = pivotData;
if (expandedRowMeasureValues?.length) {
const queryErrors = getExpandedQueryErrors(
expandedRowMeasureValues,
);
if (queryErrors.length) return getErrorState(queryErrors);
tableDataExpanded = addExpandedDataToPivot(
config,
pivotData,
rowDimensionNames,
columnDimensionAxes?.data || {},
expandedRowMeasureValues,
);
const key = getPivotConfigKey(config);
expandedTableMap = {};
expandedTableMap[key] = tableDataExpanded;
}
// Add "Show more" row for outermost dimension if needed
const effectiveOutermostLimit =
config.pivot.outermostRowLimit ?? config.pivot.rowLimit;
if (
hasMoreRows &&
effectiveOutermostLimit &&
effectiveOutermostLimit < MAX_ROW_EXPANSION_LIMIT
) {
const showMoreRow: PivotDataRow = {
[anchorDimension]: SHOW_MORE_BUTTON,
__currentLimit: effectiveOutermostLimit,
} as PivotDataRow;
tableDataExpanded = [...tableDataExpanded, showMoreRow];
}
const activeCell = config.pivot.activeCell;
let activeCellFilters: PivotFilter | undefined = undefined;
if (activeCell) {
activeCellFilters = getFiltersForCell(
config,
activeCell.rowId,
activeCell.columnId,
columnDimensionAxes?.data,
tableDataExpanded,
);
}
lastPivotData = tableDataExpanded;
lastPivotColumnDef = columnDef;
lastTotalColumns = totalColumns;
let reachedEndForRowData = false;
if (isFlat) {
reachedEndForRowData = isCellDataEmpty && rowPage > 1;
} else {
const rowLimit = config.pivot.rowLimit;
if (rowLimit !== undefined) {
// Check if we've fetched all rows allowed by rowLimit
// This includes both the current page data and any previous pages
const totalRowsFetched =
rowOffset + rowDimensionValues.length;
reachedEndForRowData = totalRowsFetched >= rowLimit;
} else {
reachedEndForRowData =
rowDimensionValues.length === 0 && rowPage > 1;
}
}
return {
isFetching: false,
data: tableDataExpanded,
columnDef,
assembled: true,
activeCellFilters,
totalColumns,
reachedEndForRowData,
totalsRowData: displayTotalsRow
? totalsRowData
: undefined,
};
},
).subscribe(cellSet);
},
).subscribe(axesSet);
},
).subscribe(columnSet);
},
).subscribe(configSet);
});
}
/**
* Memoized version of the store. Currently, memoized by metrics view name.
*/
export const usePivotForExplore = memoizeMetricsStore<PivotDataStore>(
(ctx: StateManagers) => {
const pivotConfig = getPivotConfig(ctx);
const pivotDashboardContext: PivotDashboardContext = {
metricsViewName: ctx.metricsViewName,
queryClient: ctx.queryClient,
enabled: !!ctx.dashboardStore,
};
return createPivotDataStore(pivotDashboardContext, pivotConfig);
},
);