add date range filter
This commit is contained in:
@@ -17,6 +17,7 @@ import java.util.ArrayList;
|
||||
import java.util.Comparator;
|
||||
import java.util.Iterator;
|
||||
import java.util.List;
|
||||
import java.util.UUID;
|
||||
import java.util.stream.Stream;
|
||||
|
||||
import org.lucares.pdb.api.Entry;
|
||||
@@ -53,13 +54,19 @@ public class Plotter {
|
||||
}
|
||||
|
||||
public File plot(final PlotSettings plotSettings) throws InternalPlottingException {
|
||||
|
||||
final String tmpSubDir = uniqueDirectoryName();
|
||||
final Path tmpDir = tmpBaseDir.resolve(tmpSubDir);
|
||||
try {
|
||||
Files.createDirectories(tmpDir);
|
||||
final List<DataSeries> dataSeries = new ArrayList<>();
|
||||
|
||||
final String query = plotSettings.getQuery();
|
||||
final String groupBy = plotSettings.getGroupBy();
|
||||
final int height = plotSettings.getHeight();
|
||||
final int width = plotSettings.getWidth();
|
||||
final OffsetDateTime dateFrom = plotSettings.dateFrom();
|
||||
final OffsetDateTime dateTo = plotSettings.dateTo();
|
||||
|
||||
final Result result = db.get(query, groupBy);
|
||||
|
||||
@@ -70,15 +77,17 @@ public class Plotter {
|
||||
|
||||
final Stream<Entry> entries = groupResult.asStream();
|
||||
|
||||
final File dataFile = File.createTempFile("data", ".dat", tmpBaseDir.toFile());
|
||||
final CsvSummary csvSummary = toCsv(entries, dataFile);
|
||||
final File dataFile = File.createTempFile("data", ".dat", tmpDir.toFile());
|
||||
final CsvSummary csvSummary = toCsv(entries, dataFile, dateFrom, dateTo);
|
||||
|
||||
final String title = title(groupResult.getGroupedBy(), csvSummary.getValues());
|
||||
final DataSeries dataSerie = new DataSeries(dataFile, title, csvSummary.getValues());
|
||||
dataSeries.add(dataSerie);
|
||||
if (dataSerie.getValues() > 0) {
|
||||
dataSeries.add(dataSerie);
|
||||
|
||||
maxDate = maxDate.compareTo(csvSummary.getMaxDate()) > 0 ? maxDate : csvSummary.getMaxDate();
|
||||
minDate = minDate.compareTo(csvSummary.getMinDate()) < 0 ? minDate : csvSummary.getMinDate();
|
||||
maxDate = maxDate.compareTo(csvSummary.getMaxDate()) > 0 ? maxDate : csvSummary.getMaxDate();
|
||||
minDate = minDate.compareTo(csvSummary.getMinDate()) < 0 ? minDate : csvSummary.getMinDate();
|
||||
}
|
||||
}
|
||||
|
||||
sortAndLimit(dataSeries, plotSettings);
|
||||
@@ -96,9 +105,16 @@ public class Plotter {
|
||||
throw new IllegalStateException("Plotting was interrupted.");
|
||||
} catch (final IOException e) {
|
||||
throw new InternalPlottingException("Plotting failed.", e);
|
||||
} finally {
|
||||
FileUtils.delete(tmpDir);
|
||||
}
|
||||
}
|
||||
|
||||
private String uniqueDirectoryName() {
|
||||
return OffsetDateTime.now().format(DateTimeFormatter.ofPattern("yyyy-MM-dd_HH_mm_ss")) + "_"
|
||||
+ UUID.randomUUID().toString();
|
||||
}
|
||||
|
||||
private String getFormatX(final OffsetDateTime minDate, final OffsetDateTime maxDate) {
|
||||
|
||||
if (minDate.until(maxDate, ChronoUnit.WEEKS) > 1) {
|
||||
@@ -176,10 +192,13 @@ public class Plotter {
|
||||
}
|
||||
}
|
||||
|
||||
private static CsvSummary toCsv(final Stream<Entry> entries, final File dataFile) throws IOException {
|
||||
private static CsvSummary toCsv(final Stream<Entry> entries, final File dataFile, final OffsetDateTime dateFrom,
|
||||
final OffsetDateTime dateTo) throws IOException {
|
||||
|
||||
final long start = System.nanoTime();
|
||||
int count = 0;
|
||||
final long fromEpochMilli = dateFrom.toInstant().toEpochMilli();
|
||||
final long toEpochMilli = dateTo.toInstant().toEpochMilli();
|
||||
OffsetDateTime maxDate = OffsetDateTime.MIN;
|
||||
OffsetDateTime minDate = OffsetDateTime.MAX;
|
||||
final int separator = ',';
|
||||
@@ -190,17 +209,19 @@ public class Plotter {
|
||||
while (it.hasNext()) {
|
||||
final Entry entry = it.next();
|
||||
|
||||
final String value = String.valueOf(entry.getValue());
|
||||
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);
|
||||
if (fromEpochMilli <= entry.getEpochMilli() && entry.getEpochMilli() <= toEpochMilli) {
|
||||
final OffsetDateTime date = entry.getDate();
|
||||
final String value = String.valueOf(entry.getValue());
|
||||
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;
|
||||
count++;
|
||||
maxDate = maxDate.compareTo(date) > 0 ? maxDate : date;
|
||||
minDate = minDate.compareTo(date) < 0 ? minDate : date;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user