Search in sources :

Example 1 with GrafanaOutputv2

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

the class GrafanaStreamingOutput method serializeOutput.

private boolean serializeOutput(OutputStream os, Iterator<GrafanaOutputv2> itr) throws IOException {
    GrafanaOutputv2 gOutput = itr.next();
    if (gOutput.getPointsIterator() != null) {
        DataPointIterator pointsIterator = gOutput.getPointsIterator();
        if (pointsIterator.hasNext()) {
            os.write(("{\"target\":\"" + gOutput.getTarget() + "\",\"datapoints\":[").getBytes());
            serializePoint(os, pointsIterator, gOutput);
            while (pointsIterator.hasNext()) {
                os.write(',');
                serializePoint(os, pointsIterator, gOutput);
            }
            os.write(']');
            os.write('}');
            return true;
        } else {
            return false;
        }
    } else {
        return false;
    }
}
Also used : DataPointIterator(com.srotya.sidewinder.core.storage.DataPointIterator) GrafanaOutputv2(com.srotya.sidewinder.core.api.grafana.GrafanaOutputv2)

Example 2 with GrafanaOutputv2

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

the class TestGrafanaStreamingOutput method testJsonValidity.

@Test
public void testJsonValidity() throws WebApplicationException, IOException {
    GrafanaOutputv2 out1 = new GrafanaOutputv2("targetname1", false);
    out1.setPointsIterator(new MockDataPointIterator(Arrays.asList(new DataPoint(1L, 2L), new DataPoint(3L, 4L))));
    GrafanaStreamingOutput output = new GrafanaStreamingOutput(Arrays.asList(Arrays.asList(out1).iterator()));
    ByteArrayOutputStream os = new ByteArrayOutputStream();
    output.write(os);
    String payload = new String(os.toByteArray());
    JsonArray ary = new Gson().fromJson(payload, JsonArray.class);
    assertEquals(1, ary.size());
    assertEquals(2, ary.get(0).getAsJsonObject().get("datapoints").getAsJsonArray().size());
    out1.setPointsIterator(new MockDataPointIterator(Arrays.asList(new DataPoint(1L, 2L), new DataPoint(3L, 4L))));
    GrafanaOutputv2 out2 = new GrafanaOutputv2("targetname2", false);
    out2.setPointsIterator(new MockDataPointIterator(Arrays.asList(new DataPoint(1L, 2L), new DataPoint(3L, 4L))));
    GrafanaOutputv2 out3 = new GrafanaOutputv2("targetname2", false);
    out3.setPointsIterator(new MockDataPointIterator(Arrays.asList(new DataPoint(1L, 2L))));
    GrafanaOutputv2 out4 = new GrafanaOutputv2("targetname2", false);
    out4.setPointsIterator(new MockDataPointIterator(Arrays.asList(new DataPoint(1L, 2L), new DataPoint(3L, 4L))));
    output = new GrafanaStreamingOutput(Arrays.asList(Arrays.asList(out1, out2).iterator(), Arrays.asList(out3, out4).iterator()));
    os = new ByteArrayOutputStream();
    output.write(os);
    payload = new String(os.toByteArray());
    ary = new Gson().fromJson(payload, JsonArray.class);
    assertEquals(4, ary.size());
    assertEquals(2, ary.get(0).getAsJsonObject().get("datapoints").getAsJsonArray().size());
    assertEquals(2, ary.get(1).getAsJsonObject().get("datapoints").getAsJsonArray().size());
    assertEquals(1, ary.get(2).getAsJsonObject().get("datapoints").getAsJsonArray().size());
}
Also used : JsonArray(com.google.gson.JsonArray) DataPoint(com.srotya.sidewinder.core.storage.DataPoint) GrafanaOutputv2(com.srotya.sidewinder.core.api.grafana.GrafanaOutputv2) MockDataPointIterator(com.srotya.sidewinder.core.storage.MockDataPointIterator) Gson(com.google.gson.Gson) ByteArrayOutputStream(java.io.ByteArrayOutputStream) Test(org.junit.Test)

Aggregations

GrafanaOutputv2 (com.srotya.sidewinder.core.api.grafana.GrafanaOutputv2)2 Gson (com.google.gson.Gson)1 JsonArray (com.google.gson.JsonArray)1 DataPoint (com.srotya.sidewinder.core.storage.DataPoint)1 DataPointIterator (com.srotya.sidewinder.core.storage.DataPointIterator)1 MockDataPointIterator (com.srotya.sidewinder.core.storage.MockDataPointIterator)1 ByteArrayOutputStream (java.io.ByteArrayOutputStream)1 Test (org.junit.Test)1