diff --git a/src/main/java/com/yammer/metrics/core/APIHistogram.java b/src/main/java/com/yammer/metrics/core/APIHistogram.java index 8ccdbb0..777abc1 100644 --- a/src/main/java/com/yammer/metrics/core/APIHistogram.java +++ b/src/main/java/com/yammer/metrics/core/APIHistogram.java @@ -6,24 +6,93 @@ package com.yammer.metrics.core; * Modified by Cloudius Systems */ +import java.lang.reflect.Field; +import java.util.concurrent.atomic.AtomicLong; +import java.util.concurrent.atomic.AtomicReference; + import com.cloudius.urchin.api.APIClient; import com.yammer.metrics.stats.Sample; import com.yammer.metrics.stats.Snapshot; public class APIHistogram extends Histogram { + Field countField; + Field minField; + Field maxField; + Field sumField; + Field varianceField; + Field sampleField; + long last_update = 0; static final long UPDATE_INTERVAL = 50; + long updateInterval; String url; private APIClient c = new APIClient(); - public APIHistogram(String _url, Sample sample) { - super(sample); - url = _url; + private void setFields() { + try { + minField = Histogram.class.getDeclaredField("min"); + minField.setAccessible(true); + maxField = Histogram.class.getDeclaredField("max"); + maxField.setAccessible(true); + sumField = Histogram.class.getDeclaredField("sum"); + sumField.setAccessible(true); + varianceField = Histogram.class.getDeclaredField("variance"); + varianceField.setAccessible(true); + sampleField = Histogram.class.getDeclaredField("sample"); + sampleField.setAccessible(true); + countField = Histogram.class.getDeclaredField("count"); + countField.setAccessible(true); + } catch (NoSuchFieldException | SecurityException e) { + e.printStackTrace(); + } } - public APIHistogram(String _url, SampleType type) { + public AtomicLong getMin() throws IllegalArgumentException, + IllegalAccessException { + return (AtomicLong) minField.get(this); + } + + public AtomicLong getMax() throws IllegalArgumentException, + IllegalAccessException { + return (AtomicLong) maxField.get(this); + } + + public AtomicLong getSum() throws IllegalArgumentException, + IllegalAccessException { + return (AtomicLong) sumField.get(this); + } + + public AtomicLong getCount() throws IllegalArgumentException, + IllegalAccessException { + return (AtomicLong) countField.get(this); + } + + @SuppressWarnings("unchecked") + public AtomicReference getVariance() + throws IllegalArgumentException, IllegalAccessException { + return (AtomicReference) varianceField.get(this); + } + + public Sample getSample() throws IllegalArgumentException, + IllegalAccessException { + return (Sample) sampleField.get(this); + } + + public APIHistogram(String url, Sample sample) { + super(sample); + setFields(); + this.url = url; + } + + public APIHistogram(String url, SampleType type, long updateInterval) { super(type); - url = _url; + setFields(); + this.url = url; + this.updateInterval = updateInterval; + } + + public APIHistogram(String url, SampleType type) { + this(url, type, UPDATE_INTERVAL); } public void update() { @@ -33,9 +102,21 @@ public class APIHistogram extends Histogram { } last_update = now; clear(); - long[] vals = c.getLongArrValue(url); - for (long v : vals) { - update(v); + HistogramValues vals = c.getHistogramValue(url); + try { + for (long v : vals.sample) { + getSample().update(v); + } + getCount().set(vals.count); + getMax().set(vals.max); + getMin().set(vals.min); + getSum().set(vals.sum); + double[] newValue = new double[2]; + newValue[0] = vals.variance; + newValue[1] = vals.svariance; + getVariance().getAndSet(newValue); + } catch (IllegalArgumentException | IllegalAccessException e) { + e.printStackTrace(); } }