|
| 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 | + * 2.0; you may not use this file except in compliance with the Elastic License |
| 5 | + * 2.0. |
| 6 | + */ |
| 7 | +package org.elasticsearch.xpack.core.ml.action; |
| 8 | + |
| 9 | +import org.elasticsearch.action.ActionType; |
| 10 | +import org.elasticsearch.action.support.tasks.BaseTasksRequest; |
| 11 | +import org.elasticsearch.action.support.tasks.BaseTasksResponse; |
| 12 | +import org.elasticsearch.common.collect.MapBuilder; |
| 13 | +import org.elasticsearch.common.io.stream.StreamInput; |
| 14 | +import org.elasticsearch.common.io.stream.StreamOutput; |
| 15 | +import org.elasticsearch.common.io.stream.Writeable; |
| 16 | +import org.elasticsearch.common.xcontent.ToXContentObject; |
| 17 | +import org.elasticsearch.common.xcontent.XContentBuilder; |
| 18 | +import org.elasticsearch.tasks.Task; |
| 19 | +import org.elasticsearch.xpack.core.ml.MlTasks; |
| 20 | + |
| 21 | +import java.io.IOException; |
| 22 | +import java.util.List; |
| 23 | +import java.util.Map; |
| 24 | +import java.util.Objects; |
| 25 | +import java.util.Optional; |
| 26 | +import java.util.Set; |
| 27 | +import java.util.stream.Collectors; |
| 28 | + |
| 29 | + |
| 30 | +/** |
| 31 | + * Internal only action to get the current running state of a datafeed |
| 32 | + */ |
| 33 | +public class GetDatafeedRunningStateAction extends ActionType<GetDatafeedRunningStateAction.Response> { |
| 34 | + |
| 35 | + public static final GetDatafeedRunningStateAction INSTANCE = new GetDatafeedRunningStateAction(); |
| 36 | + public static final String NAME = "cluster:internal/xpack/ml/datafeed/running_state"; |
| 37 | + |
| 38 | + private GetDatafeedRunningStateAction() { |
| 39 | + super(NAME, GetDatafeedRunningStateAction.Response::new); |
| 40 | + } |
| 41 | + |
| 42 | + public static class Request extends BaseTasksRequest<Request> { |
| 43 | + |
| 44 | + private final Set<String> datafeedTaskIds; |
| 45 | + |
| 46 | + public Request(List<String> datafeedIds) { |
| 47 | + this.datafeedTaskIds = datafeedIds.stream().map(MlTasks::datafeedTaskId).collect(Collectors.toSet()); |
| 48 | + } |
| 49 | + |
| 50 | + public Request(StreamInput in) throws IOException { |
| 51 | + super(in); |
| 52 | + this.datafeedTaskIds = in.readSet(StreamInput::readString); |
| 53 | + } |
| 54 | + |
| 55 | + @Override |
| 56 | + public void writeTo(StreamOutput out) throws IOException { |
| 57 | + super.writeTo(out); |
| 58 | + out.writeStringCollection(datafeedTaskIds); |
| 59 | + } |
| 60 | + |
| 61 | + public Set<String> getDatafeedTaskIds() { |
| 62 | + return datafeedTaskIds; |
| 63 | + } |
| 64 | + |
| 65 | + @Override |
| 66 | + public boolean match(Task task) { |
| 67 | + return task instanceof StartDatafeedAction.DatafeedTaskMatcher && datafeedTaskIds.contains(task.getDescription()); |
| 68 | + } |
| 69 | + } |
| 70 | + |
| 71 | + public static class Response extends BaseTasksResponse { |
| 72 | + |
| 73 | + public static class RunningState implements Writeable, ToXContentObject { |
| 74 | + |
| 75 | + // Is the datafeed a "realtime" datafeed, meaning it was started without an end_time |
| 76 | + private final boolean realTimeConfigured; |
| 77 | + // Has the look back finished and are we now running on "real-time" data |
| 78 | + private final boolean realTimeRunning; |
| 79 | + |
| 80 | + public RunningState(boolean realTimeConfigured, boolean realTimeRunning) { |
| 81 | + this.realTimeConfigured = realTimeConfigured; |
| 82 | + this.realTimeRunning = realTimeRunning; |
| 83 | + } |
| 84 | + |
| 85 | + public RunningState(StreamInput in) throws IOException { |
| 86 | + this.realTimeConfigured = in.readBoolean(); |
| 87 | + this.realTimeRunning = in.readBoolean(); |
| 88 | + } |
| 89 | + |
| 90 | + @Override |
| 91 | + public boolean equals(Object o) { |
| 92 | + if (this == o) return true; |
| 93 | + if (o == null || getClass() != o.getClass()) return false; |
| 94 | + RunningState that = (RunningState) o; |
| 95 | + return realTimeConfigured == that.realTimeConfigured && realTimeRunning == that.realTimeRunning; |
| 96 | + } |
| 97 | + |
| 98 | + @Override |
| 99 | + public int hashCode() { |
| 100 | + return Objects.hash(realTimeConfigured, realTimeRunning); |
| 101 | + } |
| 102 | + |
| 103 | + @Override |
| 104 | + public void writeTo(StreamOutput out) throws IOException { |
| 105 | + out.writeBoolean(realTimeConfigured); |
| 106 | + out.writeBoolean(realTimeRunning); |
| 107 | + } |
| 108 | + |
| 109 | + @Override |
| 110 | + public XContentBuilder toXContent(XContentBuilder builder, Params params) throws IOException { |
| 111 | + builder.startObject(); |
| 112 | + builder.field("real_time_configured", realTimeConfigured); |
| 113 | + builder.field("real_time_running", realTimeRunning); |
| 114 | + builder.endObject(); |
| 115 | + return builder; |
| 116 | + } |
| 117 | + } |
| 118 | + |
| 119 | + private final Map<String, RunningState> datafeedRunningState; |
| 120 | + |
| 121 | + public static Response fromResponses(List<Response> responses) { |
| 122 | + return new Response(responses.stream() |
| 123 | + .flatMap(r -> r.datafeedRunningState.entrySet().stream()) |
| 124 | + .filter(entry -> entry.getValue() != null) |
| 125 | + .collect(Collectors.toMap(Map.Entry::getKey, Map.Entry::getValue))); |
| 126 | + } |
| 127 | + |
| 128 | + public static Response fromTaskAndState(String datafeedId, RunningState runningState) { |
| 129 | + return new Response(MapBuilder.<String, RunningState>newMapBuilder().put(datafeedId, runningState).map()); |
| 130 | + } |
| 131 | + |
| 132 | + public Response(StreamInput in) throws IOException { |
| 133 | + super(in); |
| 134 | + datafeedRunningState = in.readMap(StreamInput::readString, RunningState::new); |
| 135 | + } |
| 136 | + |
| 137 | + public Response(Map<String, RunningState> runtimeStateMap) { |
| 138 | + super(null, null); |
| 139 | + this.datafeedRunningState = runtimeStateMap; |
| 140 | + } |
| 141 | + |
| 142 | + public Optional<RunningState> getRunningState(String datafeedId) { |
| 143 | + return Optional.ofNullable(datafeedRunningState.get(datafeedId)); |
| 144 | + } |
| 145 | + |
| 146 | + public Map<String, RunningState> getDatafeedRunningState() { |
| 147 | + return datafeedRunningState; |
| 148 | + } |
| 149 | + |
| 150 | + @Override |
| 151 | + public void writeTo(StreamOutput out) throws IOException { |
| 152 | + super.writeTo(out); |
| 153 | + out.writeMap(datafeedRunningState, StreamOutput::writeString, (o, w) -> w.writeTo(o)); |
| 154 | + } |
| 155 | + |
| 156 | + @Override |
| 157 | + public boolean equals(Object o) { |
| 158 | + if (this == o) return true; |
| 159 | + if (o == null || getClass() != o.getClass()) return false; |
| 160 | + Response response = (Response) o; |
| 161 | + return Objects.equals(this.datafeedRunningState, response.datafeedRunningState); |
| 162 | + } |
| 163 | + |
| 164 | + @Override |
| 165 | + public int hashCode() { |
| 166 | + return Objects.hash(datafeedRunningState); |
| 167 | + } |
| 168 | + } |
| 169 | + |
| 170 | +} |
0 commit comments