Skip to content

Commit

Permalink
number filter query (#1029)
Browse files Browse the repository at this point in the history
* number filter query

* remove meaningles tests

* fix typo
  • Loading branch information
adrianmroz-allegro authored Feb 13, 2023
1 parent 4df5eb4 commit 20648b3
Show file tree
Hide file tree
Showing 6 changed files with 51 additions and 108 deletions.

This file was deleted.

This file was deleted.

17 changes: 7 additions & 10 deletions src/client/components/number-range-picker/number-range-picker.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -15,13 +15,14 @@
* limitations under the License.
*/

import { $, Dataset, ply } from "plywood";
import { Dataset } from "plywood";
import React from "react";
import { Dimension } from "../../../common/models/dimension/dimension";
import { Essence } from "../../../common/models/essence/essence";
import { Timekeeper } from "../../../common/models/timekeeper/timekeeper";
import { getNumberOfWholeDigits, isTruthy, toSignificantDigits } from "../../../common/utils/general/general";
import { clamp, classNames, getXFromEvent } from "../../utils/dom/dom";
import { ApiContext, ApiContextValue } from "../../views/cube-view/api-context";
import { Loader } from "../loader/loader";
import { QueryError } from "../query-error/query-error";
import { RangeHandle } from "../range-handle/range-handle";
Expand Down Expand Up @@ -72,6 +73,9 @@ export interface NumberRangePickerState {
}

export class NumberRangePicker extends React.Component<NumberRangePickerProps, NumberRangePickerState> {
static contextType = ApiContext;

context: ApiContextValue;
public mounted: boolean;
private picker = React.createRef<HTMLDivElement>();

Expand All @@ -87,19 +91,12 @@ export class NumberRangePicker extends React.Component<NumberRangePickerProps, N
}

fetchData(essence: Essence, timekeeper: Timekeeper, dimension: Dimension, rightBound: number): void {
const { dataCube } = essence;
const filterExpression = essence.getEffectiveFilter(timekeeper, { unfilterDimension: dimension }).toExpression(dataCube);
const $main = $("main");
const query = ply()
.apply("main", $main.filter(filterExpression))
.apply("Min", $main.min($(dimension.name)))
.apply("Max", $main.max($(dimension.name)));

const { numberFilterQuery } = this.context;
this.setState({
loading: true
});

dataCube.executor(query)
numberFilterQuery(essence, dimension)
.then(
(dataset: Dataset) => {
if (!this.mounted) return;
Expand Down
14 changes: 13 additions & 1 deletion src/client/views/cube-view/api-context.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -32,14 +32,21 @@ export type RawDataQuery = Unary<Essence, Promise<Dataset>>;
export type BooleanFilterQuery = Binary<Essence, Dimension, Promise<Dataset>>;

export type StringFilterQuery = Binary<Essence, StringFilterClause, Promise<Dataset>>;

export type NumberFilterQuery = Binary<Essence, Dimension, Promise<Dataset>>;

export interface ApiContextValue {
visualizationQuery: VisualizationQuery;
booleanFilterQuery: BooleanFilterQuery;
rawDataQuery: RawDataQuery;
stringFilterQuery: StringFilterQuery;
numberFilterQuery: NumberFilterQuery;
}

export const ApiContext = React.createContext<ApiContextValue>({
get numberFilterQuery(): NumberFilterQuery {
throw new Error("Attempted to consume ApiContext when there was no Provider");
},
get booleanFilterQuery(): BooleanFilterQuery {
throw new Error("Attempted to consume ApiContext when there was no Provider");
},
Expand All @@ -62,7 +69,7 @@ interface QueryResponse {
result: DatasetJS;
}

type QueryEndpoints = "visualization" | "boolean-filter" | "string-filter" | "number-filter" | "raw-data";
type QueryEndpoints = "visualization" | "pinboard" | "boolean-filter" | "string-filter" | "number-filter" | "raw-data";

type ExtraParams = Record<string, unknown>;

Expand Down Expand Up @@ -97,6 +104,10 @@ function createVizQueryApi(settings: ClientAppSettings): VisualizationQuery {
return createApiCall(settings, "visualization", emptyParams);
}

function createNumberFilterQuery(settings: ClientAppSettings): NumberFilterQuery {
return createApiCall(settings, "number-filter", (dimension: Dimension) => ({ dimension: dimension.name }));
}

function createBooleanFilterQuery(settings: ClientAppSettings): BooleanFilterQuery {
return createApiCall(settings, "boolean-filter", (dimension: Dimension) => ({ dimension: dimension.name }));
}
Expand All @@ -114,6 +125,7 @@ function createRawDataQueryApi(settings: ClientAppSettings): RawDataQuery {

function createApi(settings: ClientAppSettings): ApiContextValue {
return {
numberFilterQuery: createNumberFilterQuery(settings),
booleanFilterQuery: createBooleanFilterQuery(settings),
visualizationQuery: createVizQueryApi(settings),
rawDataQuery: createRawDataQueryApi(settings),
Expand Down
4 changes: 2 additions & 2 deletions src/common/utils/functional/functional.ts
Original file line number Diff line number Diff line change
Expand Up @@ -119,10 +119,10 @@ export function range(from: number, to: number): number[] {
}

// TODO: fix to use infer on arguments tuple https://stackoverflow.com/a/50014868/1089761
export function debounceWithPromise<T extends (...args: any[]) => any>(fn: T, ms: number): ((...args: any[]) => Promise<any>) & { cancel: Fn } {
export function debounceWithPromise<T extends (...args: any[]) => Promise<any>>(fn: T, ms: number): ((...args: Parameters<T>) => Promise<any>) & { cancel: Fn } {
let timeoutId: any;

const debouncedFn = (...args: any[]) => {
const debouncedFn = (...args: Parameters<T>) => {
let resolve: Function;
const promise = new Promise(pResolve => {
resolve = pResolve;
Expand Down
29 changes: 29 additions & 0 deletions src/server/routes/query/query.ts
Original file line number Diff line number Diff line change
Expand Up @@ -154,7 +154,36 @@ export function queryRouter(settings: Pick<SettingsManager, "logger" | "getSourc
const queryDecorator = getQueryDecorator(req, dataCube, settings);
const result = await executeQuery(dataCube, query, essence.timezone, queryDecorator);
res.json({ result });
} catch (error) {
handleRequestErrors(error, res, settings.logger);
}
});

router.post("/number-filter", async (req: Request, res: Response) => {

function getQuery(essence: Essence, dimension: Dimension, timekeeper: Timekeeper): Expression {
const { dataCube } = essence;
const filterExpression = essence
.getEffectiveFilter(timekeeper, { unfilterDimension: dimension })
.toExpression(dataCube);
const $main = $("main");
return ply()
.apply("main", $main.filter(filterExpression))
.apply("Min", $main.min($(dimension.name)))
.apply("Max", $main.max($(dimension.name)));
}

try {
const dataCube = await parseDataCube(req, settings);
const viewDefinition = parseViewDefinition(req);
const dimension = parseDimension(req, dataCube);

const essence = createEssence(viewDefinition, converter, dataCube, settings.appSettings);

const query = getQuery(essence, dimension, settings.getTimekeeper());
const queryDecorator = getQueryDecorator(req, dataCube, settings);
const result = await executeQuery(dataCube, query, essence.timezone, queryDecorator);
res.json({ result });
} catch (error) {
handleRequestErrors(error, res, settings.logger);
}
Expand Down

0 comments on commit 20648b3

Please sign in to comment.