add links to current settings and to current image

This commit is contained in:
2018-04-10 19:59:09 +02:00
parent fe29b0d738
commit 7018a11ab3
7 changed files with 198 additions and 95 deletions

View File

@@ -29,24 +29,20 @@ import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
public class PercentilePlot implements ConcretePlotter {
private static final Logger LOGGER = LoggerFactory
.getLogger(ScatterPlot.class);
private static final Logger METRICS_LOGGER = LoggerFactory
.getLogger("org.lucares.metrics.plotter.percentile");
private PerformanceDb db;
private Path tmpBaseDir;
private Path outputDir;
private static final Logger LOGGER = LoggerFactory.getLogger(ScatterPlot.class);
private static final Logger METRICS_LOGGER = LoggerFactory.getLogger("org.lucares.metrics.plotter.percentile");
private final PerformanceDb db;
private final Path tmpBaseDir;
private final Path outputDir;
public PercentilePlot(PerformanceDb db, final Path tmpBaseDir,
Path outputDir) {
public PercentilePlot(final PerformanceDb db, final Path tmpBaseDir, final Path outputDir) {
this.db = db;
this.tmpBaseDir = tmpBaseDir;
this.outputDir = outputDir;
}
@Override
public PlotResult plot(PlotSettings plotSettings)
throws InternalPlottingException {
public PlotResult plot(final PlotSettings plotSettings) throws InternalPlottingException {
LOGGER.trace("start plot: {}", plotSettings);
@@ -54,8 +50,7 @@ public class PercentilePlot implements ConcretePlotter {
final Path tmpDir = tmpBaseDir.resolve(tmpSubDir);
try {
Files.createDirectories(tmpDir);
final List<DataSeries> dataSeries = Collections
.synchronizedList(new ArrayList<>());
final List<DataSeries> dataSeries = Collections.synchronizedList(new ArrayList<>());
final String query = plotSettings.getQuery();
final List<String> groupBy = plotSettings.getGroupBy();
@@ -68,43 +63,35 @@ public class PercentilePlot implements ConcretePlotter {
final long start = System.nanoTime();
final AtomicInteger idCounter = new AtomicInteger(0);
result.getGroups()
.stream()
.parallel()
.forEach(
groupResult -> {
try {
final int id = idCounter.getAndIncrement();
result.getGroups().stream().parallel().forEach(groupResult -> {
try {
final int id = idCounter.getAndIncrement();
final FileBackedDataSeries dataSerie = toCsv(
id, groupResult, tmpDir, dateFrom,
dateTo, plotSettings);
final FileBackedDataSeries dataSerie = toCsv(id, groupResult, tmpDir, dateFrom, dateTo,
plotSettings);
if (dataSerie.getValues() > 0) {
dataSeries.add(dataSerie);
}
} catch (Exception e) {
throw new IllegalStateException(e); // TODO
// handle
}
});
METRICS_LOGGER.debug("csv generation took: "
+ (System.nanoTime() - start) / 1_000_000.0 + "ms");
if (dataSerie.getValues() > 0) {
dataSeries.add(dataSerie);
}
} catch (final Exception e) {
throw new IllegalStateException(e); // TODO
// handle
}
});
METRICS_LOGGER.debug("csv generation took: " + (System.nanoTime() - start) / 1_000_000.0 + "ms");
if (dataSeries.isEmpty()) {
throw new NoDataPointsException();
}
final Limit limitBy = plotSettings.getLimitBy();
int limit = plotSettings.getLimit();
final int limit = plotSettings.getLimit();
DataSeries.sortAndLimit(dataSeries, limitBy, limit);
DataSeries.setColors(dataSeries);
final Path outputFile = Files.createTempFile(outputDir, "out",
".png");
final Path outputFile = Files.createTempFile(outputDir, "out", ".png");
final Gnuplot gnuplot = new Gnuplot(tmpBaseDir);
final GnuplotSettings gnuplotSettings = new GnuplotSettings(
outputFile);
final GnuplotSettings gnuplotSettings = new GnuplotSettings(outputFile);
gnuplotSettings.setHeight(height);
gnuplotSettings.setWidth(width);
defineXAxis(gnuplotSettings);
@@ -114,22 +101,21 @@ public class PercentilePlot implements ConcretePlotter {
gnuplotSettings.setKeyOutside(plotSettings.isKeyOutside());
gnuplot.plot(gnuplotSettings, dataSeries);
return new PlotResult(outputFile.getFileName(), dataSeries);
return new PlotResult(outputFile, dataSeries);
} catch (final InterruptedException e) {
Thread.currentThread().interrupt();
throw new IllegalStateException("Plotting was interrupted.");
} catch (final IOException e) {
throw new InternalPlottingException("Plotting failed: "
+ e.getMessage(), e);
throw new InternalPlottingException("Plotting failed: " + e.getMessage(), e);
} finally {
FileUtils.delete(tmpDir);
LOGGER.trace("done plot");
}
}
private FileBackedDataSeries toCsv(int id, GroupResult groupResult,
Path tmpDir, OffsetDateTime dateFrom, OffsetDateTime dateTo,
PlotSettings plotSettings) throws IOException {
private FileBackedDataSeries toCsv(final int id, final GroupResult groupResult, final Path tmpDir,
final OffsetDateTime dateFrom, final OffsetDateTime dateTo, final PlotSettings plotSettings)
throws IOException {
final long start = System.nanoTime();
final Stream<Entry> entries = groupResult.asStream();
@@ -143,13 +129,11 @@ public class PercentilePlot implements ConcretePlotter {
final IntList values = new IntList(); // TODO should be a LongList
long maxValue = 0;
final Iterator<Entry> it = entries.iterator();
while (it.hasNext()) {
final Entry entry = it.next();
long epochMilli = entry.getEpochMilli();
final long epochMilli = entry.getEpochMilli();
if (fromEpochMilli <= epochMilli && epochMilli <= toEpochMilli) {
final long value = entry.getValue();
@@ -160,20 +144,19 @@ public class PercentilePlot implements ConcretePlotter {
}
}
values.parallelSort();
final File dataFile = File.createTempFile("data", ".dat", tmpDir.toFile());
try(final Writer output = new BufferedWriter(new OutputStreamWriter(new FileOutputStream(dataFile), StandardCharsets.US_ASCII));){
try (final Writer output = new BufferedWriter(
new OutputStreamWriter(new FileOutputStream(dataFile), StandardCharsets.US_ASCII));) {
final StringBuilder data = new StringBuilder();
if (values.size() > 0) {
// compute the percentiles
for (int i = 0; i < 100; i++) {
data.append(i);
data.append(separator);
data.append(values.get((int) Math.floor(values.size()
/ 100.0 * i)));
data.append(values.get((int) Math.floor(values.size() / 100.0 * i)));
data.append(newline);
}
maxValue = values.get(values.size() - 1);
@@ -184,19 +167,16 @@ public class PercentilePlot implements ConcretePlotter {
}
output.write(data.toString());
}
METRICS_LOGGER
.debug("wrote {} values to csv in: {}ms (ignored {} values) grouping={}",
count, (System.nanoTime() - start) / 1_000_000.0,
ignoredValues, groupResult.getGroupedBy());
METRICS_LOGGER.debug("wrote {} values to csv in: {}ms (ignored {} values) grouping={}", count,
(System.nanoTime() - start) / 1_000_000.0, ignoredValues, groupResult.getGroupedBy());
final String title = ConcretePlotter.title(groupResult.getGroupedBy(),
values.size());
final String title = ConcretePlotter.title(groupResult.getGroupedBy(), values.size());
CsvSummary csvSummary = new CsvSummary(dataFile, values.size(), maxValue, null);
return new FileBackedDataSeries(id, title, csvSummary, GnuplotLineType.LINE);
final CsvSummary csvSummary = new CsvSummary(dataFile, values.size(), maxValue, null);
return new FileBackedDataSeries(id, title, csvSummary, GnuplotLineType.LINE);
}
private void defineXAxis(GnuplotSettings gnuplotSettings) {
private void defineXAxis(final GnuplotSettings gnuplotSettings) {
final XAxisSettings xAxis = gnuplotSettings.getxAxisSettings();
xAxis.setxDataTime(false);
xAxis.setFrom("0");

View File

@@ -4,17 +4,21 @@ import java.nio.file.Path;
import java.util.List;
public class PlotResult {
private final Path imageName;
private final Path imagePath;
private final List<DataSeries> dataSeries;
public PlotResult(final Path imageName, final List<DataSeries> dataSeries) {
public PlotResult(final Path imagePath, final List<DataSeries> dataSeries) {
super();
this.imageName = imageName;
this.imagePath = imagePath;
this.dataSeries = dataSeries;
}
public Path getImageName() {
return imageName;
return imagePath.getFileName();
}
public Path getImagePath() {
return imagePath;
}
public List<DataSeries> getDataSeries() {

View File

@@ -118,7 +118,7 @@ public class ScatterPlot implements ConcretePlotter {
gnuplotSettings.setKeyOutside(plotSettings.isKeyOutside());
gnuplot.plot(gnuplotSettings, dataSeries);
return new PlotResult(outputFile.getFileName(), dataSeries);
return new PlotResult(outputFile, dataSeries);
} catch (final InterruptedException e) {
Thread.currentThread().interrupt();
throw new IllegalStateException("Plotting was interrupted.");