|
| 1 | +/* |
| 2 | + * Licensed to Elasticsearch under one or more contributor |
| 3 | + * license agreements. See the NOTICE file distributed with |
| 4 | + * this work for additional information regarding copyright |
| 5 | + * ownership. Elasticsearch licenses this file to you under |
| 6 | + * the Apache License, Version 2.0 (the "License"); you may |
| 7 | + * not use this file except in compliance with the License. |
| 8 | + * You may obtain a copy of the License at |
| 9 | + * |
| 10 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 11 | + * |
| 12 | + * Unless required by applicable law or agreed to in writing, |
| 13 | + * software distributed under the License is distributed on an |
| 14 | + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY |
| 15 | + * KIND, either express or implied. See the License for the |
| 16 | + * specific language governing permissions and limitations |
| 17 | + * under the License. |
| 18 | + */ |
| 19 | +package org.elasticsearch.client.ml; |
| 20 | + |
| 21 | +import org.elasticsearch.action.ActionRequest; |
| 22 | +import org.elasticsearch.action.ActionRequestValidationException; |
| 23 | +import org.elasticsearch.client.ml.job.config.Job; |
| 24 | +import org.elasticsearch.common.ParseField; |
| 25 | +import org.elasticsearch.common.Strings; |
| 26 | +import org.elasticsearch.common.unit.TimeValue; |
| 27 | +import org.elasticsearch.common.xcontent.ConstructingObjectParser; |
| 28 | +import org.elasticsearch.common.xcontent.ToXContentObject; |
| 29 | +import org.elasticsearch.common.xcontent.XContentBuilder; |
| 30 | + |
| 31 | +import java.io.IOException; |
| 32 | +import java.util.ArrayList; |
| 33 | +import java.util.Arrays; |
| 34 | +import java.util.List; |
| 35 | +import java.util.Objects; |
| 36 | + |
| 37 | +/** |
| 38 | + * POJO for a delete forecast request |
| 39 | + */ |
| 40 | +public class DeleteForecastRequest extends ActionRequest implements ToXContentObject { |
| 41 | + |
| 42 | + public static final ParseField FORECAST_ID = new ParseField("forecast_id"); |
| 43 | + public static final ParseField ALLOW_NO_FORECASTS = new ParseField("allow_no_forecasts"); |
| 44 | + public static final ParseField TIMEOUT = new ParseField("timeout"); |
| 45 | + public static final String ALL = "_all"; |
| 46 | + |
| 47 | + public static final ConstructingObjectParser<DeleteForecastRequest, Void> PARSER = |
| 48 | + new ConstructingObjectParser<>("delete_forecast_request", (a) -> new DeleteForecastRequest((String) a[0])); |
| 49 | + |
| 50 | + static { |
| 51 | + PARSER.declareString(ConstructingObjectParser.constructorArg(), Job.ID); |
| 52 | + PARSER.declareStringOrNull( |
| 53 | + (c, p) -> c.setForecastIds(Strings.commaDelimitedListToStringArray(p)), FORECAST_ID); |
| 54 | + PARSER.declareBoolean(DeleteForecastRequest::setAllowNoForecasts, ALLOW_NO_FORECASTS); |
| 55 | + PARSER.declareString(DeleteForecastRequest::timeout, TIMEOUT); |
| 56 | + } |
| 57 | + |
| 58 | + /** |
| 59 | + * Create a new {@link DeleteForecastRequest} that explicitly deletes all forecasts |
| 60 | + * |
| 61 | + * @param jobId the jobId of the Job whose forecasts to delete |
| 62 | + */ |
| 63 | + public static DeleteForecastRequest deleteAllForecasts(String jobId) { |
| 64 | + DeleteForecastRequest request = new DeleteForecastRequest(jobId); |
| 65 | + request.setForecastIds(ALL); |
| 66 | + return request; |
| 67 | + } |
| 68 | + |
| 69 | + private final String jobId; |
| 70 | + private List<String> forecastIds = new ArrayList<>(); |
| 71 | + private Boolean allowNoForecasts; |
| 72 | + private TimeValue timeout; |
| 73 | + |
| 74 | + /** |
| 75 | + * Create a new DeleteForecastRequest for the given Job ID |
| 76 | + * |
| 77 | + * @param jobId the jobId of the Job whose forecast(s) to delete |
| 78 | + */ |
| 79 | + public DeleteForecastRequest(String jobId) { |
| 80 | + this.jobId = Objects.requireNonNull(jobId, Job.ID.getPreferredName()); |
| 81 | + } |
| 82 | + |
| 83 | + public String getJobId() { |
| 84 | + return jobId; |
| 85 | + } |
| 86 | + |
| 87 | + public List<String> getForecastIds() { |
| 88 | + return forecastIds; |
| 89 | + } |
| 90 | + |
| 91 | + /** |
| 92 | + * The forecast IDs to delete. Can be also be {@link DeleteForecastRequest#ALL} to explicitly delete ALL forecasts |
| 93 | + * |
| 94 | + * @param forecastIds forecast IDs to delete |
| 95 | + */ |
| 96 | + public void setForecastIds(String... forecastIds) { |
| 97 | + setForecastIds(Arrays.asList(forecastIds)); |
| 98 | + } |
| 99 | + |
| 100 | + void setForecastIds(List<String> forecastIds) { |
| 101 | + if (forecastIds.stream().anyMatch(Objects::isNull)) { |
| 102 | + throw new NullPointerException("forecastIds must not contain null values"); |
| 103 | + } |
| 104 | + this.forecastIds = new ArrayList<>(forecastIds); |
| 105 | + } |
| 106 | + |
| 107 | + public Boolean isAllowNoForecasts() { |
| 108 | + return allowNoForecasts; |
| 109 | + } |
| 110 | + |
| 111 | + /** |
| 112 | + * Sets the `allow_no_forecasts` field. |
| 113 | + * |
| 114 | + * @param allowNoForecasts when {@code true} no error is thrown when {@link DeleteForecastRequest#ALL} does not find any forecasts |
| 115 | + */ |
| 116 | + public void setAllowNoForecasts(boolean allowNoForecasts) { |
| 117 | + this.allowNoForecasts = allowNoForecasts; |
| 118 | + } |
| 119 | + |
| 120 | + /** |
| 121 | + * Allows to set the timeout |
| 122 | + * @param timeout timeout as a string (e.g. 1s) |
| 123 | + */ |
| 124 | + public void timeout(String timeout) { |
| 125 | + this.timeout = TimeValue.parseTimeValue(timeout, this.timeout, getClass().getSimpleName() + ".timeout"); |
| 126 | + } |
| 127 | + |
| 128 | + /** |
| 129 | + * Allows to set the timeout |
| 130 | + * @param timeout timeout as a {@link TimeValue} |
| 131 | + */ |
| 132 | + public void timeout(TimeValue timeout) { |
| 133 | + this.timeout = timeout; |
| 134 | + } |
| 135 | + |
| 136 | + public TimeValue timeout() { |
| 137 | + return timeout; |
| 138 | + } |
| 139 | + |
| 140 | + @Override |
| 141 | + public boolean equals(Object other) { |
| 142 | + if (this == other) { |
| 143 | + return true; |
| 144 | + } |
| 145 | + |
| 146 | + if (other == null || getClass() != other.getClass()) { |
| 147 | + return false; |
| 148 | + } |
| 149 | + |
| 150 | + DeleteForecastRequest that = (DeleteForecastRequest) other; |
| 151 | + return Objects.equals(jobId, that.jobId) && |
| 152 | + Objects.equals(forecastIds, that.forecastIds) && |
| 153 | + Objects.equals(allowNoForecasts, that.allowNoForecasts) && |
| 154 | + Objects.equals(timeout, that.timeout); |
| 155 | + } |
| 156 | + |
| 157 | + @Override |
| 158 | + public int hashCode() { |
| 159 | + return Objects.hash(jobId, forecastIds, allowNoForecasts, timeout); |
| 160 | + } |
| 161 | + |
| 162 | + @Override |
| 163 | + public ActionRequestValidationException validate() { |
| 164 | + return null; |
| 165 | + } |
| 166 | + |
| 167 | + @Override |
| 168 | + public XContentBuilder toXContent(XContentBuilder builder, Params params) throws IOException { |
| 169 | + builder.startObject(); |
| 170 | + builder.field(Job.ID.getPreferredName(), jobId); |
| 171 | + if (forecastIds != null) { |
| 172 | + builder.field(FORECAST_ID.getPreferredName(), Strings.collectionToCommaDelimitedString(forecastIds)); |
| 173 | + } |
| 174 | + if (allowNoForecasts != null) { |
| 175 | + builder.field(ALLOW_NO_FORECASTS.getPreferredName(), allowNoForecasts); |
| 176 | + } |
| 177 | + if (timeout != null) { |
| 178 | + builder.field(TIMEOUT.getPreferredName(), timeout.getStringRep()); |
| 179 | + } |
| 180 | + builder.endObject(); |
| 181 | + return builder; |
| 182 | + } |
| 183 | +} |
0 commit comments