|
| 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.xcontent.ConstructingObjectParser; |
| 27 | +import org.elasticsearch.common.xcontent.ObjectParser; |
| 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 | +/** |
| 39 | + * Request object to get {@link org.elasticsearch.client.ml.job.stats.JobStats} by their respective jobIds |
| 40 | + * |
| 41 | + * `_all` explicitly gets all the jobs' statistics in the cluster |
| 42 | + * An empty request (no `jobId`s) implicitly gets all the jobs' statistics in the cluster |
| 43 | + */ |
| 44 | +public class GetJobStatsRequest extends ActionRequest implements ToXContentObject { |
| 45 | + |
| 46 | + public static final ParseField ALLOW_NO_JOBS = new ParseField("allow_no_jobs"); |
| 47 | + |
| 48 | + @SuppressWarnings("unchecked") |
| 49 | + public static final ConstructingObjectParser<GetJobStatsRequest, Void> PARSER = new ConstructingObjectParser<>( |
| 50 | + "get_jobs_stats_request", a -> new GetJobStatsRequest((List<String>) a[0])); |
| 51 | + |
| 52 | + static { |
| 53 | + PARSER.declareField(ConstructingObjectParser.constructorArg(), |
| 54 | + p -> Arrays.asList(Strings.commaDelimitedListToStringArray(p.text())), |
| 55 | + Job.ID, ObjectParser.ValueType.STRING_ARRAY); |
| 56 | + PARSER.declareBoolean(GetJobStatsRequest::setAllowNoJobs, ALLOW_NO_JOBS); |
| 57 | + } |
| 58 | + |
| 59 | + private static final String ALL_JOBS = "_all"; |
| 60 | + |
| 61 | + private final List<String> jobIds; |
| 62 | + private Boolean allowNoJobs; |
| 63 | + |
| 64 | + /** |
| 65 | + * Explicitly gets all jobs statistics |
| 66 | + * |
| 67 | + * @return a {@link GetJobStatsRequest} for all existing jobs |
| 68 | + */ |
| 69 | + public static GetJobStatsRequest getAllJobStatsRequest(){ |
| 70 | + return new GetJobStatsRequest(ALL_JOBS); |
| 71 | + } |
| 72 | + |
| 73 | + GetJobStatsRequest(List<String> jobIds) { |
| 74 | + if (jobIds.stream().anyMatch(Objects::isNull)) { |
| 75 | + throw new NullPointerException("jobIds must not contain null values"); |
| 76 | + } |
| 77 | + this.jobIds = new ArrayList<>(jobIds); |
| 78 | + } |
| 79 | + |
| 80 | + /** |
| 81 | + * Get the specified Job's statistics via their unique jobIds |
| 82 | + * |
| 83 | + * @param jobIds must be non-null and each jobId must be non-null |
| 84 | + */ |
| 85 | + public GetJobStatsRequest(String... jobIds) { |
| 86 | + this(Arrays.asList(jobIds)); |
| 87 | + } |
| 88 | + |
| 89 | + /** |
| 90 | + * All the jobIds for which to get statistics |
| 91 | + */ |
| 92 | + public List<String> getJobIds() { |
| 93 | + return jobIds; |
| 94 | + } |
| 95 | + |
| 96 | + public Boolean isAllowNoJobs() { |
| 97 | + return this.allowNoJobs; |
| 98 | + } |
| 99 | + |
| 100 | + /** |
| 101 | + * Whether to ignore if a wildcard expression matches no jobs. |
| 102 | + * |
| 103 | + * This includes `_all` string or when no jobs have been specified |
| 104 | + * |
| 105 | + * @param allowNoJobs When {@code true} ignore if wildcard or `_all` matches no jobs. Defaults to {@code true} |
| 106 | + */ |
| 107 | + public void setAllowNoJobs(boolean allowNoJobs) { |
| 108 | + this.allowNoJobs = allowNoJobs; |
| 109 | + } |
| 110 | + |
| 111 | + @Override |
| 112 | + public int hashCode() { |
| 113 | + return Objects.hash(jobIds, allowNoJobs); |
| 114 | + } |
| 115 | + |
| 116 | + @Override |
| 117 | + public boolean equals(Object other) { |
| 118 | + if (this == other) { |
| 119 | + return true; |
| 120 | + } |
| 121 | + |
| 122 | + if (other == null || getClass() != other.getClass()) { |
| 123 | + return false; |
| 124 | + } |
| 125 | + |
| 126 | + GetJobStatsRequest that = (GetJobStatsRequest) other; |
| 127 | + return Objects.equals(jobIds, that.jobIds) && |
| 128 | + Objects.equals(allowNoJobs, that.allowNoJobs); |
| 129 | + } |
| 130 | + |
| 131 | + @Override |
| 132 | + public ActionRequestValidationException validate() { |
| 133 | + return null; |
| 134 | + } |
| 135 | + |
| 136 | + @Override |
| 137 | + public XContentBuilder toXContent(XContentBuilder builder, Params params) throws IOException { |
| 138 | + builder.startObject(); |
| 139 | + builder.field(Job.ID.getPreferredName(), Strings.collectionToCommaDelimitedString(jobIds)); |
| 140 | + if (allowNoJobs != null) { |
| 141 | + builder.field(ALLOW_NO_JOBS.getPreferredName(), allowNoJobs); |
| 142 | + } |
| 143 | + builder.endObject(); |
| 144 | + return builder; |
| 145 | + } |
| 146 | +} |
0 commit comments