add time to x-axis labels only if range is less than two weeks
This commit is contained in:
@@ -0,0 +1,28 @@
|
||||
package org.lucares.recommind.logs;
|
||||
|
||||
import java.time.OffsetDateTime;
|
||||
|
||||
class CsvSummary {
|
||||
private final int values;
|
||||
private final OffsetDateTime maxDate;
|
||||
private final OffsetDateTime minDate;
|
||||
|
||||
public CsvSummary(final int values, final OffsetDateTime maxDate, final OffsetDateTime minDate) {
|
||||
super();
|
||||
this.values = values;
|
||||
this.maxDate = maxDate;
|
||||
this.minDate = minDate;
|
||||
}
|
||||
|
||||
public int getValues() {
|
||||
return values;
|
||||
}
|
||||
|
||||
public OffsetDateTime getMaxDate() {
|
||||
return maxDate;
|
||||
}
|
||||
|
||||
public OffsetDateTime getMinDate() {
|
||||
return minDate;
|
||||
}
|
||||
}
|
||||
@@ -10,7 +10,9 @@ import java.nio.file.Files;
|
||||
import java.nio.file.LinkOption;
|
||||
import java.nio.file.Path;
|
||||
import java.nio.file.Paths;
|
||||
import java.time.OffsetDateTime;
|
||||
import java.time.format.DateTimeFormatter;
|
||||
import java.time.temporal.ChronoUnit;
|
||||
import java.util.ArrayList;
|
||||
import java.util.Comparator;
|
||||
import java.util.Iterator;
|
||||
@@ -61,16 +63,22 @@ public class Plotter {
|
||||
|
||||
final Result result = db.get(query, groupBy);
|
||||
|
||||
OffsetDateTime maxDate = OffsetDateTime.MIN;
|
||||
OffsetDateTime minDate = OffsetDateTime.MAX;
|
||||
|
||||
for (final GroupResult groupResult : result.getGroups()) {
|
||||
|
||||
final Stream<Entry> entries = groupResult.asStream();
|
||||
|
||||
final File dataFile = File.createTempFile("data", ".dat", tmpBaseDir.toFile());
|
||||
final int values = toCsv(entries, dataFile);
|
||||
final CsvSummary csvSummary = toCsv(entries, dataFile);
|
||||
|
||||
final String title = title(groupResult.getGroupedBy(), values);
|
||||
final DataSeries dataSerie = new DataSeries(dataFile, title, values);
|
||||
final String title = title(groupResult.getGroupedBy(), csvSummary.getValues());
|
||||
final DataSeries dataSerie = new DataSeries(dataFile, title, csvSummary.getValues());
|
||||
dataSeries.add(dataSerie);
|
||||
|
||||
maxDate = maxDate.compareTo(csvSummary.getMaxDate()) > 0 ? maxDate : csvSummary.getMaxDate();
|
||||
minDate = minDate.compareTo(csvSummary.getMinDate()) < 0 ? minDate : csvSummary.getMinDate();
|
||||
}
|
||||
|
||||
sortAndLimit(dataSeries, plotSettings);
|
||||
@@ -80,6 +88,7 @@ public class Plotter {
|
||||
final GnuplotSettings gnuplotSettings = new GnuplotSettings(outputFile);
|
||||
gnuplotSettings.setHeight(height);
|
||||
gnuplotSettings.setWidth(width);
|
||||
gnuplotSettings.setFormatX(getFormatX(minDate, maxDate));
|
||||
gnuplot.plot(gnuplotSettings, dataSeries);
|
||||
return outputFile;
|
||||
} catch (final InterruptedException e) {
|
||||
@@ -90,6 +99,15 @@ public class Plotter {
|
||||
}
|
||||
}
|
||||
|
||||
private String getFormatX(final OffsetDateTime minDate, final OffsetDateTime maxDate) {
|
||||
|
||||
if (minDate.until(maxDate, ChronoUnit.WEEKS) > 1) {
|
||||
return "%Y-%m-%d";
|
||||
}
|
||||
|
||||
return "%Y-%m-%d %H:%M:%S";
|
||||
}
|
||||
|
||||
private void sortAndLimit(final List<DataSeries> dataSeries, final PlotSettings plotSettings) {
|
||||
|
||||
final Limit limitBy = plotSettings.getLimitBy();
|
||||
@@ -158,10 +176,12 @@ public class Plotter {
|
||||
}
|
||||
}
|
||||
|
||||
private static int toCsv(final Stream<Entry> entries, final File dataFile) throws IOException {
|
||||
private static CsvSummary toCsv(final Stream<Entry> entries, final File dataFile) throws IOException {
|
||||
|
||||
final long start = System.nanoTime();
|
||||
int count = 0;
|
||||
OffsetDateTime maxDate = OffsetDateTime.MIN;
|
||||
OffsetDateTime minDate = OffsetDateTime.MAX;
|
||||
final int separator = ',';
|
||||
final int newline = '\n';
|
||||
try (final Writer output = new OutputStreamWriter(new FileOutputStream(dataFile), StandardCharsets.US_ASCII);) {
|
||||
@@ -171,16 +191,20 @@ public class Plotter {
|
||||
final Entry entry = it.next();
|
||||
|
||||
final String value = String.valueOf(entry.getValue());
|
||||
final String date = entry.getDate().format(DateTimeFormatter.ISO_LOCAL_DATE_TIME);
|
||||
output.write(date);
|
||||
final OffsetDateTime date = entry.getDate();
|
||||
final String formattedDate = date.format(DateTimeFormatter.ISO_LOCAL_DATE_TIME);
|
||||
output.write(formattedDate);
|
||||
output.write(separator);
|
||||
output.write(value);
|
||||
output.write(newline);
|
||||
|
||||
count++;
|
||||
maxDate = maxDate.compareTo(date) > 0 ? maxDate : date;
|
||||
minDate = minDate.compareTo(date) < 0 ? minDate : date;
|
||||
}
|
||||
}
|
||||
|
||||
System.out.println("wrote " + count + " values to csv in: " + (System.nanoTime() - start) / 1_000_000.0 + "ms");
|
||||
return count;
|
||||
return new CsvSummary(count, maxDate, minDate);
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user