public static HistogramFactory forValue(GaugeMetricValue metricValue) { return new HistogramFactory(metricValue); }
private List<Bin> collect(List<Bin> bins, double startValue, double endValue) { int startBin = Double.NEGATIVE_INFINITY != startValue ? binNumberFor(startValue) : 0; int endBin = Double.POSITIVE_INFINITY != endValue ? binNumberFor(endValue) : bins.size(); if (endBin > bins.size()) { endBin = bins.size(); } List<Bin> collected = startBin < bins.size() ? bins.subList(startBin, endBin) : Collections.<Bin>emptyList(); if (collected.isEmpty()) { return Collections.singletonList(new Bin(0, startValue, endValue)); } return collected; }
/** * Create a histogram factory for the GaugeMetricValue */ public HistogramFactory getHistogramFactory(TimeRange range) { GaugeMetricValue value = getRollupValue(range); return HistogramFactory.forValue(value).definition(INSIGHT_HISTO); }
public Histogram build() { SparseHistogram sparseMetric=metricValue.getHistogram(); Histogram metricHistogram=sparseMetric.getFullHistogram(definition); List<Bin> rawBins = growTo(metricHistogram.getBins(), end, binsCount); List<Bin> histBins = new ArrayList<Bin>(); int binCount = this.binsCount; // force start and end to bin boundaries double startHistory = definition.getBinDefinition(definition.getBinForValue(this.start)).getStart(); double endHistory = definition.getBinDefinition(definition.getBinForValue(this.end)).getEnd(); if (lowOutliers) { binCount--; histBins.add(new Bin(collapse(collect(rawBins, Double.NEGATIVE_INFINITY, startHistory)).getCount(), Double.NEGATIVE_INFINITY, startHistory)); } if (highOutliers) { binCount--; histBins.add(new Bin(collapse(collect(rawBins, endHistory, Double.POSITIVE_INFINITY)).getCount(), endHistory, Double.POSITIVE_INFINITY)); } histBins.addAll(toSize(collect(rawBins, startHistory, endHistory), binCount)); Collections.sort(histBins); return new Histogram(histBins); }
/** * Collapse bins until the given number of bins is all that is returned. * If numBins <= bins.size(), no collapsing is done, and a bin list * smaller than numBins can be returned. * * This method does not mutate this object. */ private List<Bin> toSize(List<Bin> bins, int numBins) { if (bins.size() <= numBins) { return growTo(bins, Double.POSITIVE_INFINITY, numBins); } List<Bin> binsCopy = new ArrayList<Bin>(bins); List<List<Bin>> subLists = ListUtil.partition(binsCopy, numBins); List<Bin> res = new ArrayList<Bin>(numBins); for (List<Bin> subList : subLists) { res.add(collapse(subList)); } return res; }
private List<Bin> growTo(List<Bin> bins, double endValue, int minCount) { int neededBins = Double.POSITIVE_INFINITY != endValue ? definition.getBinForValue(endValue) : minCount; if (neededBins < minCount) { neededBins = minCount; } List<Bin> grownBins = new ArrayList<Bin>(bins); if (grownBins.isEmpty()) { grownBins.add(emptyBinAt(0)); } if (grownBins.get(0).getStart() != 0) { // fill bins from 0 to start double fillTo = grownBins.get(0).getStart(); List<Bin> prefixBins = new ArrayList<Bin>(); BinDefinition binDef; int count = 0; do { binDef = definition.getBinDefinition(count); count++; prefixBins.add(new Bin(0, binDef.getStart(), binDef.getEnd())); } while (fillTo > binDef.getEnd()); grownBins.addAll(0, prefixBins); } // fill bins from end to needed bins while (grownBins.size() < neededBins) { grownBins.add(emptyBinAt(grownBins.size())); } return Collections.unmodifiableList(grownBins); }