Skip to content

Commit c32f187

Browse files
committed
Aggregations Refactor: Refactor Cumulative Sum Aggregation
1 parent 7c84e39 commit c32f187

File tree

3 files changed

+103
-14
lines changed

3 files changed

+103
-14
lines changed

core/src/main/java/org/elasticsearch/search/aggregations/pipeline/cumulativesum/CumulativeSumParser.java

Lines changed: 4 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -24,8 +24,6 @@
2424
import org.elasticsearch.search.SearchParseException;
2525
import org.elasticsearch.search.aggregations.pipeline.PipelineAggregator;
2626
import org.elasticsearch.search.aggregations.pipeline.PipelineAggregatorFactory;
27-
import org.elasticsearch.search.aggregations.support.format.ValueFormat;
28-
import org.elasticsearch.search.aggregations.support.format.ValueFormatter;
2927
import org.elasticsearch.search.internal.SearchContext;
3028

3129
import java.io.IOException;
@@ -85,20 +83,16 @@ public PipelineAggregatorFactory parse(String pipelineAggregatorName, XContentPa
8583
+ "] for derivative aggregation [" + pipelineAggregatorName + "]", parser.getTokenLocation());
8684
}
8785

88-
ValueFormatter formatter = null;
86+
CumulativeSumPipelineAggregator.Factory factory = new CumulativeSumPipelineAggregator.Factory(pipelineAggregatorName, bucketsPaths);
8987
if (format != null) {
90-
formatter = ValueFormat.Patternable.Number.format(format).formatter();
91-
} else {
92-
formatter = ValueFormatter.RAW;
88+
factory.format(format);
9389
}
94-
95-
return new CumulativeSumPipelineAggregator.Factory(pipelineAggregatorName, bucketsPaths, formatter);
90+
return factory;
9691
}
9792

98-
// NORELEASE implement this method when refactoring this aggregation
9993
@Override
10094
public PipelineAggregatorFactory getFactoryPrototype() {
101-
return null;
95+
return new CumulativeSumPipelineAggregator.Factory(null, null);
10296
}
10397

10498
}

core/src/main/java/org/elasticsearch/search/aggregations/pipeline/cumulativesum/CumulativeSumPipelineAggregator.java

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

2222
import org.elasticsearch.common.io.stream.StreamInput;
2323
import org.elasticsearch.common.io.stream.StreamOutput;
24+
import org.elasticsearch.common.xcontent.XContentBuilder;
2425
import org.elasticsearch.search.aggregations.AggregatorFactory;
2526
import org.elasticsearch.search.aggregations.InternalAggregation;
2627
import org.elasticsearch.search.aggregations.InternalAggregation.ReduceContext;
@@ -33,13 +34,16 @@
3334
import org.elasticsearch.search.aggregations.pipeline.PipelineAggregator;
3435
import org.elasticsearch.search.aggregations.pipeline.PipelineAggregatorFactory;
3536
import org.elasticsearch.search.aggregations.pipeline.PipelineAggregatorStreams;
37+
import org.elasticsearch.search.aggregations.pipeline.bucketmetrics.BucketMetricsParser;
38+
import org.elasticsearch.search.aggregations.support.format.ValueFormat;
3639
import org.elasticsearch.search.aggregations.support.format.ValueFormatter;
3740
import org.elasticsearch.search.aggregations.support.format.ValueFormatterStreams;
3841

3942
import java.io.IOException;
4043
import java.util.ArrayList;
4144
import java.util.List;
4245
import java.util.Map;
46+
import java.util.Objects;
4347
import java.util.stream.Collectors;
4448
import java.util.stream.StreamSupport;
4549

@@ -109,16 +113,37 @@ public void doWriteTo(StreamOutput out) throws IOException {
109113

110114
public static class Factory extends PipelineAggregatorFactory {
111115

112-
private final ValueFormatter formatter;
116+
private String format;
113117

114-
public Factory(String name, String[] bucketsPaths, ValueFormatter formatter) {
118+
public Factory(String name, String[] bucketsPaths) {
115119
super(name, TYPE.name(), bucketsPaths);
116-
this.formatter = formatter;
120+
}
121+
122+
/**
123+
* Sets the format to use on the output of this aggregation.
124+
*/
125+
public void format(String format) {
126+
this.format = format;
127+
}
128+
129+
/**
130+
* Gets the format to use on the output of this aggregation.
131+
*/
132+
public String format() {
133+
return format;
134+
}
135+
136+
protected ValueFormatter formatter() {
137+
if (format != null) {
138+
return ValueFormat.Patternable.Number.format(format).formatter();
139+
} else {
140+
return ValueFormatter.RAW;
141+
}
117142
}
118143

119144
@Override
120145
protected PipelineAggregator createInternal(Map<String, Object> metaData) throws IOException {
121-
return new CumulativeSumPipelineAggregator(name, bucketsPaths, formatter, metaData);
146+
return new CumulativeSumPipelineAggregator(name, bucketsPaths, formatter(), metaData);
122147
}
123148

124149
@Override
@@ -139,5 +164,35 @@ public void doValidate(AggregatorFactory parent, AggregatorFactory[] aggFactorie
139164
}
140165
}
141166

167+
@Override
168+
protected final XContentBuilder internalXContent(XContentBuilder builder, Params params) throws IOException {
169+
if (format != null) {
170+
builder.field(BucketMetricsParser.FORMAT.getPreferredName(), format);
171+
}
172+
return builder;
173+
}
174+
175+
@Override
176+
protected final PipelineAggregatorFactory doReadFrom(String name, String[] bucketsPaths, StreamInput in) throws IOException {
177+
Factory factory = new Factory(name, bucketsPaths);
178+
factory.format = in.readOptionalString();
179+
return factory;
180+
}
181+
182+
@Override
183+
protected final void doWriteTo(StreamOutput out) throws IOException {
184+
out.writeOptionalString(format);
185+
}
186+
187+
@Override
188+
protected int doHashCode() {
189+
return Objects.hash(format);
190+
}
191+
192+
@Override
193+
protected boolean doEquals(Object obj) {
194+
Factory other = (Factory) obj;
195+
return Objects.equals(format, other.format);
196+
}
142197
}
143198
}
Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
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;
21+
22+
import org.elasticsearch.search.aggregations.BasePipelineAggregationTestCase;
23+
import org.elasticsearch.search.aggregations.pipeline.cumulativesum.CumulativeSumPipelineAggregator;
24+
import org.elasticsearch.search.aggregations.pipeline.cumulativesum.CumulativeSumPipelineAggregator.Factory;
25+
26+
public class CumulativeSumTests extends BasePipelineAggregationTestCase<CumulativeSumPipelineAggregator.Factory> {
27+
28+
@Override
29+
protected Factory createTestAggregatorFactory() {
30+
String name = randomAsciiOfLengthBetween(3, 20);
31+
String[] bucketsPaths = new String[1];
32+
bucketsPaths[0] = randomAsciiOfLengthBetween(3, 20);
33+
Factory factory = new Factory(name, bucketsPaths);
34+
if (randomBoolean()) {
35+
factory.format(randomAsciiOfLengthBetween(1, 10));
36+
}
37+
return factory;
38+
}
39+
40+
}

0 commit comments

Comments
 (0)