Skip to content

Commit

Permalink
Refac: Move totals row out of data (#4470)
Browse files Browse the repository at this point in the history
* Move totals row out of pivot data

* Merge totals row with data

* Add missing check
  • Loading branch information
djbarnwal authored Mar 31, 2024
1 parent 8b6f3eb commit 695172c
Show file tree
Hide file tree
Showing 4 changed files with 78 additions and 29 deletions.
36 changes: 21 additions & 15 deletions web-common/src/features/dashboards/pivot/PivotTable.svelte
Original file line number Diff line number Diff line change
Expand Up @@ -34,21 +34,27 @@
const options: Readable<TableOptions<PivotDataRow>> = derived(
[pivotDashboardStore, pivotDataStore],
([pivotConfig, pivotData]) => ({
data: pivotData.data,
columns: pivotData.columnDef,
state: {
expanded: pivotConfig.expanded,
sorting: pivotConfig.sorting,
},
onExpandedChange: handleExpandedChange,
getSubRows: (row) => row.subRows,
onSortingChange: handleSorting,
getExpandedRowModel: getExpandedRowModel(),
getCoreRowModel: getCoreRowModel(),
enableSortingRemoval: false,
enableExpanding: true,
}),
([pivotConfig, pivotData]) => {
let tableData = pivotData.data;
if (pivotData.totalsRowData) {
tableData = [pivotData.totalsRowData, ...pivotData.data];
}
return {
data: tableData,
columns: pivotData.columnDef,
state: {
expanded: pivotConfig.expanded,
sorting: pivotConfig.sorting,
},
onExpandedChange: handleExpandedChange,
getSubRows: (row) => row.subRows,
onSortingChange: handleSorting,
getExpandedRowModel: getExpandedRowModel(),
getCoreRowModel: getCoreRowModel(),
enableSortingRemoval: false,
enableExpanding: true,
};
},
);
const table = createSvelteTable(options);
Expand Down
42 changes: 28 additions & 14 deletions web-common/src/features/dashboards/pivot/pivot-data-store.ts
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ import {
} from "./pivot-queries";
import {
getTotalsRow,
getTotalsRowSkeleton,
mergeRowTotals,
prepareNestedPivotData,
reduceTableCellDataIntoRows,
Expand Down Expand Up @@ -372,6 +373,9 @@ function createPivotDataStore(ctx: StateManagers): PivotDataStore {
"5000", // Using 5000 for cache hit
);
}

const displayTotalsRow =
rowDimensionNames.length && measureNames.length;
if (
(rowDimensionNames.length || colDimensionNames.length) &&
measureNames.length
Expand All @@ -398,16 +402,23 @@ function createPivotDataStore(ctx: StateManagers): PivotDataStore {
(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,
});
}

const totalsRow = getTotalsRow(
const totalsRowData = getTotalsRow(
config,
columnDimensionAxes?.data,
totalsRowResponse?.data?.data,
Expand All @@ -420,15 +431,16 @@ function createPivotDataStore(ctx: StateManagers): PivotDataStore {
if (rowDimensionValues.length === 0 && rowPage > 1) {
return axesSet({
isFetching: false,
data: [totalsRow, ...lastPivotData],
data: lastPivotData,
columnDef: lastPivotColumnDef,
assembled: true,
totalColumns: lastTotalColumns,
totalsRowData: displayTotalsRow ? totalsRowData : undefined,
reachedEndForRowData: true,
});
}

const totalColumns = getTotalColumnCount(totalsRow);
const totalColumns = getTotalColumnCount(totalsRowData);

const axesRowTotals =
rowDimensionAxes?.totals?.[anchorDimension] || [];
Expand All @@ -452,28 +464,28 @@ function createPivotDataStore(ctx: StateManagers): PivotDataStore {
const slicedAxesDataForDef = sliceColumnAxesDataForDef(
config,
columnDimensionAxes?.data,
totalsRow,
totalsRowData,
);

columnDef = getColumnDefForPivot(
config,
slicedAxesDataForDef,
totalsRow,
totalsRowData,
);

initialTableCellQuery = createTableCellQuery(
ctx,
config,
rowDimensionNames[0],
columnDimensionAxes?.data,
totalsRow,
totalsRowData,
rowDimensionValues,
);
} else {
columnDef = getColumnDefForPivot(
config,
columnDimensionAxes?.data,
totalsRow,
totalsRowData,
);
}
/**
Expand All @@ -489,6 +501,7 @@ function createPivotDataStore(ctx: StateManagers): PivotDataStore {
columnDef,
assembled: false,
totalColumns,
totalsRowData: displayTotalsRow ? totalsRowData : undefined,
});
}

Expand Down Expand Up @@ -519,6 +532,9 @@ function createPivotDataStore(ctx: StateManagers): PivotDataStore {
columnDef,
assembled: false,
totalColumns,
totalsRowData: displayTotalsRow
? totalsRowData
: undefined,
});
}
cellData = initialTableCellData.data?.data || [];
Expand All @@ -539,7 +555,7 @@ function createPivotDataStore(ctx: StateManagers): PivotDataStore {
config,
pivotData,
columnDimensionAxes?.data,
totalsRow,
totalsRowData,
);

/**
Expand Down Expand Up @@ -567,17 +583,15 @@ function createPivotDataStore(ctx: StateManagers): PivotDataStore {
lastPivotColumnDef = columnDef;
lastTotalColumns = totalColumns;

let assembledTableData = tableDataExpanded;
if (rowDimensionNames.length && measureNames.length) {
assembledTableData = [totalsRow, ...tableDataExpanded];
}

return {
isFetching: false,
data: assembledTableData,
data: tableDataExpanded,
columnDef,
assembled: true,
totalColumns,
totalsRowData: displayTotalsRow
? totalsRowData
: undefined,
};
},
).subscribe(cellSet);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -107,6 +107,34 @@ export function reduceTableCellDataIntoRows(
return tableData;
}

export function getTotalsRowSkeleton(
config: PivotDataStoreConfig,
columnDimensionAxes: Record<string, string[]> = {},
) {
const { rowDimensionNames, measureNames } = config;
const anchorDimensionName = rowDimensionNames[0];

let totalsRow: PivotDataRow = {};
if (measureNames.length) {
const totalsRowTable = reduceTableCellDataIntoRows(
config,
"",
[],
columnDimensionAxes || {},
[],
[],
);

totalsRow = totalsRowTable[0] || {};

if (anchorDimensionName) {
totalsRow[anchorDimensionName] = "Total";
}
}

return totalsRow;
}

export function getTotalsRow(
config: PivotDataStoreConfig,
columnDimensionAxes: Record<string, string[]> = {},
Expand Down
1 change: 1 addition & 0 deletions web-common/src/features/dashboards/pivot/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ export interface PivotDataState {
assembled: boolean;
totalColumns: number; // total columns excluding row and group totals columns
reachedEndForRowData?: boolean;
totalsRowData?: PivotDataRow;
}

export type PivotDataStore = Readable<PivotDataState>;
Expand Down

1 comment on commit 695172c

@github-actions
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please sign in to comment.