|
| 1 | +/* |
| 2 | + * Licensed to Elasticsearch under one or more contributor |
| 3 | + * license agreements. See the NOTICE file distributed with |
| 4 | + * this work for additional information regarding copyright |
| 5 | + * ownership. Elasticsearch licenses this file to you under |
| 6 | + * the Apache License, Version 2.0 (the "License"); you may |
| 7 | + * not use this file except in compliance with the License. |
| 8 | + * You may obtain a copy of the License at |
| 9 | + * |
| 10 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 11 | + * |
| 12 | + * Unless required by applicable law or agreed to in writing, |
| 13 | + * software distributed under the License is distributed on an |
| 14 | + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY |
| 15 | + * KIND, either express or implied. See the License for the |
| 16 | + * specific language governing permissions and limitations |
| 17 | + * under the License. |
| 18 | + */ |
| 19 | + |
| 20 | +package org.elasticsearch.search.aggregations.bucket.histogram; |
| 21 | + |
| 22 | +import org.apache.lucene.util.TestUtil; |
| 23 | +import org.elasticsearch.common.io.stream.Writeable; |
| 24 | +import org.elasticsearch.common.unit.TimeValue; |
| 25 | +import org.elasticsearch.search.DocValueFormat; |
| 26 | +import org.elasticsearch.search.aggregations.InternalAggregationTestCase; |
| 27 | +import org.elasticsearch.search.aggregations.InternalAggregations; |
| 28 | +import org.elasticsearch.search.aggregations.pipeline.PipelineAggregator; |
| 29 | +import org.joda.time.DateTime; |
| 30 | + |
| 31 | +import java.util.ArrayList; |
| 32 | +import java.util.List; |
| 33 | +import java.util.Map; |
| 34 | +import java.util.TreeMap; |
| 35 | + |
| 36 | +import static org.elasticsearch.common.unit.TimeValue.timeValueHours; |
| 37 | +import static org.elasticsearch.common.unit.TimeValue.timeValueMinutes; |
| 38 | +import static org.elasticsearch.common.unit.TimeValue.timeValueSeconds; |
| 39 | + |
| 40 | +public class InternalDateHistogramTests extends InternalAggregationTestCase<InternalDateHistogram> { |
| 41 | + |
| 42 | + @Override |
| 43 | + protected InternalDateHistogram createTestInstance(String name, List<PipelineAggregator> pipelineAggregators, |
| 44 | + Map<String, Object> metaData) { |
| 45 | + |
| 46 | + boolean keyed = randomBoolean(); |
| 47 | + DocValueFormat format = DocValueFormat.RAW; |
| 48 | + int nbBuckets = randomInt(10); |
| 49 | + List<InternalDateHistogram.Bucket> buckets = new ArrayList<>(nbBuckets); |
| 50 | + long startingDate = System.currentTimeMillis(); |
| 51 | + |
| 52 | + long interval = randomIntBetween(1, 3); |
| 53 | + long intervalMillis = randomFrom(timeValueSeconds(interval), timeValueMinutes(interval), timeValueHours(interval)).getMillis(); |
| 54 | + |
| 55 | + for (int i = 0; i < nbBuckets; i++) { |
| 56 | + long key = startingDate + (intervalMillis * i); |
| 57 | + buckets.add(i, new InternalDateHistogram.Bucket(key, randomIntBetween(1, 100), keyed, format, InternalAggregations.EMPTY)); |
| 58 | + } |
| 59 | + |
| 60 | + InternalOrder order = (InternalOrder) randomFrom(InternalHistogram.Order.KEY_ASC, InternalHistogram.Order.KEY_DESC); |
| 61 | + return new InternalDateHistogram(name, buckets, order, 1, 0L, null, format, keyed, pipelineAggregators, metaData); |
| 62 | + } |
| 63 | + |
| 64 | + @Override |
| 65 | + protected void assertReduced(InternalDateHistogram reduced, List<InternalDateHistogram> inputs) { |
| 66 | + Map<Long, Long> expectedCounts = new TreeMap<>(); |
| 67 | + for (Histogram histogram : inputs) { |
| 68 | + for (Histogram.Bucket bucket : histogram.getBuckets()) { |
| 69 | + expectedCounts.compute(((DateTime) bucket.getKey()).getMillis(), |
| 70 | + (key, oldValue) -> (oldValue == null ? 0 : oldValue) + bucket.getDocCount()); |
| 71 | + } |
| 72 | + } |
| 73 | + Map<Long, Long> actualCounts = new TreeMap<>(); |
| 74 | + for (Histogram.Bucket bucket : reduced.getBuckets()) { |
| 75 | + actualCounts.compute(((DateTime) bucket.getKey()).getMillis(), |
| 76 | + (key, oldValue) -> (oldValue == null ? 0 : oldValue) + bucket.getDocCount()); |
| 77 | + } |
| 78 | + assertEquals(expectedCounts, actualCounts); |
| 79 | + } |
| 80 | + |
| 81 | + @Override |
| 82 | + protected Writeable.Reader<InternalDateHistogram> instanceReader() { |
| 83 | + return InternalDateHistogram::new; |
| 84 | + } |
| 85 | +} |
0 commit comments