|
| 1 | +/* |
| 2 | + * Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one |
| 3 | + * or more contributor license agreements. Licensed under the Elastic License; |
| 4 | + * you may not use this file except in compliance with the Elastic License. |
| 5 | + */ |
| 6 | + |
| 7 | +package org.elasticsearch.xpack.dataframe.action; |
| 8 | + |
| 9 | +import org.elasticsearch.action.Action; |
| 10 | +import org.elasticsearch.action.ActionRequestValidationException; |
| 11 | +import org.elasticsearch.action.ActionResponse; |
| 12 | +import org.elasticsearch.action.support.master.AcknowledgedRequest; |
| 13 | +import org.elasticsearch.action.support.master.MasterNodeOperationRequestBuilder; |
| 14 | +import org.elasticsearch.client.ElasticsearchClient; |
| 15 | +import org.elasticsearch.common.ParseField; |
| 16 | +import org.elasticsearch.common.bytes.BytesReference; |
| 17 | +import org.elasticsearch.common.io.stream.StreamInput; |
| 18 | +import org.elasticsearch.common.io.stream.StreamOutput; |
| 19 | +import org.elasticsearch.common.xcontent.LoggingDeprecationHandler; |
| 20 | +import org.elasticsearch.common.xcontent.ObjectParser; |
| 21 | +import org.elasticsearch.common.xcontent.ToXContentObject; |
| 22 | +import org.elasticsearch.common.xcontent.XContentBuilder; |
| 23 | +import org.elasticsearch.common.xcontent.XContentFactory; |
| 24 | +import org.elasticsearch.common.xcontent.XContentParser; |
| 25 | +import org.elasticsearch.common.xcontent.XContentType; |
| 26 | +import org.elasticsearch.xpack.dataframe.transforms.DataFrameTransformConfig; |
| 27 | + |
| 28 | +import java.io.IOException; |
| 29 | +import java.util.ArrayList; |
| 30 | +import java.util.List; |
| 31 | +import java.util.Map; |
| 32 | +import java.util.Objects; |
| 33 | + |
| 34 | +public class PreviewDataFrameTransformAction extends Action<PreviewDataFrameTransformAction.Response> { |
| 35 | + |
| 36 | + public static final PreviewDataFrameTransformAction INSTANCE = new PreviewDataFrameTransformAction(); |
| 37 | + public static final String NAME = "cluster:admin/data_frame/preview"; |
| 38 | + |
| 39 | + private PreviewDataFrameTransformAction() { |
| 40 | + super(NAME); |
| 41 | + } |
| 42 | + |
| 43 | + @Override |
| 44 | + public Response newResponse() { |
| 45 | + return new Response(); |
| 46 | + } |
| 47 | + |
| 48 | + public static class Request extends AcknowledgedRequest<Request> implements ToXContentObject { |
| 49 | + |
| 50 | + private DataFrameTransformConfig config; |
| 51 | + |
| 52 | + public Request(DataFrameTransformConfig config) { |
| 53 | + this.setConfig(config); |
| 54 | + } |
| 55 | + |
| 56 | + public Request() { } |
| 57 | + |
| 58 | + public static Request fromXContent(final XContentParser parser) throws IOException { |
| 59 | + Map<String, Object> content = parser.map(); |
| 60 | + // Destination and ID are not required for Preview, so we just supply our own |
| 61 | + content.put(DataFrameTransformConfig.DESTINATION.getPreferredName(), "unused-transform-preview-index"); |
| 62 | + try(XContentBuilder xContentBuilder = XContentFactory.jsonBuilder().map(content); |
| 63 | + XContentParser newParser = XContentType.JSON |
| 64 | + .xContent() |
| 65 | + .createParser(parser.getXContentRegistry(), |
| 66 | + LoggingDeprecationHandler.INSTANCE, |
| 67 | + BytesReference.bytes(xContentBuilder).streamInput())) { |
| 68 | + return new Request(DataFrameTransformConfig.fromXContent(newParser, "transform-preview", true)); |
| 69 | + } |
| 70 | + } |
| 71 | + |
| 72 | + @Override |
| 73 | + public ActionRequestValidationException validate() { |
| 74 | + return null; |
| 75 | + } |
| 76 | + |
| 77 | + @Override |
| 78 | + public XContentBuilder toXContent(XContentBuilder builder, Params params) throws IOException { |
| 79 | + return this.config.toXContent(builder, params); |
| 80 | + } |
| 81 | + |
| 82 | + public DataFrameTransformConfig getConfig() { |
| 83 | + return config; |
| 84 | + } |
| 85 | + |
| 86 | + public void setConfig(DataFrameTransformConfig config) { |
| 87 | + this.config = config; |
| 88 | + } |
| 89 | + |
| 90 | + @Override |
| 91 | + public void readFrom(StreamInput in) throws IOException { |
| 92 | + super.readFrom(in); |
| 93 | + this.config = new DataFrameTransformConfig(in); |
| 94 | + } |
| 95 | + |
| 96 | + @Override |
| 97 | + public void writeTo(StreamOutput out) throws IOException { |
| 98 | + super.writeTo(out); |
| 99 | + this.config.writeTo(out); |
| 100 | + } |
| 101 | + |
| 102 | + @Override |
| 103 | + public int hashCode() { |
| 104 | + return Objects.hash(config); |
| 105 | + } |
| 106 | + |
| 107 | + @Override |
| 108 | + public boolean equals(Object obj) { |
| 109 | + if (obj == this) { |
| 110 | + return true; |
| 111 | + } |
| 112 | + if (obj == null || getClass() != obj.getClass()) { |
| 113 | + return false; |
| 114 | + } |
| 115 | + Request other = (Request) obj; |
| 116 | + return Objects.equals(config, other.config); |
| 117 | + } |
| 118 | + } |
| 119 | + |
| 120 | + public static class RequestBuilder extends MasterNodeOperationRequestBuilder<Request, Response, RequestBuilder> { |
| 121 | + |
| 122 | + protected RequestBuilder(ElasticsearchClient client, PreviewDataFrameTransformAction action) { |
| 123 | + super(client, action, new Request()); |
| 124 | + } |
| 125 | + } |
| 126 | + |
| 127 | + public static class Response extends ActionResponse implements ToXContentObject { |
| 128 | + |
| 129 | + private List<Map<String, Object>> docs; |
| 130 | + public static ParseField PREVIEW = new ParseField("preview"); |
| 131 | + |
| 132 | + static ObjectParser<Response, Void> PARSER = new ObjectParser<>("data_frame_transform_preview", Response::new); |
| 133 | + static { |
| 134 | + PARSER.declareObjectArray(Response::setDocs, (p, c) -> p.mapOrdered(), PREVIEW); |
| 135 | + } |
| 136 | + public Response() {} |
| 137 | + |
| 138 | + public Response(StreamInput in) throws IOException { |
| 139 | + int size = in.readInt(); |
| 140 | + this.docs = new ArrayList<>(size); |
| 141 | + for (int i = 0; i < size; i++) { |
| 142 | + this.docs.add(in.readMap()); |
| 143 | + } |
| 144 | + } |
| 145 | + |
| 146 | + public Response(List<Map<String, Object>> docs) { |
| 147 | + this.docs = new ArrayList<>(docs); |
| 148 | + } |
| 149 | + |
| 150 | + public void setDocs(List<Map<String, Object>> docs) { |
| 151 | + this.docs = new ArrayList<>(docs); |
| 152 | + } |
| 153 | + |
| 154 | + @Override |
| 155 | + public void readFrom(StreamInput in) throws IOException { |
| 156 | + int size = in.readInt(); |
| 157 | + this.docs = new ArrayList<>(size); |
| 158 | + for (int i = 0; i < size; i++) { |
| 159 | + this.docs.add(in.readMap()); |
| 160 | + } |
| 161 | + } |
| 162 | + |
| 163 | + @Override |
| 164 | + public void writeTo(StreamOutput out) throws IOException { |
| 165 | + out.writeInt(docs.size()); |
| 166 | + for (Map<String, Object> doc : docs) { |
| 167 | + out.writeMapWithConsistentOrder(doc); |
| 168 | + } |
| 169 | + } |
| 170 | + |
| 171 | + @Override |
| 172 | + public XContentBuilder toXContent(XContentBuilder builder, Params params) throws IOException { |
| 173 | + builder.startObject(); |
| 174 | + builder.field(PREVIEW.getPreferredName(), docs); |
| 175 | + builder.endObject(); |
| 176 | + return builder; |
| 177 | + } |
| 178 | + |
| 179 | + @Override |
| 180 | + public boolean equals(Object obj) { |
| 181 | + if (obj == this) { |
| 182 | + return true; |
| 183 | + } |
| 184 | + |
| 185 | + if (obj == null || obj.getClass() != getClass()) { |
| 186 | + return false; |
| 187 | + } |
| 188 | + |
| 189 | + Response other = (Response) obj; |
| 190 | + return Objects.equals(other.docs, docs); |
| 191 | + } |
| 192 | + |
| 193 | + @Override |
| 194 | + public int hashCode() { |
| 195 | + return Objects.hashCode(docs); |
| 196 | + } |
| 197 | + |
| 198 | + public static Response fromXContent(final XContentParser parser) throws IOException { |
| 199 | + return PARSER.parse(parser, null); |
| 200 | + } |
| 201 | + } |
| 202 | +} |
0 commit comments