make location of gnuplot configurable
This commit is contained in:
@@ -6,10 +6,18 @@ import java.nio.charset.StandardCharsets;
|
|||||||
import java.nio.file.Path;
|
import java.nio.file.Path;
|
||||||
import java.util.Collection;
|
import java.util.Collection;
|
||||||
|
|
||||||
|
import org.slf4j.Logger;
|
||||||
|
import org.slf4j.LoggerFactory;
|
||||||
|
|
||||||
import com.google.common.io.Files;
|
import com.google.common.io.Files;
|
||||||
|
|
||||||
public class Gnuplot {
|
public class Gnuplot {
|
||||||
|
|
||||||
|
private static final Logger LOGGER = LoggerFactory.getLogger(Gnuplot.class);
|
||||||
|
private static final Logger METRICS_LOGGER = LoggerFactory.getLogger("org.lucares.metrics.gnuplot");
|
||||||
|
|
||||||
|
private static final String ENV_GNUPLOT_HOME = "GNUPLOT_HOME";
|
||||||
|
private static final String PROPERTY_GNUPLOT_HOME = "gnuplot.home";
|
||||||
private final Path tmpDirectory;
|
private final Path tmpDirectory;
|
||||||
|
|
||||||
public Gnuplot(final Path tmpDirectory) {
|
public Gnuplot(final Path tmpDirectory) {
|
||||||
@@ -22,18 +30,54 @@ public class Gnuplot {
|
|||||||
final GnuplotFileGenerator generator = new GnuplotFileGenerator();
|
final GnuplotFileGenerator generator = new GnuplotFileGenerator();
|
||||||
|
|
||||||
final String gnuplotFileContent = generator.generate(settings, dataSeries);
|
final String gnuplotFileContent = generator.generate(settings, dataSeries);
|
||||||
System.out.println(gnuplotFileContent);
|
LOGGER.debug(gnuplotFileContent);
|
||||||
|
|
||||||
final File gnuplotFile = File.createTempFile("gnuplot", ".dem", tmpDirectory.toFile());
|
final File gnuplotFile = File.createTempFile("gnuplot", ".dem", tmpDirectory.toFile());
|
||||||
Files.write(gnuplotFileContent, gnuplotFile, StandardCharsets.UTF_8);
|
Files.write(gnuplotFileContent, gnuplotFile, StandardCharsets.UTF_8);
|
||||||
|
|
||||||
final long start = System.nanoTime();
|
final long start = System.nanoTime();
|
||||||
|
|
||||||
final ProcessBuilder processBuilder = new ProcessBuilder("gnuplot", gnuplotFile.getAbsolutePath());
|
try {
|
||||||
|
final ProcessBuilder processBuilder = new ProcessBuilder(gnuplotBinary(), gnuplotFile.getAbsolutePath());
|
||||||
processBuilder.inheritIO();
|
processBuilder.inheritIO();
|
||||||
final Process process = processBuilder.start();
|
final Process process = processBuilder.start();
|
||||||
process.waitFor();
|
process.waitFor();
|
||||||
|
} catch (final IOException e) {
|
||||||
|
if (e.getMessage().contains("No such file or directory")) {
|
||||||
|
throw new IOException("Did not find gnuplot. Add it to the 'PATH' or create an environment variable '"
|
||||||
|
+ ENV_GNUPLOT_HOME + "' or add the java property '" + PROPERTY_GNUPLOT_HOME + "': "
|
||||||
|
+ e.getMessage(), e);
|
||||||
|
} else {
|
||||||
|
throw e;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
System.out.println("gnuplot: " + (System.nanoTime() - start) / 1_000_000.0 + "ms");
|
METRICS_LOGGER.debug("gnuplot: {}ms", (System.nanoTime() - start) / 1_000_000.0);
|
||||||
|
}
|
||||||
|
|
||||||
|
private String gnuplotBinary() {
|
||||||
|
|
||||||
|
if (System.getProperty(PROPERTY_GNUPLOT_HOME) != null) {
|
||||||
|
|
||||||
|
if (isWindows()) {
|
||||||
|
return System.getProperty(PROPERTY_GNUPLOT_HOME) + "\\bin\\+gnuplot.exe";
|
||||||
|
} else {
|
||||||
|
return System.getProperty(PROPERTY_GNUPLOT_HOME) + "/bin/gnuplot";
|
||||||
|
}
|
||||||
|
}
|
||||||
|
if (System.getenv(ENV_GNUPLOT_HOME) != null) {
|
||||||
|
|
||||||
|
if (isWindows()) {
|
||||||
|
return System.getenv(ENV_GNUPLOT_HOME) + "\\bin\\+gnuplot.exe";
|
||||||
|
} else {
|
||||||
|
return System.getenv(ENV_GNUPLOT_HOME) + "/bin/gnuplot";
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return "gnuplot";
|
||||||
|
}
|
||||||
|
|
||||||
|
private boolean isWindows() {
|
||||||
|
return System.getProperty("os.name").toLowerCase().startsWith("windows");
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -108,7 +108,7 @@ public class Plotter {
|
|||||||
Thread.currentThread().interrupt();
|
Thread.currentThread().interrupt();
|
||||||
throw new IllegalStateException("Plotting was interrupted.");
|
throw new IllegalStateException("Plotting was interrupted.");
|
||||||
} catch (final IOException e) {
|
} catch (final IOException e) {
|
||||||
throw new InternalPlottingException("Plotting failed.", e);
|
throw new InternalPlottingException("Plotting failed: " + e.getMessage(), e);
|
||||||
} finally {
|
} finally {
|
||||||
FileUtils.delete(tmpDir);
|
FileUtils.delete(tmpDir);
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user