limit by max/min value
This commit is contained in:
@@ -1,5 +1,5 @@
|
|||||||
package org.lucares.pdb.plot.api;
|
package org.lucares.pdb.plot.api;
|
||||||
|
|
||||||
public enum Limit {
|
public enum Limit {
|
||||||
NO_LIMIT, MOST_VALUES, FEWEST_VALUES
|
NO_LIMIT, MOST_VALUES, FEWEST_VALUES, MAX_VALUE, MIN_VALUE
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,28 +1,21 @@
|
|||||||
package org.lucares.recommind.logs;
|
package org.lucares.recommind.logs;
|
||||||
|
|
||||||
import java.time.OffsetDateTime;
|
|
||||||
|
|
||||||
class CsvSummary {
|
class CsvSummary {
|
||||||
private final int values;
|
private final int values;
|
||||||
private final OffsetDateTime maxDate;
|
private long maxValue;
|
||||||
private final OffsetDateTime minDate;
|
|
||||||
|
|
||||||
public CsvSummary(final int values, final OffsetDateTime maxDate, final OffsetDateTime minDate) {
|
public CsvSummary(final int values, long maxValue) {
|
||||||
super();
|
super();
|
||||||
this.values = values;
|
this.values = values;
|
||||||
this.maxDate = maxDate;
|
this.maxValue = maxValue;
|
||||||
this.minDate = minDate;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
public int getValues() {
|
public int getValues() {
|
||||||
return values;
|
return values;
|
||||||
}
|
}
|
||||||
|
|
||||||
public OffsetDateTime getMaxDate() {
|
public long getMaxValue() {
|
||||||
return maxDate;
|
return maxValue;
|
||||||
}
|
|
||||||
|
|
||||||
public OffsetDateTime getMinDate() {
|
|
||||||
return minDate;
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -7,10 +7,15 @@ import java.util.List;
|
|||||||
import java.util.Map;
|
import java.util.Map;
|
||||||
|
|
||||||
public class DataSeries {
|
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();
|
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;
|
private final File dataFile;
|
||||||
|
|
||||||
private final String title;
|
private final String title;
|
||||||
@@ -21,11 +26,14 @@ public class DataSeries {
|
|||||||
|
|
||||||
private final int values;
|
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();
|
super();
|
||||||
this.dataFile = dataFile;
|
this.dataFile = dataFile;
|
||||||
this.title = title;
|
this.title = title;
|
||||||
this.values = values;
|
this.values = values;
|
||||||
|
this.maxValue = maxValue;
|
||||||
this.color = null;
|
this.color = null;
|
||||||
this.pointType = null;
|
this.pointType = null;
|
||||||
}
|
}
|
||||||
@@ -50,6 +58,10 @@ public class DataSeries {
|
|||||||
return values;
|
return values;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public long getMaxValue() {
|
||||||
|
return maxValue;
|
||||||
|
}
|
||||||
|
|
||||||
public static Map<String, Integer> toMap(final List<DataSeries> dataSeries) {
|
public static Map<String, Integer> toMap(final List<DataSeries> dataSeries) {
|
||||||
final Map<String, Integer> result = new LinkedHashMap<>();
|
final Map<String, Integer> result = new LinkedHashMap<>();
|
||||||
|
|
||||||
|
|||||||
@@ -76,13 +76,6 @@ public class Plotter {
|
|||||||
|
|
||||||
final Result result = db.get(query, groupBy);
|
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()) {
|
for (final GroupResult groupResult : result.getGroups()) {
|
||||||
|
|
||||||
final Stream<Entry> entries = groupResult.asStream();
|
final Stream<Entry> entries = groupResult.asStream();
|
||||||
@@ -91,14 +84,9 @@ public class Plotter {
|
|||||||
final CsvSummary csvSummary = toCsv(entries, dataFile, dateFrom, dateTo);
|
final CsvSummary csvSummary = toCsv(entries, dataFile, dateFrom, dateTo);
|
||||||
|
|
||||||
final String title = title(groupResult.getGroupedBy(), csvSummary.getValues());
|
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) {
|
if (dataSerie.getValues() > 0) {
|
||||||
dataSeries.add(dataSerie);
|
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) {
|
private void sortAndLimit(final List<DataSeries> dataSeries, final PlotSettings plotSettings) {
|
||||||
|
|
||||||
final Limit limitBy = plotSettings.getLimitBy();
|
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()) {
|
while (dataSeries.size() > plotSettings.getLimit()) {
|
||||||
dataSeries.remove(plotSettings.getLimit());
|
dataSeries.remove(plotSettings.getLimit());
|
||||||
}
|
}
|
||||||
} else {
|
break;
|
||||||
dataSeries.sort(getDataSeriesComparator(Limit.MOST_VALUES));
|
case NO_LIMIT:
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
private Comparator<? super DataSeries> getDataSeriesComparator(final Limit limitBy) {
|
private Comparator<? super DataSeries> getDataSeriesComparator(final Limit limitBy) {
|
||||||
|
|
||||||
if (limitBy == Limit.MOST_VALUES) {
|
switch (limitBy) {
|
||||||
return DataSeries.BY_VALUES.reversed();
|
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;
|
||||||
}
|
}
|
||||||
|
throw new IllegalStateException("unhandled enum: "+ limitBy);
|
||||||
return DataSeries.BY_VALUES;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
private String title(final Tags tags, final int values) {
|
private String title(final Tags tags, final int values) {
|
||||||
@@ -215,8 +213,7 @@ public class Plotter {
|
|||||||
int count = 0;
|
int count = 0;
|
||||||
final long fromEpochMilli = dateFrom.toInstant().toEpochMilli();
|
final long fromEpochMilli = dateFrom.toInstant().toEpochMilli();
|
||||||
final long toEpochMilli = dateTo.toInstant().toEpochMilli();
|
final long toEpochMilli = dateTo.toInstant().toEpochMilli();
|
||||||
OffsetDateTime maxDate = OffsetDateTime.MIN;
|
long maxValue = 0;
|
||||||
OffsetDateTime minDate = OffsetDateTime.MAX;
|
|
||||||
final int separator = ',';
|
final int separator = ',';
|
||||||
final int newline = '\n';
|
final int newline = '\n';
|
||||||
try (final Writer output = new OutputStreamWriter(new FileOutputStream(dataFile), StandardCharsets.US_ASCII);) {
|
try (final Writer output = new OutputStreamWriter(new FileOutputStream(dataFile), StandardCharsets.US_ASCII);) {
|
||||||
@@ -235,13 +232,12 @@ public class Plotter {
|
|||||||
output.write(newline);
|
output.write(newline);
|
||||||
|
|
||||||
count++;
|
count++;
|
||||||
maxDate = maxDate.compareTo(date) > 0 ? maxDate : date;
|
maxValue = Math.max(maxValue, entry.getValue());
|
||||||
minDate = minDate.compareTo(date) < 0 ? minDate : date;
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
METRICS_LOGGER.debug("wrote {} values to csv in: {}ms", count, (System.nanoTime() - start) / 1_000_000.0);
|
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);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -57,6 +57,10 @@ class PlotSettingsTransformer {
|
|||||||
return Limit.FEWEST_VALUES;
|
return Limit.FEWEST_VALUES;
|
||||||
case MOST_VALUES:
|
case MOST_VALUES:
|
||||||
return Limit.MOST_VALUES;
|
return Limit.MOST_VALUES;
|
||||||
|
case MAX_VALUE:
|
||||||
|
return Limit.MAX_VALUE;
|
||||||
|
case MIN_VALUE:
|
||||||
|
return Limit.MIN_VALUE;
|
||||||
default:
|
default:
|
||||||
throw new IllegalStateException("unhandled enum: " + limitBy);
|
throw new IllegalStateException("unhandled enum: " + limitBy);
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,5 +1,5 @@
|
|||||||
package org.lucares.pdbui.domain;
|
package org.lucares.pdbui.domain;
|
||||||
|
|
||||||
public enum LimitBy {
|
public enum LimitBy {
|
||||||
NO_LIMIT, MOST_VALUES, FEWEST_VALUES
|
NO_LIMIT, MOST_VALUES, FEWEST_VALUES, MAX_VALUE, MIN_VALUE
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -31,6 +31,8 @@
|
|||||||
<option value="NO_LIMIT" selected="selected">no limit</option>
|
<option value="NO_LIMIT" selected="selected">no limit</option>
|
||||||
<option value="MOST_VALUES">most values</option>
|
<option value="MOST_VALUES">most values</option>
|
||||||
<option value="FEWEST_VALUES">fewest values</option>
|
<option value="FEWEST_VALUES">fewest values</option>
|
||||||
|
<option value="MAX_VALUE">max value</option>
|
||||||
|
<option value="MIN_VALUE">min value</option>
|
||||||
</select>
|
</select>
|
||||||
<input type="number" id="search-limit-value" name="search-limit-value" min="1" max="1000" value="10"/>
|
<input type="number" id="search-limit-value" name="search-limit-value" min="1" max="1000" value="10"/>
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user