Search in sources :

Example 1 with TimeSeries

use of org.spf4j.tsdb2.TimeSeries in project google-cloud-java by GoogleCloudPlatform.

the class MetricServiceClientTest method createTimeSeriesTest.

@Test
@SuppressWarnings("all")
public void createTimeSeriesTest() {
    Empty expectedResponse = Empty.newBuilder().build();
    mockMetricService.addResponse(expectedResponse);
    ProjectName name = ProjectName.create("[PROJECT]");
    List<TimeSeries> timeSeries = new ArrayList<>();
    client.createTimeSeries(name, timeSeries);
    List<GeneratedMessageV3> actualRequests = mockMetricService.getRequests();
    Assert.assertEquals(1, actualRequests.size());
    CreateTimeSeriesRequest actualRequest = (CreateTimeSeriesRequest) actualRequests.get(0);
    Assert.assertEquals(name, actualRequest.getNameAsProjectName());
    Assert.assertEquals(timeSeries, actualRequest.getTimeSeriesList());
}
Also used : Empty(com.google.protobuf.Empty) TimeSeries(com.google.monitoring.v3.TimeSeries) ProjectName(com.google.monitoring.v3.ProjectName) CreateTimeSeriesRequest(com.google.monitoring.v3.CreateTimeSeriesRequest) ArrayList(java.util.ArrayList) GeneratedMessageV3(com.google.protobuf.GeneratedMessageV3) Test(org.junit.Test)

Example 2 with TimeSeries

use of org.spf4j.tsdb2.TimeSeries in project spf4j by zolyfarkas.

the class TSDBQuery method writeAsCsv.

public static void writeAsCsv(final Appendable writer, final File tsDB, final String tableName) throws IOException {
    List<TableDef> tableDefs = getTableDef(tsDB, tableName);
    TimeSeries data = getTimeSeries(tsDB, getIds(tableDefs), 0, Long.MAX_VALUE);
    Csv.writeCsvElement("timestamp", writer);
    for (ColumnDef col : tableDefs.get(0).getColumns()) {
        writer.append(',');
        Csv.writeCsvElement(col.getName(), writer);
    }
    writer.append('\n');
    long[] timestamps = data.getTimeStamps();
    long[][] values = data.getValues();
    for (int i = 0; i < timestamps.length; i++) {
        Csv.writeCsvElement(DateTimeFormats.TS_FORMAT.format(Instant.ofEpochMilli(timestamps[i])), writer);
        for (long val : values[i]) {
            writer.append(',');
            Csv.writeCsvElement(Long.toString(val), writer);
        }
        writer.append('\n');
    }
}
Also used : ColumnDef(org.spf4j.tsdb2.avro.ColumnDef) TableDef(org.spf4j.tsdb2.avro.TableDef)

Example 3 with TimeSeries

use of org.spf4j.tsdb2.TimeSeries in project spf4j by zolyfarkas.

the class TSDBTailerTest method main.

public static void main(final String[] parameters) throws IOException {
    final MutableHolder<Integer> counter = new MutableHolder<>(0);
    TimeSeriesDatabase tsdb = new TimeSeriesDatabase(new File(parameters[0]));
    tsdb.tail(10, 0, new TSDataHandler() {

        private int count = 0;

        @Override
        public void newTable(final String tableName, final String[] columnNames) {
            LOG.info("New Table: {} columns: {}", tableName, columnNames);
        }

        @Override
        public void newData(final String tableName, final TimeSeries data) {
            LOG.debug("Table {} - {}", tableName, data);
            counter.setValue(counter.getValue() + data.getTimeStamps().length);
        }

        @Override
        public boolean finish() {
            return count++ > 400;
        }
    });
    System.exit(counter.getValue());
}
Also used : MutableHolder(org.spf4j.base.MutableHolder) TimeSeries(org.spf4j.tsdb2.TimeSeries) File(java.io.File)

Example 4 with TimeSeries

use of org.spf4j.tsdb2.TimeSeries in project spf4j by zolyfarkas.

the class TimeSeriesDatabaseTest method testWriteTSDB.

/**
 * Test of close method, of class TimeSeriesDatabase.
 */
@Test
public void testWriteTSDB() throws Exception {
    if (new File(FILE_NAME).delete()) {
        LOG.debug("existing tsdb {} file deleted", FILE_NAME);
    }
    try (TimeSeriesDatabase instance = new TimeSeriesDatabase(new File(FILE_NAME), new byte[] {})) {
        instance.addTSTable("gr1", new byte[] {}, 5, new String[] { "a", "b" }, new byte[][] {});
        instance.write(System.currentTimeMillis(), "gr1", new long[] { 0, 1 });
        Thread.sleep(5);
        instance.write(System.currentTimeMillis(), "gr1", new long[] { 1, 2 });
        Thread.sleep(5);
        instance.write(System.currentTimeMillis(), "gr1", new long[] { 3, 4 });
        Thread.sleep(5);
        instance.addTSTable("gr2", new byte[] {}, 5, new String[] { "a", "b" }, new byte[][] {});
        instance.write(System.currentTimeMillis(), "gr2", new long[] { 7, 8 });
        instance.flush();
        instance.addTSTable("gr3", new byte[] {}, 5, new String[] { "a", "b" }, new byte[][] {});
        instance.write(System.currentTimeMillis(), "gr3", new long[] { 7, 8 });
        Thread.sleep(5);
        instance.write(System.currentTimeMillis(), "gr3", new long[] { 9, 10 });
        instance.flush();
        LOG.debug("TSTables = {}", instance.getTSTables());
        TimeSeries readAll = instance.readAll("gr1");
        Assert.assertEquals(2, readAll.getValues()[1][1]);
        LOG.debug("TimeSeries = {}", readAll);
        readAll = instance.readAll("gr2");
        Assert.assertEquals(7, readAll.getValues()[0][0]);
        LOG.debug("TimeSeries = {}", readAll);
        readAll = instance.readAll("gr3");
        Assert.assertEquals(10, readAll.getValues()[1][1]);
        LOG.debug("TimeSeries = {}", readAll);
    }
    TimeSeriesDatabase instanceRead = new TimeSeriesDatabase(new File(FILE_NAME), null);
    Collection<TSTable> tsTables = instanceRead.getTSTables();
    LOG.debug("Tables = {}", tsTables);
    Assert.assertEquals(3, tsTables.size());
    instanceRead.writeCsvTable("gr1", File.createTempFile("test", ".csv"));
    instanceRead.writeCsvTables(Arrays.asList("gr1", "gr2", "gr3"), File.createTempFile("testAll", ".csv"));
}
Also used : TimeSeries(org.spf4j.tsdb2.TimeSeries) File(java.io.File) Test(org.junit.Test)

Example 5 with TimeSeries

use of org.spf4j.tsdb2.TimeSeries in project spf4j by zolyfarkas.

the class TimeSeriesDatabase method writeCsvTables.

public void writeCsvTables(final List<String> tableNames, final File output) throws IOException {
    DateTimeFormatter formatter = DateTimeFormats.TS_FORMAT;
    try (Writer writer = new BufferedWriter(new OutputStreamWriter(Files.newOutputStream(output.toPath()), Charsets.UTF_8))) {
        String firstTable = tableNames.get(0);
        TSTable table = getTSTable(firstTable);
        Csv.writeCsvElement("table", writer);
        writer.append(',');
        Csv.writeCsvElement("timestamp", writer);
        for (String colName : table.getColumnNames()) {
            writer.append(',');
            Csv.writeCsvElement(colName, writer);
        }
        writer.write('\n');
        for (String tableName : tableNames) {
            TimeSeries data = readAll(tableName);
            long[] timestamps = data.getTimeStamps();
            long[][] values = data.getValues();
            for (int i = 0; i < timestamps.length; i++) {
                Csv.writeCsvElement(tableName, writer);
                writer.append(',');
                Csv.writeCsvElement(formatter.format(Instant.ofEpochMilli(timestamps[i])), writer);
                for (long val : values[i]) {
                    writer.append(',');
                    Csv.writeCsvElement(Long.toString(val), writer);
                }
                writer.write('\n');
            }
        }
    }
}
Also used : TimeSeries(org.spf4j.tsdb2.TimeSeries) OutputStreamWriter(java.io.OutputStreamWriter) DateTimeFormatter(java.time.format.DateTimeFormatter) OutputStreamWriter(java.io.OutputStreamWriter) BufferedWriter(java.io.BufferedWriter) Writer(java.io.Writer) BufferedWriter(java.io.BufferedWriter)

Aggregations

TimeSeries (com.google.monitoring.v3.TimeSeries)17 TimeSeries (org.spf4j.tsdb2.TimeSeries)12 ArrayList (java.util.ArrayList)10 ProjectName (com.google.monitoring.v3.ProjectName)9 Test (org.junit.Test)9 TimeInterval (com.google.monitoring.v3.TimeInterval)7 MetricServiceClient (com.google.cloud.monitoring.v3.MetricServiceClient)6 ListTimeSeriesRequest (com.google.monitoring.v3.ListTimeSeriesRequest)6 ListTimeSeriesPagedResponse (com.google.cloud.monitoring.v3.MetricServiceClient.ListTimeSeriesPagedResponse)5 CreateTimeSeriesRequest (com.google.monitoring.v3.CreateTimeSeriesRequest)5 HashMap (java.util.HashMap)5 TableDef (org.spf4j.tsdb2.avro.TableDef)5 ColumnDef (org.spf4j.tsdb2.avro.ColumnDef)4 MonitoredResource (com.google.api.MonitoredResource)3 Point (com.google.monitoring.v3.Point)3 SuppressFBWarnings (edu.umd.cs.findbugs.annotations.SuppressFBWarnings)3 TLongArrayList (gnu.trove.list.array.TLongArrayList)3 File (java.io.File)3 Map (java.util.Map)3 Metric (com.google.api.Metric)2