forked from OpenTSDB/opentsdb
/
ArithmeticExpressionCalculator.java
376 lines (302 loc) · 13 KB
/
ArithmeticExpressionCalculator.java
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
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
package net.opentsdb.expression;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import net.opentsdb.core.DataPoints;
import org.antlr.runtime.ANTLRStringStream;
import org.antlr.runtime.CommonTokenStream;
import org.antlr.runtime.RecognitionException;
import org.antlr.runtime.tree.CommonTreeNodeStream;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
public class ArithmeticExpressionCalculator {
private static final Logger LOG = LoggerFactory
.getLogger(ArithmeticExpressionCalculator.class);
private final Map<String, FunctionCalculator> FUNCTION_CALCULATORS;
private final ArithmeticNode rootNode;
public ArithmeticExpressionCalculator(String arithmeticExpression) {
rootNode = parseArithmeticExpression(arithmeticExpression);
FUNCTION_CALCULATORS = FunctionUtils.getFunctions();
}
public List<DataPoints> calculateArithmeticExpression(
final Map<String, DataPoints[]> queryResults) {
List<DataPoints> result = null;
addDataPointsToMetricNodes(rootNode, queryResults);
result = calculate(rootNode);
return result;
}
private ArithmeticNode parseArithmeticExpression(
final String arithmeticExpression) {
ArithmeticNode result = null;
if (arithmeticExpression != null && !arithmeticExpression.isEmpty()) {
try {
ANTLRStringStream input = new ANTLRStringStream(arithmeticExpression);
MetricArithmeticExpressionLexer lexer = new MetricArithmeticExpressionLexer(
input);
CommonTokenStream tokens = new CommonTokenStream(lexer);
MetricArithmeticExpressionParser parser = new MetricArithmeticExpressionParser(
tokens);
MetricArithmeticExpressionParser.parse_return parseReturn = parser
.parse();
CommonTreeNodeStream nodes = new CommonTreeNodeStream(parseReturn.tree);
nodes.setTokenStream(tokens);
MetricArithmeticExpressionTreeWalker walker = new MetricArithmeticExpressionTreeWalker(
nodes);
result = walker.parse();
} catch (RecognitionException e) {
throw new RuntimeException("error parsing arithmetic expression: "
+ arithmeticExpression, e);
}
}
return result;
}
private void addDataPointsToMetricNodes(final ArithmeticNode arithmeticNode,
final Map<String, DataPoints[]> queryResults) {
if (arithmeticNode instanceof OperatorNode) {
OperatorNode operatorNode = (OperatorNode) arithmeticNode;
addDataPointsToMetricNodes(operatorNode.getOperandOne(), queryResults);
addDataPointsToMetricNodes(operatorNode.getOperandTwo(), queryResults);
} else if (arithmeticNode instanceof FunctionNode) {
FunctionNode functionNode = (FunctionNode) arithmeticNode;
for (ArithmeticNode childNode : functionNode.getParameters()) {
addDataPointsToMetricNodes(childNode, queryResults);
}
} else if (arithmeticNode instanceof MetricNode) {
MetricNode metricNode = (MetricNode) arithmeticNode;
DataPoints[] dataPoints = queryResults.get(metricNode.getName());
metricNode.setDataPoints(dataPoints);
}
}
private List<DataPoints> calculate(ArithmeticNode rootNode) {
return repackageNodeResults(calculateInternal(rootNode));
}
private NodeResult[] calculateInternal(ArithmeticNode node) {
NodeResult[] result = null;
if (node instanceof OperatorNode) {
result = calculateOperation((OperatorNode) node);
} else if (node instanceof FunctionNode) {
result = calculateFunction((FunctionNode) node);
} else if (node instanceof MetricNode) {
result = ((MetricNode) node).getDataPointsValues();
} else if (node instanceof LiteralNode) {
result = ((LiteralNode) node).getDataPointsValues();
}
return result;
}
private NodeResult[] calculateOperation(OperatorNode operator) {
List<NodeResult> result = new ArrayList<NodeResult>();
ArithmeticNode operandOne = operator.getOperandOne();
ArithmeticNode operandTwo = operator.getOperandTwo();
NodeResult[] operandOneNodeResults = calculateInternal(operandOne);
NodeResult[] operandTwoNodeResults = calculateInternal(operandTwo);
for (NodeResult operandOneNodeResult : operandOneNodeResults) {
for (NodeResult operandTwoNodeResult : operandTwoNodeResults) {
result.add(calculateOperandValues(operandOneNodeResult,
operandTwoNodeResult, operator.getOperator()));
}
}
return result.toArray(new NodeResult[result.size()]);
}
private NodeResult calculateOperandValues(NodeResult operandOneNodeResult,
NodeResult operandTwoNodeResult, Operator operator) {
NodeResult result = null;
if (operandOneNodeResult != null && operandTwoNodeResult != null) {
String label = operandOneNodeResult.getName() + operator.getValue()
+ operandTwoNodeResult.getName();
if (operandOneNodeResult instanceof LiteralNodeResult
&& operandTwoNodeResult instanceof LiteralNodeResult) {
result = calculateTwoLiteralNodeOperands(
(LiteralNodeResult) operandOneNodeResult,
(LiteralNodeResult) operandTwoNodeResult, operator);
} else if (operandOneNodeResult instanceof LiteralNodeResult) {
result = calculateLiteralNodeAndMetricNodeOperands(label,
(ArithmeticNodeResult) operandTwoNodeResult,
(LiteralNodeResult) operandOneNodeResult, operator);
} else if (operandTwoNodeResult instanceof LiteralNodeResult) {
result = calculateLiteralNodeAndMetricNodeOperands(label,
(ArithmeticNodeResult) operandOneNodeResult,
(LiteralNodeResult) operandTwoNodeResult, operator);
} else {
result = calculateTwoArithmeticNodeOperands(label,
(ArithmeticNodeResult) operandOneNodeResult,
(ArithmeticNodeResult) operandTwoNodeResult, operator);
}
}
return result;
}
private LiteralNodeResult calculateTwoLiteralNodeOperands(
LiteralNodeResult literalNodeResultOne,
LiteralNodeResult literalNodeResultTwo, Operator operator) {
LiteralNodeResult result = null;
final double valueOne = literalNodeResultOne.getValue();
final double valueTwo = literalNodeResultTwo.getValue();
try {
result = new LiteralNodeResult(calculateValues(valueOne, valueTwo,
operator));
} catch (MetricFormulaException e) {
LOG.info(e.getMessage());
}
return result;
}
private ArithmeticNodeResult calculateLiteralNodeAndMetricNodeOperands(
String label, ArithmeticNodeResult metricNodeResult,
LiteralNodeResult literalNodeResult, Operator operator) {
final ArithmeticNodeResult result = new ArithmeticNodeResult(label);
final double operandOneValue = literalNodeResult.getValue();
final Iterator<TimestampValue> iterator = metricNodeResult.iterator();
while (iterator.hasNext()) {
TimestampValue timestampValue = iterator.next();
try {
result.add(new TimestampValue(timestampValue.getTimestamp(),
calculateValues(operandOneValue, timestampValue.getValue(),
operator)));
} catch (MetricFormulaException e) {
LOG.info(e.getMessage());
}
}
return result;
}
private ArithmeticNodeResult calculateTwoArithmeticNodeOperands(String label,
ArithmeticNodeResult arithmeticNodeResultOne,
ArithmeticNodeResult arithmeticNodeResultTwo, Operator operator) {
final ArithmeticNodeResult result = new ArithmeticNodeResult(label);
final Iterator<TimestampValue> iteratorOne = arithmeticNodeResultOne
.iterator();
final Iterator<TimestampValue> iteratorTwo = arithmeticNodeResultTwo
.iterator();
TimestampValue timestampValueOne = iteratorOne.next();
TimestampValue lastTimestampValueOne = null;
TimestampValue timestampValueTwo = iteratorTwo.next();
TimestampValue lastTimestampValueTwo = null;
while (!timestampValueOne.equals(lastTimestampValueOne)
&& !timestampValueTwo.equals(lastTimestampValueTwo)) {
if (timestampValueOne.getTimestamp() == timestampValueTwo.getTimestamp()) {
// move on in both iterators
lastTimestampValueOne = timestampValueOne;
lastTimestampValueTwo = timestampValueTwo;
timestampValueOne = iterate(iteratorOne, timestampValueOne);
timestampValueTwo = iterate(iteratorTwo, timestampValueTwo);
} else if (timestampValueOne.getTimestamp() < timestampValueTwo
.getTimestamp()) {
// move on only in iterator one
lastTimestampValueOne = timestampValueOne;
timestampValueOne = iterate(iteratorOne, timestampValueOne);
} else {
// move on only in iterator two
lastTimestampValueTwo = timestampValueTwo;
timestampValueTwo = iterate(iteratorTwo, timestampValueTwo);
}
if (lastTimestampValueOne != null && lastTimestampValueTwo != null) {
try {
result.add(calculateTimestampValues(lastTimestampValueOne,
lastTimestampValueTwo, operator));
} catch (MetricFormulaException e) {
LOG.info(e.getMessage());
}
}
}
return result;
}
private NodeResult[] calculateFunction(FunctionNode function) {
List<NodeResult> result = new ArrayList<NodeResult>();
FunctionCalculator calculator = FUNCTION_CALCULATORS
.get(function.getName());
List<ArithmeticNode> parameterNodes = function.getParameters();
List<NodeResult[]> parameterNodesResults = calculateParameterNodesResults(parameterNodes);
NodeResult[][] cartesianParameters = getCartesianParameters(parameterNodesResults);
for (NodeResult[] parameters : cartesianParameters) {
result.add(calculator.calculate(parameters));
}
return result.toArray(new NodeResult[result.size()]);
}
private List<DataPoints> repackageNodeResults(NodeResult[] nodeResults) {
final List<DataPoints> result = new ArrayList<DataPoints>();
for (NodeResult nodeResult : nodeResults) {
if (nodeResult instanceof ArithmeticNodeResult) {
final ArithmeticExpressionResultDataPoints datapoints = new ArithmeticExpressionResultDataPoints(
nodeResult.getName());
for (TimestampValue timestampValue : (ArithmeticNodeResult) nodeResult) {
datapoints.add(new ArithmeticExpressionResultDataPoint(timestampValue
.getTimestamp(), timestampValue.getValue()));
}
result.add(datapoints);
}
}
return result;
}
private TimestampValue calculateTimestampValues(
TimestampValue timestampValueOne, TimestampValue timestampValueTwo,
Operator operator) throws MetricFormulaException {
TimestampValue result = null;
long timestampOne = timestampValueOne.getTimestamp();
double valueOne = timestampValueOne.getValue();
long timestampTwo = timestampValueTwo.getTimestamp();
double valueTwo = timestampValueTwo.getValue();
long resultTimestamp = timestampOne < timestampTwo ? timestampOne
: timestampTwo;
result = new TimestampValue(resultTimestamp, calculateValues(valueOne,
valueTwo, operator));
return result;
}
private double calculateValues(double valueOne, double valueTwo,
Operator operator) throws MetricFormulaException {
double result = 0.0;
final char operatorValue = operator.getValue();
switch (operatorValue) {
case '+':
result = valueOne + valueTwo;
break;
case '-':
result = valueOne - valueTwo;
break;
case '*':
result = valueOne * valueTwo;
break;
case '/':
if (valueTwo != 0.0) {
result = valueOne / valueTwo;
} else {
throw new MetricFormulaException("division by zero: " + valueOne + "/"
+ valueTwo);
}
break;
}
return result;
}
private List<NodeResult[]> calculateParameterNodesResults(
List<ArithmeticNode> parameterNodes) {
List<NodeResult[]> result = new ArrayList<NodeResult[]>();
for (ArithmeticNode node : parameterNodes) {
result.add(calculateInternal(node));
}
return result;
}
private NodeResult[][] getCartesianParameters(
List<NodeResult[]> parameterNodesResults) {
int n = parameterNodesResults.size();
int solutions = 1;
for (NodeResult[] parameterValues : parameterNodesResults) {
solutions *= parameterValues.length;
}
NodeResult[][] allCombinations = new NodeResult[solutions][];
for (int i = 0; i < solutions; i++) {
List<NodeResult> combination = new ArrayList<NodeResult>();
int j = 1;
for (NodeResult[] parameterValues : parameterNodesResults) {
combination.add(parameterValues[(i / j) % parameterValues.length]);
j *= parameterValues.length;
}
allCombinations[i] = combination.toArray(new NodeResult[n]);
}
return allCombinations;
}
private TimestampValue iterate(final Iterator<TimestampValue> iterator,
final TimestampValue fallback) {
TimestampValue result = fallback;
if (iterator.hasNext()) {
result = iterator.next();
}
return result;
}
}