Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Post processing functions on query #3509

Merged
merged 2 commits into from
Oct 24, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 4 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,13 +1,16 @@
# Changelog

## 60.0.0-SNAPSHOT - unreleased
## 59.2.0 - 2023-10-16

### 🎁 New Features

* New `DockViewConfig.onClose` hook invoked when a user attempts to remove a `DockContainer` view
* Add `GridModel` APIs to lookup and show / hide entire column groups
* Left / right borders are now rendered along `Grid` `ColumnGroup` edges by default. Control
with new boolean property `ColumnGroupSpec.borders`
* The Cube package has been enhanced to support `Query` specific post-processing functions. See
new properties `Query.omitFn`, `Query.bucketSpecFn` and `Query.lockFn`. These properties default
to their respective properties on `Cube`.

### 🐞 Bug Fixes

Expand Down
40 changes: 37 additions & 3 deletions data/cube/Query.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
* Copyright © 2023 Extremely Heavy Industries Inc.
*/

import {Filter, parseFilter, StoreRecord} from '@xh/hoist/data';
import {BucketSpecFn, Filter, LockFn, OmitFn, parseFilter, StoreRecord} from '@xh/hoist/data';
import {isEqual, find} from 'lodash';
import {FilterLike, FilterTestFn} from '../filter/Types';
import {CubeField} from './CubeField';
Expand Down Expand Up @@ -51,6 +51,25 @@ export interface QueryConfig {

/** True to include leaf nodes in return.*/
includeLeaves?: boolean;

/**
* Optional function to be called for each aggregate node to determine if it should be "locked",
* preventing drill-down into its children. Defaults to Cube.lockFn.
*/
lockFn?: LockFn;

/**
* Optional function to be called for each dimension during row generation to determine if the
* children of that dimension should be bucketed into additional dynamic dimensions.
* Defaults to Cube.bucketSpecFn.
*/
bucketSpecFn?: BucketSpecFn;

/**
* Optional function to be called on all single child rows during view processing.
* Return true to omit the row. Defaults to Cube.omitFn.
*/
omitFn?: OmitFn;
}

/** {@inheritDoc QueryConfig} */
Expand All @@ -61,6 +80,9 @@ export class Query {
readonly includeRoot: boolean;
readonly includeLeaves: boolean;
readonly cube: Cube;
readonly lockFn: LockFn;
readonly bucketSpecFn: BucketSpecFn;
readonly omitFn: OmitFn;

private readonly _testFn: FilterTestFn;

Expand All @@ -70,14 +92,20 @@ export class Query {
dimensions,
filter = null,
includeRoot = false,
includeLeaves = false
includeLeaves = false,
lockFn = cube.lockFn,
bucketSpecFn = cube.bucketSpecFn,
omitFn = cube.omitFn
}: QueryConfig) {
this.cube = cube;
this.fields = this.parseFields(fields);
this.dimensions = this.parseDimensions(dimensions);
this.includeRoot = includeRoot;
this.includeLeaves = includeLeaves;
this.filter = parseFilter(filter);
this.lockFn = lockFn;
this.bucketSpecFn = bucketSpecFn;
this.omitFn = omitFn;

this._testFn = this.filter?.getTestFn(this.cube.store) ?? null;
}
Expand All @@ -89,6 +117,9 @@ export class Query {
filter: this.filter,
includeRoot: this.includeRoot,
includeLeaves: this.includeLeaves,
lockFn: this.lockFn,
bucketSpecFn: this.bucketSpecFn,
omitFn: this.omitFn,
cube: this.cube,
...overrides
};
Expand Down Expand Up @@ -121,7 +152,10 @@ export class Query {
isEqual(this.dimensions, other.dimensions) &&
this.cube === other.cube &&
this.includeRoot === other.includeRoot &&
this.includeLeaves === other.includeLeaves
this.includeLeaves === other.includeLeaves &&
this.bucketSpecFn == other.bucketSpecFn &&
this.omitFn == other.omitFn &&
this.lockFn == other.lockFn
);
}

Expand Down
4 changes: 2 additions & 2 deletions data/cube/View.ts
Original file line number Diff line number Diff line change
Expand Up @@ -351,9 +351,9 @@ export class View extends HoistBase {
parentId: string,
appliedDimensions: PlainObject
): BaseRow[] {
if (!this.cube.bucketSpecFn) return rows;
if (!this.query.bucketSpecFn) return rows;

const bucketSpec = this.cube.bucketSpecFn(rows);
const bucketSpec = this.query.bucketSpecFn(rows);
if (!bucketSpec) return rows;

if (!this.query.includeLeaves && rows[0]?.isLeaf) return rows;
Expand Down
4 changes: 2 additions & 2 deletions data/cube/row/BaseRow.ts
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ export abstract class BaseRow {
let dataChildren = this.getVisibleChildrenDatas();

// 2) If omitting ourselves, we are done, return visible children.
if (!isLeaf && view.cube.omitFn?.(this as any)) return dataChildren;
if (!isLeaf && view.query.omitFn?.(this as any)) return dataChildren;

// 3) Otherwise, we can attach this data to the children data and return.

Expand Down Expand Up @@ -89,7 +89,7 @@ export abstract class BaseRow {
}

// Skip all children in a locked node
if (view.cube.lockFn?.(this as any)) {
if (view.query.lockFn?.(this as any)) {
this.locked = true;
return null;
}
Expand Down