Skip to content

Commit 46d5d68

Browse files
authored
Add version and create_time to data frame analytics config (#43683)
1 parent 66e1853 commit 46d5d68

File tree

11 files changed

+335
-23
lines changed

11 files changed

+335
-23
lines changed

client/rest-high-level/src/main/java/org/elasticsearch/client/ml/PutDataFrameAnalyticsRequest.java

+6
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@
2222
import org.elasticsearch.client.Validatable;
2323
import org.elasticsearch.client.ValidationException;
2424
import org.elasticsearch.client.ml.dataframe.DataFrameAnalyticsConfig;
25+
import org.elasticsearch.common.Strings;
2526
import org.elasticsearch.common.xcontent.ToXContentObject;
2627
import org.elasticsearch.common.xcontent.XContentBuilder;
2728

@@ -67,4 +68,9 @@ public boolean equals(Object o) {
6768
public int hashCode() {
6869
return Objects.hash(config);
6970
}
71+
72+
@Override
73+
public String toString() {
74+
return Strings.toString(this);
75+
}
7076
}

client/rest-high-level/src/main/java/org/elasticsearch/client/ml/dataframe/DataFrameAnalyticsConfig.java

+60-9
Original file line numberDiff line numberDiff line change
@@ -19,23 +19,24 @@
1919

2020
package org.elasticsearch.client.ml.dataframe;
2121

22+
import org.elasticsearch.Version;
23+
import org.elasticsearch.client.dataframe.transforms.util.TimeUtil;
2224
import org.elasticsearch.common.Nullable;
2325
import org.elasticsearch.common.ParseField;
2426
import org.elasticsearch.common.Strings;
2527
import org.elasticsearch.common.unit.ByteSizeValue;
2628
import org.elasticsearch.common.xcontent.ObjectParser;
29+
import org.elasticsearch.common.xcontent.ObjectParser.ValueType;
2730
import org.elasticsearch.common.xcontent.ToXContentObject;
2831
import org.elasticsearch.common.xcontent.XContentBuilder;
2932
import org.elasticsearch.common.xcontent.XContentParser;
3033
import org.elasticsearch.common.xcontent.XContentParserUtils;
3134
import org.elasticsearch.search.fetch.subphase.FetchSourceContext;
3235

3336
import java.io.IOException;
37+
import java.time.Instant;
3438
import java.util.Objects;
3539

36-
import static org.elasticsearch.common.xcontent.ObjectParser.ValueType.OBJECT_ARRAY_BOOLEAN_OR_STRING;
37-
import static org.elasticsearch.common.xcontent.ObjectParser.ValueType.VALUE;
38-
3940
public class DataFrameAnalyticsConfig implements ToXContentObject {
4041

4142
public static DataFrameAnalyticsConfig fromXContent(XContentParser parser) {
@@ -52,6 +53,8 @@ public static Builder builder(String id) {
5253
private static final ParseField ANALYSIS = new ParseField("analysis");
5354
private static final ParseField ANALYZED_FIELDS = new ParseField("analyzed_fields");
5455
private static final ParseField MODEL_MEMORY_LIMIT = new ParseField("model_memory_limit");
56+
private static final ParseField CREATE_TIME = new ParseField("create_time");
57+
private static final ParseField VERSION = new ParseField("version");
5558

5659
private static ObjectParser<Builder, Void> PARSER = new ObjectParser<>("data_frame_analytics_config", true, Builder::new);
5760

@@ -63,9 +66,24 @@ public static Builder builder(String id) {
6366
PARSER.declareField(Builder::setAnalyzedFields,
6467
(p, c) -> FetchSourceContext.fromXContent(p),
6568
ANALYZED_FIELDS,
66-
OBJECT_ARRAY_BOOLEAN_OR_STRING);
69+
ValueType.OBJECT_ARRAY_BOOLEAN_OR_STRING);
6770
PARSER.declareField(Builder::setModelMemoryLimit,
68-
(p, c) -> ByteSizeValue.parseBytesSizeValue(p.text(), MODEL_MEMORY_LIMIT.getPreferredName()), MODEL_MEMORY_LIMIT, VALUE);
71+
(p, c) -> ByteSizeValue.parseBytesSizeValue(p.text(), MODEL_MEMORY_LIMIT.getPreferredName()),
72+
MODEL_MEMORY_LIMIT,
73+
ValueType.VALUE);
74+
PARSER.declareField(Builder::setCreateTime,
75+
p -> TimeUtil.parseTimeFieldToInstant(p, CREATE_TIME.getPreferredName()),
76+
CREATE_TIME,
77+
ValueType.VALUE);
78+
PARSER.declareField(Builder::setVersion,
79+
p -> {
80+
if (p.currentToken() == XContentParser.Token.VALUE_STRING) {
81+
return Version.fromString(p.text());
82+
}
83+
throw new IllegalArgumentException("Unsupported token [" + p.currentToken() + "]");
84+
},
85+
VERSION,
86+
ValueType.STRING);
6987
}
7088

7189
private static DataFrameAnalysis parseAnalysis(XContentParser parser) throws IOException {
@@ -82,15 +100,20 @@ private static DataFrameAnalysis parseAnalysis(XContentParser parser) throws IOE
82100
private final DataFrameAnalysis analysis;
83101
private final FetchSourceContext analyzedFields;
84102
private final ByteSizeValue modelMemoryLimit;
103+
private final Instant createTime;
104+
private final Version version;
85105

86106
private DataFrameAnalyticsConfig(String id, DataFrameAnalyticsSource source, DataFrameAnalyticsDest dest, DataFrameAnalysis analysis,
87-
@Nullable FetchSourceContext analyzedFields, @Nullable ByteSizeValue modelMemoryLimit) {
107+
@Nullable FetchSourceContext analyzedFields, @Nullable ByteSizeValue modelMemoryLimit,
108+
@Nullable Instant createTime, @Nullable Version version) {
88109
this.id = Objects.requireNonNull(id);
89110
this.source = Objects.requireNonNull(source);
90111
this.dest = Objects.requireNonNull(dest);
91112
this.analysis = Objects.requireNonNull(analysis);
92113
this.analyzedFields = analyzedFields;
93114
this.modelMemoryLimit = modelMemoryLimit;
115+
this.createTime = createTime == null ? null : Instant.ofEpochMilli(createTime.toEpochMilli());;
116+
this.version = version;
94117
}
95118

96119
public String getId() {
@@ -117,6 +140,14 @@ public ByteSizeValue getModelMemoryLimit() {
117140
return modelMemoryLimit;
118141
}
119142

143+
public Instant getCreateTime() {
144+
return createTime;
145+
}
146+
147+
public Version getVersion() {
148+
return version;
149+
}
150+
120151
@Override
121152
public XContentBuilder toXContent(XContentBuilder builder, Params params) throws IOException {
122153
builder.startObject();
@@ -132,6 +163,12 @@ public XContentBuilder toXContent(XContentBuilder builder, Params params) throws
132163
if (modelMemoryLimit != null) {
133164
builder.field(MODEL_MEMORY_LIMIT.getPreferredName(), modelMemoryLimit.getStringRep());
134165
}
166+
if (createTime != null) {
167+
builder.timeField(CREATE_TIME.getPreferredName(), CREATE_TIME.getPreferredName() + "_string", createTime.toEpochMilli());
168+
}
169+
if (version != null) {
170+
builder.field(VERSION.getPreferredName(), version);
171+
}
135172
builder.endObject();
136173
return builder;
137174
}
@@ -147,12 +184,14 @@ public boolean equals(Object o) {
147184
&& Objects.equals(dest, other.dest)
148185
&& Objects.equals(analysis, other.analysis)
149186
&& Objects.equals(analyzedFields, other.analyzedFields)
150-
&& Objects.equals(modelMemoryLimit, other.modelMemoryLimit);
187+
&& Objects.equals(modelMemoryLimit, other.modelMemoryLimit)
188+
&& Objects.equals(createTime, other.createTime)
189+
&& Objects.equals(version, other.version);
151190
}
152191

153192
@Override
154193
public int hashCode() {
155-
return Objects.hash(id, source, dest, analysis, analyzedFields, getModelMemoryLimit());
194+
return Objects.hash(id, source, dest, analysis, analyzedFields, modelMemoryLimit, createTime, version);
156195
}
157196

158197
@Override
@@ -168,6 +207,8 @@ public static class Builder {
168207
private DataFrameAnalysis analysis;
169208
private FetchSourceContext analyzedFields;
170209
private ByteSizeValue modelMemoryLimit;
210+
private Instant createTime;
211+
private Version version;
171212

172213
private Builder() {}
173214

@@ -201,8 +242,18 @@ public Builder setModelMemoryLimit(ByteSizeValue modelMemoryLimit) {
201242
return this;
202243
}
203244

245+
public Builder setCreateTime(Instant createTime) {
246+
this.createTime = createTime;
247+
return this;
248+
}
249+
250+
public Builder setVersion(Version version) {
251+
this.version = version;
252+
return this;
253+
}
254+
204255
public DataFrameAnalyticsConfig build() {
205-
return new DataFrameAnalyticsConfig(id, source, dest, analysis, analyzedFields, modelMemoryLimit);
256+
return new DataFrameAnalyticsConfig(id, source, dest, analysis, analyzedFields, modelMemoryLimit, createTime, version);
206257
}
207258
}
208259
}

client/rest-high-level/src/test/java/org/elasticsearch/client/ml/dataframe/DataFrameAnalyticsConfigTests.java

+8
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919

2020
package org.elasticsearch.client.ml.dataframe;
2121

22+
import org.elasticsearch.Version;
2223
import org.elasticsearch.common.settings.Settings;
2324
import org.elasticsearch.common.unit.ByteSizeUnit;
2425
import org.elasticsearch.common.unit.ByteSizeValue;
@@ -29,6 +30,7 @@
2930
import org.elasticsearch.test.AbstractXContentTestCase;
3031

3132
import java.io.IOException;
33+
import java.time.Instant;
3234
import java.util.ArrayList;
3335
import java.util.Collections;
3436
import java.util.List;
@@ -54,6 +56,12 @@ public static DataFrameAnalyticsConfig randomDataFrameAnalyticsConfig() {
5456
if (randomBoolean()) {
5557
builder.setModelMemoryLimit(new ByteSizeValue(randomIntBetween(1, 16), randomFrom(ByteSizeUnit.MB, ByteSizeUnit.GB)));
5658
}
59+
if (randomBoolean()) {
60+
builder.setCreateTime(Instant.now());
61+
}
62+
if (randomBoolean()) {
63+
builder.setVersion(Version.CURRENT);
64+
}
5765
return builder.build();
5866
}
5967

0 commit comments

Comments
 (0)