Search in sources :

Example 1 with FunctionIteratorFactory

use of com.srotya.sidewinder.core.functions.iterative.FunctionIteratorFactory 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 FunctionIteratorFactory

use of com.srotya.sidewinder.core.functions.iterative.FunctionIteratorFactory in project sidewinder by srotya.

the class GrafanaUtils method extractGrafanaFunction.

public static FunctionIteratorFactory extractGrafanaFunction(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 FunctionIterator> functionIteratorClass = FunctionIteratorTable.get().lookupFunction(name);
    if (functionIteratorClass != null) {
        try {
            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;
                }
            }
            FunctionIteratorFactory template = new FunctionIteratorFactory(Arrays.asList(new FunctionIteratorFactory.FunctionTemplate(functionIteratorClass, args)));
            return template;
        } catch (Exception e) {
            logger.log(Level.SEVERE, "Failed to extract aggregate function:" + jsonElement, e);
        }
    }
    return null;
}
Also used : JsonArray(com.google.gson.JsonArray) JsonElement(com.google.gson.JsonElement) JsonObject(com.google.gson.JsonObject) 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 3 with FunctionIteratorFactory

use of com.srotya.sidewinder.core.functions.iterative.FunctionIteratorFactory in project sidewinder by srotya.

the class MiscUtils method createIteratorChain.

public static FunctionIteratorFactory createIteratorChain(String[] parts, int startIndex) throws Exception {
    List<FunctionTemplate> templates = new ArrayList<>();
    for (int k = startIndex; k < parts.length; k++) {
        String[] args = parts[k].split(",");
        Class<? extends FunctionIterator> lookupFunction = FunctionIteratorTable.get().lookupFunction(args[0]);
        if (lookupFunction == null) {
            throw new BadRequestException("Unknown function:" + args[0]);
        }
        FunctionIterator instance = FunctionIterator.getDummyInstance(lookupFunction);
        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);
        templates.add(new FunctionTemplate(lookupFunction, ary));
    }
    return new FunctionIteratorFactory(templates);
}
Also used : FunctionIterator(com.srotya.sidewinder.core.functions.iterative.FunctionIterator) Matcher(java.util.regex.Matcher) ArrayList(java.util.ArrayList) ByteString(com.srotya.sidewinder.core.storage.ByteString) DataPoint(com.srotya.sidewinder.core.storage.DataPoint) Point(com.srotya.sidewinder.core.rpc.Point) FunctionTemplate(com.srotya.sidewinder.core.functions.iterative.FunctionIteratorFactory.FunctionTemplate) BadRequestException(javax.ws.rs.BadRequestException) FunctionIteratorFactory(com.srotya.sidewinder.core.functions.iterative.FunctionIteratorFactory)

Example 4 with FunctionIteratorFactory

use of com.srotya.sidewinder.core.functions.iterative.FunctionIteratorFactory 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);
}
Also used : ChainFunction(com.srotya.sidewinder.core.functions.list.ChainFunction) Function(com.srotya.sidewinder.core.functions.list.Function) TagFilter(com.srotya.sidewinder.core.filters.TagFilter) SimpleTagFilter(com.srotya.sidewinder.core.filters.SimpleTagFilter) ComplexTagFilter(com.srotya.sidewinder.core.filters.ComplexTagFilter) TargetSeries(com.srotya.sidewinder.core.api.grafana.TargetSeries) BadRequestException(javax.ws.rs.BadRequestException) FunctionIteratorFactory(com.srotya.sidewinder.core.functions.iterative.FunctionIteratorFactory) ByteString(com.srotya.sidewinder.core.storage.ByteString)

Aggregations

FunctionIteratorFactory (com.srotya.sidewinder.core.functions.iterative.FunctionIteratorFactory)4 BadRequestException (javax.ws.rs.BadRequestException)4 DataPoint (com.srotya.sidewinder.core.storage.DataPoint)3 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 Function (com.srotya.sidewinder.core.functions.list.Function)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 FunctionIterator (com.srotya.sidewinder.core.functions.iterative.FunctionIterator)1 FunctionTemplate (com.srotya.sidewinder.core.functions.iterative.FunctionIteratorFactory.FunctionTemplate)1 ChainFunction (com.srotya.sidewinder.core.functions.list.ChainFunction)1 Point (com.srotya.sidewinder.core.rpc.Point)1