show rendered image
added loading icon image is scaled to available space
This commit is contained in:
@@ -0,0 +1,10 @@
|
||||
package org.lucares.recommind.logs;
|
||||
|
||||
public class InternalPlottingException extends Exception {
|
||||
|
||||
private static final long serialVersionUID = 1L;
|
||||
|
||||
public InternalPlottingException(final String message, final Throwable cause) {
|
||||
super(message, cause);
|
||||
}
|
||||
}
|
||||
@@ -7,6 +7,7 @@ import java.io.OutputStreamWriter;
|
||||
import java.io.Writer;
|
||||
import java.nio.charset.StandardCharsets;
|
||||
import java.nio.file.Files;
|
||||
import java.nio.file.LinkOption;
|
||||
import java.nio.file.Path;
|
||||
import java.nio.file.Paths;
|
||||
import java.time.format.DateTimeFormatter;
|
||||
@@ -20,34 +21,72 @@ import org.lucares.performance.db.FileUtils;
|
||||
import org.lucares.performance.db.PerformanceDb;
|
||||
|
||||
public class Plotter {
|
||||
|
||||
private final PerformanceDb db;
|
||||
private final Path tmpBaseDir;
|
||||
private final Path outputDir;
|
||||
|
||||
public Plotter(final PerformanceDb db, final Path tmpBaseDir, final Path outputDir) {
|
||||
this.db = db;
|
||||
this.tmpBaseDir = tmpBaseDir;
|
||||
this.outputDir = outputDir;
|
||||
|
||||
if (!Files.isDirectory(tmpBaseDir, LinkOption.NOFOLLOW_LINKS)) {
|
||||
throw new IllegalArgumentException(tmpBaseDir + " is not a directory");
|
||||
}
|
||||
if (!Files.isDirectory(outputDir)) {
|
||||
throw new IllegalArgumentException(outputDir + " is not a directory");
|
||||
}
|
||||
}
|
||||
|
||||
public Path getOutputDir() {
|
||||
return outputDir;
|
||||
}
|
||||
|
||||
public File plot(final String query, final int height, final int width) throws InternalPlottingException {
|
||||
try {
|
||||
final Collection<DataSeries> dataSeries = new ArrayList<>();
|
||||
|
||||
final Stream<Entry> entries = db.get(query).singleGroup().asStream();
|
||||
|
||||
final File dataFile = File.createTempFile("data", ".dat", tmpBaseDir.toFile());
|
||||
final DataSeries dataSerie = new DataSeries(dataFile, query);
|
||||
toCsv(entries, dataFile);
|
||||
|
||||
dataSeries.add(dataSerie);
|
||||
|
||||
final File outputFile = File.createTempFile("out", ".png", outputDir.toFile());
|
||||
final Gnuplot gnuplot = new Gnuplot(tmpBaseDir);
|
||||
final GnuplotSettings settings = new GnuplotSettings(outputFile);
|
||||
settings.setHeight(height);
|
||||
settings.setWidth(width);
|
||||
gnuplot.plot(settings, dataSeries);
|
||||
return outputFile;
|
||||
} catch (final InterruptedException e) {
|
||||
Thread.currentThread().interrupt();
|
||||
throw new IllegalStateException("Plotting was interrupted.");
|
||||
} catch (final IOException e) {
|
||||
throw new InternalPlottingException("Plotting failed.", e);
|
||||
}
|
||||
}
|
||||
|
||||
public static void main(final String[] args) throws Exception {
|
||||
final Path dataDirectory = Paths.get(args[0]);
|
||||
final Path outputDirectory = Paths.get(args[1]);
|
||||
final String query = args[2];
|
||||
final Path tmpBaseDir = Paths.get(args[0], "tmp");
|
||||
final Path tmpBaseDir = Paths.get(args[0], "tmp", "gnuplot");
|
||||
Files.createDirectories(tmpBaseDir);
|
||||
Files.createDirectories(outputDirectory);
|
||||
final Path tmpDirectory = Files.createTempDirectory(tmpBaseDir, "gnuplot");
|
||||
try {
|
||||
|
||||
final Collection<DataSeries> dataSeries = new ArrayList<>();
|
||||
|
||||
try (PerformanceDb db = new PerformanceDb(dataDirectory)) {
|
||||
final Stream<Entry> entries = db.get(query).singleGroup().asStream();
|
||||
|
||||
final File dataFile = File.createTempFile("data", ".dat", tmpDirectory.toFile());
|
||||
final DataSeries dataSerie = new DataSeries(dataFile, query);
|
||||
toCsv(entries, dataFile);
|
||||
|
||||
dataSeries.add(dataSerie);
|
||||
final Plotter plotter = new Plotter(db, tmpBaseDir, outputDirectory);
|
||||
final File image = plotter.plot(query, 1600, 1200);
|
||||
System.out.println("plotted image: " + image);
|
||||
}
|
||||
|
||||
final File outputFile = File.createTempFile("out", ".png", outputDirectory.toFile());
|
||||
final Gnuplot gnuplot = new Gnuplot(tmpDirectory);
|
||||
final GnuplotSettings settings = new GnuplotSettings(outputFile);
|
||||
gnuplot.plot(settings, dataSeries);
|
||||
} finally {
|
||||
FileUtils.delete(tmpDirectory);
|
||||
FileUtils.delete(tmpBaseDir);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user