use of com.srotya.sidewinder.core.functions.list.ChainFunction in project sidewinder by srotya.
the class TestChainFunction method testTwoFunctions.
@Test
public void testTwoFunctions() throws Exception {
SeriesOutput series = new SeriesOutput("cpu", "test", Arrays.asList(Tag.newBuilder().setTagKey("t").setTagValue("1").build(), Tag.newBuilder().setTagKey("t").setTagValue("2").build()));
List<DataPoint> dps = new ArrayList<>();
long baseTs = 1486617103629L;
for (int i = 0; i < 4; i++) {
dps.add(new DataPoint(baseTs + 30_000 * i, 1));
}
series.setDataPoints(dps);
series.setFp(false);
List<SeriesOutput> seriesList = Arrays.asList(series);
ChainFunction cf = new ChainFunction();
WindowedAggregator rwa = new WindowedMean();
rwa.init(new Object[] { 70, "smean" });
WindowedAggregator rwa2 = new WindowedMean();
rwa2.init(new Object[] { 200, "smean" });
cf.init(new Function[] { rwa, rwa2 });
List<SeriesOutput> apply = cf.apply(seriesList);
List<DataPoint> result = apply.get(0).getDataPoints();
assertEquals(1, result.size());
assertEquals(1, result.get(0).getLongValue());
}
use of com.srotya.sidewinder.core.functions.list.ChainFunction in project sidewinder by srotya.
the class TestChainFunction method testSingleFunction.
@Test
public void testSingleFunction() throws Exception {
SeriesOutput series = new SeriesOutput("cpu", "test", Arrays.asList(Tag.newBuilder().setTagKey("t").setTagValue("1").build(), Tag.newBuilder().setTagKey("t").setTagValue("2").build()));
List<DataPoint> dps = new ArrayList<>();
long baseTs = 1486617103629L;
for (int i = 0; i < 4; i++) {
dps.add(new DataPoint(baseTs + 30_000 * i, 1));
}
series.setDataPoints(dps);
series.setFp(false);
List<SeriesOutput> seriesList = Arrays.asList(series);
ChainFunction cf = new ChainFunction();
WindowedAggregator rwa = new WindowedMean();
rwa.init(new Object[] { 70, "smean" });
cf.init(new Function[] { rwa });
List<SeriesOutput> apply = cf.apply(seriesList);
List<DataPoint> result = apply.get(0).getDataPoints();
assertEquals(2, result.size());
}
use of com.srotya.sidewinder.core.functions.list.ChainFunction 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;
}
Aggregations