|
| 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.client.ml.job.util.PageParams; |
| 25 | +import org.elasticsearch.common.ParseField; |
| 26 | +import org.elasticsearch.common.xcontent.ConstructingObjectParser; |
| 27 | +import org.elasticsearch.common.xcontent.ToXContentObject; |
| 28 | +import org.elasticsearch.common.xcontent.XContentBuilder; |
| 29 | + |
| 30 | +import java.io.IOException; |
| 31 | +import java.util.Objects; |
| 32 | + |
| 33 | +/** |
| 34 | + * A request to retrieve information about model snapshots for a given job |
| 35 | + */ |
| 36 | +public class GetModelSnapshotsRequest extends ActionRequest implements ToXContentObject { |
| 37 | + |
| 38 | + |
| 39 | + public static final ParseField SNAPSHOT_ID = new ParseField("snapshot_id"); |
| 40 | + public static final ParseField SORT = new ParseField("sort"); |
| 41 | + public static final ParseField START = new ParseField("start"); |
| 42 | + public static final ParseField END = new ParseField("end"); |
| 43 | + public static final ParseField DESC = new ParseField("desc"); |
| 44 | + |
| 45 | + public static final ConstructingObjectParser<GetModelSnapshotsRequest, Void> PARSER = new ConstructingObjectParser<>( |
| 46 | + "get_model_snapshots_request", a -> new GetModelSnapshotsRequest((String) a[0])); |
| 47 | + |
| 48 | + |
| 49 | + static { |
| 50 | + PARSER.declareString(ConstructingObjectParser.constructorArg(), Job.ID); |
| 51 | + PARSER.declareString(GetModelSnapshotsRequest::setSnapshotId, SNAPSHOT_ID); |
| 52 | + PARSER.declareString(GetModelSnapshotsRequest::setSort, SORT); |
| 53 | + PARSER.declareStringOrNull(GetModelSnapshotsRequest::setStart, START); |
| 54 | + PARSER.declareStringOrNull(GetModelSnapshotsRequest::setEnd, END); |
| 55 | + PARSER.declareBoolean(GetModelSnapshotsRequest::setDesc, DESC); |
| 56 | + PARSER.declareObject(GetModelSnapshotsRequest::setPageParams, PageParams.PARSER, PageParams.PAGE); |
| 57 | + } |
| 58 | + |
| 59 | + private final String jobId; |
| 60 | + private String snapshotId; |
| 61 | + private String sort; |
| 62 | + private String start; |
| 63 | + private String end; |
| 64 | + private Boolean desc; |
| 65 | + private PageParams pageParams; |
| 66 | + |
| 67 | + /** |
| 68 | + * Constructs a request to retrieve snapshot information from a given job |
| 69 | + * @param jobId id of the job from which to retrieve results |
| 70 | + */ |
| 71 | + public GetModelSnapshotsRequest(String jobId) { |
| 72 | + this.jobId = Objects.requireNonNull(jobId); |
| 73 | + } |
| 74 | + |
| 75 | + public String getJobId() { |
| 76 | + return jobId; |
| 77 | + } |
| 78 | + |
| 79 | + public String getSnapshotId() { |
| 80 | + return snapshotId; |
| 81 | + } |
| 82 | + |
| 83 | + /** |
| 84 | + * Sets the id of the snapshot to retrieve. |
| 85 | + * @param snapshotId the snapshot id |
| 86 | + */ |
| 87 | + public void setSnapshotId(String snapshotId) { |
| 88 | + this.snapshotId = snapshotId; |
| 89 | + } |
| 90 | + |
| 91 | + public String getSort() { |
| 92 | + return sort; |
| 93 | + } |
| 94 | + |
| 95 | + /** |
| 96 | + * Sets the value of "sort". |
| 97 | + * Specifies the snapshot field to sort on. |
| 98 | + * @param sort value of "sort". |
| 99 | + */ |
| 100 | + public void setSort(String sort) { |
| 101 | + this.sort = sort; |
| 102 | + } |
| 103 | + |
| 104 | + public PageParams getPageParams() { |
| 105 | + return pageParams; |
| 106 | + } |
| 107 | + |
| 108 | + /** |
| 109 | + * Sets the paging parameters |
| 110 | + * @param pageParams the paging parameters |
| 111 | + */ |
| 112 | + public void setPageParams(PageParams pageParams) { |
| 113 | + this.pageParams = pageParams; |
| 114 | + } |
| 115 | + |
| 116 | + public String getStart() { |
| 117 | + return start; |
| 118 | + } |
| 119 | + |
| 120 | + /** |
| 121 | + * Sets the value of "start" which is a timestamp. |
| 122 | + * Only snapshots whose timestamp is on or after the "start" value will be returned. |
| 123 | + * @param start String representation of a timestamp; may be an epoch seconds, epoch millis or an ISO string |
| 124 | + */ |
| 125 | + public void setStart(String start) { |
| 126 | + this.start = start; |
| 127 | + } |
| 128 | + |
| 129 | + |
| 130 | + public String getEnd() { |
| 131 | + return end; |
| 132 | + } |
| 133 | + |
| 134 | + /** |
| 135 | + * Sets the value of "end" which is a timestamp. |
| 136 | + * Only snapshots whose timestamp is before the "end" value will be returned. |
| 137 | + * @param end String representation of a timestamp; may be an epoch seconds, epoch millis or an ISO string |
| 138 | + */ |
| 139 | + public void setEnd(String end) { |
| 140 | + this.end = end; |
| 141 | + } |
| 142 | + |
| 143 | + public Boolean getDesc() { |
| 144 | + return desc; |
| 145 | + } |
| 146 | + |
| 147 | + /** |
| 148 | + * Sets the value of "desc". |
| 149 | + * Specifies the sorting order. |
| 150 | + * @param desc value of "desc" |
| 151 | + */ |
| 152 | + public void setDesc(boolean desc) { |
| 153 | + this.desc = desc; |
| 154 | + } |
| 155 | + |
| 156 | + @Override |
| 157 | + public ActionRequestValidationException validate() { |
| 158 | + return null; |
| 159 | + } |
| 160 | + |
| 161 | + @Override |
| 162 | + public XContentBuilder toXContent(XContentBuilder builder, Params params) throws IOException { |
| 163 | + builder.startObject(); |
| 164 | + builder.field(Job.ID.getPreferredName(), jobId); |
| 165 | + if (snapshotId != null) { |
| 166 | + builder.field(SNAPSHOT_ID.getPreferredName(), snapshotId); |
| 167 | + } |
| 168 | + if (sort != null) { |
| 169 | + builder.field(SORT.getPreferredName(), sort); |
| 170 | + } |
| 171 | + if (start != null) { |
| 172 | + builder.field(START.getPreferredName(), start); |
| 173 | + } |
| 174 | + if (end != null) { |
| 175 | + builder.field(END.getPreferredName(), end); |
| 176 | + } |
| 177 | + if (desc != null) { |
| 178 | + builder.field(DESC.getPreferredName(), desc); |
| 179 | + } |
| 180 | + if (pageParams != null) { |
| 181 | + builder.field(PageParams.PAGE.getPreferredName(), pageParams); |
| 182 | + } builder.endObject(); |
| 183 | + return builder; |
| 184 | + } |
| 185 | + |
| 186 | + @Override |
| 187 | + public boolean equals(Object obj) { |
| 188 | + if (obj == null) { |
| 189 | + return false; |
| 190 | + } |
| 191 | + if (getClass() != obj.getClass()) { |
| 192 | + return false; |
| 193 | + } |
| 194 | + GetModelSnapshotsRequest request = (GetModelSnapshotsRequest) obj; |
| 195 | + return Objects.equals(jobId, request.jobId) |
| 196 | + && Objects.equals(snapshotId, request.snapshotId) |
| 197 | + && Objects.equals(sort, request.sort) |
| 198 | + && Objects.equals(start, request.start) |
| 199 | + && Objects.equals(end, request.end) |
| 200 | + && Objects.equals(desc, request.desc) |
| 201 | + && Objects.equals(pageParams, request.pageParams); |
| 202 | + } |
| 203 | + |
| 204 | + @Override |
| 205 | + public int hashCode() { |
| 206 | + return Objects.hash(jobId, snapshotId, pageParams, start, end, sort, desc); |
| 207 | + } |
| 208 | +} |
0 commit comments