Skip to content

Commit

Permalink
usedMeasures function
Browse files Browse the repository at this point in the history
  • Loading branch information
adrianmroz-allegro committed Feb 15, 2023
1 parent a59554d commit 87ede20
Show file tree
Hide file tree
Showing 8 changed files with 174 additions and 24 deletions.
4 changes: 4 additions & 0 deletions src/common/models/measure/measure.fixtures.ts
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,10 @@ export class MeasureFixtures {
return createMeasure("avg_delta", $("main").average($("delta")));
}

static histogram(): Measure {
return createMeasure("histogram", $("main").quantile($("create_to_collect_duration_histogram"), 0.95, "k=128"));
}

static wikiCountJS(): MeasureJS {
return {
name: "count",
Expand Down
15 changes: 1 addition & 14 deletions src/common/models/series/expression-series.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@

import { Record } from "immutable";
import { RequireOnly } from "../../utils/functional/functional";
import { Expression, ExpressionSeriesOperation, fromJS } from "../expression/expression";
import { Expression, fromJS } from "../expression/expression";
import { getNameWithDerivation, SeriesDerivation } from "./concrete-series";
import { BasicSeriesValue, SeriesBehaviours } from "./series";
import { DEFAULT_FORMAT, SeriesFormat } from "./series-format";
Expand Down Expand Up @@ -58,17 +58,4 @@ export class ExpressionSeries extends Record<ExpressionSeriesValue>(defaultSerie
plywoodKey(period = SeriesDerivation.CURRENT): string {
return getNameWithDerivation(this.key(), period);
}

measures(): string[] {
switch (this.expression.operation) {
case ExpressionSeriesOperation.PERCENT_OF_PARENT:
case ExpressionSeriesOperation.PERCENT_OF_TOTAL:
return [this.reference];
case ExpressionSeriesOperation.ADD:
case ExpressionSeriesOperation.SUBTRACT:
case ExpressionSeriesOperation.MULTIPLY:
case ExpressionSeriesOperation.DIVIDE:
return [this.reference, this.expression.reference];
}
}
}
4 changes: 0 additions & 4 deletions src/common/models/series/measure-series.ts
Original file line number Diff line number Diff line change
Expand Up @@ -54,8 +54,4 @@ export class MeasureSeries extends Record<MeasureSeriesValue>(defaultMeasureSeri
plywoodKey(derivation = SeriesDerivation.CURRENT): string {
return getNameWithDerivation(this.reference, derivation);
}

measures(): string[] {
return [this.reference];
}
}
4 changes: 0 additions & 4 deletions src/common/models/series/quantile-series.ts
Original file line number Diff line number Diff line change
Expand Up @@ -71,8 +71,4 @@ export class QuantileSeries extends Record<QuantileSeriesValue>(defaultQuantileS
plywoodKey(derivation = SeriesDerivation.CURRENT): string {
return getNameWithDerivation(this.key(), derivation);
}

measures(): string[] {
return [this.reference];
}
}
1 change: 0 additions & 1 deletion src/common/models/series/series.ts
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,6 @@ export interface BasicSeriesValue {
export interface SeriesBehaviours {
key: () => string;
plywoodKey: (period?: SeriesDerivation) => string;
measures: () => string[];
}

export type Series = MeasureSeries | ExpressionSeries | QuantileSeries;
Expand Down
123 changes: 123 additions & 0 deletions src/common/models/series/used-measures.mocha.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,123 @@
/*
* Copyright 2017-2022 Allegro.pl
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

import { expect } from "chai";
import { ArithmeticExpression } from "../expression/concreteArithmeticOperation";
import { ExpressionSeriesOperation } from "../expression/expression";
import { PercentExpression } from "../expression/percent";
import { MeasureFixtures } from "../measure/measure.fixtures";
import { ExpressionSeries } from "./expression-series";
import { MeasureSeries } from "./measure-series";
import { QuantileSeries } from "./quantile-series";
import { usedMeasures } from "./used-measures";

const avgDelta = MeasureFixtures.avgDelta();
const avgAdded = MeasureFixtures.avgAdded();
const histogram = MeasureFixtures.histogram();

describe("usedMeasures", () => {
describe("MeasureSeries", () => {
it("should return array with measure reference", () => {
const series = MeasureSeries.fromMeasure(avgDelta);
expect(usedMeasures(series)).to.be.deep.equal(["avg_delta"]);
});
});

describe("QuantileSeries", () => {
it("should return array with measure reference", () => {
const series = QuantileSeries.fromQuantileMeasure(histogram);
expect(usedMeasures(series)).to.be.deep.equal(["histogram"]);
});
});

describe("ExpressionSeries", () => {
describe("Percent of total", () => {
it("should return array with measure reference", () => {
const series = ExpressionSeries.fromJS({
reference: avgDelta.name,
expression: new PercentExpression({
operation: ExpressionSeriesOperation.PERCENT_OF_TOTAL
})
});
expect(usedMeasures(series)).to.be.deep.equal(["avg_delta"]);
});
});

describe("Percent of parent", () => {
it("should return array with measure reference", () => {
const series = ExpressionSeries.fromJS({
reference: avgDelta.name,
expression: new PercentExpression({
operation: ExpressionSeriesOperation.PERCENT_OF_PARENT
})
});
expect(usedMeasures(series)).to.be.deep.equal(["avg_delta"]);
});
});

describe("Arithmetic ADD", () => {
it("should return array with measure reference and reference of operand", () => {
const series = ExpressionSeries.fromJS({
reference: avgDelta.name,
expression: new ArithmeticExpression({
operation: ExpressionSeriesOperation.ADD,
reference: avgAdded.name
})
});
expect(usedMeasures(series)).to.be.deep.equal(["avg_delta", "avg_added"]);
});
});

describe("Arithmetic SUBTRACT", () => {
it("should return array with measure reference and reference of operand", () => {
const series = ExpressionSeries.fromJS({
reference: avgDelta.name,
expression: new ArithmeticExpression({
operation: ExpressionSeriesOperation.SUBTRACT,
reference: avgAdded.name
})
});
expect(usedMeasures(series)).to.be.deep.equal(["avg_delta", "avg_added"]);
});
});

describe("Arithmetic MULTIPLY", () => {
it("should return array with measure reference and reference of operand", () => {
const series = ExpressionSeries.fromJS({
reference: avgDelta.name,
expression: new ArithmeticExpression({
operation: ExpressionSeriesOperation.MULTIPLY,
reference: avgAdded.name
})
});
expect(usedMeasures(series)).to.be.deep.equal(["avg_delta", "avg_added"]);
});
});

describe("Arithmetic DIVIDE", () => {
it("should return array with measure reference and reference of operand", () => {
const series = ExpressionSeries.fromJS({
reference: avgDelta.name,
expression: new ArithmeticExpression({
operation: ExpressionSeriesOperation.DIVIDE,
reference: avgAdded.name
})
});
expect(usedMeasures(series)).to.be.deep.equal(["avg_delta", "avg_added"]);
});
});
});
});
44 changes: 44 additions & 0 deletions src/common/models/series/used-measures.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
/*
* Copyright 2017-2022 Allegro.pl
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

import { ExpressionSeriesOperation } from "../expression/expression";
import { ExpressionSeries } from "./expression-series";
import { Series } from "./series";
import { SeriesType } from "./series-type";

function usedMeasuresInExpressionSeries(series: ExpressionSeries): string[] {
switch (series.expression.operation) {
case ExpressionSeriesOperation.PERCENT_OF_PARENT:
case ExpressionSeriesOperation.PERCENT_OF_TOTAL:
return [series.reference];
case ExpressionSeriesOperation.ADD:
case ExpressionSeriesOperation.SUBTRACT:
case ExpressionSeriesOperation.MULTIPLY:
case ExpressionSeriesOperation.DIVIDE:
return [series.reference, series.expression.reference];
}
}

export function usedMeasures(series: Series): string[] {
switch (series.type) {
case SeriesType.MEASURE:
return [series.reference];
case SeriesType.EXPRESSION:
return usedMeasuresInExpressionSeries(series);
case SeriesType.QUANTILE:
return [series.reference];
}
}
3 changes: 2 additions & 1 deletion src/server/routes/query/routes/visualization.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ import makeGridQuery from "../../../../client/visualizations/grid/make-query";
import { Logger } from "../../../../common/logger/logger";
import { Essence } from "../../../../common/models/essence/essence";
import { FixedTimeFilterClause } from "../../../../common/models/filter-clause/filter-clause";
import { usedMeasures } from "../../../../common/models/series/used-measures";
import { Timekeeper } from "../../../../common/models/timekeeper/timekeeper";
import makeQuery from "../../../../common/utils/query/visualization-query";
import { executeQuery } from "../../../utils/query/execute-query";
Expand Down Expand Up @@ -70,7 +71,7 @@ function logQueryInfo(essence: Essence, timekeeper: Timekeeper, logger: Logger)
visualization: essence.visualization.name,
filters: nonTimeFilters.clauses.map(clause => clause.reference).toArray(),
splits: essence.splits.splits.map(split => split.reference).toArray(),
measures: essence.series.series.flatMap(series => series.measures()).toSet().toArray()
measures: essence.series.series.flatMap(usedMeasures).toSet().toArray()
});
}

Expand Down

0 comments on commit 87ede20

Please sign in to comment.