Skip to content

Commit 01e03ce

Browse files
committed
Add parsing for InternalStatsBucket
1 parent 9087313 commit 01e03ce

File tree

5 files changed

+125
-1
lines changed

5 files changed

+125
-1
lines changed
Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
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.pipeline.bucketmetrics.stats;
21+
22+
import org.elasticsearch.common.xcontent.ObjectParser;
23+
import org.elasticsearch.common.xcontent.XContentParser;
24+
import org.elasticsearch.search.aggregations.metrics.stats.ParsedStats;
25+
26+
27+
public class ParsedStatsBucket extends ParsedStats implements StatsBucket {
28+
29+
@Override
30+
protected String getType() {
31+
return StatsBucketPipelineAggregationBuilder.NAME;
32+
}
33+
34+
private static final ObjectParser<ParsedStatsBucket, Void> PARSER = new ObjectParser<>(
35+
ParsedStatsBucket.class.getSimpleName(), true, ParsedStatsBucket::new);
36+
37+
static {
38+
declareStatsFields(PARSER);
39+
}
40+
41+
public static ParsedStatsBucket fromXContent(XContentParser parser, final String name) {
42+
ParsedStatsBucket parsedStatsBucket = PARSER.apply(parser, null);
43+
parsedStatsBucket.setName(name);
44+
return parsedStatsBucket;
45+
}
46+
}

core/src/test/java/org/elasticsearch/search/aggregations/InternalAggregationTestCase.java

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -63,6 +63,8 @@
6363
import org.elasticsearch.search.aggregations.pipeline.PipelineAggregator;
6464
import org.elasticsearch.search.aggregations.pipeline.bucketmetrics.InternalBucketMetricValue;
6565
import org.elasticsearch.search.aggregations.pipeline.bucketmetrics.ParsedBucketMetricValue;
66+
import org.elasticsearch.search.aggregations.pipeline.bucketmetrics.stats.ParsedStatsBucket;
67+
import org.elasticsearch.search.aggregations.pipeline.bucketmetrics.stats.StatsBucketPipelineAggregationBuilder;
6668
import org.elasticsearch.search.aggregations.pipeline.bucketmetrics.stats.extended.ExtendedStatsBucketPipelineAggregationBuilder;
6769
import org.elasticsearch.search.aggregations.pipeline.bucketmetrics.stats.extended.ParsedExtendedStatsBucket;
6870
import org.elasticsearch.search.aggregations.pipeline.derivative.DerivativePipelineAggregationBuilder;
@@ -106,6 +108,7 @@ static List<NamedXContentRegistry.Entry> getNamedXContents() {
106108
namedXContents.put(DerivativePipelineAggregationBuilder.NAME, (p, c) -> ParsedDerivative.fromXContent(p, (String) c));
107109
namedXContents.put(InternalBucketMetricValue.NAME, (p, c) -> ParsedBucketMetricValue.fromXContent(p, (String) c));
108110
namedXContents.put(StatsAggregationBuilder.NAME, (p, c) -> ParsedStats.fromXContent(p, (String) c));
111+
namedXContents.put(StatsBucketPipelineAggregationBuilder.NAME, (p, c) -> ParsedStatsBucket.fromXContent(p, (String) c));
109112
namedXContents.put(ExtendedStatsAggregationBuilder.NAME, (p, c) -> ParsedExtendedStats.fromXContent(p, (String) c));
110113
namedXContents.put(ExtendedStatsBucketPipelineAggregationBuilder.NAME, (p, c) -> ParsedExtendedStatsBucket.fromXContent(p, (String) c));
111114

Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
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.metrics;
21+
22+
import org.elasticsearch.common.io.stream.Writeable;
23+
import org.elasticsearch.search.DocValueFormat;
24+
import org.elasticsearch.search.aggregations.ParsedAggregation;
25+
import org.elasticsearch.search.aggregations.metrics.stats.InternalStats;
26+
import org.elasticsearch.search.aggregations.pipeline.PipelineAggregator;
27+
import org.elasticsearch.search.aggregations.pipeline.bucketmetrics.stats.InternalStatsBucket;
28+
import org.elasticsearch.search.aggregations.pipeline.bucketmetrics.stats.ParsedStatsBucket;
29+
30+
import java.util.Collections;
31+
import java.util.List;
32+
import java.util.Map;
33+
34+
public class InternalStatsBucketTests extends InternalStatsTests {
35+
36+
@Override
37+
protected InternalStatsBucket createInstance(String name, long count, double sum, double min, double max,
38+
DocValueFormat formatter, List<PipelineAggregator> pipelineAggregators, Map<String, Object> metaData) {
39+
return new InternalStatsBucket(name, count, sum, min, max, formatter, pipelineAggregators, metaData);
40+
}
41+
42+
@Override
43+
public void testReduceRandom() {
44+
expectThrows(UnsupportedOperationException.class,
45+
() -> createTestInstance("name", Collections.emptyList(), null).reduce(null, null));
46+
}
47+
48+
@Override
49+
protected void assertReduced(InternalStats reduced, List<InternalStats> inputs) {
50+
// no test since reduce operation is unsupported
51+
}
52+
53+
@Override
54+
protected Writeable.Reader<InternalStats> instanceReader() {
55+
return InternalStatsBucket::new;
56+
}
57+
58+
@Override
59+
protected void assertFromXContent(InternalStats aggregation, ParsedAggregation parsedAggregation) {
60+
super.assertFromXContent(aggregation, parsedAggregation);
61+
assertTrue(parsedAggregation instanceof ParsedStatsBucket);
62+
}
63+
}

core/src/test/java/org/elasticsearch/search/aggregations/metrics/InternalStatsTests.java

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,12 @@ protected InternalStats createTestInstance(String name, List<PipelineAggregator>
3838
double max = randomDoubleBetween(-1000000, 1000000, true);
3939
double sum = randomDoubleBetween(-1000000, 1000000, true);
4040
DocValueFormat format = randomNumericDocValueFormat();
41-
return new InternalStats(name, count, sum, min, max, format, pipelineAggregators, metaData);
41+
return createInstance(name, count, sum, min, max, format, pipelineAggregators, metaData);
42+
}
43+
44+
protected InternalStats createInstance(String name, long count, double sum, double min, double max, DocValueFormat formatter,
45+
List<PipelineAggregator> pipelineAggregators, Map<String, Object> metaData) {
46+
return new InternalStats(name, count, sum, min, max, formatter, pipelineAggregators, metaData);
4247
}
4348

4449
@Override

core/src/test/java/org/elasticsearch/search/aggregations/pipeline/bucketmetrics/stats/extended/InternalExtendedStatsBucketTests.java

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@
2121

2222
import org.elasticsearch.common.io.stream.Writeable;
2323
import org.elasticsearch.search.DocValueFormat;
24+
import org.elasticsearch.search.aggregations.ParsedAggregation;
2425
import org.elasticsearch.search.aggregations.metrics.InternalExtendedStatsTests;
2526
import org.elasticsearch.search.aggregations.metrics.stats.extended.InternalExtendedStats;
2627
import org.elasticsearch.search.aggregations.pipeline.PipelineAggregator;
@@ -53,4 +54,10 @@ protected void assertReduced(InternalExtendedStats reduced, List<InternalExtende
5354
protected Writeable.Reader<InternalExtendedStats> instanceReader() {
5455
return InternalExtendedStatsBucket::new;
5556
}
57+
58+
@Override
59+
protected void assertFromXContent(InternalExtendedStats aggregation, ParsedAggregation parsedAggregation) {
60+
super.assertFromXContent(aggregation, parsedAggregation);
61+
assertTrue(parsedAggregation instanceof ParsedExtendedStatsBucket);
62+
}
5663
}

0 commit comments

Comments
 (0)