Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[7.x] Add version and create_time to data frame analytics config (#43683) #43712

Merged
merged 1 commit into from
Jun 28, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
import org.elasticsearch.client.Validatable;
import org.elasticsearch.client.ValidationException;
import org.elasticsearch.client.ml.dataframe.DataFrameAnalyticsConfig;
import org.elasticsearch.common.Strings;
import org.elasticsearch.common.xcontent.ToXContentObject;
import org.elasticsearch.common.xcontent.XContentBuilder;

Expand Down Expand Up @@ -67,4 +68,9 @@ public boolean equals(Object o) {
public int hashCode() {
return Objects.hash(config);
}

@Override
public String toString() {
return Strings.toString(this);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -19,23 +19,24 @@

package org.elasticsearch.client.ml.dataframe;

import org.elasticsearch.Version;
import org.elasticsearch.client.dataframe.transforms.util.TimeUtil;
import org.elasticsearch.common.Nullable;
import org.elasticsearch.common.ParseField;
import org.elasticsearch.common.Strings;
import org.elasticsearch.common.unit.ByteSizeValue;
import org.elasticsearch.common.xcontent.ObjectParser;
import org.elasticsearch.common.xcontent.ObjectParser.ValueType;
import org.elasticsearch.common.xcontent.ToXContentObject;
import org.elasticsearch.common.xcontent.XContentBuilder;
import org.elasticsearch.common.xcontent.XContentParser;
import org.elasticsearch.common.xcontent.XContentParserUtils;
import org.elasticsearch.search.fetch.subphase.FetchSourceContext;

import java.io.IOException;
import java.time.Instant;
import java.util.Objects;

import static org.elasticsearch.common.xcontent.ObjectParser.ValueType.OBJECT_ARRAY_BOOLEAN_OR_STRING;
import static org.elasticsearch.common.xcontent.ObjectParser.ValueType.VALUE;

public class DataFrameAnalyticsConfig implements ToXContentObject {

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

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

Expand All @@ -63,9 +66,24 @@ public static Builder builder(String id) {
PARSER.declareField(Builder::setAnalyzedFields,
(p, c) -> FetchSourceContext.fromXContent(p),
ANALYZED_FIELDS,
OBJECT_ARRAY_BOOLEAN_OR_STRING);
ValueType.OBJECT_ARRAY_BOOLEAN_OR_STRING);
PARSER.declareField(Builder::setModelMemoryLimit,
(p, c) -> ByteSizeValue.parseBytesSizeValue(p.text(), MODEL_MEMORY_LIMIT.getPreferredName()), MODEL_MEMORY_LIMIT, VALUE);
(p, c) -> ByteSizeValue.parseBytesSizeValue(p.text(), MODEL_MEMORY_LIMIT.getPreferredName()),
MODEL_MEMORY_LIMIT,
ValueType.VALUE);
PARSER.declareField(Builder::setCreateTime,
p -> TimeUtil.parseTimeFieldToInstant(p, CREATE_TIME.getPreferredName()),
CREATE_TIME,
ValueType.VALUE);
PARSER.declareField(Builder::setVersion,
p -> {
if (p.currentToken() == XContentParser.Token.VALUE_STRING) {
return Version.fromString(p.text());
}
throw new IllegalArgumentException("Unsupported token [" + p.currentToken() + "]");
},
VERSION,
ValueType.STRING);
}

private static DataFrameAnalysis parseAnalysis(XContentParser parser) throws IOException {
Expand All @@ -82,15 +100,20 @@ private static DataFrameAnalysis parseAnalysis(XContentParser parser) throws IOE
private final DataFrameAnalysis analysis;
private final FetchSourceContext analyzedFields;
private final ByteSizeValue modelMemoryLimit;
private final Instant createTime;
private final Version version;

private DataFrameAnalyticsConfig(String id, DataFrameAnalyticsSource source, DataFrameAnalyticsDest dest, DataFrameAnalysis analysis,
@Nullable FetchSourceContext analyzedFields, @Nullable ByteSizeValue modelMemoryLimit) {
@Nullable FetchSourceContext analyzedFields, @Nullable ByteSizeValue modelMemoryLimit,
@Nullable Instant createTime, @Nullable Version version) {
this.id = Objects.requireNonNull(id);
this.source = Objects.requireNonNull(source);
this.dest = Objects.requireNonNull(dest);
this.analysis = Objects.requireNonNull(analysis);
this.analyzedFields = analyzedFields;
this.modelMemoryLimit = modelMemoryLimit;
this.createTime = createTime == null ? null : Instant.ofEpochMilli(createTime.toEpochMilli());;
this.version = version;
}

public String getId() {
Expand All @@ -117,6 +140,14 @@ public ByteSizeValue getModelMemoryLimit() {
return modelMemoryLimit;
}

public Instant getCreateTime() {
return createTime;
}

public Version getVersion() {
return version;
}

@Override
public XContentBuilder toXContent(XContentBuilder builder, Params params) throws IOException {
builder.startObject();
Expand All @@ -132,6 +163,12 @@ public XContentBuilder toXContent(XContentBuilder builder, Params params) throws
if (modelMemoryLimit != null) {
builder.field(MODEL_MEMORY_LIMIT.getPreferredName(), modelMemoryLimit.getStringRep());
}
if (createTime != null) {
builder.timeField(CREATE_TIME.getPreferredName(), CREATE_TIME.getPreferredName() + "_string", createTime.toEpochMilli());
}
if (version != null) {
builder.field(VERSION.getPreferredName(), version);
}
builder.endObject();
return builder;
}
Expand All @@ -147,12 +184,14 @@ public boolean equals(Object o) {
&& Objects.equals(dest, other.dest)
&& Objects.equals(analysis, other.analysis)
&& Objects.equals(analyzedFields, other.analyzedFields)
&& Objects.equals(modelMemoryLimit, other.modelMemoryLimit);
&& Objects.equals(modelMemoryLimit, other.modelMemoryLimit)
&& Objects.equals(createTime, other.createTime)
&& Objects.equals(version, other.version);
}

@Override
public int hashCode() {
return Objects.hash(id, source, dest, analysis, analyzedFields, getModelMemoryLimit());
return Objects.hash(id, source, dest, analysis, analyzedFields, modelMemoryLimit, createTime, version);
}

@Override
Expand All @@ -168,6 +207,8 @@ public static class Builder {
private DataFrameAnalysis analysis;
private FetchSourceContext analyzedFields;
private ByteSizeValue modelMemoryLimit;
private Instant createTime;
private Version version;

private Builder() {}

Expand Down Expand Up @@ -201,8 +242,18 @@ public Builder setModelMemoryLimit(ByteSizeValue modelMemoryLimit) {
return this;
}

public Builder setCreateTime(Instant createTime) {
this.createTime = createTime;
return this;
}

public Builder setVersion(Version version) {
this.version = version;
return this;
}

public DataFrameAnalyticsConfig build() {
return new DataFrameAnalyticsConfig(id, source, dest, analysis, analyzedFields, modelMemoryLimit);
return new DataFrameAnalyticsConfig(id, source, dest, analysis, analyzedFields, modelMemoryLimit, createTime, version);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@

package org.elasticsearch.client.ml.dataframe;

import org.elasticsearch.Version;
import org.elasticsearch.common.settings.Settings;
import org.elasticsearch.common.unit.ByteSizeUnit;
import org.elasticsearch.common.unit.ByteSizeValue;
Expand All @@ -29,6 +30,7 @@
import org.elasticsearch.test.AbstractXContentTestCase;

import java.io.IOException;
import java.time.Instant;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
Expand All @@ -54,6 +56,12 @@ public static DataFrameAnalyticsConfig randomDataFrameAnalyticsConfig() {
if (randomBoolean()) {
builder.setModelMemoryLimit(new ByteSizeValue(randomIntBetween(1, 16), randomFrom(ByteSizeUnit.MB, ByteSizeUnit.GB)));
}
if (randomBoolean()) {
builder.setCreateTime(Instant.now());
}
if (randomBoolean()) {
builder.setVersion(Version.CURRENT);
}
return builder.build();
}

Expand Down
Loading