Search in sources :

Example 1 with ItemNotFoundException

use of com.srotya.sidewinder.core.storage.ItemNotFoundException 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 2 with ItemNotFoundException

use of com.srotya.sidewinder.core.storage.ItemNotFoundException in project sidewinder by srotya.

the class MeasurementOpsApi method getSeries.

public List<Number[]> getSeries(@PathParam(DatabaseOpsApi.DB_NAME) String dbName, @PathParam(MEASUREMENT) String measurementName, @QueryParam("field") String valueFieldName, @DefaultValue("now-1h") @QueryParam(START_TIME) String startTime, @QueryParam(END_TIME) String endTime) {
    if (valueFieldName == null) {
        throw new BadRequestException("Must specify a value field");
    }
    try {
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        long endTs = System.currentTimeMillis();
        long startTs = endTs;
        if (startTime.contains("now")) {
            String[] split = startTime.split("-");
            int offset = Integer.parseInt(split[1].charAt(0) + "");
            startTs = startTs - (offset * 3600 * 1000);
        } else {
            startTs = sdf.parse(startTime).getTime();
            endTs = sdf.parse(endTime).getTime();
        }
        List<Series> points = engine.queryDataPoints(dbName, measurementName, valueFieldName, startTs, endTs, null);
        List<Number[]> response = new ArrayList<>();
        for (Series entry : points) {
            for (DataPoint dataPoint : entry.getDataPoints()) {
                if (entry.isFp()) {
                    response.add(new Number[] { dataPoint.getLongValue(), dataPoint.getTimestamp() });
                } else {
                    response.add(new Number[] { dataPoint.getValue(), dataPoint.getTimestamp() });
                }
            }
        }
        return response;
    } catch (ItemNotFoundException e) {
        throw new NotFoundException(e);
    } catch (Exception e) {
        throw new InternalServerErrorException(e);
    }
}
Also used : ArrayList(java.util.ArrayList) ItemNotFoundException(com.srotya.sidewinder.core.storage.ItemNotFoundException) NotFoundException(javax.ws.rs.NotFoundException) DataPoint(com.srotya.sidewinder.core.storage.DataPoint) ItemNotFoundException(com.srotya.sidewinder.core.storage.ItemNotFoundException) BadRequestException(javax.ws.rs.BadRequestException) IOException(java.io.IOException) InternalServerErrorException(javax.ws.rs.InternalServerErrorException) NotFoundException(javax.ws.rs.NotFoundException) Series(com.srotya.sidewinder.core.storage.Series) DataPoint(com.srotya.sidewinder.core.storage.DataPoint) BadRequestException(javax.ws.rs.BadRequestException) InternalServerErrorException(javax.ws.rs.InternalServerErrorException) SimpleDateFormat(java.text.SimpleDateFormat) ItemNotFoundException(com.srotya.sidewinder.core.storage.ItemNotFoundException)

Example 3 with ItemNotFoundException

use of com.srotya.sidewinder.core.storage.ItemNotFoundException in project sidewinder by srotya.

the class MeasurementOpsApi method updateRetentionPolicy.

@Path("/series/retention/{retentionPolicy}")
@PUT
public void updateRetentionPolicy(@PathParam(DatabaseOpsApi.DB_NAME) String dbName, @PathParam(MEASUREMENT) String measurementName, @PathParam("retentionPolicy") int retentionPolicy) {
    try {
        engine.updateDefaultTimeSeriesRetentionPolicy(dbName, retentionPolicy);
        logger.info("Updated retention policy for:" + dbName + "\t" + retentionPolicy + " hours");
    } catch (ItemNotFoundException e) {
        throw new NotFoundException(e);
    }
}
Also used : ItemNotFoundException(com.srotya.sidewinder.core.storage.ItemNotFoundException) NotFoundException(javax.ws.rs.NotFoundException) ItemNotFoundException(com.srotya.sidewinder.core.storage.ItemNotFoundException) Path(javax.ws.rs.Path) PUT(javax.ws.rs.PUT)

Example 4 with ItemNotFoundException

use of com.srotya.sidewinder.core.storage.ItemNotFoundException in project sidewinder by srotya.

the class MeasurementOpsApi method listMeasurements.

@GET
@Produces({ MediaType.APPLICATION_JSON })
public String listMeasurements(@PathParam(DatabaseOpsApi.DB_NAME) String dbName, @PathParam(MEASUREMENT) String measurementName) {
    try {
        Set<String> fields = engine.getFieldsForMeasurement(dbName, measurementName);
        Set<String> tagsForMeasurement = engine.getTagKeysForMeasurement(dbName, measurementName);
        JsonObject obj = new JsonObject();
        JsonArray fieldAry = new JsonArray();
        for (String field : fields) {
            fieldAry.add(field);
        }
        obj.add("fields", fieldAry);
        JsonArray tagAry = new JsonArray();
        for (String tag : tagsForMeasurement) {
            tagAry.add(tag);
        }
        obj.add("tags", tagAry);
        return new Gson().toJson(obj);
    } catch (ItemNotFoundException e) {
        throw new NotFoundException(e);
    } catch (Exception e) {
        throw new InternalServerErrorException(e);
    }
}
Also used : JsonArray(com.google.gson.JsonArray) JsonObject(com.google.gson.JsonObject) Gson(com.google.gson.Gson) ItemNotFoundException(com.srotya.sidewinder.core.storage.ItemNotFoundException) NotFoundException(javax.ws.rs.NotFoundException) InternalServerErrorException(javax.ws.rs.InternalServerErrorException) ItemNotFoundException(com.srotya.sidewinder.core.storage.ItemNotFoundException) BadRequestException(javax.ws.rs.BadRequestException) IOException(java.io.IOException) InternalServerErrorException(javax.ws.rs.InternalServerErrorException) NotFoundException(javax.ws.rs.NotFoundException) ItemNotFoundException(com.srotya.sidewinder.core.storage.ItemNotFoundException) Produces(javax.ws.rs.Produces) GET(javax.ws.rs.GET)

Example 5 with ItemNotFoundException

use of com.srotya.sidewinder.core.storage.ItemNotFoundException in project sidewinder by srotya.

the class GrafanaQueryApi method queryFields.

@Path("/query/fields")
@POST
@Produces({ MediaType.APPLICATION_JSON })
@Consumes({ MediaType.APPLICATION_JSON })
public Set<String> queryFields(@PathParam(DatabaseOpsApi.DB_NAME) String dbName, String queryString) {
    try {
        Gson gson = new Gson();
        JsonObject measurement = gson.fromJson(queryString, JsonObject.class);
        if (measurement.has("target")) {
            Set<String> response = engine.getFieldsForMeasurement(dbName, measurement.get("target").getAsString());
            logger.log(Level.FINE, () -> "Query fields for db:" + dbName + "\t" + response + "\t" + queryString);
            return response;
        } else {
            throw new ItemNotFoundException("Bad request");
        }
    } catch (ItemNotFoundException e) {
        throw new NotFoundException(e.getMessage());
    } catch (Exception e) {
        throw new InternalServerErrorException(e.getMessage());
    }
}
Also used : Gson(com.google.gson.Gson) JsonObject(com.google.gson.JsonObject) ItemNotFoundException(com.srotya.sidewinder.core.storage.ItemNotFoundException) NotFoundException(javax.ws.rs.NotFoundException) InternalServerErrorException(javax.ws.rs.InternalServerErrorException) ItemNotFoundException(com.srotya.sidewinder.core.storage.ItemNotFoundException) SQLException(java.sql.SQLException) RejectException(com.srotya.sidewinder.core.storage.RejectException) BadRequestException(javax.ws.rs.BadRequestException) ParseException(java.text.ParseException) IOException(java.io.IOException) InternalServerErrorException(javax.ws.rs.InternalServerErrorException) NotFoundException(javax.ws.rs.NotFoundException) 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)

Aggregations

ItemNotFoundException (com.srotya.sidewinder.core.storage.ItemNotFoundException)15 IOException (java.io.IOException)13 RejectException (com.srotya.sidewinder.core.storage.RejectException)9 Series (com.srotya.sidewinder.core.storage.Series)8 NotFoundException (javax.ws.rs.NotFoundException)8 StorageEngine (com.srotya.sidewinder.core.storage.StorageEngine)7 BadRequestException (javax.ws.rs.BadRequestException)7 Test (org.junit.Test)7 DataPoint (com.srotya.sidewinder.core.storage.DataPoint)6 HashMap (java.util.HashMap)6 LinkedHashMap (java.util.LinkedHashMap)6 InternalServerErrorException (javax.ws.rs.InternalServerErrorException)6 Gson (com.google.gson.Gson)5 TimeSeries (com.srotya.sidewinder.core.storage.TimeSeries)5 File (java.io.File)5 Path (javax.ws.rs.Path)5 Produces (javax.ws.rs.Produces)5 JsonObject (com.google.gson.JsonObject)4 Point (com.srotya.sidewinder.core.rpc.Point)4 Consumes (javax.ws.rs.Consumes)4