improvements

- split the 'sortby' select field into two fields
- sort by average
- legend shows plotted and total values in the date range
- removed InlineDataSeries, because it was not used anymore
This commit is contained in:
2018-05-01 17:32:25 +02:00
parent 82dca3a885
commit bda2de672e
11 changed files with 284 additions and 129 deletions

View File

@@ -4,33 +4,57 @@ import java.io.File;
import org.lucares.pdb.plot.api.AggregatedData;
class CsvSummary {
private final int values;
private long maxValue;
private File dataFile;
private AggregatedData aggregatedData;
private final long maxValue;
private final File dataFile;
private final AggregatedData aggregatedData;
private final double statsAverage;
private final int plottedValues;
public CsvSummary(File dataFile, final int values, long maxValue, AggregatedData aggregatedData) {
public CsvSummary(final File dataFile, final int values, final int plottedValues, final long maxValue,
final double statsAverage, final AggregatedData aggregatedData) {
super();
this.dataFile = dataFile;
this.values = values;
this.plottedValues = plottedValues;
this.maxValue = maxValue;
this.statsAverage = statsAverage;
this.aggregatedData = aggregatedData;
}
public File getDataFile() {
return dataFile;
}
/**
* Total number of values in the selected date range.
*
* @see CsvSummary#getPlottedValues()
* @return total number of values
*/
public int getValues() {
return values;
}
/**
* Number of plotted values in the selected date range <em>and</em> y-range.
*
* @see CsvSummary#getValues()
* @return number of plotted values
*/
public int getPlottedValues() {
return plottedValues;
}
public long getMaxValue() {
return maxValue;
}
public double getStatsAverage() {
return statsAverage;
}
public AggregatedData getAggregatedData() {
return aggregatedData;
}

View File

@@ -30,8 +30,12 @@ public interface DataSeries {
public int getValues();
public int getPlottedValues();
public long getMaxValue();
public double getAverage();
public void setStyle(String style);
public String getStyle();

View File

@@ -8,22 +8,23 @@ public class FileBackedDataSeries implements DataSeries {
private final String title;
private CsvSummary csvSummary;
private final CsvSummary csvSummary;
private int id;
private final int id;
private GnuplotLineType linetype;
private final GnuplotLineType linetype;
private String style;
public FileBackedDataSeries(int id, String title, CsvSummary csvSummary,
GnuplotLineType linetype) {
public FileBackedDataSeries(final int id, final String title, final CsvSummary csvSummary,
final GnuplotLineType linetype) {
this.id = id;
this.title = title;
this.csvSummary = csvSummary;
this.linetype = linetype;
}
@Override
public String getIdAsString() {
return "id" + id;
}
@@ -34,7 +35,7 @@ public class FileBackedDataSeries implements DataSeries {
}
@Override
public void setStyle(String style) {
public void setStyle(final String style) {
this.style = style;
}
@@ -47,34 +48,44 @@ public class FileBackedDataSeries implements DataSeries {
return csvSummary.getDataFile();
}
@Override
public String getTitle() {
return title;
}
@Override
public int getValues() {
return csvSummary.getValues();
}
@Override
public int getPlottedValues() {
return csvSummary.getPlottedValues();
}
@Override
public long getMaxValue() {
return csvSummary.getMaxValue();
}
@Override
public double getAverage() {
return csvSummary.getStatsAverage();
}
@Override
public AggregatedData getAggregatedData() {
return csvSummary.getAggregatedData();
}
@Override
public String getGnuplotPlotDefinition() {
String average="";
if (getAggregatedData() != null){
average = String.format(" [%.2f]", getAggregatedData().getAverage());
}
return String.format("'%s' using 1:2 title '%s' with %s %s, \\", //
getDataFile(),//
getTitle()+average,//
getDataFile(), //
getTitle(), //
linetype, // line or points
style//
);
);
}
}

View File

@@ -1,71 +0,0 @@
package org.lucares.recommind.logs;
import org.lucares.pdb.plot.api.AggregatedData;
public class InlineDataSeries implements DataSeries{
private long maxValue;
private int numValues;
private String title;
private int id;
private String inlineData;
private String style;
public InlineDataSeries(long maxValue, int numValues, String title,
int id, String inlineData) {
this.maxValue = maxValue;
this.numValues = numValues;
this.title = title;
this.id = id;
this.inlineData = inlineData;
}
@Override
public String getIdAsString() {
return "id"+id;
}
@Override
public int getId() {
return id;
}
@Override
public void setStyle(String style) {
this.style = style;
}
@Override
public String getStyle() {
return style;
}
@Override
public String getTitle() {
return title;
}
@Override
public int getValues() {
return numValues;
}
@Override
public long getMaxValue() {
return maxValue;
}
@Override
public AggregatedData getAggregatedData() {
return null;
}
@Override
public String getGnuplotPlotDefinition() {
return String.format("'-' u 1:2 title '%s' with line\n%s,", title, inlineData);
}
}

View File

@@ -90,7 +90,7 @@ public class ScatterPlot {
final CsvSummary csvSummary = toCsv(groupResult, tmpDir, dateFrom, dateTo, plotSettings);
final int id = idCounter.incrementAndGet();
final String title = title(groupResult.getGroupedBy(), csvSummary.getValues());
final String title = title(groupResult.getGroupedBy(), csvSummary);
final DataSeries dataSerie = new FileBackedDataSeries(id, title, csvSummary,
GnuplotLineType.Points);
if (dataSerie.getValues() > 0) {
@@ -201,7 +201,6 @@ public class ScatterPlot {
final File dataFile = File.createTempFile("data", ".dat", tmpDir.toFile());
final long start = System.nanoTime();
final Stream<Entry> entries = groupResult.asStream();
int count = 0;
final long fromEpochMilli = dateFrom.toInstant().toEpochMilli();
final long toEpochMilli = dateTo.toInstant().toEpochMilli();
final boolean useMillis = (toEpochMilli - fromEpochMilli) < TimeUnit.MINUTES.toMillis(5);
@@ -213,7 +212,10 @@ public class ScatterPlot {
final CustomAggregator aggregator = plotSettings.getAggregate().createCustomAggregator(tmpDir);
int count = 0; // number of values in the x-axis range (used to compute stats)
int plottedValues = 0;
long statsMaxValue = 0;
double statsCurrentAverage = 0.0;
long ignoredValues = 0;
final int separator = ',';
final int newline = '\n';
@@ -234,6 +236,15 @@ public class ScatterPlot {
final long value = entry.getValue();
aggregator.addValue(epochMilli, value);
// compute stats
count++;
statsMaxValue = Math.max(statsMaxValue, value);
// compute average (important to do this after 'count' has been incremented)
statsCurrentAverage = statsCurrentAverage + (value - statsCurrentAverage) / count;
// check if value is in the selected y-range
if (value < minValue || value > maxValue) {
ignoredValues++;
continue;
@@ -255,16 +266,16 @@ public class ScatterPlot {
output.write(stringValue);
output.write(newline);
count++;
statsMaxValue = Math.max(statsMaxValue, value);
plottedValues++;
}
}
METRICS_LOGGER.debug("wrote {} values to csv in: {}ms (ignored {} values) use millis: {}, grouping={}, file={}",
count, (System.nanoTime() - start) / 1_000_000.0, ignoredValues, Boolean.toString(useMillis),
groupResult.getGroupedBy().asString(), dataFile);
return new CsvSummary(dataFile, count, statsMaxValue, aggregator.getAggregatedData());
return new CsvSummary(dataFile, count, plottedValues, statsMaxValue, statsCurrentAverage,
aggregator.getAggregatedData());
}
static String uniqueDirectoryName() {
@@ -272,10 +283,13 @@ public class ScatterPlot {
+ UUID.randomUUID().toString();
}
static String title(final Tags tags, final int values) {
static String title(final Tags tags, final CsvSummary csvSummary) {
final StringBuilder result = new StringBuilder();
final int values = csvSummary.getValues();
final int plottedValues = csvSummary.getPlottedValues();
if (tags.isEmpty()) {
result.append(DEFAULT_GROUP);
} else {
@@ -288,7 +302,11 @@ public class ScatterPlot {
}
result.append(" (");
result.append(String.format("%,d", values));
if (plottedValues != values) {
result.append(String.format("%,d/%,d", plottedValues, values));
} else {
result.append(String.format("%,d", values));
}
result.append(")");
return result.toString();