-
Notifications
You must be signed in to change notification settings - Fork 169
/
visualization-query.ts
151 lines (131 loc) · 6 KB
/
visualization-query.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
/*
* Copyright 2017-2018 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 { Duration } from "chronoshift";
import { List } from "immutable";
import { $, Expression, LimitExpression, ply } from "plywood";
import { SPLIT } from "../../../client/config/constants";
import { Split, toExpression as splitToExpression } from "../../../common/models/split/split";
import { Colors } from "../../models/colors/colors";
import { DataCube } from "../../models/data-cube/data-cube";
import { Dimension } from "../../models/dimension/dimension";
import { Essence } from "../../models/essence/essence";
import { toExpression } from "../../models/filter-clause/filter-clause";
import { Filter } from "../../models/filter/filter";
import { ConcreteSeries } from "../../models/series/concrete-series";
import { Sort } from "../../models/sort/sort";
import { TimeShiftEnv } from "../../models/time-shift/time-shift-env";
import { Timekeeper } from "../../models/timekeeper/timekeeper";
import { CANONICAL_LENGTH_ID } from "../canonical-length/query";
import splitCanonicalLength from "../canonical-length/split-canonical-length";
import timeFilterCanonicalLength from "../canonical-length/time-filter-canonical-length";
import { thread } from "../functional/functional";
const $main = $("main");
function applySeries(series: List<ConcreteSeries>, timeShiftEnv: TimeShiftEnv, nestingLevel = 0) {
return (query: Expression) => {
return series.reduce((query, series) => {
return query.performAction(series.plywoodExpression(nestingLevel, timeShiftEnv));
}, query);
};
}
function applySort(sort: Sort) {
return (query: Expression) => query.performAction(sort.toExpression());
}
function applyLimit(colors: Colors, limit: number, dimension: Dimension) {
return (query: Expression) => {
if (colors && colors.dimension === dimension.name) {
return query.performAction(colors.toLimitExpression());
}
if (limit) {
return query.performAction(new LimitExpression({ value: limit }));
}
if (dimension.kind === "number") {
// Hack: Plywood converts groupBys to topN if the limit is below a certain threshold. Currently sorting on dimension in a groupBy query does not
// behave as expected and in the future plywood will handle this, but for now add a limit so a topN query is performed.
// 5000 is just a randomly selected number that's high enough that it's not immediately obvious that there's a limit.
return query.limit(5000);
}
return query;
};
}
function applyHaving(colors: Colors, splitDimension: Dimension) {
return (query: Expression): Expression => {
if (colors && colors.dimension === splitDimension.name) {
const havingFilter = colors.toHavingFilter(splitDimension.name);
if (havingFilter) {
return query.performAction(havingFilter);
}
}
return query;
};
}
function applySubSplit(nestingLevel: number, essence: Essence, timeShiftEnv: TimeShiftEnv) {
return (query: Expression) => {
if (nestingLevel >= essence.splits.length()) return query;
return query.apply(SPLIT, applySplit(nestingLevel, essence, timeShiftEnv));
};
}
function applyCanonicalLengthForTimeSplit(split: Split, dataCube: DataCube) {
return (exp: Expression) => {
const canonicalLength = splitCanonicalLength(split, dataCube);
if (!canonicalLength) return exp;
return exp.apply(CANONICAL_LENGTH_ID, canonicalLength);
};
}
function applyDimensionFilter(dimension: Dimension, filter: Filter) {
return (query: Expression) => {
if (!dimension.multiValue) return query;
const filterClause = filter.clauseForReference(dimension.name);
if (!filterClause) return query;
return query.filter(toExpression(filterClause, dimension));
};
}
function applySplit(index: number, essence: Essence, timeShiftEnv: TimeShiftEnv): Expression {
const { splits, dataCube, colors } = essence;
const split = splits.getSplit(index);
const dimension = dataCube.getDimension(split.reference);
const { sort, limit } = split;
if (!sort) {
throw new Error("something went wrong during query generation");
}
const nestingLevel = index + 1;
const currentSplit = splitToExpression(split, dimension, timeShiftEnv);
return thread(
$main.split(currentSplit, dimension.name),
applyDimensionFilter(dimension, essence.filter),
applyCanonicalLengthForTimeSplit(split, dataCube),
applyHaving(colors, dimension),
applySeries(essence.getConcreteSeries(), timeShiftEnv, nestingLevel),
applySort(sort),
applyLimit(colors, limit, dimension),
applySubSplit(nestingLevel, essence, timeShiftEnv)
);
}
export default function makeQuery(essence: Essence, timekeeper: Timekeeper): Expression {
const { splits, dataCube } = essence;
if (splits.length() > dataCube.getMaxSplits()) throw new Error(`Too many splits in query. DataCube "${dataCube.name}" supports only ${dataCube.getMaxSplits()} splits`);
const hasComparison = essence.hasComparison();
const mainFilter = essence.getEffectiveFilter(timekeeper, { combineWithPrevious: hasComparison });
const timeShiftEnv: TimeShiftEnv = essence.getTimeShiftEnv(timekeeper);
const mainExp: Expression = ply()
.apply("main", $main.filter(mainFilter.toExpression(dataCube)))
.apply(CANONICAL_LENGTH_ID, timeFilterCanonicalLength(essence, timekeeper));
const queryWithMeasures = applySeries(essence.getConcreteSeries(), timeShiftEnv)(mainExp);
if (splits.length() > 0) {
return queryWithMeasures
.apply(SPLIT, applySplit(0, essence, timeShiftEnv));
}
return queryWithMeasures;
}