Search in sources :

Example 1 with Function

use of com.srotya.sidewinder.core.functions.list.Function in project sidewinder by srotya.

the class GrafanaUtils method extractTargetsFromJson.

/**
 * Extract target series from Grafana Json query payload
 *
 * @param json
 * @param targetSeries
 * @throws InvalidFilterException
 */
public static void extractTargetsFromJson(JsonObject json, List<TargetSeries> targetSeries) throws InvalidFilterException {
    JsonArray targets = json.get("targets").getAsJsonArray();
    for (int i = 0; i < targets.size(); i++) {
        JsonObject jsonElement = targets.get(i).getAsJsonObject();
        if (jsonElement == null) {
            continue;
        }
        if (jsonElement.has("target") && jsonElement.has("field")) {
            TagFilter filter = extractGrafanaFilter(jsonElement);
            Function aggregationFunction = extractGrafanaAggregation(jsonElement);
            FunctionIteratorFactory functionIterator = extractGrafanaFunction(jsonElement);
            boolean correlate = false;
            if (jsonElement.has("correlate")) {
                correlate = jsonElement.get("correlate").getAsBoolean();
            }
            TargetSeries e = new TargetSeries(jsonElement.get("target").getAsString(), jsonElement.get("field").getAsString(), filter, aggregationFunction, correlate, functionIterator);
            targetSeries.add(e);
            logger.log(Level.FINE, () -> "Parsed and extracted target:" + e);
        } else if (jsonElement.has("raw") && jsonElement.get("rawQuery").getAsBoolean()) {
            // raw query recieved
            TargetSeries e;
            try {
                e = MiscUtils.extractTargetFromQuery(jsonElement.get("raw").getAsString());
            } catch (Exception e1) {
                throw new InvalidFilterException("Bad function expression:" + jsonElement.get("raw").getAsString() + " reason:" + e1.getMessage());
            }
            logger.log(Level.FINE, () -> "Parsed and extracted raw query:" + e);
            if (e != null) {
                targetSeries.add(e);
            }
        }
    }
}
Also used : JsonArray(com.google.gson.JsonArray) Function(com.srotya.sidewinder.core.functions.list.Function) InvalidFilterException(com.srotya.sidewinder.core.utils.InvalidFilterException) TagFilter(com.srotya.sidewinder.core.filters.TagFilter) SimpleTagFilter(com.srotya.sidewinder.core.filters.SimpleTagFilter) ComplexTagFilter(com.srotya.sidewinder.core.filters.ComplexTagFilter) JsonObject(com.google.gson.JsonObject) FunctionIteratorFactory(com.srotya.sidewinder.core.functions.iterative.FunctionIteratorFactory) DataPoint(com.srotya.sidewinder.core.storage.DataPoint) ItemNotFoundException(com.srotya.sidewinder.core.storage.ItemNotFoundException) InvalidFilterException(com.srotya.sidewinder.core.utils.InvalidFilterException) BadRequestException(javax.ws.rs.BadRequestException) IOException(java.io.IOException) NotFoundException(javax.ws.rs.NotFoundException)

Example 2 with Function

use of com.srotya.sidewinder.core.functions.list.Function in project sidewinder by srotya.

the class TestTransformFunctions method testLog10.

@Test
public void testLog10() {
    Function f = new Log10Function();
    List<SeriesOutput> series = new ArrayList<>();
    SeriesOutput s = new SeriesOutput(Arrays.asList(new DataPoint(1L, 100), new DataPoint(1L, 1000)));
    series.add(s);
    List<SeriesOutput> apply = f.apply(series);
    assertEquals(2, apply.get(0).getDataPoints().size());
    assertEquals(2, apply.get(0).getDataPoints().get(0).getLongValue(), 0.1);
    assertEquals(3, apply.get(0).getDataPoints().get(1).getLongValue(), 0.1);
}
Also used : Function(com.srotya.sidewinder.core.functions.list.Function) DataPoint(com.srotya.sidewinder.core.storage.DataPoint) ArrayList(java.util.ArrayList) SeriesOutput(com.srotya.sidewinder.core.storage.SeriesOutput) Test(org.junit.Test)

Example 3 with Function

use of com.srotya.sidewinder.core.functions.list.Function in project sidewinder by srotya.

the class TestTransformFunctions method testLog.

@Test
public void testLog() {
    Function f = new LogFunction();
    List<SeriesOutput> series = new ArrayList<>();
    SeriesOutput s = new SeriesOutput(Arrays.asList(new DataPoint(1L, 100), new DataPoint(1L, 1000)));
    series.add(s);
    List<SeriesOutput> apply = f.apply(series);
    assertEquals(2, apply.get(0).getDataPoints().size());
    assertEquals(4.0, apply.get(0).getDataPoints().get(0).getLongValue(), 0.1);
    assertEquals(6.0, apply.get(0).getDataPoints().get(1).getLongValue(), 0.1);
}
Also used : Function(com.srotya.sidewinder.core.functions.list.Function) DataPoint(com.srotya.sidewinder.core.storage.DataPoint) ArrayList(java.util.ArrayList) SeriesOutput(com.srotya.sidewinder.core.storage.SeriesOutput) Test(org.junit.Test)

Example 4 with Function

use of com.srotya.sidewinder.core.functions.list.Function in project sidewinder by srotya.

the class TestTransformFunctions method testCeil.

@Test
public void testCeil() {
    Function f = new CeilFunction();
    List<SeriesOutput> series = new ArrayList<>();
    SeriesOutput s = new SeriesOutput(Arrays.asList(new DataPoint(1L, 3.2), new DataPoint(1L, 4.7)));
    series.add(s);
    List<SeriesOutput> apply = f.apply(series);
    assertEquals(2, apply.get(0).getDataPoints().size());
    assertEquals(3.2, apply.get(0).getDataPoints().get(0).getValue(), 0.1);
    assertEquals(4.7, apply.get(0).getDataPoints().get(1).getValue(), 0.1);
}
Also used : Function(com.srotya.sidewinder.core.functions.list.Function) DataPoint(com.srotya.sidewinder.core.storage.DataPoint) ArrayList(java.util.ArrayList) SeriesOutput(com.srotya.sidewinder.core.storage.SeriesOutput) Test(org.junit.Test)

Example 5 with Function

use of com.srotya.sidewinder.core.functions.list.Function in project sidewinder by srotya.

the class TestTransformFunctions method testSquareRoot.

@Test
public void testSquareRoot() {
    Function f = new SqrtFunction();
    List<SeriesOutput> series = new ArrayList<>();
    SeriesOutput s = new SeriesOutput(Arrays.asList(new DataPoint(1L, 9), new DataPoint(1L, 16)));
    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());
}
Also used : Function(com.srotya.sidewinder.core.functions.list.Function) DataPoint(com.srotya.sidewinder.core.storage.DataPoint) ArrayList(java.util.ArrayList) SeriesOutput(com.srotya.sidewinder.core.storage.SeriesOutput) Test(org.junit.Test)

Aggregations

Function (com.srotya.sidewinder.core.functions.list.Function)11 DataPoint (com.srotya.sidewinder.core.storage.DataPoint)10 SeriesOutput (com.srotya.sidewinder.core.storage.SeriesOutput)7 ArrayList (java.util.ArrayList)7 Test (org.junit.Test)7 BadRequestException (javax.ws.rs.BadRequestException)4 JsonArray (com.google.gson.JsonArray)2 JsonObject (com.google.gson.JsonObject)2 ComplexTagFilter (com.srotya.sidewinder.core.filters.ComplexTagFilter)2 SimpleTagFilter (com.srotya.sidewinder.core.filters.SimpleTagFilter)2 TagFilter (com.srotya.sidewinder.core.filters.TagFilter)2 FunctionIteratorFactory (com.srotya.sidewinder.core.functions.iterative.FunctionIteratorFactory)2 ChainFunction (com.srotya.sidewinder.core.functions.list.ChainFunction)2 ByteString (com.srotya.sidewinder.core.storage.ByteString)2 ItemNotFoundException (com.srotya.sidewinder.core.storage.ItemNotFoundException)2 InvalidFilterException (com.srotya.sidewinder.core.utils.InvalidFilterException)2 IOException (java.io.IOException)2 NotFoundException (javax.ws.rs.NotFoundException)2 JsonElement (com.google.gson.JsonElement)1 TargetSeries (com.srotya.sidewinder.core.api.grafana.TargetSeries)1