use of com.srotya.sidewinder.core.functions.list.Function in project sidewinder by srotya.
the class TestTransformFunctions method testSquare.
@Test
public void testSquare() {
Function f = new SquareFunction();
List<SeriesOutput> series = new ArrayList<>();
SeriesOutput s = new SeriesOutput(Arrays.asList(new DataPoint(1L, 3), new DataPoint(1L, 4)));
series.add(s);
List<SeriesOutput> apply = f.apply(series);
assertEquals(2, apply.get(0).getDataPoints().size());
assertEquals(9, apply.get(0).getDataPoints().get(0).getLongValue());
assertEquals(16, apply.get(0).getDataPoints().get(1).getLongValue());
}
use of com.srotya.sidewinder.core.functions.list.Function in project sidewinder by srotya.
the class GrafanaUtils method extractGrafanaAggregation.
public static Function extractGrafanaAggregation(JsonObject jsonElement) {
if (!jsonElement.has("aggregator")) {
return null;
}
JsonObject obj = jsonElement.get("aggregator").getAsJsonObject();
if (!obj.has("name") || !obj.has("args")) {
return null;
}
String name = obj.get("name").getAsString();
if (name.equalsIgnoreCase("none")) {
return null;
}
int multipleFactor = 1;
if (obj.has("unit")) {
multipleFactor = toSeconds(obj.get("unit").getAsString());
}
Class<? extends Function> lookupFunction = FunctionTable.get().lookupFunction(name);
if (lookupFunction != null) {
try {
Function instance = (Function) lookupFunction.newInstance();
JsonArray ary = obj.get("args").getAsJsonArray();
Object[] args = new Object[ary.size()];
for (JsonElement element : ary) {
JsonObject arg = element.getAsJsonObject();
// ignore invalid aggregation function
if (!arg.has("index") || !arg.has("value")) {
return null;
}
int index = arg.get("index").getAsInt();
switch(arg.get("type").getAsString().toLowerCase()) {
case "string":
args[index] = arg.get("value").getAsString();
break;
case "int":
args[index] = arg.get("value").getAsInt() * multipleFactor;
break;
case "long":
args[index] = arg.get("value").getAsLong() * multipleFactor;
break;
case "double":
args[index] = arg.get("value").getAsDouble();
break;
}
}
instance.init(args);
return instance;
} catch (Exception e) {
logger.log(Level.FINE, "Failed to extract aggregate function:" + jsonElement, e);
}
}
return null;
}
use of com.srotya.sidewinder.core.functions.list.Function in project sidewinder by srotya.
the class MiscUtils method createFunctionChain.
public static Function createFunctionChain(String[] parts, int startIndex) throws InstantiationException, IllegalAccessException, Exception {
Function[] arguments = new Function[parts.length - startIndex];
for (int k = startIndex, p = 0; k < parts.length; k++, p++) {
String[] args = parts[k].split(",");
Class<? extends Function> lookupFunction = FunctionTable.get().lookupFunction(args[0]);
if (lookupFunction == null) {
throw new BadRequestException("Unknown function:" + args[0]);
}
Function instance = (Function) lookupFunction.newInstance();
if (args.length - 1 < instance.getNumberOfArgs()) {
throw new BadRequestException("Insufficient arguments for aggregation function, needed:" + instance.getNumberOfArgs() + ", found:" + (args.length - 1));
}
Object[] ary = new Object[args.length - 1];
for (int i = 1; i < args.length; i++) {
Matcher matcher = NUMBER.matcher(args[i]);
if (matcher.matches()) {
if (matcher.group(1) != null) {
ary[i - 1] = Double.parseDouble(args[i]);
} else {
ary[i - 1] = Integer.parseInt(args[i]);
}
} else {
ary[i - 1] = args[i];
}
}
instance.init(ary);
arguments[p] = instance;
}
ChainFunction function = new ChainFunction();
function.init(arguments);
return function;
}
use of com.srotya.sidewinder.core.functions.list.Function in project sidewinder by srotya.
the class MiscUtils method extractTargetFromQuery.
public static TargetSeries extractTargetFromQuery(String query) throws Exception {
if (query == null || query.isEmpty()) {
return null;
}
String[] queryParts = query.split("<=?");
if (queryParts.length > 1) {
query = queryParts[1];
}
String[] parts = query.split("=>");
// select part
query = parts[0];
String[] splits = query.split("\\.");
if (splits.length < 2) {
throw new BadRequestException("Invalid query string:" + query + ". Must contain measurement and value field name");
}
String measurementName = splits[0];
String valueFieldName = splits[1];
TagFilter tagFilter = null;
if (splits.length >= 3) {
try {
tagFilter = buildTagFilter(splits[2]);
} catch (InvalidFilterException e) {
throw new BadRequestException(e);
}
}
Function aggregationFunction = null;
// if (parts.length > 1) {
// try {
// aggregationFunction = createFunctionChain(parts, 1);
// } catch (Exception e) {
// throw new BadRequestException(e.getMessage());
// }
// }
FunctionIteratorFactory template = null;
if (parts.length > 1) {
template = createIteratorChain(parts, 1);
}
return new TargetSeries(measurementName, valueFieldName, tagFilter, aggregationFunction, false, template);
}
use of com.srotya.sidewinder.core.functions.list.Function in project sidewinder by srotya.
the class TestTransformFunctions method testCubeRoot.
@Test
public void testCubeRoot() {
Function f = new CbrtFunction();
List<SeriesOutput> series = new ArrayList<>();
SeriesOutput s = new SeriesOutput(Arrays.asList(new DataPoint(1L, 27), new DataPoint(1L, 64)));
series.add(s);
List<SeriesOutput> apply = f.apply(series);
assertEquals(2, apply.get(0).getDataPoints().size());
assertEquals(3, apply.get(0).getDataPoints().get(0).getLongValue());
assertEquals(4, apply.get(0).getDataPoints().get(1).getLongValue());
}
Aggregations