Skip to content

Commit

Permalink
Integrate measure row filters to pivot (#4062)
Browse files Browse the repository at this point in the history
* Measure body as param

* Switch to measureBody for axes query

* Switch to measure filter for initial rows

* Add measureBody for totals row query

* Remove stale comment

* Remove unused method
  • Loading branch information
djbarnwal authored Feb 20, 2024
1 parent 182ab98 commit 68c5289
Show file tree
Hide file tree
Showing 4 changed files with 46 additions and 114 deletions.
66 changes: 28 additions & 38 deletions web-common/src/features/dashboards/pivot/pivot-data-store.ts
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,6 @@ import {
getTimeGrainFromDimension,
getTotalColumnCount,
isTimeDimension,
reconcileMissingDimensionValues,
} from "./pivot-utils";
import {
PivotChipType,
Expand Down Expand Up @@ -151,6 +150,7 @@ export function createTableCellQuery(
};
} else return { name: dimension };
});
const measureBody = config.measureNames.map((m) => ({ name: m }));

const { filters: filterForInitialTable, timeFilters } =
getFilterForPivotTable(
Expand All @@ -174,7 +174,7 @@ export function createTableCellQuery(
];
return createPivotAggregationRowQuery(
ctx,
config.measureNames,
measureBody,
dimensionBody,
mergedFilter,
config.measureFilter,
Expand Down Expand Up @@ -235,7 +235,6 @@ function createPivotDataStore(ctx: StateManagers): PivotDataStore {
*/
return derived(getPivotConfig(ctx), (config, configSet) => {
const { rowDimensionNames, colDimensionNames, measureNames } = config;

if (
(!rowDimensionNames.length && !measureNames.length) ||
(colDimensionNames.length && !measureNames.length)
Expand All @@ -252,10 +251,13 @@ function createPivotDataStore(ctx: StateManagers): PivotDataStore {
totalColumns: 0,
});
}
const measureBody = measureNames.map((m) => ({ name: m }));

const columnDimensionAxesQuery = getAxisForDimensions(
ctx,
config,
colDimensionNames,
measureBody,
config.whereFilter,
);

Expand All @@ -273,32 +275,35 @@ function createPivotDataStore(ctx: StateManagers): PivotDataStore {
}
const anchorDimension = rowDimensionNames[0];

const { where, sortPivotBy, timeRange } = getSortForAccessor(
const {
where: measureWhere,
sortPivotBy,
timeRange,
} = getSortForAccessor(
anchorDimension,
config,
columnDimensionAxes?.data,
);

let sortFilteredMeasureBody = measureBody;
if (sortPivotBy.length && measureWhere) {
const accessor = sortPivotBy[0]?.name;
sortFilteredMeasureBody = measureBody.map((m) => {
if (m.name === accessor) return { ...m, filter: measureWhere };
return m;
});
}

const rowDimensionAxisQuery = getAxisForDimensions(
ctx,
config,
rowDimensionNames.slice(0, 1),
where,
sortFilteredMeasureBody,
config.whereFilter,
sortPivotBy,
timeRange,
);

/**
* We need to query the unsorted row dimension values because the sorted
* row dimension values may not have all the dimensions values
*/
const rowDimensionUnsortedAxisQuery = getAxisForDimensions(
ctx,
config,
rowDimensionNames.slice(0, 1),
config.whereFilter,
);

let globalTotalsQuery:
| Readable<null>
| CreateQueryResult<V1MetricsViewAggregationResponse, unknown> =
Expand All @@ -310,7 +315,7 @@ function createPivotDataStore(ctx: StateManagers): PivotDataStore {
if (rowDimensionNames.length && measureNames.length) {
globalTotalsQuery = createPivotAggregationRowQuery(
ctx,
config.measureNames,
config.measureNames.map((m) => ({ name: m })),
[],
config.whereFilter,
config.measureFilter,
Expand All @@ -333,27 +338,16 @@ function createPivotDataStore(ctx: StateManagers): PivotDataStore {
* Derive a store from axes queries
*/
return derived(
[
rowDimensionAxisQuery,
rowDimensionUnsortedAxisQuery,
globalTotalsQuery,
totalsRowQuery,
],
[rowDimensionAxisQuery, globalTotalsQuery, totalsRowQuery],
(
[
rowDimensionAxes,
rowDimensionUnsortedAxis,
globalTotalsResponse,
totalsRowResponse,
],
[rowDimensionAxes, globalTotalsResponse, totalsRowResponse],
axesSet,
) => {
if (
(globalTotalsResponse !== null &&
globalTotalsResponse?.isFetching) ||
(totalsRowResponse !== null && totalsRowResponse?.isFetching) ||
rowDimensionAxes?.isFetching ||
rowDimensionUnsortedAxis?.isFetching
rowDimensionAxes?.isFetching
) {
return axesSet({
isFetching: true,
Expand All @@ -364,6 +358,9 @@ function createPivotDataStore(ctx: StateManagers): PivotDataStore {
});
}

const rowDimensionValues =
rowDimensionAxes?.data?.[anchorDimension] || [];
const rowTotals = rowDimensionAxes?.totals?.[anchorDimension] || [];
const totalsRow = getTotalsRow(
config,
columnDimensionAxes?.data,
Expand All @@ -373,13 +370,6 @@ function createPivotDataStore(ctx: StateManagers): PivotDataStore {

const totalColumns = getTotalColumnCount(totalsRow);

const { rows: rowDimensionValues, totals: rowTotals } =
reconcileMissingDimensionValues(
anchorDimension,
rowDimensionAxes,
rowDimensionUnsortedAxis,
);

let initialTableCellQuery:
| Readable<null>
| CreateQueryResult<V1MetricsViewAggregationResponse, unknown> =
Expand Down
5 changes: 4 additions & 1 deletion web-common/src/features/dashboards/pivot/pivot-expansion.ts
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,7 @@ export function createSubTableCellQuery(
};
} else return { name: dimension };
});
const measureBody = config.measureNames.map((m) => ({ name: m }));

const { filters: filterForSubTable, timeFilters: colTimeFilters } =
getFilterForPivotTable(config, columnDimensionAxesData, totalsRow);
Expand All @@ -105,7 +106,7 @@ export function createSubTableCellQuery(
];
return createPivotAggregationRowQuery(
ctx,
config.measureNames,
measureBody,
dimensionBody,
filterForSubTable,
config.measureFilter,
Expand Down Expand Up @@ -140,6 +141,7 @@ export function queryExpandedRowMeasureValues(
const expanded = config.pivot.expanded;
if (!tableData || Object.keys(expanded).length == 0) return writable(null);

const measureBody = config.measureNames.map((m) => ({ name: m }));
return derived(
Object.keys(expanded)?.map((expandIndex) => {
const nestLevel = expandIndex?.split(".")?.length;
Expand Down Expand Up @@ -225,6 +227,7 @@ export function queryExpandedRowMeasureValues(
ctx,
config,
[anchorDimension],
measureBody,
allMergedFilters,
sortPivotBy,
timeRange,
Expand Down
14 changes: 8 additions & 6 deletions web-common/src/features/dashboards/pivot/pivot-queries.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ import type { TimeRangeString } from "@rilldata/web-common/lib/time/types";
import {
V1Expression,
V1MetricsViewAggregationDimension,
V1MetricsViewAggregationMeasure,
V1MetricsViewAggregationResponseDataItem,
V1MetricsViewAggregationSort,
createQueryServiceMetricsViewAggregation,
Expand All @@ -32,7 +33,7 @@ import { mergeFilters } from "./pivot-merge-filters";
*/
export function createPivotAggregationRowQuery(
ctx: StateManagers,
measures: string[],
measures: V1MetricsViewAggregationMeasure[],
dimensions: V1MetricsViewAggregationDimension[],
whereFilter: V1Expression,
measureFilter: ResolvedMeasureFilter | undefined,
Expand All @@ -45,7 +46,7 @@ export function createPivotAggregationRowQuery(
sort = [
{
desc: false,
name: measures[0] || dimensions?.[0]?.name,
name: measures[0]?.name || dimensions?.[0]?.name,
},
];
}
Expand All @@ -57,7 +58,7 @@ export function createPivotAggregationRowQuery(
runtime.instanceId,
metricViewName,
{
measures: measures.map((measure) => ({ name: measure })),
measures,
dimensions,
where: sanitiseExpression(whereFilter, measureFilter?.filter),
timeRange: {
Expand Down Expand Up @@ -86,21 +87,21 @@ export function getAxisForDimensions(
ctx: StateManagers,
config: PivotDataStoreConfig,
dimensions: string[],
measures: V1MetricsViewAggregationMeasure[],
whereFilter: V1Expression,
sortBy: V1MetricsViewAggregationSort[] = [],
timeRange: TimeRangeString | undefined = undefined,
): Readable<PivotAxesData | null> {
if (!dimensions.length) return readable(null);

const measures = config.measureNames;
const { time } = config;

let sortProvided = true;
if (!sortBy.length) {
sortBy = [
{
desc: true,
name: measures[0] || dimensions?.[0],
name: measures[0]?.name || dimensions?.[0],
},
];
sortProvided = false;
Expand Down Expand Up @@ -180,6 +181,7 @@ export function getTotalsRowQuery(
const { colDimensionNames } = config;

const { time } = config;
const measureBody = config.measureNames.map((m) => ({ name: m }));
const dimensionBody = colDimensionNames.map((dimension) => {
if (isTimeDimension(dimension, time.timeDimension)) {
return {
Expand Down Expand Up @@ -210,7 +212,7 @@ export function getTotalsRowQuery(
];
return createPivotAggregationRowQuery(
ctx,
config.measureNames,
measureBody,
dimensionBody,
mergedFilter,
config.measureFilter,
Expand Down
Loading

1 comment on commit 68c5289

@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.