From d8a4be9fce601f43d5befdab6916e48b44c234c5 Mon Sep 17 00:00:00 2001 From: Benjamin Trent <4357155+benwtrent@users.noreply.github.com> Date: Tue, 18 May 2021 09:22:38 -0400 Subject: [PATCH] [ML] fix empty body on post issue for datafeed _preview --- .../core/ml/action/PreviewDatafeedAction.java | 10 ++++++++-- .../datafeeds/RestPreviewDatafeedAction.java | 5 ++++- .../test/ml/preview_datafeed.yml | 19 +++++++++++++++++++ 3 files changed, 31 insertions(+), 3 deletions(-) diff --git a/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/ml/action/PreviewDatafeedAction.java b/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/ml/action/PreviewDatafeedAction.java index afeaa2b33e486..c4e639cc57413 100644 --- a/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/ml/action/PreviewDatafeedAction.java +++ b/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/ml/action/PreviewDatafeedAction.java @@ -10,6 +10,7 @@ import org.elasticsearch.action.ActionRequestValidationException; import org.elasticsearch.action.ActionResponse; import org.elasticsearch.action.ActionType; +import org.elasticsearch.common.Nullable; import org.elasticsearch.common.ParseField; import org.elasticsearch.common.Strings; import org.elasticsearch.common.bytes.BytesReference; @@ -53,8 +54,13 @@ public static class Request extends ActionRequest implements ToXContentObject { PARSER.declareObject(Builder::setJobBuilder, Job.STRICT_PARSER, JOB_CONFIG); } - public static Request fromXContent(XContentParser parser) { - return PARSER.apply(parser, null).build(); + public static Request fromXContent(XContentParser parser, @Nullable String datafeedId) { + Builder builder = PARSER.apply(parser, null); + // We don't need to check for "inconsistent ids" as we don't parse an ID from the body + if (datafeedId != null) { + builder.setDatafeedId(datafeedId); + } + return builder.build(); } private final String datafeedId; diff --git a/x-pack/plugin/ml/src/main/java/org/elasticsearch/xpack/ml/rest/datafeeds/RestPreviewDatafeedAction.java b/x-pack/plugin/ml/src/main/java/org/elasticsearch/xpack/ml/rest/datafeeds/RestPreviewDatafeedAction.java index a7c264d513d30..537775850e044 100644 --- a/x-pack/plugin/ml/src/main/java/org/elasticsearch/xpack/ml/rest/datafeeds/RestPreviewDatafeedAction.java +++ b/x-pack/plugin/ml/src/main/java/org/elasticsearch/xpack/ml/rest/datafeeds/RestPreviewDatafeedAction.java @@ -40,7 +40,10 @@ public String getName() { @Override protected RestChannelConsumer prepareRequest(RestRequest restRequest, NodeClient client) throws IOException { PreviewDatafeedAction.Request request = restRequest.hasContentOrSourceParam() ? - PreviewDatafeedAction.Request.fromXContent(restRequest.contentOrSourceParamParser()) : + PreviewDatafeedAction.Request.fromXContent( + restRequest.contentOrSourceParamParser(), + restRequest.param(DatafeedConfig.ID.getPreferredName(), null) + ) : new PreviewDatafeedAction.Request(restRequest.param(DatafeedConfig.ID.getPreferredName())); return channel -> client.execute(PreviewDatafeedAction.INSTANCE, request, new RestToXContentListener<>(channel)); } diff --git a/x-pack/plugin/src/yamlRestTest/resources/rest-api-spec/test/ml/preview_datafeed.yml b/x-pack/plugin/src/yamlRestTest/resources/rest-api-spec/test/ml/preview_datafeed.yml index 77a9fc71379e5..929182e6dad7e 100644 --- a/x-pack/plugin/src/yamlRestTest/resources/rest-api-spec/test/ml/preview_datafeed.yml +++ b/x-pack/plugin/src/yamlRestTest/resources/rest-api-spec/test/ml/preview_datafeed.yml @@ -109,6 +109,25 @@ setup: - match: { 3.airline: foo } - match: { 3.responsetime: 42.0 } + - do: + ml.preview_datafeed: + datafeed_id: preview-datafeed-feed + body: > + {} + - length: { $body: 4 } + - match: { 0.time: 1487376000000 } + - match: { 0.airline: foo } + - match: { 0.responsetime: 1.0 } + - match: { 1.time: 1487377800000 } + - match: { 1.airline: foo } + - match: { 1.responsetime: 1.0 } + - match: { 2.time: 1487379600000 } + - match: { 2.airline: bar } + - match: { 2.responsetime: 42.0 } + - match: { 3.time: 1487379660000 } + - match: { 3.airline: foo } + - match: { 3.responsetime: 42.0 } + - do: ml.preview_datafeed: body: >