limit by max/min value

This commit is contained in:
2017-09-23 18:56:02 +02:00
parent 347f1fdc74
commit 70e586b7e9
7 changed files with 53 additions and 46 deletions

View File

@@ -1,5 +1,5 @@
package org.lucares.pdb.plot.api;
public enum Limit {
NO_LIMIT, MOST_VALUES, FEWEST_VALUES
NO_LIMIT, MOST_VALUES, FEWEST_VALUES, MAX_VALUE, MIN_VALUE
}

View File

@@ -1,28 +1,21 @@
package org.lucares.recommind.logs;
import java.time.OffsetDateTime;
class CsvSummary {
private final int values;
private final OffsetDateTime maxDate;
private final OffsetDateTime minDate;
private long maxValue;
public CsvSummary(final int values, final OffsetDateTime maxDate, final OffsetDateTime minDate) {
public CsvSummary(final int values, long maxValue) {
super();
this.values = values;
this.maxDate = maxDate;
this.minDate = minDate;
this.maxValue = maxValue;
}
public int getValues() {
return values;
}
public OffsetDateTime getMaxDate() {
return maxDate;
}
public OffsetDateTime getMinDate() {
return minDate;
public long getMaxValue() {
return maxValue;
}
}

View File

@@ -7,9 +7,14 @@ import java.util.List;
import java.util.Map;
public class DataSeries {
public static final Comparator<? super DataSeries> BY_VALUES = (a, b) -> {
public static final Comparator<? super DataSeries> BY_NUMBER_OF_VALUES = (a, b) -> {
return a.getValues() - b.getValues();
};
public static final Comparator<? super DataSeries> BY_MAX_VALUE = (a, b) -> {
final long result = a.getMaxValue() - b.getMaxValue();
return result <0 ? -1 : (result > 0 ? 1 : 0);
};
private final File dataFile;
@@ -21,11 +26,14 @@ public class DataSeries {
private final int values;
public DataSeries(final File dataFile, final String title, final int values) {
private long maxValue;
public DataSeries(final File dataFile, final String title, final int values, long maxValue) {
super();
this.dataFile = dataFile;
this.title = title;
this.values = values;
this.maxValue = maxValue;
this.color = null;
this.pointType = null;
}
@@ -49,6 +57,10 @@ public class DataSeries {
public int getValues() {
return values;
}
public long getMaxValue() {
return maxValue;
}
public static Map<String, Integer> toMap(final List<DataSeries> dataSeries) {
final Map<String, Integer> result = new LinkedHashMap<>();

View File

@@ -76,13 +76,6 @@ public class Plotter {
final Result result = db.get(query, groupBy);
// OffsetDateTime maxDate =
// OffsetDateTime.ofInstant(Instant.ofEpochMilli(Long.MIN_VALUE),
// ZoneOffset.UTC);
// OffsetDateTime minDate =
// OffsetDateTime.ofInstant(Instant.ofEpochMilli(Long.MAX_VALUE),
// ZoneOffset.UTC);
for (final GroupResult groupResult : result.getGroups()) {
final Stream<Entry> entries = groupResult.asStream();
@@ -91,14 +84,9 @@ public class Plotter {
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());
final DataSeries dataSerie = new DataSeries(dataFile, title, csvSummary.getValues(), csvSummary.getMaxValue());
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();
}
}
@@ -163,26 +151,36 @@ public class Plotter {
private void sortAndLimit(final List<DataSeries> dataSeries, final PlotSettings plotSettings) {
final Limit limitBy = plotSettings.getLimitBy();
if (limitBy != Limit.NO_LIMIT) {
dataSeries.sort(getDataSeriesComparator(limitBy));
dataSeries.sort(getDataSeriesComparator(limitBy));
switch (limitBy) {
case FEWEST_VALUES:
case MOST_VALUES:
case MAX_VALUE:
case MIN_VALUE:
while (dataSeries.size() > plotSettings.getLimit()) {
dataSeries.remove(plotSettings.getLimit());
}
} else {
dataSeries.sort(getDataSeriesComparator(Limit.MOST_VALUES));
break;
case NO_LIMIT:
}
}
private Comparator<? super DataSeries> getDataSeriesComparator(final Limit limitBy) {
if (limitBy == Limit.MOST_VALUES) {
return DataSeries.BY_VALUES.reversed();
switch (limitBy) {
case MOST_VALUES:
return DataSeries.BY_NUMBER_OF_VALUES.reversed();
case FEWEST_VALUES:
return DataSeries.BY_NUMBER_OF_VALUES;
case MAX_VALUE:
return DataSeries.BY_MAX_VALUE.reversed();
case MIN_VALUE:
return DataSeries.BY_MAX_VALUE;
case NO_LIMIT:
return DataSeries.BY_NUMBER_OF_VALUES;
}
return DataSeries.BY_VALUES;
throw new IllegalStateException("unhandled enum: "+ limitBy);
}
private String title(final Tags tags, final int values) {
@@ -215,8 +213,7 @@ public class Plotter {
int count = 0;
final long fromEpochMilli = dateFrom.toInstant().toEpochMilli();
final long toEpochMilli = dateTo.toInstant().toEpochMilli();
OffsetDateTime maxDate = OffsetDateTime.MIN;
OffsetDateTime minDate = OffsetDateTime.MAX;
long maxValue = 0;
final int separator = ',';
final int newline = '\n';
try (final Writer output = new OutputStreamWriter(new FileOutputStream(dataFile), StandardCharsets.US_ASCII);) {
@@ -235,13 +232,12 @@ public class Plotter {
output.write(newline);
count++;
maxDate = maxDate.compareTo(date) > 0 ? maxDate : date;
minDate = minDate.compareTo(date) < 0 ? minDate : date;
maxValue = Math.max(maxValue, entry.getValue());
}
}
}
METRICS_LOGGER.debug("wrote {} values to csv in: {}ms", count, (System.nanoTime() - start) / 1_000_000.0);
return new CsvSummary(count, maxDate, minDate);
return new CsvSummary(count, maxValue);
}
}