Skip to content

Commit 937dcfd

Browse files
authored
[Rollup] Remove builders from MetricConfig (#32536)
Related to #29827
1 parent f809d6f commit 937dcfd

File tree

10 files changed

+155
-253
lines changed

10 files changed

+155
-253
lines changed

x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/rollup/job/MetricConfig.java

Lines changed: 52 additions & 74 deletions
Original file line numberDiff line numberDiff line change
@@ -12,9 +12,10 @@
1212
import org.elasticsearch.common.io.stream.StreamInput;
1313
import org.elasticsearch.common.io.stream.StreamOutput;
1414
import org.elasticsearch.common.io.stream.Writeable;
15-
import org.elasticsearch.common.xcontent.ObjectParser;
16-
import org.elasticsearch.common.xcontent.ToXContentFragment;
15+
import org.elasticsearch.common.xcontent.ConstructingObjectParser;
16+
import org.elasticsearch.common.xcontent.ToXContentObject;
1717
import org.elasticsearch.common.xcontent.XContentBuilder;
18+
import org.elasticsearch.common.xcontent.XContentParser;
1819
import org.elasticsearch.search.aggregations.metrics.avg.AvgAggregationBuilder;
1920
import org.elasticsearch.search.aggregations.metrics.max.MaxAggregationBuilder;
2021
import org.elasticsearch.search.aggregations.metrics.min.MinAggregationBuilder;
@@ -32,6 +33,8 @@
3233
import java.util.Objects;
3334
import java.util.stream.Collectors;
3435

36+
import static org.elasticsearch.common.xcontent.ConstructingObjectParser.constructorArg;
37+
3538
/**
3639
* The configuration object for the metrics portion of a rollup job config
3740
*
@@ -48,14 +51,7 @@
4851
* ]
4952
* }
5053
*/
51-
public class MetricConfig implements Writeable, ToXContentFragment {
52-
private static final String NAME = "metric_config";
53-
54-
private String field;
55-
private List<String> metrics;
56-
57-
private static final ParseField FIELD = new ParseField("field");
58-
private static final ParseField METRICS = new ParseField("metrics");
54+
public class MetricConfig implements Writeable, ToXContentObject {
5955

6056
// TODO: replace these with an enum
6157
private static final ParseField MIN = new ParseField("min");
@@ -64,27 +60,54 @@ public class MetricConfig implements Writeable, ToXContentFragment {
6460
private static final ParseField AVG = new ParseField("avg");
6561
private static final ParseField VALUE_COUNT = new ParseField("value_count");
6662

67-
public static final ObjectParser<MetricConfig.Builder, Void> PARSER = new ObjectParser<>(NAME, MetricConfig.Builder::new);
68-
63+
private static final String NAME = "metrics";
64+
private static final String FIELD = "field";
65+
private static final String METRICS = "metrics";
66+
private static final ConstructingObjectParser<MetricConfig, Void> PARSER;
6967
static {
70-
PARSER.declareString(MetricConfig.Builder::setField, FIELD);
71-
PARSER.declareStringArray(MetricConfig.Builder::setMetrics, METRICS);
68+
PARSER = new ConstructingObjectParser<>(NAME, args -> {
69+
@SuppressWarnings("unchecked") List<String> metrics = (List<String>) args[1];
70+
return new MetricConfig((String) args[0], metrics);
71+
});
72+
PARSER.declareString(constructorArg(), new ParseField(FIELD));
73+
PARSER.declareStringArray(constructorArg(), new ParseField(METRICS));
7274
}
7375

74-
MetricConfig(String name, List<String> metrics) {
75-
this.field = name;
76+
private final String field;
77+
private final List<String> metrics;
78+
79+
public MetricConfig(final String field, final List<String> metrics) {
80+
if (field == null || field.isEmpty()) {
81+
throw new IllegalArgumentException("Field must be a non-null, non-empty string");
82+
}
83+
if (metrics == null || metrics.isEmpty()) {
84+
throw new IllegalArgumentException("Metrics must be a non-null, non-empty array of strings");
85+
}
86+
metrics.forEach(m -> {
87+
if (RollupField.SUPPORTED_METRICS.contains(m) == false) {
88+
throw new IllegalArgumentException("Unsupported metric [" + m + "]. " +
89+
"Supported metrics include: " + RollupField.SUPPORTED_METRICS);
90+
}
91+
});
92+
this.field = field;
7693
this.metrics = metrics;
7794
}
7895

79-
MetricConfig(StreamInput in) throws IOException {
96+
MetricConfig(final StreamInput in) throws IOException {
8097
field = in.readString();
8198
metrics = in.readList(StreamInput::readString);
8299
}
83100

101+
/**
102+
* @return the name of the field used in the metric configuration. Never {@code null}.
103+
*/
84104
public String getField() {
85105
return field;
86106
}
87107

108+
/**
109+
* @return the names of the metrics used in the metric configuration. Never {@code null}.
110+
*/
88111
public List<String> getMetrics() {
89112
return metrics;
90113
}
@@ -159,10 +182,13 @@ public void validateMappings(Map<String, Map<String, FieldCapabilities>> fieldCa
159182
}
160183

161184
@Override
162-
public XContentBuilder toXContent(XContentBuilder builder, Params params) throws IOException {
163-
builder.field(FIELD.getPreferredName(), field);
164-
builder.field(METRICS.getPreferredName(), metrics);
165-
return builder;
185+
public XContentBuilder toXContent(final XContentBuilder builder, final Params params) throws IOException {
186+
builder.startObject();
187+
{
188+
builder.field(FIELD, field);
189+
builder.field(METRICS, metrics);
190+
}
191+
return builder.endObject();
166192
}
167193

168194
@Override
@@ -172,19 +198,16 @@ public void writeTo(StreamOutput out) throws IOException {
172198
}
173199

174200
@Override
175-
public boolean equals(Object other) {
201+
public boolean equals(final Object other) {
176202
if (this == other) {
177203
return true;
178204
}
179-
180205
if (other == null || getClass() != other.getClass()) {
181206
return false;
182207
}
183208

184-
MetricConfig that = (MetricConfig) other;
185-
186-
return Objects.equals(this.field, that.field)
187-
&& Objects.equals(this.metrics, that.metrics);
209+
final MetricConfig that = (MetricConfig) other;
210+
return Objects.equals(field, that.field) && Objects.equals(metrics, that.metrics);
188211
}
189212

190213
@Override
@@ -197,52 +220,7 @@ public String toString() {
197220
return Strings.toString(this, true, true);
198221
}
199222

200-
201-
public static class Builder {
202-
private String field;
203-
private List<String> metrics;
204-
205-
public Builder() {
206-
}
207-
208-
public Builder(MetricConfig config) {
209-
this.field = config.getField();
210-
this.metrics = config.getMetrics();
211-
}
212-
213-
public String getField() {
214-
return field;
215-
}
216-
217-
public MetricConfig.Builder setField(String field) {
218-
this.field = field;
219-
return this;
220-
}
221-
222-
public List<String> getMetrics() {
223-
return metrics;
224-
}
225-
226-
public MetricConfig.Builder setMetrics(List<String> metrics) {
227-
this.metrics = metrics;
228-
return this;
229-
}
230-
231-
public MetricConfig build() {
232-
if (Strings.isNullOrEmpty(field) == true) {
233-
throw new IllegalArgumentException("Parameter [" + FIELD.getPreferredName() + "] must be a non-null, non-empty string.");
234-
}
235-
if (metrics == null || metrics.isEmpty()) {
236-
throw new IllegalArgumentException("Parameter [" + METRICS.getPreferredName()
237-
+ "] must be a non-null, non-empty array of strings.");
238-
}
239-
metrics.forEach(m -> {
240-
if (RollupField.SUPPORTED_METRICS.contains(m) == false) {
241-
throw new IllegalArgumentException("Unsupported metric [" + m + "]. " +
242-
"Supported metrics include: " + RollupField.SUPPORTED_METRICS);
243-
}
244-
});
245-
return new MetricConfig(field, metrics);
246-
}
223+
public static MetricConfig fromXContent(final XContentParser parser) throws IOException {
224+
return PARSER.parse(parser, null);
247225
}
248226
}

x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/rollup/job/RollupJobConfig.java

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -63,7 +63,7 @@ public class RollupJobConfig implements NamedWriteable, ToXContentObject {
6363
static {
6464
PARSER.declareString(RollupJobConfig.Builder::setId, RollupField.ID);
6565
PARSER.declareObject(RollupJobConfig.Builder::setGroupConfig, (p, c) -> GroupConfig.PARSER.apply(p,c).build(), GROUPS);
66-
PARSER.declareObjectArray(RollupJobConfig.Builder::setMetricsConfig, (p, c) -> MetricConfig.PARSER.apply(p, c).build(), METRICS);
66+
PARSER.declareObjectArray(RollupJobConfig.Builder::setMetricsConfig, (p, c) -> MetricConfig.fromXContent(p), METRICS);
6767
PARSER.declareString((params, val) ->
6868
params.setTimeout(TimeValue.parseTimeValue(val, TIMEOUT.getPreferredName())), TIMEOUT);
6969
PARSER.declareString(RollupJobConfig.Builder::setIndexPattern, INDEX_PATTERN);
@@ -160,10 +160,8 @@ public XContentBuilder toXContent(XContentBuilder builder, ToXContent.Params par
160160
}
161161
if (metricsConfig != null) {
162162
builder.startArray(METRICS.getPreferredName());
163-
for (MetricConfig config : metricsConfig) {
164-
builder.startObject();
165-
config.toXContent(builder, params);
166-
builder.endObject();
163+
for (MetricConfig metric : metricsConfig) {
164+
metric.toXContent(builder, params);
167165
}
168166
builder.endArray();
169167
}

x-pack/plugin/core/src/test/java/org/elasticsearch/xpack/core/rollup/ConfigTestHelpers.java

Lines changed: 35 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
import org.elasticsearch.xpack.core.rollup.job.TermsGroupConfig;
1818

1919
import java.util.ArrayList;
20+
import java.util.Collections;
2021
import java.util.List;
2122
import java.util.Random;
2223
import java.util.stream.Collectors;
@@ -38,11 +39,7 @@ public static RollupJobConfig.Builder getRollupJob(String jobId) {
3839
builder.setGroupConfig(ConfigTestHelpers.getGroupConfig().build());
3940
builder.setPageSize(ESTestCase.randomIntBetween(1,10));
4041
if (ESTestCase.randomBoolean()) {
41-
List<MetricConfig> metrics = IntStream.range(1, ESTestCase.randomIntBetween(1,10))
42-
.mapToObj(n -> ConfigTestHelpers.getMetricConfig().build())
43-
.collect(Collectors.toList());
44-
45-
builder.setMetricsConfig(metrics);
42+
builder.setMetricsConfig(randomMetricsConfigs(ESTestCase.random()));
4643
}
4744
return builder;
4845
}
@@ -59,32 +56,6 @@ public static GroupConfig.Builder getGroupConfig() {
5956
return groupBuilder;
6057
}
6158

62-
public static MetricConfig.Builder getMetricConfig() {
63-
MetricConfig.Builder builder = new MetricConfig.Builder();
64-
builder.setField(ESTestCase.randomAlphaOfLength(15)); // large names so we don't accidentally collide
65-
List<String> metrics = new ArrayList<>();
66-
if (ESTestCase.randomBoolean()) {
67-
metrics.add("min");
68-
}
69-
if (ESTestCase.randomBoolean()) {
70-
metrics.add("max");
71-
}
72-
if (ESTestCase.randomBoolean()) {
73-
metrics.add("sum");
74-
}
75-
if (ESTestCase.randomBoolean()) {
76-
metrics.add("avg");
77-
}
78-
if (ESTestCase.randomBoolean()) {
79-
metrics.add("value_count");
80-
}
81-
if (metrics.size() == 0) {
82-
metrics.add("min");
83-
}
84-
builder.setMetrics(metrics);
85-
return builder;
86-
}
87-
8859
private static final String[] TIME_SUFFIXES = new String[]{"d", "h", "ms", "s", "m"};
8960
public static String randomPositiveTimeValue() {
9061
return ESTestCase.randomIntBetween(1, 1000) + ESTestCase.randomFrom(TIME_SUFFIXES);
@@ -123,6 +94,39 @@ public static HistogramGroupConfig randomHistogramGroupConfig(final Random rando
12394
return new HistogramGroupConfig(randomInterval(random), randomFields(random));
12495
}
12596

97+
public static List<MetricConfig> randomMetricsConfigs(final Random random) {
98+
final int numMetrics = randomIntBetween(random, 1, 10);
99+
final List<MetricConfig> metrics = new ArrayList<>(numMetrics);
100+
for (int i = 0; i < numMetrics; i++) {
101+
metrics.add(randomMetricConfig(random));
102+
}
103+
return Collections.unmodifiableList(metrics);
104+
}
105+
106+
public static MetricConfig randomMetricConfig(final Random random) {
107+
final String field = randomAsciiAlphanumOfLengthBetween(random, 15, 25); // large names so we don't accidentally collide
108+
final List<String> metrics = new ArrayList<>();
109+
if (random.nextBoolean()) {
110+
metrics.add("min");
111+
}
112+
if (random.nextBoolean()) {
113+
metrics.add("max");
114+
}
115+
if (random.nextBoolean()) {
116+
metrics.add("sum");
117+
}
118+
if (random.nextBoolean()) {
119+
metrics.add("avg");
120+
}
121+
if (random.nextBoolean()) {
122+
metrics.add("value_count");
123+
}
124+
if (metrics.size() == 0) {
125+
metrics.add("min");
126+
}
127+
return new MetricConfig(field, Collections.unmodifiableList(metrics));
128+
}
129+
126130
public static TermsGroupConfig randomTermsGroupConfig(final Random random) {
127131
return new TermsGroupConfig(randomFields(random));
128132
}

0 commit comments

Comments
 (0)