From 0a42f18cc3ed40015fbe84f4157782f31c320ac9 Mon Sep 17 00:00:00 2001 From: Nelson Ray Date: Mon, 5 Nov 2012 14:33:15 -0800 Subject: [PATCH 1/2] HistogramVisual: add support for quantiles --- .../main/java/com/metamx/druid/aggregation/Histogram.java | 2 +- .../java/com/metamx/druid/aggregation/HistogramVisual.java | 7 +++++++ .../java/com/metamx/druid/histogram/HistogramTest.java | 1 + 3 files changed, 9 insertions(+), 1 deletion(-) diff --git a/common/src/main/java/com/metamx/druid/aggregation/Histogram.java b/common/src/main/java/com/metamx/druid/aggregation/Histogram.java index 8f081ca7ade..7199d098d6a 100644 --- a/common/src/main/java/com/metamx/druid/aggregation/Histogram.java +++ b/common/src/main/java/com/metamx/druid/aggregation/Histogram.java @@ -135,7 +135,7 @@ public class Histogram public HistogramVisual asVisual() { float[] visualCounts = new float[bins.length - 2]; for(int i = 0; i < visualCounts.length; ++i) visualCounts[i] = (float)bins[i + 1]; - return new HistogramVisual(breaks, visualCounts, min, max); + return new HistogramVisual(breaks, visualCounts, new float[]{}, min, max); } public static Histogram fromBytes(byte[] bytes) { diff --git a/common/src/main/java/com/metamx/druid/aggregation/HistogramVisual.java b/common/src/main/java/com/metamx/druid/aggregation/HistogramVisual.java index 5ce5217e1b2..4a88e652c7b 100644 --- a/common/src/main/java/com/metamx/druid/aggregation/HistogramVisual.java +++ b/common/src/main/java/com/metamx/druid/aggregation/HistogramVisual.java @@ -29,6 +29,7 @@ public class HistogramVisual { @JsonProperty final public double[] breaks; @JsonProperty final public double[] counts; + @JsonProperty final public double[] quantiles; @JsonProperty final double min; @JsonProperty final double max; @@ -36,6 +37,7 @@ public class HistogramVisual public HistogramVisual( @JsonProperty double[] breaks, @JsonProperty double[] counts, + @JsonProperty double[] quantiles, @JsonProperty double min, @JsonProperty double max ) @@ -46,6 +48,7 @@ public class HistogramVisual this.breaks = breaks; this.counts = counts; + this.quantiles = quantiles; this.min = min; this.max = max; } @@ -53,6 +56,7 @@ public class HistogramVisual public HistogramVisual( float[] breaks, float[] counts, + float[] quantiles, float min, float max ) @@ -63,8 +67,10 @@ public class HistogramVisual this.breaks = new double[breaks.length]; this.counts = new double[counts.length]; + this.quantiles = new double[quantiles.length]; for(int i = 0; i < breaks.length; ++i) this.breaks[i] = breaks[i]; for(int i = 0; i < counts.length; ++i) this.counts[i] = counts[i]; + for(int i = 0; i < quantiles.length; ++i) this.quantiles[i] = quantiles[i]; this.min = min; this.max = max; } @@ -76,6 +82,7 @@ public class HistogramVisual "counts=" + Arrays.toString(counts) + ", breaks=" + Arrays.toString(breaks) + ", min=" + min + + ", quantiles=" + Arrays.toString(quantiles) + ", max=" + max + '}'; } diff --git a/common/src/test/java/com/metamx/druid/histogram/HistogramTest.java b/common/src/test/java/com/metamx/druid/histogram/HistogramTest.java index e40cecaa203..7b764365f91 100644 --- a/common/src/test/java/com/metamx/druid/histogram/HistogramTest.java +++ b/common/src/test/java/com/metamx/druid/histogram/HistogramTest.java @@ -68,6 +68,7 @@ public class HistogramTest Map expectedObj = Maps.newLinkedHashMap(); expectedObj.put("breaks", Arrays.asList(visualBreaks)); expectedObj.put("counts", Arrays.asList(visualCounts)); + expectedObj.put("quantiles", Arrays.asList(new Double[]{})); expectedObj.put("min", -1.0); expectedObj.put("max", 1.0); From 217caa3a04fad7f0f95de7d178c91f16a2498f04 Mon Sep 17 00:00:00 2001 From: Nelson Ray Date: Tue, 6 Nov 2012 16:55:35 -0800 Subject: [PATCH 2/2] HistogramVisual: put min. and max. in quantile array --- .../com/metamx/druid/aggregation/Histogram.java | 7 ++++++- .../druid/aggregation/HistogramVisual.java | 17 +++-------------- .../metamx/druid/histogram/HistogramTest.java | 4 +--- 3 files changed, 10 insertions(+), 18 deletions(-) diff --git a/common/src/main/java/com/metamx/druid/aggregation/Histogram.java b/common/src/main/java/com/metamx/druid/aggregation/Histogram.java index 7199d098d6a..bb27e317eb4 100644 --- a/common/src/main/java/com/metamx/druid/aggregation/Histogram.java +++ b/common/src/main/java/com/metamx/druid/aggregation/Histogram.java @@ -132,10 +132,15 @@ public class Histogram return buf.array(); } + /** + * Returns a visual representation of a histogram object. + * Initially returns an array of just the min. and max. values + * but can also support the addition of quantiles. + */ public HistogramVisual asVisual() { float[] visualCounts = new float[bins.length - 2]; for(int i = 0; i < visualCounts.length; ++i) visualCounts[i] = (float)bins[i + 1]; - return new HistogramVisual(breaks, visualCounts, new float[]{}, min, max); + return new HistogramVisual(breaks, visualCounts, new float[]{min, max}); } public static Histogram fromBytes(byte[] bytes) { diff --git a/common/src/main/java/com/metamx/druid/aggregation/HistogramVisual.java b/common/src/main/java/com/metamx/druid/aggregation/HistogramVisual.java index 4a88e652c7b..ab4e8836fe1 100644 --- a/common/src/main/java/com/metamx/druid/aggregation/HistogramVisual.java +++ b/common/src/main/java/com/metamx/druid/aggregation/HistogramVisual.java @@ -29,17 +29,14 @@ public class HistogramVisual { @JsonProperty final public double[] breaks; @JsonProperty final public double[] counts; + // an array of the quantiles including the min. and max. @JsonProperty final public double[] quantiles; - @JsonProperty final double min; - @JsonProperty final double max; @JsonCreator public HistogramVisual( @JsonProperty double[] breaks, @JsonProperty double[] counts, - @JsonProperty double[] quantiles, - @JsonProperty double min, - @JsonProperty double max + @JsonProperty double[] quantiles ) { Preconditions.checkArgument(breaks != null, "breaks must not be null"); @@ -49,16 +46,12 @@ public class HistogramVisual this.breaks = breaks; this.counts = counts; this.quantiles = quantiles; - this.min = min; - this.max = max; } public HistogramVisual( float[] breaks, float[] counts, - float[] quantiles, - float min, - float max + float[] quantiles ) { Preconditions.checkArgument(breaks != null, "breaks must not be null"); @@ -71,8 +64,6 @@ public class HistogramVisual for(int i = 0; i < breaks.length; ++i) this.breaks[i] = breaks[i]; for(int i = 0; i < counts.length; ++i) this.counts[i] = counts[i]; for(int i = 0; i < quantiles.length; ++i) this.quantiles[i] = quantiles[i]; - this.min = min; - this.max = max; } @Override @@ -81,9 +72,7 @@ public class HistogramVisual return "HistogramVisual{" + "counts=" + Arrays.toString(counts) + ", breaks=" + Arrays.toString(breaks) + - ", min=" + min + ", quantiles=" + Arrays.toString(quantiles) + - ", max=" + max + '}'; } } diff --git a/common/src/test/java/com/metamx/druid/histogram/HistogramTest.java b/common/src/test/java/com/metamx/druid/histogram/HistogramTest.java index 7b764365f91..c280507e207 100644 --- a/common/src/test/java/com/metamx/druid/histogram/HistogramTest.java +++ b/common/src/test/java/com/metamx/druid/histogram/HistogramTest.java @@ -68,9 +68,7 @@ public class HistogramTest Map expectedObj = Maps.newLinkedHashMap(); expectedObj.put("breaks", Arrays.asList(visualBreaks)); expectedObj.put("counts", Arrays.asList(visualCounts)); - expectedObj.put("quantiles", Arrays.asList(new Double[]{})); - expectedObj.put("min", -1.0); - expectedObj.put("max", 1.0); + expectedObj.put("quantiles", Arrays.asList(new Double[]{-1.0, 1.0})); Map obj = (Map)objectMapper.readValue(json, Object.class); Assert.assertEquals(expectedObj, obj);