public void toData(DataOutput dataOutput) throws IOException { super.toData(serialVersionUID, dataOutput); dataOutput.writeInt(slots.size()); int usedSlots = 0; for (GaugeMetricValue v : slots) { if (v != null) usedSlots++; } dataOutput.writeInt(usedSlots); int idx = -1; for (GaugeMetricValue v : slots) { idx++; if (v == null) { continue; } dataOutput.writeInt(idx); dataOutput.writeDouble(v.getMin()); dataOutput.writeDouble(v.getMax()); dataOutput.writeLong(v.getN()); dataOutput.writeDouble(v.getSum()); dataOutput.writeDouble(v.getSumOfSquares()); dataOutput.writeLong(v.getRange().getStart()); SparseHistogram histo = v.getHistogram(); dataOutput.writeInt(histo.getBins().size()); for (IndexedBin bin : histo.getBins()) { dataOutput.writeInt(bin.getBinNumber()); dataOutput.writeLong(bin.getCount()); } } }
/** * Create a {@link GaugeMetricValue} from a series of {@link GaugeMetricValue}s. The * resulting value will be a summarization of each individual value. * * @param range The range which represents the aggregated value */ public GaugeMetricValue fromValues(TimeRange range, Collection<GaugeMetricValue> values) { if (values.isEmpty()) { return new GaugeMetricValue(range); } HistogramAccumulator accum = makeHistoAccumulator(); Min min = new Min(); Max max = new Max(); Sum total = new Sum(); Sum n = new Sum(); Sum sumOfSquares = new Sum(); for (GaugeMetricValue v : values) { min.increment(v.getMin()); max.increment(v.getMax()); n.increment(v.getN()); total.increment(v.getSum()); sumOfSquares.increment(v.getSumOfSquares()); accum.add(v.getHistogram()); } return new GaugeMetricValue(range, min.getResult(), max.getResult(), (long)n.getResult(), total.getResult(), sumOfSquares.getResult(), accum.getSparseHistogram()); }