|
@@ -6,18 +6,23 @@
|
|
|
*/
|
|
|
package org.elasticsearch.xpack.core.ml.action;
|
|
|
|
|
|
+import org.elasticsearch.Version;
|
|
|
import org.elasticsearch.action.ActionRequest;
|
|
|
import org.elasticsearch.action.ActionRequestValidationException;
|
|
|
import org.elasticsearch.action.ActionResponse;
|
|
|
import org.elasticsearch.action.ActionType;
|
|
|
+import org.elasticsearch.common.ParseField;
|
|
|
import org.elasticsearch.common.Strings;
|
|
|
import org.elasticsearch.common.bytes.BytesReference;
|
|
|
import org.elasticsearch.common.io.stream.StreamInput;
|
|
|
import org.elasticsearch.common.io.stream.StreamOutput;
|
|
|
+import org.elasticsearch.common.xcontent.ObjectParser;
|
|
|
import org.elasticsearch.common.xcontent.ToXContentObject;
|
|
|
import org.elasticsearch.common.xcontent.XContentBuilder;
|
|
|
+import org.elasticsearch.common.xcontent.XContentParser;
|
|
|
import org.elasticsearch.common.xcontent.XContentType;
|
|
|
import org.elasticsearch.xpack.core.ml.datafeed.DatafeedConfig;
|
|
|
+import org.elasticsearch.xpack.core.ml.job.config.Job;
|
|
|
import org.elasticsearch.xpack.core.ml.utils.ExceptionsHelper;
|
|
|
|
|
|
import java.io.IOException;
|
|
@@ -35,23 +40,62 @@ public class PreviewDatafeedAction extends ActionType<PreviewDatafeedAction.Resp
|
|
|
|
|
|
public static class Request extends ActionRequest implements ToXContentObject {
|
|
|
|
|
|
- private String datafeedId;
|
|
|
+ private static final String BLANK_ID = "";
|
|
|
+
|
|
|
+ public static final ParseField DATAFEED_CONFIG = new ParseField("datafeed_config");
|
|
|
+ public static final ParseField JOB_CONFIG = new ParseField("job_config");
|
|
|
+
|
|
|
+ private static final ObjectParser<Builder, Void> PARSER = new ObjectParser<>(
|
|
|
+ "preview_datafeed_action",
|
|
|
+ Request.Builder::new
|
|
|
+ );
|
|
|
+ static {
|
|
|
+ PARSER.declareObject(Builder::setDatafeedBuilder, DatafeedConfig.STRICT_PARSER, DATAFEED_CONFIG);
|
|
|
+ PARSER.declareObject(Builder::setJobBuilder, Job.STRICT_PARSER, JOB_CONFIG);
|
|
|
+ }
|
|
|
+
|
|
|
+ public static Request fromXContent(XContentParser parser) {
|
|
|
+ return PARSER.apply(parser, null).build();
|
|
|
+ }
|
|
|
+
|
|
|
+ private final String datafeedId;
|
|
|
+ private final DatafeedConfig datafeedConfig;
|
|
|
+ private final Job.Builder jobConfig;
|
|
|
|
|
|
public Request(StreamInput in) throws IOException {
|
|
|
super(in);
|
|
|
datafeedId = in.readString();
|
|
|
+ if (in.getVersion().onOrAfter(Version.V_8_0_0)) {
|
|
|
+ datafeedConfig = in.readOptionalWriteable(DatafeedConfig::new);
|
|
|
+ jobConfig = in.readOptionalWriteable(Job.Builder::new);
|
|
|
+ } else {
|
|
|
+ datafeedConfig = null;
|
|
|
+ jobConfig = null;
|
|
|
+ }
|
|
|
}
|
|
|
|
|
|
public Request(String datafeedId) {
|
|
|
- setDatafeedId(datafeedId);
|
|
|
+ this.datafeedId = ExceptionsHelper.requireNonNull(datafeedId, DatafeedConfig.ID);
|
|
|
+ this.datafeedConfig = null;
|
|
|
+ this.jobConfig = null;
|
|
|
+ }
|
|
|
+
|
|
|
+ public Request(DatafeedConfig datafeedConfig, Job.Builder jobConfig) {
|
|
|
+ this.datafeedId = BLANK_ID;
|
|
|
+ this.datafeedConfig = ExceptionsHelper.requireNonNull(datafeedConfig, DATAFEED_CONFIG.getPreferredName());
|
|
|
+ this.jobConfig = jobConfig;
|
|
|
}
|
|
|
|
|
|
public String getDatafeedId() {
|
|
|
return datafeedId;
|
|
|
}
|
|
|
|
|
|
- public final void setDatafeedId(String datafeedId) {
|
|
|
- this.datafeedId = ExceptionsHelper.requireNonNull(datafeedId, DatafeedConfig.ID.getPreferredName());
|
|
|
+ public DatafeedConfig getDatafeedConfig() {
|
|
|
+ return datafeedConfig;
|
|
|
+ }
|
|
|
+
|
|
|
+ public Job.Builder getJobConfig() {
|
|
|
+ return jobConfig;
|
|
|
}
|
|
|
|
|
|
@Override
|
|
@@ -63,19 +107,31 @@ public class PreviewDatafeedAction extends ActionType<PreviewDatafeedAction.Resp
|
|
|
public void writeTo(StreamOutput out) throws IOException {
|
|
|
super.writeTo(out);
|
|
|
out.writeString(datafeedId);
|
|
|
+ if (out.getVersion().onOrAfter(Version.V_8_0_0)) {
|
|
|
+ out.writeOptionalWriteable(datafeedConfig);
|
|
|
+ out.writeOptionalWriteable(jobConfig);
|
|
|
+ }
|
|
|
}
|
|
|
|
|
|
@Override
|
|
|
public XContentBuilder toXContent(XContentBuilder builder, Params params) throws IOException {
|
|
|
builder.startObject();
|
|
|
- builder.field(DatafeedConfig.ID.getPreferredName(), datafeedId);
|
|
|
+ if (datafeedId.equals("") == false) {
|
|
|
+ builder.field(DatafeedConfig.ID.getPreferredName(), datafeedId);
|
|
|
+ }
|
|
|
+ if (datafeedConfig != null) {
|
|
|
+ builder.field(DATAFEED_CONFIG.getPreferredName(), datafeedConfig);
|
|
|
+ }
|
|
|
+ if (jobConfig != null) {
|
|
|
+ builder.field(JOB_CONFIG.getPreferredName(), jobConfig);
|
|
|
+ }
|
|
|
builder.endObject();
|
|
|
return builder;
|
|
|
}
|
|
|
|
|
|
@Override
|
|
|
public int hashCode() {
|
|
|
- return Objects.hash(datafeedId);
|
|
|
+ return Objects.hash(datafeedId, datafeedConfig, jobConfig);
|
|
|
}
|
|
|
|
|
|
@Override
|
|
@@ -87,7 +143,56 @@ public class PreviewDatafeedAction extends ActionType<PreviewDatafeedAction.Resp
|
|
|
return false;
|
|
|
}
|
|
|
Request other = (Request) obj;
|
|
|
- return Objects.equals(datafeedId, other.datafeedId);
|
|
|
+ return Objects.equals(datafeedId, other.datafeedId)
|
|
|
+ && Objects.equals(datafeedConfig, other.datafeedConfig)
|
|
|
+ && Objects.equals(jobConfig, other.jobConfig);
|
|
|
+ }
|
|
|
+
|
|
|
+ public static class Builder {
|
|
|
+ private String datafeedId;
|
|
|
+ private DatafeedConfig.Builder datafeedBuilder;
|
|
|
+ private Job.Builder jobBuilder;
|
|
|
+
|
|
|
+ public Builder setDatafeedId(String datafeedId) {
|
|
|
+ this.datafeedId = datafeedId;
|
|
|
+ return this;
|
|
|
+ }
|
|
|
+
|
|
|
+ public Builder setDatafeedBuilder(DatafeedConfig.Builder datafeedBuilder) {
|
|
|
+ this.datafeedBuilder = datafeedBuilder;
|
|
|
+ return this;
|
|
|
+ }
|
|
|
+
|
|
|
+ public Builder setJobBuilder(Job.Builder jobBuilder) {
|
|
|
+ this.jobBuilder = jobBuilder;
|
|
|
+ return this;
|
|
|
+ }
|
|
|
+
|
|
|
+ public Request build() {
|
|
|
+ if (datafeedBuilder != null) {
|
|
|
+ datafeedBuilder.setId("preview_id");
|
|
|
+ if (datafeedBuilder.getJobId() == null && jobBuilder == null) {
|
|
|
+ throw new IllegalArgumentException("[datafeed_config.job_id] must be set or a [job_config] must be provided");
|
|
|
+ }
|
|
|
+ if (datafeedBuilder.getJobId() == null) {
|
|
|
+ datafeedBuilder.setJobId("preview_job_id");
|
|
|
+ }
|
|
|
+ }
|
|
|
+ if (jobBuilder != null) {
|
|
|
+ jobBuilder.setId("preview_job_id");
|
|
|
+ if (datafeedBuilder == null) {
|
|
|
+ throw new IllegalArgumentException("[datafeed_config] must be present when a [job_config] is provided");
|
|
|
+ }
|
|
|
+ }
|
|
|
+ if (datafeedId != null && (datafeedBuilder != null || jobBuilder != null)) {
|
|
|
+ throw new IllegalArgumentException(
|
|
|
+ "[datafeed_id] cannot be supplied when either [job_config] or [datafeed_config] is present"
|
|
|
+ );
|
|
|
+ }
|
|
|
+ return datafeedId != null ?
|
|
|
+ new Request(datafeedId) :
|
|
|
+ new Request(datafeedBuilder == null ? null : datafeedBuilder.build(), jobBuilder);
|
|
|
+ }
|
|
|
}
|
|
|
}
|
|
|
|