Search in sources :

Example 11 with TableDef

use of org.spf4j.tsdb2.avro.TableDef 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 12 with TableDef

use of org.spf4j.tsdb2.avro.TableDef 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 13 with TableDef

use of org.spf4j.tsdb2.avro.TableDef in project spf4j by zolyfarkas.

the class TSDBQuery method getTables.

@SuppressFBWarnings("RV_RETURN_VALUE_IGNORED")
public static ListMultimap<String, TableDef> getTables(final File tsdbFile, final Set<String> tables) throws IOException {
    ListMultimap<String, TableDef> result = ArrayListMultimap.create();
    try (TSDBReader reader = new TSDBReader(tsdbFile, 8192)) {
        Either<TableDef, DataBlock> read;
        while ((read = reader.read()) != null) {
            if (read.isLeft()) {
                final TableDef tdef = read.getLeft();
                final String name = tdef.getName();
                if (tables.contains(name)) {
                    result.put(name, tdef);
                }
            }
        }
    }
    return result;
}
Also used : TableDef(org.spf4j.tsdb2.avro.TableDef) DataBlock(org.spf4j.tsdb2.avro.DataBlock) SuppressFBWarnings(edu.umd.cs.findbugs.annotations.SuppressFBWarnings)

Example 14 with TableDef

use of org.spf4j.tsdb2.avro.TableDef in project spf4j by zolyfarkas.

the class TSDBQuery method getColumnUnitsOfMeasurement.

public static String[] getColumnUnitsOfMeasurement(final TableDef td) {
    List<ColumnDef> columns = td.getColumns();
    String[] result = new String[columns.size()];
    int i = 0;
    for (ColumnDef cd : columns) {
        result[i++] = cd.getUnitOfMeasurement();
    }
    return result;
}
Also used : ColumnDef(org.spf4j.tsdb2.avro.ColumnDef)

Example 15 with TableDef

use of org.spf4j.tsdb2.avro.TableDef in project spf4j by zolyfarkas.

the class TSDBReader method read.

@Nullable
public synchronized Either<TableDef, DataBlock> read() throws IOException {
    final long position = bis.getCount();
    if (position >= size) {
        return null;
    }
    Object result;
    try {
        result = recordReader.read(null, decoder);
    } catch (IOException | RuntimeException ex) {
        if (CORUPTION_LENIENT) {
            return null;
        } else {
            throw new IOException("Error reading tsdb file at " + position + ", this= " + this, ex);
        }
    }
    if (result instanceof TableDef) {
        final TableDef td = (TableDef) result;
        if (position != td.id) {
            throw new IOException("Table Id should be equal with file position " + position + ", " + td.id);
        }
        return Either.left(td);
    } else {
        return Either.right((DataBlock) result);
    }
}
Also used : IOException(java.io.IOException) TableDef(org.spf4j.tsdb2.avro.TableDef) Nullable(javax.annotation.Nullable)

Aggregations

TableDef (org.spf4j.tsdb2.avro.TableDef)14 ColumnDef (org.spf4j.tsdb2.avro.ColumnDef)7 DataBlock (org.spf4j.tsdb2.avro.DataBlock)6 SuppressFBWarnings (edu.umd.cs.findbugs.annotations.SuppressFBWarnings)5 File (java.io.File)4 ArrayList (java.util.ArrayList)3 Test (org.junit.Test)3 TLongObjectHashMap (gnu.trove.map.hash.TLongObjectHashMap)2 IOException (java.io.IOException)2 Map (java.util.Map)2 JFreeChart (org.jfree.chart.JFreeChart)2 TSDBMeasurementStore (org.spf4j.perf.impl.ms.tsdb.TSDBMeasurementStore)2 TSDBWriter (org.spf4j.tsdb2.TSDBWriter)2 DataRow (org.spf4j.tsdb2.avro.DataRow)2 TLongList (gnu.trove.list.TLongList)1 TLongArrayList (gnu.trove.list.array.TLongArrayList)1 TLongObjectMap (gnu.trove.map.TLongObjectMap)1 Dimension (java.awt.Dimension)1 BufferedWriter (java.io.BufferedWriter)1 OutputStreamWriter (java.io.OutputStreamWriter)1