Search in sources :

Example 1 with TargetSeries

use of com.srotya.sidewinder.core.api.grafana.TargetSeries in project sidewinder by srotya.

the class TestMiscUtils method testExtractTargetFromQuery.

@Test
public void testExtractTargetFromQuery() {
    TargetSeries series = MiscUtils.extractTargetFromQuery("cpu.value.tes=2|tes=3");
    assertEquals("cpu", series.getMeasurementName());
    assertEquals("value", series.getFieldName());
    try {
        series = MiscUtils.extractTargetFromQuery("cpuvalue|tes=3");
        fail("Invalid request must throw an exception");
    } catch (BadRequestException e) {
    }
}
Also used : TargetSeries(com.srotya.sidewinder.core.api.grafana.TargetSeries) BadRequestException(javax.ws.rs.BadRequestException) Test(org.junit.Test)

Example 2 with TargetSeries

use of com.srotya.sidewinder.core.api.grafana.TargetSeries in project sidewinder by srotya.

the class DatabaseOpsApi method querySeries.

@Path("/{" + DB_NAME + "}/query")
@POST
@Produces({ MediaType.APPLICATION_JSON })
@Consumes({ MediaType.TEXT_PLAIN })
public String querySeries(@PathParam(DatabaseOpsApi.DB_NAME) String dbName, String query) {
    try {
        String[] queryParts = query.split("<=?");
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss");
        long endTs = System.currentTimeMillis();
        long startTs = endTs;
        String startTime = queryParts[0];
        String endTime = queryParts[2];
        if (startTime.contains("now")) {
            String[] split = startTime.split("-");
            int offset = Integer.parseInt(split[1].charAt(0) + "");
            startTs = startTs - (offset * 3600 * 1000);
        } else if (startTime.matches("\\d+")) {
            startTs = Long.parseLong(startTime);
            endTs = Long.parseLong(endTime);
        } else {
            startTs = sdf.parse(startTime).getTime();
            endTs = sdf.parse(endTime).getTime();
        }
        // cpu.load.host=v1.domain=test\.com=>derivative,10,mean
        TargetSeries tagSeries = MiscUtils.extractTargetFromQuery(query);
        List<Series> points = storageEngine.queryDataPoints(dbName, tagSeries.getMeasurementName(), tagSeries.getFieldName(), startTs, endTs, tagSeries.getTagFilter(), null, tagSeries.getAggregationFunction());
        return new Gson().toJson(points);
    } catch (ItemNotFoundException e) {
        throw new NotFoundException(e);
    } catch (BadRequestException e) {
        throw e;
    } catch (Exception e) {
        e.printStackTrace();
        throw new InternalServerErrorException(e);
    }
}
Also used : Gson(com.google.gson.Gson) ItemNotFoundException(com.srotya.sidewinder.core.storage.ItemNotFoundException) NotFoundException(javax.ws.rs.NotFoundException) ItemNotFoundException(com.srotya.sidewinder.core.storage.ItemNotFoundException) IOException(java.io.IOException) InternalServerErrorException(javax.ws.rs.InternalServerErrorException) NotFoundException(javax.ws.rs.NotFoundException) BadRequestException(javax.ws.rs.BadRequestException) TargetSeries(com.srotya.sidewinder.core.api.grafana.TargetSeries) Series(com.srotya.sidewinder.core.storage.Series) TargetSeries(com.srotya.sidewinder.core.api.grafana.TargetSeries) BadRequestException(javax.ws.rs.BadRequestException) InternalServerErrorException(javax.ws.rs.InternalServerErrorException) SimpleDateFormat(java.text.SimpleDateFormat) ItemNotFoundException(com.srotya.sidewinder.core.storage.ItemNotFoundException) Path(javax.ws.rs.Path) POST(javax.ws.rs.POST) Produces(javax.ws.rs.Produces) Consumes(javax.ws.rs.Consumes)

Example 3 with TargetSeries

use of com.srotya.sidewinder.core.api.grafana.TargetSeries in project sidewinder by srotya.

the class MiscUtils method extractTargetFromQuery.

public static TargetSeries extractTargetFromQuery(String query) {
    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);
        }
    }
    return new TargetSeries(measurementName, valueFieldName, tagFilter, aggregationFunction, false);
}
Also used : ChainFunction(com.srotya.sidewinder.core.functions.multiseries.ChainFunction) Function(com.srotya.sidewinder.core.functions.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) BadRequestException(javax.ws.rs.BadRequestException) IOException(java.io.IOException)

Aggregations

TargetSeries (com.srotya.sidewinder.core.api.grafana.TargetSeries)3 BadRequestException (javax.ws.rs.BadRequestException)3 IOException (java.io.IOException)2 Gson (com.google.gson.Gson)1 ComplexTagFilter (com.srotya.sidewinder.core.filters.ComplexTagFilter)1 SimpleTagFilter (com.srotya.sidewinder.core.filters.SimpleTagFilter)1 TagFilter (com.srotya.sidewinder.core.filters.TagFilter)1 Function (com.srotya.sidewinder.core.functions.Function)1 ChainFunction (com.srotya.sidewinder.core.functions.multiseries.ChainFunction)1 ItemNotFoundException (com.srotya.sidewinder.core.storage.ItemNotFoundException)1 Series (com.srotya.sidewinder.core.storage.Series)1 SimpleDateFormat (java.text.SimpleDateFormat)1 Consumes (javax.ws.rs.Consumes)1 InternalServerErrorException (javax.ws.rs.InternalServerErrorException)1 NotFoundException (javax.ws.rs.NotFoundException)1 POST (javax.ws.rs.POST)1 Path (javax.ws.rs.Path)1 Produces (javax.ws.rs.Produces)1 Test (org.junit.Test)1