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;
}
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");
}
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()][]));
}
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');
}
}
}
}
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]);
}
Aggregations