Search in sources :

Example 1 with TimeSeriesRecord

use of org.spf4j.perf.TimeSeriesRecord in project spf4j by zolyfarkas.

the class TableDefs method toRecord.

public static TimeSeriesRecord toRecord(final Schema rSchema, final Observation row) {
    GenericRecord rec = new GenericData.Record(rSchema);
    rec.put(0, Instant.ofEpochMilli(row.getRelTimeStamp()));
    List<Long> nrs = row.getData();
    List<Schema.Field> fields = rSchema.getFields();
    for (int i = 1, l = fields.size(), j = 0; i < l; i++, j++) {
        Schema.Type type = fields.get(i).schema().getType();
        switch(type) {
            case DOUBLE:
                rec.put(i, Double.longBitsToDouble(nrs.get(j)));
                break;
            case LONG:
                rec.put(i, nrs.get(j));
                break;
            default:
                throw new IllegalStateException("Unsupported data type: " + type);
        }
    }
    return TimeSeriesRecord.from(rec);
}
Also used : Schema(org.apache.avro.Schema) GenericRecord(org.apache.avro.generic.GenericRecord) TimeSeriesRecord(org.spf4j.perf.TimeSeriesRecord) GenericRecord(org.apache.avro.generic.GenericRecord)

Example 2 with TimeSeriesRecord

use of org.spf4j.perf.TimeSeriesRecord in project spf4j by zolyfarkas.

the class MStoreViewJInternalFrame method exportButtonActionPerformed.

// GEN-LAST:event_plotButtonActionPerformed
@SuppressFBWarnings("UP_UNUSED_PARAMETER")
private void exportButtonActionPerformed(java.awt.event.ActionEvent evt) {
    // GEN-FIRST:event_exportButtonActionPerformed
    TreePath[] selectionPaths = measurementTree.getSelectionPaths();
    Set<Schema> selectedTables = getSelectedTables(selectionPaths);
    if (!selectedTables.isEmpty()) {
        JFileChooser chooser = new JFileChooser();
        chooser.setDialogType(JFileChooser.SAVE_DIALOG);
        int returnVal = chooser.showOpenDialog(this);
        if (returnVal == JFileChooser.APPROVE_OPTION) {
            File file = chooser.getSelectedFile();
            for (Schema metric : selectedTables) {
                try (AvroCloseableIterable<TimeSeriesRecord> obs = reader.getMeasurementData(metric, Instant.EPOCH, Instant.now())) {
                    CsvWriter writer = Csv.CSV.writer(Files.newBufferedWriter(file.toPath(), StandardCharsets.UTF_8));
                    CsvEncoder encoder = new CsvEncoder(writer, Schema.createArray(metric));
                    encoder.writeHeader();
                    GenericDatumWriter dw = new GenericDatumWriter(metric);
                    for (TimeSeriesRecord o : obs) {
                        dw.write(o, encoder);
                    }
                    encoder.flush();
                } catch (IOException ex) {
                    throw new UncheckedIOException(ex);
                }
            }
        }
    }
}
Also used : CsvWriter(org.spf4j.io.csv.CsvWriter) Schema(org.apache.avro.Schema) UncheckedIOException(java.io.UncheckedIOException) GenericDatumWriter(org.apache.avro.generic.GenericDatumWriter) IOException(java.io.IOException) UncheckedIOException(java.io.UncheckedIOException) TreePath(javax.swing.tree.TreePath) JFileChooser(javax.swing.JFileChooser) TimeSeriesRecord(org.spf4j.perf.TimeSeriesRecord) File(java.io.File) CsvEncoder(org.spf4j.avro.csv.CsvEncoder) SuppressFBWarnings(edu.umd.cs.findbugs.annotations.SuppressFBWarnings)

Example 3 with TimeSeriesRecord

use of org.spf4j.perf.TimeSeriesRecord in project spf4j by zolyfarkas.

the class TSDBQueryTest method testTsDbQuery.

@Test
public void testTsDbQuery() throws IOException {
    File testFile = File.createTempFile("test", ".tsdb2");
    try (TSDBWriter writer = new TSDBWriter(testFile, 4, "test", false)) {
        long tableId = writer.writeTableDef(tableDef);
        writer.writeDataRow(tableId, System.currentTimeMillis(), 0, 1, 2);
        writer.writeDataRow(tableId, System.currentTimeMillis(), 1, 1, 2);
        writer.writeDataRow(tableId, System.currentTimeMillis(), 2, 1, 2);
        writer.writeDataRow(tableId, System.currentTimeMillis(), 3, 1, 2);
        writer.writeDataRow(tableId, System.currentTimeMillis(), 4, 1, 2);
        writer.flush();
        writer.writeDataRow(tableId, System.currentTimeMillis(), 0, 1, 2);
        writer.writeDataRow(tableId, System.currentTimeMillis(), 1, 1, 2);
        writer.writeDataRow(tableId, System.currentTimeMillis(), 2, 1, 2);
        writer.writeDataRow(tableId, System.currentTimeMillis(), 3, 1, 2);
        writer.writeDataRow(tableId, System.currentTimeMillis(), 4, 1, 2);
    }
    try (CloseableIterable<TimeSeriesRecord> res = TSDBQuery.getTimeSeriesData(testFile, "test", System.currentTimeMillis() - 10000, System.currentTimeMillis())) {
        int i = 0;
        for (TimeSeriesRecord rec : res) {
            LOG.debug("measurement", rec);
            i++;
        }
        Assert.assertEquals(10, i);
    }
}
Also used : TimeSeriesRecord(org.spf4j.perf.TimeSeriesRecord) File(java.io.File) Test(org.junit.Test)

Example 4 with TimeSeriesRecord

use of org.spf4j.perf.TimeSeriesRecord in project spf4j by zolyfarkas.

the class AvroMeasurementStoreTest method getMetrics.

public static List<TimeSeriesRecord> getMetrics(final MeasurementStoreQuery query, final Schema metric, final Instant from, final Instant to, final int aggMillis) throws IOException {
    List<TimeSeriesRecord> results = new ArrayList<>();
    try (AvroCloseableIterable<TimeSeriesRecord> data = query.getAggregatedMeasurementData(metric, from, to, aggMillis, TimeUnit.MILLISECONDS)) {
        for (TimeSeriesRecord rec : data) {
            LOG.debug("agg", rec);
            results.add(rec);
        }
    }
    return results;
}
Also used : TimeSeriesRecord(org.spf4j.perf.TimeSeriesRecord) ArrayList(java.util.ArrayList)

Example 5 with TimeSeriesRecord

use of org.spf4j.perf.TimeSeriesRecord in project spf4j by zolyfarkas.

the class TableDefs method toRecord.

public static TimeSeriesRecord toRecord(final Schema rSchema, final long baseTs, final DataRow row) {
    GenericRecord rec = new GenericData.Record(rSchema);
    long ts = baseTs + row.getRelTimeStamp();
    rec.put(0, Instant.ofEpochMilli(ts));
    List<Long> nrs = row.getData();
    List<Schema.Field> fields = rSchema.getFields();
    for (int i = 1, l = fields.size(), j = 0; i < l; i++, j++) {
        Schema.Type type = fields.get(i).schema().getType();
        switch(type) {
            case DOUBLE:
                rec.put(i, Double.longBitsToDouble(nrs.get(j)));
                break;
            case LONG:
                rec.put(i, nrs.get(j));
                break;
            default:
                throw new IllegalStateException("Unsupported data type: " + type);
        }
    }
    return TimeSeriesRecord.from(rec);
}
Also used : Schema(org.apache.avro.Schema) GenericRecord(org.apache.avro.generic.GenericRecord) TimeSeriesRecord(org.spf4j.perf.TimeSeriesRecord) GenericRecord(org.apache.avro.generic.GenericRecord)

Aggregations

TimeSeriesRecord (org.spf4j.perf.TimeSeriesRecord)7 Schema (org.apache.avro.Schema)4 SuppressFBWarnings (edu.umd.cs.findbugs.annotations.SuppressFBWarnings)2 File (java.io.File)2 ArrayList (java.util.ArrayList)2 GenericRecord (org.apache.avro.generic.GenericRecord)2 Test (org.junit.Test)2 IOException (java.io.IOException)1 UncheckedIOException (java.io.UncheckedIOException)1 JFileChooser (javax.swing.JFileChooser)1 TreePath (javax.swing.tree.TreePath)1 GenericDatumWriter (org.apache.avro.generic.GenericDatumWriter)1 CsvEncoder (org.spf4j.avro.csv.CsvEncoder)1 CsvWriter (org.spf4j.io.csv.CsvWriter)1 MeasurementStoreQuery (org.spf4j.perf.MeasurementStoreQuery)1 MeasurementsInfoImpl (org.spf4j.perf.impl.MeasurementsInfoImpl)1 Aggregation (org.spf4j.tsdb2.avro.Aggregation)1