Search in sources :

Example 1 with SeriesOutput

use of com.srotya.sidewinder.core.storage.SeriesOutput 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<SeriesOutput> 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) SeriesOutput(com.srotya.sidewinder.core.storage.SeriesOutput) 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) 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 SeriesOutput

use of com.srotya.sidewinder.core.storage.SeriesOutput 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<SeriesOutput> points = engine.queryDataPoints(dbName, measurementName, valueFieldName, startTs, endTs, null);
        List<Number[]> response = new ArrayList<>();
        for (SeriesOutput 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) SeriesOutput(com.srotya.sidewinder.core.storage.SeriesOutput) 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) 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 SeriesOutput

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

the class ReduceFunction method apply.

@Override
public SeriesOutput apply(SeriesOutput dataPoints) {
    SeriesOutput output = new SeriesOutput(dataPoints.getMeasurementName(), dataPoints.getValueFieldName(), dataPoints.getTags());
    output.setFp(dataPoints.isFp());
    DataPoint single = new DataPoint();
    single.setTimestamp(dataPoints.getDataPoints().get(0).getTimestamp());
    aggregateToSingle(dataPoints.getDataPoints(), single, dataPoints.isFp());
    output.setDataPoints(Arrays.asList(single));
    return output;
}
Also used : DataPoint(com.srotya.sidewinder.core.storage.DataPoint) SeriesOutput(com.srotya.sidewinder.core.storage.SeriesOutput)

Example 4 with SeriesOutput

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

the class TestGRPWriterServiceImpl method testPointWritesRejects.

@Test
public void testPointWritesRejects() throws Exception {
    WriterServiceBlockingStub client = WriterServiceGrpc.newBlockingStub(channel);
    long sts = 1497720452566L;
    String dbName = "test3";
    String measurementName = "cpu4";
    List<Point> points = Arrays.asList(Point.newBuilder().setDbName(dbName).addFp(false).setMeasurementName(measurementName).addTags(Tag.newBuilder().setTagKey("host").setTagValue("1").build()).setTimestamp(sts).addValue(1L).addValueFieldName("usage").build(), Point.newBuilder().setDbName(dbName).addFp(false).setMeasurementName(measurementName).addTags(Tag.newBuilder().setTagKey("host").setTagValue("1").build()).setTimestamp(sts + 1).addValue(2L).addValueFieldName("usage").build());
    try {
        Ack response = client.writeBatchDataPoint(BatchData.newBuilder().setMessageId(sts).addAllPoints(points).build());
        if (response.getResponseCode() != 200) {
            fail("Must return 200");
        }
    } catch (Exception e) {
    }
    assertTrue(engine.checkIfExists(dbName));
    assertTrue(engine.checkIfExists(dbName, measurementName));
    assertEquals("host", engine.getTagKeysForMeasurement(dbName, measurementName).iterator().next());
    List<SeriesOutput> result = engine.queryDataPoints(dbName, measurementName, "usage", sts, sts + 1, null);
    assertEquals(1, result.size());
    assertEquals(2, result.iterator().next().getDataPoints().size());
    assertEquals(1L, result.iterator().next().getDataPoints().iterator().next().getLongValue());
}
Also used : WriterServiceBlockingStub(com.srotya.sidewinder.core.rpc.WriterServiceGrpc.WriterServiceBlockingStub) SeriesOutput(com.srotya.sidewinder.core.storage.SeriesOutput) Test(org.junit.Test)

Example 5 with SeriesOutput

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

the class TestGRPWriterServiceImpl method testMultiDataPointWrites.

// @Test
public void testMultiDataPointWrites() throws Exception {
    WriterServiceBlockingStub client = WriterServiceGrpc.newBlockingStub(channel);
    long sts = 1497720452566L;
    String dbName = "test2";
    String measurementName = "cpu";
    List<Point> points = Arrays.asList(Point.newBuilder().setDbName(dbName).addFp(false).setMeasurementName(measurementName).addTags(Tag.newBuilder().setTagKey("host").setTagValue("1").build()).setTimestamp(sts).addValue(1L).addValueFieldName("usage").build(), Point.newBuilder().setDbName(dbName).addFp(false).setMeasurementName(measurementName).addTags(Tag.newBuilder().setTagKey("host").setTagValue("1").build()).setTimestamp(sts + 1).addValue(2L).addValueFieldName("usage").build());
    client.writeBatchDataPoint(BatchData.newBuilder().setMessageId(sts).addAllPoints(points).build());
    assertTrue(engine.checkIfExists(dbName));
    assertTrue(engine.checkIfExists(dbName, measurementName));
    assertEquals("host=1", engine.getTagKeysForMeasurement(dbName, measurementName).iterator().next());
    List<SeriesOutput> result = engine.queryDataPoints(dbName, measurementName, "usage", sts, sts + 1, null);
    assertEquals(1, result.size());
    assertEquals(2, result.iterator().next().getDataPoints().size());
    assertEquals(1L, result.iterator().next().getDataPoints().iterator().next().getLongValue());
}
Also used : WriterServiceBlockingStub(com.srotya.sidewinder.core.rpc.WriterServiceGrpc.WriterServiceBlockingStub) SeriesOutput(com.srotya.sidewinder.core.storage.SeriesOutput)

Aggregations

SeriesOutput (com.srotya.sidewinder.core.storage.SeriesOutput)40 DataPoint (com.srotya.sidewinder.core.storage.DataPoint)35 Test (org.junit.Test)32 ArrayList (java.util.ArrayList)31 WindowedAggregator (com.srotya.sidewinder.core.functions.list.WindowedAggregator)10 Function (com.srotya.sidewinder.core.functions.list.Function)7 IOException (java.io.IOException)6 TagFilter (com.srotya.sidewinder.core.filters.TagFilter)4 ItemNotFoundException (com.srotya.sidewinder.core.storage.ItemNotFoundException)4 Tag (com.srotya.sidewinder.core.rpc.Tag)3 WriterServiceBlockingStub (com.srotya.sidewinder.core.rpc.WriterServiceGrpc.WriterServiceBlockingStub)3 BadRequestException (javax.ws.rs.BadRequestException)3 NotFoundException (javax.ws.rs.NotFoundException)3 WindowedMean (com.srotya.sidewinder.core.functions.list.BasicWindowedFunctions.WindowedMean)2 ChainFunction (com.srotya.sidewinder.core.functions.list.ChainFunction)2 ReduceFunction (com.srotya.sidewinder.core.functions.list.ReduceFunction)2 Point (com.srotya.sidewinder.core.rpc.Point)2 SimpleDateFormat (java.text.SimpleDateFormat)2 HashMap (java.util.HashMap)2 List (java.util.List)2