Search in sources :

Example 6 with TimeSeries

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

the class TimeSeriesDatabase method read.

/**
 * Read measurements from table.
 *
 * @param tableName
 * @param startTime start time including
 * @param endTime end time including
 * @return
 * @throws IOException
 */
private TimeSeries read(final long startTime, final long endTime, final long startAtFragment, final long endAtFragment, final boolean skipFirst) throws IOException {
    synchronized (path) {
        TLongArrayList timeStamps = new TLongArrayList();
        List<long[]> data = new ArrayList<>();
        if (startAtFragment > 0) {
            FileLock lock = ch.lock(0, Long.MAX_VALUE, true);
            try {
                DataFragment frag;
                long nextFragmentLocation = startAtFragment;
                boolean last = false;
                boolean psFirst = skipFirst;
                do {
                    if (nextFragmentLocation == endAtFragment) {
                        last = true;
                    }
                    file.seek(nextFragmentLocation);
                    frag = new DataFragment(file);
                    if (psFirst) {
                        psFirst = false;
                    } else {
                        long fragStartTime = frag.getStartTimeMillis();
                        if (fragStartTime >= startTime) {
                            TIntArrayList fragTimestamps = frag.getTimestamps();
                            int nr = 0;
                            for (int i = 0; i < fragTimestamps.size(); i++) {
                                long ts = fragStartTime + fragTimestamps.get(i);
                                if (ts <= endTime) {
                                    timeStamps.add(ts);
                                    nr++;
                                } else {
                                    break;
                                }
                            }
                            int i = 0;
                            for (long[] d : frag.getData()) {
                                if (i < nr) {
                                    data.add(d);
                                } else {
                                    break;
                                }
                                nr++;
                            }
                            if (fragTimestamps.size() > nr) {
                                break;
                            }
                        }
                    }
                    nextFragmentLocation = frag.getNextDataFragment();
                } while (nextFragmentLocation > 0 && !last);
            } catch (IOException | RuntimeException e) {
                try {
                    lock.release();
                    throw e;
                } catch (IOException ex) {
                    ex.addSuppressed(e);
                    throw ex;
                }
            }
            lock.release();
        }
        return new TimeSeries(timeStamps.toArray(), data.toArray(new long[data.size()][]));
    }
}
Also used : TimeSeries(org.spf4j.tsdb2.TimeSeries) TLongArrayList(gnu.trove.list.array.TLongArrayList) TIntArrayList(gnu.trove.list.array.TIntArrayList) ArrayList(java.util.ArrayList) TLongArrayList(gnu.trove.list.array.TLongArrayList) IOException(java.io.IOException) TIntArrayList(gnu.trove.list.array.TIntArrayList) FileLock(java.nio.channels.FileLock)

Example 7 with TimeSeries

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

the class TimeSeriesDatabase method writeCsvTable.

public void writeCsvTable(final String tableName, final File output) throws IOException {
    TSTable table = getTSTable(tableName);
    TimeSeries data = readAll(tableName);
    DateTimeFormatter formatter = DateTimeFormats.TS_FORMAT;
    try (Writer writer = new BufferedWriter(new OutputStreamWriter(Files.newOutputStream(output.toPath()), Charsets.UTF_8))) {
        Csv.writeCsvElement("timestamp", writer);
        for (String colName : table.getColumnNames()) {
            writer.append(',');
            Csv.writeCsvElement(colName, writer);
        }
        writer.write('\n');
        long[] timestamps = data.getTimeStamps();
        long[][] values = data.getValues();
        for (int i = 0; i < timestamps.length; i++) {
            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)

Example 8 with TimeSeries

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

the class TimeSeriesDatabase method createJFreeCharts.

public List<JFreeChart> createJFreeCharts(final String tableName, final long startTime, final long endTime) throws IOException {
    TSTable info = this.getTSTable(tableName);
    TimeSeries data = this.read(tableName, startTime, endTime);
    return createJFreeCharts(data, info);
}
Also used : TimeSeries(org.spf4j.tsdb2.TimeSeries)

Example 9 with TimeSeries

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

the class RecorderFactoryTest method assertData.

@SuppressFBWarnings("CLI_CONSTANT_LIST_INDEX")
public static void assertData(final String forWhat, final long expectedValue) throws IOException {
    TSDBWriter dbWriter = ((TSDBMeasurementStore) RecorderFactory.MEASUREMENT_STORE).getDBWriter();
    dbWriter.flush();
    final File file = dbWriter.getFile();
    List<TableDef> tableDefs = TSDBQuery.getTableDef(file, forWhat);
    TableDef tableDef = tableDefs.get(0);
    TimeSeries timeSeries = TSDBQuery.getTimeSeries(file, new long[] { tableDef.id }, 0, Long.MAX_VALUE);
    long sum = 0;
    long[][] values = timeSeries.getValues();
    for (long[] row : values) {
        sum += row[0];
    }
    Assert.assertEquals(expectedValue, sum);
}
Also used : TSDBWriter(org.spf4j.tsdb2.TSDBWriter) TimeSeries(org.spf4j.tsdb2.TimeSeries) File(java.io.File) TableDef(org.spf4j.tsdb2.avro.TableDef) TSDBMeasurementStore(org.spf4j.perf.impl.ms.tsdb.TSDBMeasurementStore) SuppressFBWarnings(edu.umd.cs.findbugs.annotations.SuppressFBWarnings)

Example 10 with TimeSeries

use of org.spf4j.tsdb2.TimeSeries in project java-docs-samples by GoogleCloudPlatform.

the class TimeSeriesSummary method getMostRecentPoint.

Point getMostRecentPoint(TimeSeries timeSeries) {
    Point max = Collections.max(timeSeries.getPointsList(), Comparator.comparingLong(p -> p.getInterval().getEndTime().getSeconds()));
    mostRecentRunTime = max.getInterval().getEndTime();
    return max;
}
Also used : List(java.util.List) Lists(com.google.common.collect.Lists) TimeSeries(com.google.monitoring.v3.TimeSeries) Collections2(com.google.common.collect.Collections2) Point(com.google.monitoring.v3.Point) Comparator(java.util.Comparator) Timestamp(com.google.protobuf.Timestamp) Collections(java.util.Collections) Collectors(java.util.stream.Collectors) Point(com.google.monitoring.v3.Point)

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