HistogramFunction extends Metrics implements AcceptableValue<BucketedValues> { @Override public void accept(final MeterEntity entity, final BucketedValues value) { if (dataset.size() > 0) { if (!value.isCompatible(dataset)) { throw new IllegalArgumentException( "Incompatible BucketedValues [" + value + "] for current HistogramFunction[" + dataset + "]"); } } this.entityId = entity.id(); final long[] values = value.getValues(); for (int i = 0; i < values.length; i++) { final long bucket = value.getBuckets()[i]; String bucketName = bucket == Long.MIN_VALUE ? Bucket.INFINITE_NEGATIVE : String.valueOf(bucket); final long bucketValue = values[i]; dataset.valueAccumulation(bucketName, bucketValue); } }  @Override void accept(final MeterEntity entity, final BucketedValues value); @Override void combine(final Metrics metrics); @Override void calculate(); @Override Metrics toHour(); @Override Metrics toDay(); @Override int remoteHashCode(); @Override void deserialize(final RemoteData remoteData); @Override RemoteData.Builder serialize(); @Override String id(); @Override Class<? extends StorageBuilder> builder(); @Override boolean equals(Object o); @Override int hashCode(); static final String DATASET; }
@Test public void testFunction() { HistogramFunctionInst inst = new HistogramFunctionInst(); inst.accept( MeterEntity.newService("service-test"), new BucketedValues( BUCKETS, new long[] { 0, 4, 10, 10 }) ); inst.accept( MeterEntity.newService("service-test"), new BucketedValues( BUCKETS, new long[] { 1, 2, 3, 4 }) ); final int[] results = inst.getDataset().sortedValues(new HeatMap.KeyComparator(true)).stream() .flatMapToInt(l -> IntStream.of(l.intValue())) .toArray(); Assert.assertArrayEquals(new int[] { 1, 6, 13, 14 }, results); }
@Test public void testFunctionWithInfinite() { HistogramFunctionInst inst = new HistogramFunctionInst(); inst.accept( MeterEntity.newService("service-test"), new BucketedValues( INFINITE_BUCKETS, new long[] { 0, 4, 10, 10 }) ); inst.accept( MeterEntity.newService("service-test"), new BucketedValues( INFINITE_BUCKETS, new long[] { 1, 2, 3, 4 }) ); Assert.assertEquals(1L, inst.getDataset().get(Bucket.INFINITE_NEGATIVE).longValue()); }
@Test(expected = IllegalArgumentException.class) public void testIncompatible() { HistogramFunctionInst inst = new HistogramFunctionInst(); inst.accept( MeterEntity.newService("service-test"), new BucketedValues( BUCKETS, new long[] { 0, 4, 10, 10 }) ); inst.accept( MeterEntity.newService("service-test"), new BucketedValues( BUCKETS_2ND, new long[] { 1, 2, 3, 4 }) ); }