Search in sources :

Example 26 with TimeSeries

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

the class Charts method createJFreeCharts.

public static List<JFreeChart> createJFreeCharts(final TimeSeries data, final TableDef info) {
    long[][] vals = data.getValues();
    Map<String, Pair<List<String>, List<double[]>>> measurementsByUom = new HashMap<>();
    // String[] columnMetaData = TSDBQuery.getColumnUnitsOfMeasurement(info);
    int i = 0;
    for (ColumnDef colDef : info.getColumns()) {
        String uom = colDef.getUnitOfMeasurement();
        Pair<List<String>, List<double[]>> meas = measurementsByUom.get(uom);
        if (meas == null) {
            meas = Pair.of((List<String>) new ArrayList<String>(), (List<double[]>) new ArrayList<double[]>());
            measurementsByUom.put(uom, meas);
        }
        meas.getFirst().add(colDef.getName());
        meas.getSecond().add(Arrays.getColumnAsDoubles(vals, i));
        i++;
    }
    long[] timestamps = data.getTimeStamps();
    List<JFreeChart> result = new ArrayList<>(measurementsByUom.size());
    for (Map.Entry<String, Pair<List<String>, List<double[]>>> entry : measurementsByUom.entrySet()) {
        Pair<List<String>, List<double[]>> p = entry.getValue();
        final List<String> measurementNames = p.getFirst();
        final List<double[]> measurements = p.getSecond();
        result.add(org.spf4j.perf.impl.chart.Charts.createTimeSeriesJFreeChart("chart for " + info.getName() + ", sampleTime " + info.getSampleTime() + " ms, generated by spf4j", timestamps, measurementNames.toArray(new String[measurementNames.size()]), entry.getKey(), measurements.toArray(new double[measurements.size()][])));
    }
    return result;
}
Also used : HashMap(java.util.HashMap) ArrayList(java.util.ArrayList) ColumnDef(org.spf4j.tsdb2.avro.ColumnDef) JFreeChart(org.jfree.chart.JFreeChart) ArrayList(java.util.ArrayList) List(java.util.List) HashMap(java.util.HashMap) Map(java.util.Map) Pair(org.spf4j.base.Pair)

Example 27 with TimeSeries

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

the class Charts method createHeatJFreeChart.

public static JFreeChart createHeatJFreeChart(final TimeSeries data, final TableDef info) {
    Pair<long[], double[][]> mData = fillGaps(data.getTimeStamps(), data.getValues(), info.getSampleTime(), info.getColumns().size());
    ColumnDef columnDef = TSDBQuery.getColumnDef(info, "total");
    return org.spf4j.perf.impl.chart.Charts.createHeatJFreeChart(TSDBQuery.getColumnNames(info), mData.getSecond(), data.getTimeStamps()[0], info.getSampleTime(), columnDef.getUnitOfMeasurement(), "Measurements distribution for " + info.getName() + ", sampleTime " + info.getSampleTime() + "ms, generated by spf4j");
}
Also used : ColumnDef(org.spf4j.tsdb2.avro.ColumnDef)

Example 28 with TimeSeries

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

the class TSDBQuery method getTimeSeries.

public static TimeSeries getTimeSeries(final File tsdbFile, final long[] tableIds, final long startTimeMillis, final long endTimeMillis) throws IOException {
    TLongList timestamps = new TLongArrayList();
    List<long[]> metrics = new ArrayList<>();
    try (TSDBReader reader = new TSDBReader(tsdbFile, 8192)) {
        Either<TableDef, DataBlock> read;
        while ((read = reader.read()) != null) {
            if (read.isRight()) {
                DataBlock data = read.getRight();
                long baseTs = data.baseTimestamp;
                for (DataRow row : data.getValues()) {
                    for (long tableId : tableIds) {
                        if (tableId == row.tableDefId) {
                            final long ts = baseTs + row.relTimeStamp;
                            if (ts >= startTimeMillis && ts <= endTimeMillis) {
                                timestamps.add(ts);
                                metrics.add(Longs.toArray(row.data));
                            }
                        }
                    }
                }
            }
        }
    }
    return new TimeSeries(timestamps.toArray(), metrics.toArray(new long[metrics.size()][]));
}
Also used : TLongArrayList(gnu.trove.list.array.TLongArrayList) TLongList(gnu.trove.list.TLongList) ArrayList(java.util.ArrayList) TLongArrayList(gnu.trove.list.array.TLongArrayList) TableDef(org.spf4j.tsdb2.avro.TableDef) DataRow(org.spf4j.tsdb2.avro.DataRow) DataBlock(org.spf4j.tsdb2.avro.DataBlock)

Example 29 with TimeSeries

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

the class TSDBQuery method writeCsvTables.

public static void writeCsvTables(final File tsDB, final Set<String> tableNames, final File output) throws IOException {
    if (tableNames.isEmpty()) {
        return;
    }
    ListMultimap<String, TableDef> tables = getTables(tsDB, tableNames);
    try (Writer writer = new BufferedWriter(new OutputStreamWriter(Files.newOutputStream(output.toPath()), Charsets.UTF_8))) {
        TableDef table = tables.values().iterator().next();
        Csv.writeCsvElement("table", writer);
        writer.append(',');
        Csv.writeCsvElement("timestamp", writer);
        for (ColumnDef col : table.getColumns()) {
            writer.append(',');
            Csv.writeCsvElement(col.getName(), writer);
        }
        writer.write('\n');
        for (Map.Entry<String, Collection<TableDef>> tEntry : tables.asMap().entrySet()) {
            TimeSeries data = getTimeSeries(tsDB, getIds(tEntry.getValue()), 0, Long.MAX_VALUE);
            long[] timestamps = data.getTimeStamps();
            long[][] values = data.getValues();
            for (int i = 0; i < timestamps.length; i++) {
                Csv.writeCsvElement(tEntry.getKey(), writer);
                writer.append(',');
                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.write('\n');
            }
        }
    }
}
Also used : ColumnDef(org.spf4j.tsdb2.avro.ColumnDef) TableDef(org.spf4j.tsdb2.avro.TableDef) BufferedWriter(java.io.BufferedWriter) Collection(java.util.Collection) OutputStreamWriter(java.io.OutputStreamWriter) Map(java.util.Map) TLongObjectHashMap(gnu.trove.map.hash.TLongObjectHashMap) TLongObjectMap(gnu.trove.map.TLongObjectMap) OutputStreamWriter(java.io.OutputStreamWriter) BufferedWriter(java.io.BufferedWriter) Writer(java.io.Writer)

Example 30 with TimeSeries

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

the class TSDBReaderTest method testTsdb.

@Test
@SuppressFBWarnings({ "RCN_REDUNDANT_NULLCHECK_OF_NONNULL_VALUE", "RCN_REDUNDANT_NULLCHECK_OF_NULL_VALUE", "NP_LOAD_OF_KNOWN_NULL_VALUE", "CLI_CONSTANT_LIST_INDEX" })
// try with resources trips up findbugs sometimes.
@SuppressWarnings("checkstyle:EmptyBlock")
public void testTsdb() throws IOException {
    File testFile = File.createTempFile("test", ".tsdb2");
    long tableId;
    try (TSDBWriter writer = new TSDBWriter(testFile, 4, "test", false)) {
    }
    try (TSDBWriter writer = new TSDBWriter(testFile, 4, "test", false)) {
        tableId = writer.writeTableDef(tableDef);
        final long time = System.currentTimeMillis();
        writer.writeDataRow(tableId, time, 0, 1, 2);
        writer.writeDataRow(tableId, time + 10, 1, 1, 2);
        writer.writeDataRow(tableId, time + 20, 2, 1, 2);
        writer.writeDataRow(tableId, time + 30, 3, 1, 2);
        writer.writeDataRow(tableId, time + 40, 4, 1, 2);
    }
    try (TSDBReader reader = new TSDBReader(testFile, 1024)) {
        Either<TableDef, DataBlock> read;
        while ((read = reader.read()) != null) {
            LOG.debug("TSDB block: {}", read);
        }
    }
    ListMultimap<String, TableDef> allTables = TSDBQuery.getAllTables(testFile);
    Assert.assertEquals(1, allTables.size());
    Assert.assertTrue(allTables.containsKey(tableDef.name));
    TimeSeries timeSeries = TSDBQuery.getTimeSeries(testFile, new long[] { tableId }, 0, Long.MAX_VALUE);
    Assert.assertEquals(2L, timeSeries.getValues()[2][0]);
}
Also used : File(java.io.File) TableDef(org.spf4j.tsdb2.avro.TableDef) DataBlock(org.spf4j.tsdb2.avro.DataBlock) Test(org.junit.Test) SuppressFBWarnings(edu.umd.cs.findbugs.annotations.SuppressFBWarnings)

Aggregations

TimeSeries (com.google.monitoring.v3.TimeSeries)12 TimeSeries (org.spf4j.tsdb2.TimeSeries)12 ProjectName (com.google.monitoring.v3.ProjectName)9 ArrayList (java.util.ArrayList)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 HashMap (java.util.HashMap)5 Test (org.junit.Test)5 TableDef (org.spf4j.tsdb2.avro.TableDef)5 CreateTimeSeriesRequest (com.google.monitoring.v3.CreateTimeSeriesRequest)4 ColumnDef (org.spf4j.tsdb2.avro.ColumnDef)4 Point (com.google.monitoring.v3.Point)3 SuppressFBWarnings (edu.umd.cs.findbugs.annotations.SuppressFBWarnings)3 TLongArrayList (gnu.trove.list.array.TLongArrayList)3 BufferedWriter (java.io.BufferedWriter)3 File (java.io.File)3 Map (java.util.Map)3 Metric (com.google.api.Metric)2