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

A few changes to the expression support. #625

Closed
Closed
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
1 change: 1 addition & 0 deletions Makefile.am
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,7 @@ tsdb_SRC := \
src/query/QueryUtil.java \
src/query/expression/Absolute.java \
src/query/expression/Expression.java \
src/query/expression/Alias.java \
src/query/expression/DiffSeries.java \
src/query/expression/DivideSeries.java \
src/query/expression/EDPtoDPS.java \
Expand Down
89 changes: 89 additions & 0 deletions src/query/expression/Alias.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,89 @@
// This file is part of OpenTSDB.
// Copyright (C) 2015 The OpenTSDB Authors.
//
// This program is free software: you can redistribute it and/or modify it
// under the terms of the GNU Lesser General Public License as published by
// the Free Software Foundation, either version 2.1 of the License, or (at your
// option) any later version. This program is distributed in the hope that it
// will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty
// of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser
// General Public License for more details. You should have received a copy
// of the GNU Lesser General Public License along with this program. If not,
// see <http://www.gnu.org/licenses/>.
package net.opentsdb.query.expression;

import java.util.List;
import java.util.Map;
import com.google.common.base.Joiner;

import net.opentsdb.core.DataPoint;
import net.opentsdb.core.DataPoints;
import net.opentsdb.core.MutableDataPoint;
import net.opentsdb.core.TSQuery;

/**
* Returns an alias if provided or the original metric name if not.
* @since 2.3
*/
public class Alias implements Expression {

static Joiner COMMA_JOINER = Joiner.on(',').skipNulls();

@Override
public DataPoints[] evaluate(TSQuery data_query, List<DataPoints[]> queryResults,
List<String> queryParams) {
if (queryResults == null || queryResults.size() == 0) {
throw new NullPointerException("No query results");
}

String aliasTemplate = "__default";

if (queryParams != null && queryParams.size() >= 0) {
aliasTemplate = COMMA_JOINER.join(queryParams);
}

DataPoints[] inputPoints = queryResults.get(0);

DataPoint[][] dps = new DataPoint[inputPoints.length][];

for (int j = 0; j < dps.length; j++) {
DataPoints base = inputPoints[j];
dps[j] = new DataPoint[base.size()];
int i = 0;

for (DataPoint pt : base) {
if (pt.isInteger()) {
dps[j][i] = MutableDataPoint.ofDoubleValue(pt.timestamp(), pt.longValue());
} else {
dps[j][i] = MutableDataPoint.ofDoubleValue(pt.timestamp(), pt.doubleValue());
}
i++;
}
}

DataPoints[] resultArray = new DataPoints[queryResults.get(0).length];
for (int i = 0; i < resultArray.length; i++) {
PostAggregatedDataPoints result = new PostAggregatedDataPoints(inputPoints[i],
dps[i]);

String alias = aliasTemplate;
for (Map.Entry<String, String> e : inputPoints[i].getTags().entrySet()) {
alias = alias.replace("@" + e.getKey(), e.getValue());
}

result.setAlias(alias);
resultArray[i] = result;
}

return resultArray;
}

@Override
public String writeStringField(List<String> queryParams, String innerExpression) {
if (queryParams == null || queryParams.size() == 0) {
return "NULL";
}

return queryParams.get(0);
}
}
5 changes: 5 additions & 0 deletions src/query/expression/ExpressionFactory.java
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ public final class ExpressionFactory {
new HashMap<String, Expression>();

static {
available_functions.put("alias", new Alias());
available_functions.put("scale", new Scale());
available_functions.put("absolute", new Absolute());
available_functions.put("movingAverage", new MovingAverage());
Expand All @@ -46,9 +47,13 @@ private ExpressionFactory() { }
*/
public static void addTSDBFunctions(final TSDB tsdb) {
available_functions.put("divideSeries", new DivideSeries(tsdb));
available_functions.put("divide", new DivideSeries(tsdb));
available_functions.put("sumSeries", new SumSeries(tsdb));
available_functions.put("sum", new SumSeries(tsdb));
available_functions.put("diffSeries", new DiffSeries(tsdb));
available_functions.put("difference", new DiffSeries(tsdb));
available_functions.put("multiplySeries", new MultiplySeries(tsdb));
available_functions.put("multiply", new MultiplySeries(tsdb));
}

/**
Expand Down