From c581e352e4c9e652352c3252beca8a20b142c8aa Mon Sep 17 00:00:00 2001 From: Andreas Huber Date: Mon, 19 Mar 2018 19:29:22 +0100 Subject: [PATCH] add method that returns a string representation of the tags in Tags --- .../main/java/org/lucares/pdb/api/Tags.java | 18 ++++++ .../lucares/recommind/logs/ScatterPlot.java | 59 +++++++++---------- 2 files changed, 46 insertions(+), 31 deletions(-) diff --git a/pdb-api/src/main/java/org/lucares/pdb/api/Tags.java b/pdb-api/src/main/java/org/lucares/pdb/api/Tags.java index 5e8c7ec..2911c47 100644 --- a/pdb-api/src/main/java/org/lucares/pdb/api/Tags.java +++ b/pdb-api/src/main/java/org/lucares/pdb/api/Tags.java @@ -225,4 +225,22 @@ public class Tags { return new Tags(filename); } + public String asString() { + + final StringBuilder result = new StringBuilder(); + final SortedSet tags = toTags(); + + for (final Tag tag : tags) { + if (result.length() > 0) { + result.append(", "); + } + + result.append(tag.getKey()); + result.append(":"); + result.append(tag.getValue()); + } + + return result.toString(); + } + } diff --git a/pdb-plotting/src/main/java/org/lucares/recommind/logs/ScatterPlot.java b/pdb-plotting/src/main/java/org/lucares/recommind/logs/ScatterPlot.java index cb4eaf0..f35da12 100644 --- a/pdb-plotting/src/main/java/org/lucares/recommind/logs/ScatterPlot.java +++ b/pdb-plotting/src/main/java/org/lucares/recommind/logs/ScatterPlot.java @@ -33,11 +33,10 @@ import org.slf4j.Logger; import org.slf4j.LoggerFactory; public class ScatterPlot implements ConcretePlotter { - + private static final Logger LOGGER = LoggerFactory.getLogger(ScatterPlot.class); private static final Logger METRICS_LOGGER = LoggerFactory.getLogger("org.lucares.metrics.plotter.scatter"); - private final PerformanceDb db; private final Path tmpBaseDir; private final Path outputDir; @@ -58,12 +57,11 @@ public class ScatterPlot implements ConcretePlotter { public Path getOutputDir() { return outputDir; } - + @Override public PlotResult plot(final PlotSettings plotSettings) throws InternalPlottingException { LOGGER.trace("start plot: {}", plotSettings); - final String tmpSubDir = ConcretePlotter.uniqueDirectoryName(); final Path tmpDir = tmpBaseDir.resolve(tmpSubDir); @@ -83,28 +81,28 @@ public class ScatterPlot implements ConcretePlotter { final long start = System.nanoTime(); final AtomicInteger idCounter = new AtomicInteger(0); result.getGroups().stream().parallel().forEach(groupResult -> { - try{ + try { final CsvSummary csvSummary = toCsv(groupResult, tmpDir, dateFrom, dateTo, plotSettings); - + final int id = idCounter.incrementAndGet(); final String title = ConcretePlotter.title(groupResult.getGroupedBy(), csvSummary.getValues()); - final DataSeries dataSerie = new FileBackedDataSeries(id, title, csvSummary, GnuplotLineType.Points); + final DataSeries dataSerie = new FileBackedDataSeries(id, title, csvSummary, + GnuplotLineType.Points); if (dataSerie.getValues() > 0) { dataSeries.add(dataSerie); } - }catch (Exception e){ - throw new IllegalStateException( e); // TODO handle + } catch (final Exception e) { + throw new IllegalStateException(e); // TODO handle } }); - METRICS_LOGGER.debug("csv generation took: " + (System.nanoTime() - start) / 1_000_000.0 - + "ms"); + 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); @@ -131,7 +129,7 @@ public class ScatterPlot implements ConcretePlotter { LOGGER.trace("done plot"); } } - + private void defineXAxis(final GnuplotSettings gnuplotSettings, final OffsetDateTime minDate, final OffsetDateTime maxDate) { @@ -158,12 +156,8 @@ public class ScatterPlot implements ConcretePlotter { gnuplotSettings.getxAxisSettings().setTo(formattedMaxDate); } - - - - private static CsvSummary toCsv(final GroupResult groupResult, final Path tmpDir, final OffsetDateTime dateFrom, - final OffsetDateTime dateTo, PlotSettings plotSettings) throws IOException { + final OffsetDateTime dateTo, final PlotSettings plotSettings) throws IOException { final File dataFile = File.createTempFile("data", ".dat", tmpDir.toFile()); final long start = System.nanoTime(); @@ -173,51 +167,54 @@ public class ScatterPlot implements ConcretePlotter { final long toEpochMilli = dateTo.toInstant().toEpochMilli(); final boolean useMillis = (toEpochMilli - fromEpochMilli) < TimeUnit.MINUTES.toMillis(5); final CustomAggregator aggregator = plotSettings.getAggregate().createCustomAggregator(tmpDir); - + long maxValue = 0; long ignoredValues = 0; final int separator = ','; final int newline = '\n'; final StringBuilder formattedDateBuilder = new StringBuilder(); - 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 Formatter formatter = new Formatter(formattedDateBuilder);) { final Iterator 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) { - - long value = entry.getValue(); + + final long value = entry.getValue(); final String stringValue = LongUtils.longToString(value); final String formattedDate; - - if (useMillis){ + + if (useMillis) { formattedDateBuilder.delete(0, formattedDateBuilder.length()); formatter.format("%.3f", epochMilli / 1000.0); formattedDate = formattedDateBuilder.toString(); - }else { + } else { formattedDate = String.valueOf(epochMilli / 1000); } - + output.write(formattedDate); output.write(separator); output.write(stringValue); output.write(newline); aggregator.addValue(epochMilli, value); - + count++; maxValue = Math.max(maxValue, value); - }else { + } else { ignoredValues++; } } } - 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(),dataFile); - return new CsvSummary(dataFile, count, maxValue, aggregator.getAggregatedData()); + 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, maxValue, aggregator.getAggregatedData()); } }