Browse Source

HLRC: Adding ML Job stats (#33183)

* HLRC: Adding pojos for get job stats

HLRC: Adding pojos for job stats request

* HLRC: Adding job stats pojos

* HLRC: ML job stats

* Minor syntax changes and adding license headers

* minor comment change

* Moving to client package, minor changes

* Addressing PR comments

* removing bad sleep

* addressing minor comment around test methods

* adding toplevel random fields for tests

* addressing minor review comments
Benjamin Trent 7 years ago
parent
commit
19b14fa5ed
21 changed files with 1596 additions and 2 deletions
  1. 18 0
      client/rest-high-level/src/main/java/org/elasticsearch/client/MLRequestConverters.java
  2. 44 0
      client/rest-high-level/src/main/java/org/elasticsearch/client/MachineLearningClient.java
  3. 146 0
      client/rest-high-level/src/main/java/org/elasticsearch/client/ml/GetJobStatsRequest.java
  4. 88 0
      client/rest-high-level/src/main/java/org/elasticsearch/client/ml/GetJobStatsResponse.java
  5. 150 0
      client/rest-high-level/src/main/java/org/elasticsearch/client/ml/NodeAttributes.java
  6. 39 0
      client/rest-high-level/src/main/java/org/elasticsearch/client/ml/job/config/JobState.java
  7. 174 0
      client/rest-high-level/src/main/java/org/elasticsearch/client/ml/job/stats/ForecastStats.java
  8. 225 0
      client/rest-high-level/src/main/java/org/elasticsearch/client/ml/job/stats/JobStats.java
  9. 117 0
      client/rest-high-level/src/main/java/org/elasticsearch/client/ml/job/stats/SimpleStats.java
  10. 18 0
      client/rest-high-level/src/test/java/org/elasticsearch/client/MLRequestConvertersTests.java
  11. 65 1
      client/rest-high-level/src/test/java/org/elasticsearch/client/MachineLearningIT.java
  12. 61 0
      client/rest-high-level/src/test/java/org/elasticsearch/client/documentation/MlClientDocumentationIT.java
  13. 7 1
      client/rest-high-level/src/test/java/org/elasticsearch/client/ml/GetJobResponseTests.java
  14. 69 0
      client/rest-high-level/src/test/java/org/elasticsearch/client/ml/GetJobStatsRequestTests.java
  15. 53 0
      client/rest-high-level/src/test/java/org/elasticsearch/client/ml/GetJobStatsResponseTests.java
  16. 64 0
      client/rest-high-level/src/test/java/org/elasticsearch/client/ml/NodeAttributesTests.java
  17. 70 0
      client/rest-high-level/src/test/java/org/elasticsearch/client/ml/job/stats/ForecastStatsTests.java
  18. 72 0
      client/rest-high-level/src/test/java/org/elasticsearch/client/ml/job/stats/JobStatsTests.java
  19. 47 0
      client/rest-high-level/src/test/java/org/elasticsearch/client/ml/job/stats/SimpleStatsTests.java
  20. 67 0
      docs/java-rest/high-level/ml/get-job-stats.asciidoc
  21. 2 0
      docs/java-rest/high-level/supported-apis.asciidoc

+ 18 - 0
client/rest-high-level/src/main/java/org/elasticsearch/client/MLRequestConverters.java

@@ -28,6 +28,7 @@ import org.elasticsearch.client.ml.CloseJobRequest;
 import org.elasticsearch.client.ml.DeleteJobRequest;
 import org.elasticsearch.client.ml.GetBucketsRequest;
 import org.elasticsearch.client.ml.GetJobRequest;
+import org.elasticsearch.client.ml.GetJobStatsRequest;
 import org.elasticsearch.client.ml.GetRecordsRequest;
 import org.elasticsearch.client.ml.OpenJobRequest;
 import org.elasticsearch.client.ml.PutJobRequest;
@@ -126,6 +127,23 @@ final class MLRequestConverters {
         return request;
     }
 
+    static Request getJobStats(GetJobStatsRequest getJobStatsRequest) {
+        String endpoint = new EndpointBuilder()
+            .addPathPartAsIs("_xpack")
+            .addPathPartAsIs("ml")
+            .addPathPartAsIs("anomaly_detectors")
+            .addPathPart(Strings.collectionToCommaDelimitedString(getJobStatsRequest.getJobIds()))
+            .addPathPartAsIs("_stats")
+            .build();
+        Request request = new Request(HttpGet.METHOD_NAME, endpoint);
+
+        RequestConverters.Params params = new RequestConverters.Params(request);
+        if (getJobStatsRequest.isAllowNoJobs() != null) {
+            params.putParam("allow_no_jobs", Boolean.toString(getJobStatsRequest.isAllowNoJobs()));
+        }
+        return request;
+    }
+
     static Request getRecords(GetRecordsRequest getRecordsRequest) throws IOException {
         String endpoint = new EndpointBuilder()
                 .addPathPartAsIs("_xpack")

+ 44 - 0
client/rest-high-level/src/main/java/org/elasticsearch/client/MachineLearningClient.java

@@ -19,6 +19,9 @@
 package org.elasticsearch.client;
 
 import org.elasticsearch.action.ActionListener;
+import org.elasticsearch.client.ml.GetJobStatsRequest;
+import org.elasticsearch.client.ml.GetJobStatsResponse;
+import org.elasticsearch.client.ml.job.stats.JobStats;
 import org.elasticsearch.client.ml.CloseJobRequest;
 import org.elasticsearch.client.ml.CloseJobResponse;
 import org.elasticsearch.client.ml.DeleteJobRequest;
@@ -288,6 +291,47 @@ public final class MachineLearningClient {
                 Collections.emptySet());
      }
 
+    /**
+     * Gets usage statistics for one or more Machine Learning jobs
+     *
+     * <p>
+     *     For additional info
+     *     see <a href="https://www.elastic.co/guide/en/elasticsearch/reference/current/ml-get-job-stats.html">Get Job stats docs</a>
+     * </p>
+     * @param request {@link GetJobStatsRequest} Request containing a list of jobId(s) and additional options
+     * @param options  Additional request options (e.g. headers), use {@link RequestOptions#DEFAULT} if nothing needs to be customized
+     * @return {@link GetJobStatsResponse} response object containing
+     * the {@link JobStats} objects and the number of jobs found
+     * @throws IOException when there is a serialization issue sending the request or receiving the response
+     */
+    public GetJobStatsResponse getJobStats(GetJobStatsRequest request, RequestOptions options) throws IOException {
+        return restHighLevelClient.performRequestAndParseEntity(request,
+            MLRequestConverters::getJobStats,
+            options,
+            GetJobStatsResponse::fromXContent,
+            Collections.emptySet());
+    }
+
+    /**
+     * Gets one or more Machine Learning job configuration info, asynchronously.
+     *
+     * <p>
+     *     For additional info
+     *     see <a href="https://www.elastic.co/guide/en/elasticsearch/reference/current/ml-get-job-stats.html">Get Job stats docs</a>
+     * </p>
+     * @param request {@link GetJobStatsRequest} Request containing a list of jobId(s) and additional options
+     * @param options  Additional request options (e.g. headers), use {@link RequestOptions#DEFAULT} if nothing needs to be customized
+     * @param listener Listener to be notified with {@link GetJobStatsResponse} upon request completion
+     */
+    public void getJobStatsAsync(GetJobStatsRequest request, RequestOptions options, ActionListener<GetJobStatsResponse> listener) {
+        restHighLevelClient.performRequestAsyncAndParseEntity(request,
+            MLRequestConverters::getJobStats,
+            options,
+            GetJobStatsResponse::fromXContent,
+            listener,
+            Collections.emptySet());
+    }
+
     /**
      * Gets the records for a Machine Learning Job.
      * <p>

+ 146 - 0
client/rest-high-level/src/main/java/org/elasticsearch/client/ml/GetJobStatsRequest.java

@@ -0,0 +1,146 @@
+/*
+ * Licensed to Elasticsearch under one or more contributor
+ * license agreements. See the NOTICE file distributed with
+ * this work for additional information regarding copyright
+ * ownership. Elasticsearch licenses this file to you under
+ * the Apache License, Version 2.0 (the "License"); you may
+ * not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ *    http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied.  See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+package org.elasticsearch.client.ml;
+
+import org.elasticsearch.action.ActionRequest;
+import org.elasticsearch.action.ActionRequestValidationException;
+import org.elasticsearch.client.ml.job.config.Job;
+import org.elasticsearch.common.ParseField;
+import org.elasticsearch.common.Strings;
+import org.elasticsearch.common.xcontent.ConstructingObjectParser;
+import org.elasticsearch.common.xcontent.ObjectParser;
+import org.elasticsearch.common.xcontent.ToXContentObject;
+import org.elasticsearch.common.xcontent.XContentBuilder;
+
+import java.io.IOException;
+import java.util.ArrayList;
+import java.util.Arrays;
+import java.util.List;
+import java.util.Objects;
+
+
+/**
+ * Request object to get {@link org.elasticsearch.client.ml.job.stats.JobStats} by their respective jobIds
+ *
+ * `_all` explicitly gets all the jobs' statistics in the cluster
+ * An empty request (no `jobId`s) implicitly gets all the jobs' statistics in the cluster
+ */
+public class GetJobStatsRequest extends ActionRequest implements ToXContentObject {
+
+    public static final ParseField ALLOW_NO_JOBS = new ParseField("allow_no_jobs");
+
+    @SuppressWarnings("unchecked")
+    public static final ConstructingObjectParser<GetJobStatsRequest, Void> PARSER = new ConstructingObjectParser<>(
+        "get_jobs_stats_request", a -> new GetJobStatsRequest((List<String>) a[0]));
+
+    static {
+        PARSER.declareField(ConstructingObjectParser.constructorArg(),
+            p -> Arrays.asList(Strings.commaDelimitedListToStringArray(p.text())),
+            Job.ID, ObjectParser.ValueType.STRING_ARRAY);
+        PARSER.declareBoolean(GetJobStatsRequest::setAllowNoJobs, ALLOW_NO_JOBS);
+    }
+
+    private static final String ALL_JOBS = "_all";
+
+    private final List<String> jobIds;
+    private Boolean allowNoJobs;
+
+    /**
+     * Explicitly gets all jobs statistics
+     *
+     * @return a {@link GetJobStatsRequest} for all existing jobs
+     */
+    public static GetJobStatsRequest getAllJobStatsRequest(){
+        return new GetJobStatsRequest(ALL_JOBS);
+    }
+
+    GetJobStatsRequest(List<String> jobIds) {
+        if (jobIds.stream().anyMatch(Objects::isNull)) {
+            throw new NullPointerException("jobIds must not contain null values");
+        }
+        this.jobIds = new ArrayList<>(jobIds);
+    }
+
+    /**
+     * Get the specified Job's statistics via their unique jobIds
+     *
+     * @param jobIds must be non-null and each jobId must be non-null
+     */
+    public GetJobStatsRequest(String... jobIds) {
+        this(Arrays.asList(jobIds));
+    }
+
+    /**
+     * All the jobIds for which to get statistics
+     */
+    public List<String> getJobIds() {
+        return jobIds;
+    }
+
+    public Boolean isAllowNoJobs() {
+        return this.allowNoJobs;
+    }
+
+    /**
+     * Whether to ignore if a wildcard expression matches no jobs.
+     *
+     * This includes `_all` string or when no jobs have been specified
+     *
+     * @param allowNoJobs When {@code true} ignore if wildcard or `_all` matches no jobs. Defaults to {@code true}
+     */
+    public void setAllowNoJobs(boolean allowNoJobs) {
+        this.allowNoJobs = allowNoJobs;
+    }
+
+    @Override
+    public int hashCode() {
+        return Objects.hash(jobIds, allowNoJobs);
+    }
+
+    @Override
+    public boolean equals(Object other) {
+        if (this == other) {
+            return true;
+        }
+
+        if (other == null || getClass() != other.getClass()) {
+            return false;
+        }
+
+        GetJobStatsRequest that = (GetJobStatsRequest) other;
+        return Objects.equals(jobIds, that.jobIds) &&
+            Objects.equals(allowNoJobs, that.allowNoJobs);
+    }
+
+    @Override
+    public ActionRequestValidationException validate() {
+        return null;
+    }
+
+    @Override
+    public XContentBuilder toXContent(XContentBuilder builder, Params params) throws IOException {
+        builder.startObject();
+        builder.field(Job.ID.getPreferredName(), Strings.collectionToCommaDelimitedString(jobIds));
+        if (allowNoJobs != null) {
+            builder.field(ALLOW_NO_JOBS.getPreferredName(), allowNoJobs);
+        }
+        builder.endObject();
+        return builder;
+    }
+}

+ 88 - 0
client/rest-high-level/src/main/java/org/elasticsearch/client/ml/GetJobStatsResponse.java

@@ -0,0 +1,88 @@
+/*
+ * Licensed to Elasticsearch under one or more contributor
+ * license agreements. See the NOTICE file distributed with
+ * this work for additional information regarding copyright
+ * ownership. Elasticsearch licenses this file to you under
+ * the Apache License, Version 2.0 (the "License"); you may
+ * not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ *    http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied.  See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+package org.elasticsearch.client.ml;
+
+import org.elasticsearch.common.ParseField;
+import org.elasticsearch.common.Strings;
+import org.elasticsearch.common.xcontent.ConstructingObjectParser;
+import org.elasticsearch.common.xcontent.XContentParser;
+import org.elasticsearch.client.ml.job.stats.JobStats;
+
+import java.io.IOException;
+import java.util.List;
+import java.util.Objects;
+
+import static org.elasticsearch.common.xcontent.ConstructingObjectParser.constructorArg;
+
+/**
+ * Contains a {@link List} of the found {@link JobStats} objects and the total count found
+ */
+public class GetJobStatsResponse extends AbstractResultResponse<JobStats>  {
+
+    public static final ParseField RESULTS_FIELD = new ParseField("jobs");
+
+    @SuppressWarnings("unchecked")
+    public static final ConstructingObjectParser<GetJobStatsResponse, Void> PARSER =
+        new ConstructingObjectParser<>("jobs_stats_response", true,
+            a -> new GetJobStatsResponse((List<JobStats>) a[0], (long) a[1]));
+
+    static {
+        PARSER.declareObjectArray(constructorArg(), JobStats.PARSER, RESULTS_FIELD);
+        PARSER.declareLong(constructorArg(), COUNT);
+    }
+
+    GetJobStatsResponse(List<JobStats> jobStats, long count) {
+        super(RESULTS_FIELD, jobStats, count);
+    }
+
+    /**
+     * The collection of {@link JobStats} objects found in the query
+     */
+    public List<JobStats> jobStats() {
+        return results;
+    }
+
+    public static GetJobStatsResponse fromXContent(XContentParser parser) throws IOException {
+        return PARSER.parse(parser, null);
+    }
+
+    @Override
+    public int hashCode() {
+        return Objects.hash(results, count);
+    }
+
+    @Override
+    public boolean equals(Object obj) {
+        if (this == obj) {
+            return true;
+        }
+
+        if (obj == null || getClass() != obj.getClass()) {
+            return false;
+        }
+
+        GetJobStatsResponse other = (GetJobStatsResponse) obj;
+        return Objects.equals(results, other.results) && count == other.count;
+    }
+
+    @Override
+    public final String toString() {
+        return Strings.toString(this);
+    }
+}

+ 150 - 0
client/rest-high-level/src/main/java/org/elasticsearch/client/ml/NodeAttributes.java

@@ -0,0 +1,150 @@
+/*
+ * Licensed to Elasticsearch under one or more contributor
+ * license agreements. See the NOTICE file distributed with
+ * this work for additional information regarding copyright
+ * ownership. Elasticsearch licenses this file to you under
+ * the Apache License, Version 2.0 (the "License"); you may
+ * not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ *    http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied.  See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+package org.elasticsearch.client.ml;
+
+import org.elasticsearch.common.ParseField;
+import org.elasticsearch.common.xcontent.ConstructingObjectParser;
+import org.elasticsearch.common.xcontent.ObjectParser;
+import org.elasticsearch.common.xcontent.ToXContentObject;
+import org.elasticsearch.common.xcontent.XContentBuilder;
+
+import java.io.IOException;
+import java.util.Collections;
+import java.util.Map;
+import java.util.Objects;
+
+/**
+ * A Pojo class containing an Elastic Node's attributes
+ */
+public class NodeAttributes implements ToXContentObject {
+
+    public static final ParseField ID = new ParseField("id");
+    public static final ParseField NAME = new ParseField("name");
+    public static final ParseField EPHEMERAL_ID = new ParseField("ephemeral_id");
+    public static final ParseField TRANSPORT_ADDRESS = new ParseField("transport_address");
+    public static final ParseField ATTRIBUTES = new ParseField("attributes");
+
+    @SuppressWarnings("unchecked")
+    public static final ConstructingObjectParser<NodeAttributes, Void> PARSER =
+        new ConstructingObjectParser<>("node", true,
+            (a) -> {
+                int i = 0;
+                String id = (String) a[i++];
+                String name = (String) a[i++];
+                String ephemeralId = (String) a[i++];
+                String transportAddress = (String) a[i++];
+                Map<String, String> attributes = (Map<String, String>) a[i];
+                return new NodeAttributes(id, name, ephemeralId, transportAddress, attributes);
+            });
+
+    static {
+        PARSER.declareString(ConstructingObjectParser.constructorArg(), ID);
+        PARSER.declareString(ConstructingObjectParser.constructorArg(), NAME);
+        PARSER.declareString(ConstructingObjectParser.constructorArg(), EPHEMERAL_ID);
+        PARSER.declareString(ConstructingObjectParser.constructorArg(), TRANSPORT_ADDRESS);
+        PARSER.declareField(ConstructingObjectParser.constructorArg(),
+            (p, c) -> p.mapStrings(),
+            ATTRIBUTES,
+            ObjectParser.ValueType.OBJECT);
+    }
+
+    private final String id;
+    private final String name;
+    private final String ephemeralId;
+    private final String transportAddress;
+    private final Map<String, String> attributes;
+
+    public NodeAttributes(String id, String name, String ephemeralId, String transportAddress, Map<String, String> attributes) {
+        this.id = id;
+        this.name = name;
+        this.ephemeralId = ephemeralId;
+        this.transportAddress = transportAddress;
+        this.attributes = Collections.unmodifiableMap(attributes);
+    }
+
+    /**
+     * The unique identifier of the node.
+     */
+    public String getId() {
+        return id;
+    }
+
+    /**
+     * The node name.
+     */
+    public String getName() {
+        return name;
+    }
+
+    /**
+     * The ephemeral id of the node.
+     */
+    public String getEphemeralId() {
+        return ephemeralId;
+    }
+
+    /**
+     * The host and port where transport HTTP connections are accepted.
+     */
+    public String getTransportAddress() {
+        return transportAddress;
+    }
+
+    /**
+     * Additional attributes related to this node e.g., {"ml.max_open_jobs": "10"}.
+     */
+    public Map<String, String> getAttributes() {
+        return attributes;
+    }
+
+    @Override
+    public XContentBuilder toXContent(XContentBuilder builder, Params params) throws IOException {
+        builder.startObject();
+        builder.field(ID.getPreferredName(), id);
+        builder.field(NAME.getPreferredName(), name);
+        builder.field(EPHEMERAL_ID.getPreferredName(), ephemeralId);
+        builder.field(TRANSPORT_ADDRESS.getPreferredName(), transportAddress);
+        builder.field(ATTRIBUTES.getPreferredName(), attributes);
+        builder.endObject();
+        return builder;
+    }
+
+    @Override
+    public int hashCode() {
+        return Objects.hash(id, name, ephemeralId, transportAddress, attributes);
+    }
+
+    @Override
+    public boolean equals(Object other) {
+        if (this == other) {
+            return true;
+        }
+
+        if (other == null || getClass() != other.getClass()) {
+            return false;
+        }
+
+        NodeAttributes that = (NodeAttributes) other;
+        return Objects.equals(id, that.id) &&
+            Objects.equals(name, that.name) &&
+            Objects.equals(ephemeralId, that.ephemeralId) &&
+            Objects.equals(transportAddress, that.transportAddress) &&
+            Objects.equals(attributes, that.attributes);
+    }
+}

+ 39 - 0
client/rest-high-level/src/main/java/org/elasticsearch/client/ml/job/config/JobState.java

@@ -0,0 +1,39 @@
+/*
+ * Licensed to Elasticsearch under one or more contributor
+ * license agreements. See the NOTICE file distributed with
+ * this work for additional information regarding copyright
+ * ownership. Elasticsearch licenses this file to you under
+ * the Apache License, Version 2.0 (the "License"); you may
+ * not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ *    http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied.  See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+package org.elasticsearch.client.ml.job.config;
+
+import java.util.Locale;
+
+/**
+ * Jobs whether running or complete are in one of these states.
+ * When a job is created it is initialised in the state closed
+ * i.e. it is not running.
+ */
+public enum JobState {
+
+    CLOSING, CLOSED, OPENED, FAILED, OPENING;
+
+    public static JobState fromString(String name) {
+        return valueOf(name.trim().toUpperCase(Locale.ROOT));
+    }
+
+    public String value() {
+        return name().toLowerCase(Locale.ROOT);
+    }
+}

+ 174 - 0
client/rest-high-level/src/main/java/org/elasticsearch/client/ml/job/stats/ForecastStats.java

@@ -0,0 +1,174 @@
+/*
+ * Licensed to Elasticsearch under one or more contributor
+ * license agreements. See the NOTICE file distributed with
+ * this work for additional information regarding copyright
+ * ownership. Elasticsearch licenses this file to you under
+ * the Apache License, Version 2.0 (the "License"); you may
+ * not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ *    http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied.  See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+package org.elasticsearch.client.ml.job.stats;
+
+import org.elasticsearch.common.ParseField;
+import org.elasticsearch.common.xcontent.ConstructingObjectParser;
+import org.elasticsearch.common.xcontent.ObjectParser;
+import org.elasticsearch.common.xcontent.ToXContentObject;
+import org.elasticsearch.common.xcontent.XContentBuilder;
+
+import java.io.IOException;
+import java.util.Collections;
+import java.util.HashMap;
+import java.util.Map;
+import java.util.Objects;
+
+/**
+ * A class to hold statistics about forecasts.
+ */
+public class ForecastStats implements ToXContentObject {
+
+    public static final ParseField TOTAL = new ParseField("total");
+    public static final ParseField FORECASTED_JOBS = new ParseField("forecasted_jobs");
+    public static final ParseField MEMORY_BYTES = new ParseField("memory_bytes");
+    public static final ParseField PROCESSING_TIME_MS = new ParseField("processing_time_ms");
+    public static final ParseField RECORDS = new ParseField("records");
+    public static final ParseField STATUS = new ParseField("status");
+
+    @SuppressWarnings("unchecked")
+    public static final ConstructingObjectParser<ForecastStats, Void> PARSER =
+        new ConstructingObjectParser<>("forecast_stats",
+            true,
+            (a) -> {
+                int i = 0;
+                long total = (long)a[i++];
+                SimpleStats memoryStats = (SimpleStats)a[i++];
+                SimpleStats recordStats = (SimpleStats)a[i++];
+                SimpleStats runtimeStats = (SimpleStats)a[i++];
+                Map<String, Long> statusCounts = (Map<String, Long>)a[i];
+                return new ForecastStats(total, memoryStats, recordStats, runtimeStats, statusCounts);
+            });
+
+    static {
+        PARSER.declareLong(ConstructingObjectParser.constructorArg(), TOTAL);
+        PARSER.declareObject(ConstructingObjectParser.optionalConstructorArg(), SimpleStats.PARSER, MEMORY_BYTES);
+        PARSER.declareObject(ConstructingObjectParser.optionalConstructorArg(), SimpleStats.PARSER, RECORDS);
+        PARSER.declareObject(ConstructingObjectParser.optionalConstructorArg(), SimpleStats.PARSER, PROCESSING_TIME_MS);
+        PARSER.declareField(ConstructingObjectParser.optionalConstructorArg(),
+            p -> {
+                Map<String, Long> counts = new HashMap<>();
+                p.map().forEach((key, value) -> counts.put(key, ((Number)value).longValue()));
+                return counts;
+            }, STATUS, ObjectParser.ValueType.OBJECT);
+    }
+
+    private final long total;
+    private final long forecastedJobs;
+    private SimpleStats memoryStats;
+    private SimpleStats recordStats;
+    private SimpleStats runtimeStats;
+    private Map<String, Long> statusCounts;
+
+    public ForecastStats(long total,
+                         SimpleStats memoryStats,
+                         SimpleStats recordStats,
+                         SimpleStats runtimeStats,
+                         Map<String, Long> statusCounts) {
+        this.total = total;
+        this.forecastedJobs = total > 0 ? 1 : 0;
+        if (total > 0) {
+            this.memoryStats = Objects.requireNonNull(memoryStats);
+            this.recordStats = Objects.requireNonNull(recordStats);
+            this.runtimeStats = Objects.requireNonNull(runtimeStats);
+            this.statusCounts = Collections.unmodifiableMap(statusCounts);
+        }
+    }
+
+    /**
+     * The number of forecasts currently available for this model.
+     */
+    public long getTotal() {
+        return total;
+    }
+
+    /**
+     * The number of jobs that have at least one forecast.
+     */
+    public long getForecastedJobs() {
+        return forecastedJobs;
+    }
+
+    /**
+     * Statistics about the memory usage: minimum, maximum, average and total.
+     */
+    public SimpleStats getMemoryStats() {
+        return memoryStats;
+    }
+
+    /**
+     * Statistics about the number of forecast records: minimum, maximum, average and total.
+     */
+    public SimpleStats getRecordStats() {
+        return recordStats;
+    }
+
+    /**
+     * Statistics about the forecast runtime in milliseconds: minimum, maximum, average and total
+     */
+    public SimpleStats getRuntimeStats() {
+        return runtimeStats;
+    }
+
+    /**
+     * Counts per forecast status, for example: {"finished" : 2}.
+     */
+    public Map<String, Long> getStatusCounts() {
+        return statusCounts;
+    }
+
+    @Override
+    public XContentBuilder toXContent(XContentBuilder builder, Params params) throws IOException {
+        builder.startObject();
+        builder.field(TOTAL.getPreferredName(), total);
+        builder.field(FORECASTED_JOBS.getPreferredName(), forecastedJobs);
+
+        if (total > 0) {
+            builder.field(MEMORY_BYTES.getPreferredName(), memoryStats);
+            builder.field(RECORDS.getPreferredName(), recordStats);
+            builder.field(PROCESSING_TIME_MS.getPreferredName(), runtimeStats);
+            builder.field(STATUS.getPreferredName(), statusCounts);
+        }
+        return builder.endObject();
+    }
+
+    @Override
+    public int hashCode() {
+        return Objects.hash(total, forecastedJobs, memoryStats, recordStats, runtimeStats, statusCounts);
+    }
+
+    @Override
+    public boolean equals(Object obj) {
+        if (this == obj) {
+            return true;
+        }
+
+        if (obj == null || getClass() != obj.getClass()) {
+            return false;
+        }
+
+        ForecastStats other = (ForecastStats) obj;
+        return Objects.equals(total, other.total) &&
+            Objects.equals(forecastedJobs, other.forecastedJobs) &&
+            Objects.equals(memoryStats, other.memoryStats) &&
+            Objects.equals(recordStats, other.recordStats) &&
+            Objects.equals(runtimeStats, other.runtimeStats) &&
+            Objects.equals(statusCounts, other.statusCounts);
+    }
+}

+ 225 - 0
client/rest-high-level/src/main/java/org/elasticsearch/client/ml/job/stats/JobStats.java

@@ -0,0 +1,225 @@
+/*
+ * Licensed to Elasticsearch under one or more contributor
+ * license agreements. See the NOTICE file distributed with
+ * this work for additional information regarding copyright
+ * ownership. Elasticsearch licenses this file to you under
+ * the Apache License, Version 2.0 (the "License"); you may
+ * not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ *    http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied.  See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+package org.elasticsearch.client.ml.job.stats;
+
+import org.elasticsearch.client.ml.job.config.Job;
+import org.elasticsearch.client.ml.job.config.JobState;
+import org.elasticsearch.client.ml.job.process.DataCounts;
+import org.elasticsearch.client.ml.job.process.ModelSizeStats;
+import org.elasticsearch.common.Nullable;
+import org.elasticsearch.common.ParseField;
+import org.elasticsearch.common.unit.TimeValue;
+import org.elasticsearch.common.xcontent.ConstructingObjectParser;
+import org.elasticsearch.common.xcontent.ObjectParser;
+import org.elasticsearch.common.xcontent.ToXContentObject;
+import org.elasticsearch.common.xcontent.XContentBuilder;
+import org.elasticsearch.client.ml.NodeAttributes;
+
+import java.io.IOException;
+import java.util.Objects;
+
+/**
+ * Class containing the statistics for a Machine Learning job.
+ *
+ */
+public class JobStats implements ToXContentObject {
+
+    private static final ParseField DATA_COUNTS = new ParseField("data_counts");
+    private static final ParseField MODEL_SIZE_STATS = new ParseField("model_size_stats");
+    private static final ParseField FORECASTS_STATS = new ParseField("forecasts_stats");
+    private static final ParseField STATE = new ParseField("state");
+    private static final ParseField NODE = new ParseField("node");
+    private static final ParseField OPEN_TIME = new ParseField("open_time");
+    private static final ParseField ASSIGNMENT_EXPLANATION = new ParseField("assignment_explanation");
+
+    public static final ConstructingObjectParser<JobStats, Void> PARSER =
+        new ConstructingObjectParser<>("job_stats",
+            true,
+            (a) -> {
+                int i = 0;
+                String jobId = (String) a[i++];
+                DataCounts dataCounts = (DataCounts) a[i++];
+                JobState jobState = (JobState) a[i++];
+                ModelSizeStats.Builder modelSizeStatsBuilder = (ModelSizeStats.Builder) a[i++];
+                ModelSizeStats modelSizeStats = modelSizeStatsBuilder == null ? null : modelSizeStatsBuilder.build();
+                ForecastStats forecastStats = (ForecastStats) a[i++];
+                NodeAttributes node = (NodeAttributes) a[i++];
+                String assignmentExplanation = (String) a[i++];
+                TimeValue openTime = (TimeValue) a[i];
+                return new JobStats(jobId,
+                    dataCounts,
+                    jobState,
+                    modelSizeStats,
+                    forecastStats,
+                    node,
+                    assignmentExplanation,
+                    openTime);
+            });
+
+    static {
+        PARSER.declareString(ConstructingObjectParser.constructorArg(), Job.ID);
+        PARSER.declareObject(ConstructingObjectParser.constructorArg(), DataCounts.PARSER, DATA_COUNTS);
+        PARSER.declareField(ConstructingObjectParser.constructorArg(),
+            (p) -> JobState.fromString(p.text()),
+            STATE,
+            ObjectParser.ValueType.VALUE);
+        PARSER.declareObject(ConstructingObjectParser.optionalConstructorArg(), ModelSizeStats.PARSER, MODEL_SIZE_STATS);
+        PARSER.declareObject(ConstructingObjectParser.optionalConstructorArg(), ForecastStats.PARSER, FORECASTS_STATS);
+        PARSER.declareObject(ConstructingObjectParser.optionalConstructorArg(), NodeAttributes.PARSER, NODE);
+        PARSER.declareString(ConstructingObjectParser.optionalConstructorArg(), ASSIGNMENT_EXPLANATION);
+        PARSER.declareField(ConstructingObjectParser.optionalConstructorArg(),
+            (p, c) -> TimeValue.parseTimeValue(p.textOrNull(), OPEN_TIME.getPreferredName()),
+            OPEN_TIME,
+            ObjectParser.ValueType.STRING_OR_NULL);
+    }
+
+
+    private final String jobId;
+    private final DataCounts dataCounts;
+    private final JobState state;
+    private final ModelSizeStats modelSizeStats;
+    private final ForecastStats forecastStats;
+    private final NodeAttributes node;
+    private final String assignmentExplanation;
+    private final TimeValue openTime;
+
+    JobStats(String jobId, DataCounts dataCounts, JobState state, @Nullable ModelSizeStats modelSizeStats,
+                    @Nullable ForecastStats forecastStats, @Nullable NodeAttributes node,
+                    @Nullable String assignmentExplanation, @Nullable TimeValue opentime) {
+        this.jobId = Objects.requireNonNull(jobId);
+        this.dataCounts = Objects.requireNonNull(dataCounts);
+        this.state = Objects.requireNonNull(state);
+        this.modelSizeStats = modelSizeStats;
+        this.forecastStats = forecastStats;
+        this.node = node;
+        this.assignmentExplanation = assignmentExplanation;
+        this.openTime = opentime;
+    }
+
+    /**
+     * The jobId referencing the job for these statistics
+     */
+    public String getJobId() {
+        return jobId;
+    }
+
+    /**
+     * An object that describes the number of records processed and any related error counts
+     * See {@link DataCounts}
+     */
+    public DataCounts getDataCounts() {
+        return dataCounts;
+    }
+
+    /**
+     * An object that provides information about the size and contents of the model.
+     * See {@link ModelSizeStats}
+     */
+    public ModelSizeStats getModelSizeStats() {
+        return modelSizeStats;
+    }
+
+    /**
+     * An object that provides statistical information about forecasts of this job.
+     * See {@link ForecastStats}
+     */
+    public ForecastStats getForecastStats() {
+        return forecastStats;
+    }
+
+    /**
+     * The status of the job
+     * See {@link JobState}
+     */
+    public JobState getState() {
+        return state;
+    }
+
+    /**
+     * For open jobs only, contains information about the node where the job runs
+     * See {@link NodeAttributes}
+     */
+    public NodeAttributes getNode() {
+        return node;
+    }
+
+    /**
+     * For open jobs only, contains messages relating to the selection of a node to run the job.
+     */
+    public String getAssignmentExplanation() {
+        return assignmentExplanation;
+    }
+
+    /**
+     * For open jobs only, the elapsed time for which the job has been open
+     */
+    public TimeValue getOpenTime() {
+        return openTime;
+    }
+
+    @Override
+    public XContentBuilder toXContent(XContentBuilder builder, Params params) throws IOException {
+        builder.startObject();
+        builder.field(Job.ID.getPreferredName(), jobId);
+        builder.field(DATA_COUNTS.getPreferredName(), dataCounts);
+        builder.field(STATE.getPreferredName(), state.toString());
+        if (modelSizeStats != null) {
+            builder.field(MODEL_SIZE_STATS.getPreferredName(), modelSizeStats);
+        }
+        if (forecastStats != null) {
+            builder.field(FORECASTS_STATS.getPreferredName(), forecastStats);
+        }
+        if (node != null) {
+            builder.field(NODE.getPreferredName(), node);
+        }
+        if (assignmentExplanation != null) {
+            builder.field(ASSIGNMENT_EXPLANATION.getPreferredName(), assignmentExplanation);
+        }
+        if (openTime != null) {
+            builder.field(OPEN_TIME.getPreferredName(), openTime.getStringRep());
+        }
+        return builder.endObject();
+    }
+
+    @Override
+    public int hashCode() {
+        return Objects.hash(jobId, dataCounts, modelSizeStats, forecastStats, state, node, assignmentExplanation, openTime);
+    }
+
+    @Override
+    public boolean equals(Object obj) {
+        if (this == obj) {
+            return true;
+        }
+
+        if (obj == null || getClass() != obj.getClass()) {
+            return false;
+        }
+
+        JobStats other = (JobStats) obj;
+        return Objects.equals(jobId, other.jobId) &&
+            Objects.equals(this.dataCounts, other.dataCounts) &&
+            Objects.equals(this.modelSizeStats, other.modelSizeStats) &&
+            Objects.equals(this.forecastStats, other.forecastStats) &&
+            Objects.equals(this.state, other.state) &&
+            Objects.equals(this.node, other.node) &&
+            Objects.equals(this.assignmentExplanation, other.assignmentExplanation) &&
+            Objects.equals(this.openTime, other.openTime);
+    }
+}

+ 117 - 0
client/rest-high-level/src/main/java/org/elasticsearch/client/ml/job/stats/SimpleStats.java

@@ -0,0 +1,117 @@
+/*
+ * Licensed to Elasticsearch under one or more contributor
+ * license agreements. See the NOTICE file distributed with
+ * this work for additional information regarding copyright
+ * ownership. Elasticsearch licenses this file to you under
+ * the Apache License, Version 2.0 (the "License"); you may
+ * not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ *    http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied.  See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+package org.elasticsearch.client.ml.job.stats;
+
+import org.elasticsearch.common.ParseField;
+import org.elasticsearch.common.xcontent.ConstructingObjectParser;
+import org.elasticsearch.common.xcontent.ToXContentObject;
+import org.elasticsearch.common.xcontent.XContentBuilder;
+
+import java.io.IOException;
+import java.util.Objects;
+
+/**
+ * Helper class for min, max, avg and total statistics for a quantity
+ */
+public class SimpleStats implements ToXContentObject {
+
+    public static final ParseField MIN = new ParseField("min");
+    public static final ParseField MAX = new ParseField("max");
+    public static final ParseField AVG = new ParseField("avg");
+    public static final ParseField TOTAL = new ParseField("total");
+
+    public static final ConstructingObjectParser<SimpleStats, Void> PARSER = new ConstructingObjectParser<>("simple_stats", true,
+        (a) -> {
+        int i = 0;
+        double total = (double)a[i++];
+            double min = (double)a[i++];
+            double max = (double)a[i++];
+            double avg = (double)a[i++];
+            return new SimpleStats(total, min, max, avg);
+        });
+
+    static {
+        PARSER.declareDouble(ConstructingObjectParser.constructorArg(), TOTAL);
+        PARSER.declareDouble(ConstructingObjectParser.constructorArg(), MIN);
+        PARSER.declareDouble(ConstructingObjectParser.constructorArg(), MAX);
+        PARSER.declareDouble(ConstructingObjectParser.constructorArg(), AVG);
+    }
+
+    private final double total;
+    private final double min;
+    private final double max;
+    private final double avg;
+
+    SimpleStats(double total, double min, double max, double avg) {
+        this.total = total;
+        this.min = min;
+        this.max = max;
+        this.avg = avg;
+    }
+
+    public double getMin() {
+        return min;
+    }
+
+    public double getMax() {
+        return max;
+    }
+
+    public double getAvg() {
+        return avg;
+    }
+
+    public double getTotal() {
+        return total;
+    }
+
+    @Override
+    public int hashCode() {
+        return Objects.hash(total, min, max, avg);
+    }
+
+    @Override
+    public boolean equals(Object obj) {
+        if (this == obj) {
+            return true;
+        }
+
+        if (obj == null || getClass() != obj.getClass()) {
+            return false;
+        }
+
+        SimpleStats other = (SimpleStats) obj;
+        return Objects.equals(total, other.total) &&
+            Objects.equals(min, other.min) &&
+            Objects.equals(avg, other.avg) &&
+            Objects.equals(max, other.max);
+    }
+
+    @Override
+    public XContentBuilder toXContent(XContentBuilder builder, Params params) throws IOException {
+        builder.startObject();
+        builder.field(MIN.getPreferredName(), min);
+        builder.field(MAX.getPreferredName(), max);
+        builder.field(AVG.getPreferredName(), avg);
+        builder.field(TOTAL.getPreferredName(), total);
+        builder.endObject();
+        return builder;
+    }
+}
+

+ 18 - 0
client/rest-high-level/src/test/java/org/elasticsearch/client/MLRequestConvertersTests.java

@@ -36,6 +36,7 @@ import org.elasticsearch.client.ml.job.util.PageParams;
 import org.elasticsearch.common.unit.TimeValue;
 import org.elasticsearch.common.xcontent.XContentParser;
 import org.elasticsearch.common.xcontent.json.JsonXContent;
+import org.elasticsearch.client.ml.GetJobStatsRequest;
 import org.elasticsearch.test.ESTestCase;
 
 import java.io.ByteArrayOutputStream;
@@ -139,6 +140,23 @@ public class MLRequestConvertersTests extends ESTestCase {
         }
     }
 
+    public void testGetJobStats() {
+        GetJobStatsRequest getJobStatsRequestRequest = new GetJobStatsRequest();
+
+        Request request = MLRequestConverters.getJobStats(getJobStatsRequestRequest);
+
+        assertEquals(HttpGet.METHOD_NAME, request.getMethod());
+        assertEquals("/_xpack/ml/anomaly_detectors/_stats", request.getEndpoint());
+        assertFalse(request.getParameters().containsKey("allow_no_jobs"));
+
+        getJobStatsRequestRequest = new GetJobStatsRequest("job1", "jobs*");
+        getJobStatsRequestRequest.setAllowNoJobs(true);
+        request = MLRequestConverters.getJobStats(getJobStatsRequestRequest);
+
+        assertEquals("/_xpack/ml/anomaly_detectors/job1,jobs*/_stats", request.getEndpoint());
+        assertEquals(Boolean.toString(true), request.getParameters().get("allow_no_jobs"));
+    }
+
     private static Job createValidJob(String jobId) {
         AnalysisConfig.Builder analysisConfig = AnalysisConfig.builder(Collections.singletonList(
                 Detector.builder().setFunction("count").build()));

+ 65 - 1
client/rest-high-level/src/test/java/org/elasticsearch/client/MachineLearningIT.java

@@ -19,6 +19,12 @@
 package org.elasticsearch.client;
 
 import com.carrotsearch.randomizedtesting.generators.CodepointSetGenerator;
+import org.elasticsearch.ElasticsearchStatusException;
+import org.elasticsearch.common.unit.TimeValue;
+import org.elasticsearch.client.ml.GetJobStatsRequest;
+import org.elasticsearch.client.ml.GetJobStatsResponse;
+import org.elasticsearch.client.ml.job.config.JobState;
+import org.elasticsearch.client.ml.job.stats.JobStats;
 import org.elasticsearch.client.ml.CloseJobRequest;
 import org.elasticsearch.client.ml.CloseJobResponse;
 import org.elasticsearch.client.ml.DeleteJobRequest;
@@ -33,7 +39,6 @@ import org.elasticsearch.client.ml.job.config.AnalysisConfig;
 import org.elasticsearch.client.ml.job.config.DataDescription;
 import org.elasticsearch.client.ml.job.config.Detector;
 import org.elasticsearch.client.ml.job.config.Job;
-import org.elasticsearch.common.unit.TimeValue;
 import org.junit.After;
 
 import java.io.IOException;
@@ -41,6 +46,7 @@ import java.util.Arrays;
 import java.util.concurrent.TimeUnit;
 import java.util.stream.Collectors;
 
+import static org.hamcrest.CoreMatchers.equalTo;
 import static org.hamcrest.CoreMatchers.hasItems;
 import static org.hamcrest.Matchers.containsInAnyOrder;
 import static org.hamcrest.Matchers.hasSize;
@@ -138,6 +144,64 @@ public class MachineLearningIT extends ESRestHighLevelClientTestCase {
         assertTrue(response.isClosed());
     }
 
+    public void testGetJobStats() throws Exception {
+        String jobId1 = "ml-get-job-stats-test-id-1";
+        String jobId2 = "ml-get-job-stats-test-id-2";
+
+        Job job1 = buildJob(jobId1);
+        Job job2 = buildJob(jobId2);
+        MachineLearningClient machineLearningClient = highLevelClient().machineLearning();
+        machineLearningClient.putJob(new PutJobRequest(job1), RequestOptions.DEFAULT);
+        machineLearningClient.putJob(new PutJobRequest(job2), RequestOptions.DEFAULT);
+
+        machineLearningClient.openJob(new OpenJobRequest(jobId1), RequestOptions.DEFAULT);
+
+        GetJobStatsRequest request = new GetJobStatsRequest(jobId1, jobId2);
+
+        // Test getting specific
+        GetJobStatsResponse response = execute(request, machineLearningClient::getJobStats, machineLearningClient::getJobStatsAsync);
+
+        assertEquals(2, response.count());
+        assertThat(response.jobStats(), hasSize(2));
+        assertThat(response.jobStats().stream().map(JobStats::getJobId).collect(Collectors.toList()), containsInAnyOrder(jobId1, jobId2));
+        for (JobStats stats : response.jobStats()) {
+            if (stats.getJobId().equals(jobId1)) {
+                assertEquals(JobState.OPENED, stats.getState());
+            } else {
+                assertEquals(JobState.CLOSED, stats.getState());
+            }
+        }
+
+        // Test getting all explicitly
+        request = GetJobStatsRequest.getAllJobStatsRequest();
+        response = execute(request, machineLearningClient::getJobStats, machineLearningClient::getJobStatsAsync);
+
+        assertTrue(response.count() >= 2L);
+        assertTrue(response.jobStats().size() >= 2L);
+        assertThat(response.jobStats().stream().map(JobStats::getJobId).collect(Collectors.toList()), hasItems(jobId1, jobId2));
+
+        // Test getting all implicitly
+        response = execute(new GetJobStatsRequest(), machineLearningClient::getJobStats, machineLearningClient::getJobStatsAsync);
+
+        assertTrue(response.count() >= 2L);
+        assertTrue(response.jobStats().size() >= 2L);
+        assertThat(response.jobStats().stream().map(JobStats::getJobId).collect(Collectors.toList()), hasItems(jobId1, jobId2));
+
+        // Test getting all with wildcard
+        request = new GetJobStatsRequest("ml-get-job-stats-test-id-*");
+        response = execute(request, machineLearningClient::getJobStats, machineLearningClient::getJobStatsAsync);
+        assertTrue(response.count() >= 2L);
+        assertTrue(response.jobStats().size() >= 2L);
+        assertThat(response.jobStats().stream().map(JobStats::getJobId).collect(Collectors.toList()), hasItems(jobId1, jobId2));
+
+        // Test when allow_no_jobs is false
+        final GetJobStatsRequest erroredRequest = new GetJobStatsRequest("jobs-that-do-not-exist*");
+        erroredRequest.setAllowNoJobs(false);
+        ElasticsearchStatusException exception = expectThrows(ElasticsearchStatusException.class,
+            () -> execute(erroredRequest, machineLearningClient::getJobStats, machineLearningClient::getJobStatsAsync));
+        assertThat(exception.status().getStatus(), equalTo(404));
+    }
+
     public static String randomValidJobId() {
         CodepointSetGenerator generator = new CodepointSetGenerator("abcdefghijklmnopqrstuvwxyz0123456789".toCharArray());
         return generator.ofCodePointsLength(random(), 10, 10);

+ 61 - 0
client/rest-high-level/src/test/java/org/elasticsearch/client/documentation/MlClientDocumentationIT.java

@@ -35,6 +35,8 @@ import org.elasticsearch.client.ml.GetBucketsRequest;
 import org.elasticsearch.client.ml.GetBucketsResponse;
 import org.elasticsearch.client.ml.GetJobRequest;
 import org.elasticsearch.client.ml.GetJobResponse;
+import org.elasticsearch.client.ml.GetJobStatsRequest;
+import org.elasticsearch.client.ml.GetJobStatsResponse;
 import org.elasticsearch.client.ml.GetRecordsRequest;
 import org.elasticsearch.client.ml.GetRecordsResponse;
 import org.elasticsearch.client.ml.OpenJobRequest;
@@ -50,6 +52,7 @@ import org.elasticsearch.client.ml.job.results.Bucket;
 import org.elasticsearch.client.ml.job.util.PageParams;
 import org.elasticsearch.common.unit.TimeValue;
 import org.elasticsearch.common.xcontent.XContentType;
+import org.elasticsearch.client.ml.job.stats.JobStats;
 import org.junit.After;
 
 import java.io.IOException;
@@ -458,6 +461,64 @@ public class MlClientDocumentationIT extends ESRestHighLevelClientTestCase {
         }
     }
 
+    public void testGetJobStats() throws Exception {
+        RestHighLevelClient client = highLevelClient();
+
+        Job job = MachineLearningIT.buildJob("get-machine-learning-job-stats1");
+        client.machineLearning().putJob(new PutJobRequest(job), RequestOptions.DEFAULT);
+
+        Job secondJob = MachineLearningIT.buildJob("get-machine-learning-job-stats2");
+        client.machineLearning().putJob(new PutJobRequest(secondJob), RequestOptions.DEFAULT);
+
+        {
+            //tag::x-pack-ml-get-job-stats-request
+            GetJobStatsRequest request = new GetJobStatsRequest("get-machine-learning-job-stats1", "get-machine-learning-job-*"); //<1>
+            request.setAllowNoJobs(true); //<2>
+            //end::x-pack-ml-get-job-stats-request
+
+            //tag::x-pack-ml-get-job-stats-execute
+            GetJobStatsResponse response = client.machineLearning().getJobStats(request, RequestOptions.DEFAULT);
+            //end::x-pack-ml-get-job-stats-execute
+
+            //tag::x-pack-ml-get-job-stats-response
+            long numberOfJobStats = response.count(); //<1>
+            List<JobStats> jobStats = response.jobStats(); //<2>
+            //end::x-pack-ml-get-job-stats-response
+
+            assertEquals(2, response.count());
+            assertThat(response.jobStats(), hasSize(2));
+            assertThat(response.jobStats().stream().map(JobStats::getJobId).collect(Collectors.toList()),
+                containsInAnyOrder(job.getId(), secondJob.getId()));
+        }
+        {
+            GetJobStatsRequest request = new GetJobStatsRequest("get-machine-learning-job-stats1", "get-machine-learning-job-*");
+
+            // tag::x-pack-ml-get-job-stats-listener
+            ActionListener<GetJobStatsResponse> listener = new ActionListener<GetJobStatsResponse>() {
+                @Override
+                public void onResponse(GetJobStatsResponse response) {
+                    // <1>
+                }
+
+                @Override
+                public void onFailure(Exception e) {
+                    // <2>
+                }
+            };
+            // end::x-pack-ml-get-job-stats-listener
+
+            // Replace the empty listener by a blocking listener in test
+            final CountDownLatch latch = new CountDownLatch(1);
+            listener = new LatchedActionListener<>(listener, latch);
+
+            // tag::x-pack-ml-get-job-stats-execute-async
+            client.machineLearning().getJobStatsAsync(request, RequestOptions.DEFAULT, listener); // <1>
+            // end::x-pack-ml-get-job-stats-execute-async
+
+            assertTrue(latch.await(30L, TimeUnit.SECONDS));
+        }
+    }
+
     public void testGetRecords() throws IOException, InterruptedException {
         RestHighLevelClient client = highLevelClient();
 

+ 7 - 1
client/rest-high-level/src/test/java/org/elasticsearch/client/ml/GetJobResponseTests.java

@@ -26,6 +26,7 @@ import org.elasticsearch.test.AbstractXContentTestCase;
 import java.io.IOException;
 import java.util.ArrayList;
 import java.util.List;
+import java.util.function.Predicate;
 
 public class GetJobResponseTests extends AbstractXContentTestCase<GetJobResponse> {
 
@@ -46,8 +47,13 @@ public class GetJobResponseTests extends AbstractXContentTestCase<GetJobResponse
         return GetJobResponse.fromXContent(parser);
     }
 
+    @Override
+    protected Predicate<String> getRandomFieldsExcludeFilter() {
+        return field -> !field.isEmpty();
+    }
+
     @Override
     protected boolean supportsUnknownFields() {
-        return false;
+        return true;
     }
 }

+ 69 - 0
client/rest-high-level/src/test/java/org/elasticsearch/client/ml/GetJobStatsRequestTests.java

@@ -0,0 +1,69 @@
+/*
+ * Licensed to Elasticsearch under one or more contributor
+ * license agreements. See the NOTICE file distributed with
+ * this work for additional information regarding copyright
+ * ownership. Elasticsearch licenses this file to you under
+ * the Apache License, Version 2.0 (the "License"); you may
+ * not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ *    http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied.  See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+package org.elasticsearch.client.ml;
+
+import org.elasticsearch.common.xcontent.XContentParser;
+import org.elasticsearch.test.AbstractXContentTestCase;
+
+import java.io.IOException;
+import java.util.ArrayList;
+import java.util.List;
+
+public class GetJobStatsRequestTests extends AbstractXContentTestCase<GetJobStatsRequest> {
+
+    public void testAllJobsRequest() {
+        GetJobStatsRequest request = GetJobStatsRequest.getAllJobStatsRequest();
+
+        assertEquals(request.getJobIds().size(), 1);
+        assertEquals(request.getJobIds().get(0), "_all");
+    }
+
+    public void testNewWithJobId() {
+        Exception exception = expectThrows(NullPointerException.class, () -> new GetJobStatsRequest("job", null));
+        assertEquals(exception.getMessage(), "jobIds must not contain null values");
+    }
+
+    @Override
+    protected GetJobStatsRequest createTestInstance() {
+        int jobCount = randomIntBetween(0, 10);
+        List<String> jobIds = new ArrayList<>(jobCount);
+
+        for (int i = 0; i < jobCount; i++) {
+            jobIds.add(randomAlphaOfLength(10));
+        }
+
+        GetJobStatsRequest request = new GetJobStatsRequest(jobIds);
+
+        if (randomBoolean()) {
+            request.setAllowNoJobs(randomBoolean());
+        }
+
+        return request;
+    }
+
+    @Override
+    protected GetJobStatsRequest doParseInstance(XContentParser parser) throws IOException {
+        return GetJobStatsRequest.PARSER.parse(parser, null);
+    }
+
+    @Override
+    protected boolean supportsUnknownFields() {
+        return false;
+    }
+}

+ 53 - 0
client/rest-high-level/src/test/java/org/elasticsearch/client/ml/GetJobStatsResponseTests.java

@@ -0,0 +1,53 @@
+/*
+ * Licensed to Elasticsearch under one or more contributor
+ * license agreements. See the NOTICE file distributed with
+ * this work for additional information regarding copyright
+ * ownership. Elasticsearch licenses this file to you under
+ * the Apache License, Version 2.0 (the "License"); you may
+ * not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ *    http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied.  See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+package org.elasticsearch.client.ml;
+
+import org.elasticsearch.common.xcontent.XContentParser;
+import org.elasticsearch.client.ml.job.stats.JobStats;
+import org.elasticsearch.client.ml.job.stats.JobStatsTests;
+import org.elasticsearch.test.AbstractXContentTestCase;
+
+import java.io.IOException;
+import java.util.ArrayList;
+import java.util.List;
+
+public class GetJobStatsResponseTests extends AbstractXContentTestCase<GetJobStatsResponse> {
+
+    @Override
+    protected GetJobStatsResponse createTestInstance() {
+
+        int count = randomIntBetween(1, 5);
+        List<JobStats> results = new ArrayList<>(count);
+        for(int i = 0; i < count; i++) {
+            results.add(JobStatsTests.createRandomInstance());
+        }
+
+        return new GetJobStatsResponse(results, count);
+    }
+
+    @Override
+    protected GetJobStatsResponse doParseInstance(XContentParser parser) throws IOException {
+        return GetJobStatsResponse.fromXContent(parser);
+    }
+
+    @Override
+    protected boolean supportsUnknownFields() {
+        return false;
+    }
+}

+ 64 - 0
client/rest-high-level/src/test/java/org/elasticsearch/client/ml/NodeAttributesTests.java

@@ -0,0 +1,64 @@
+/*
+ * Licensed to Elasticsearch under one or more contributor
+ * license agreements. See the NOTICE file distributed with
+ * this work for additional information regarding copyright
+ * ownership. Elasticsearch licenses this file to you under
+ * the Apache License, Version 2.0 (the "License"); you may
+ * not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ *    http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied.  See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+package org.elasticsearch.client.ml;
+
+import org.elasticsearch.common.xcontent.XContentParser;
+import org.elasticsearch.test.AbstractXContentTestCase;
+
+import java.io.IOException;
+import java.util.HashMap;
+import java.util.Map;
+import java.util.function.Predicate;
+
+public class NodeAttributesTests extends AbstractXContentTestCase<NodeAttributes> {
+
+    public static NodeAttributes createRandom() {
+        int numberOfAttributes = randomIntBetween(1, 10);
+        Map<String, String> attributes = new HashMap<>(numberOfAttributes);
+        for(int i = 0; i < numberOfAttributes; i++) {
+            String val = randomAlphaOfLength(10);
+            attributes.put("key-"+i, val);
+        }
+        return new NodeAttributes(randomAlphaOfLength(10),
+            randomAlphaOfLength(10),
+            randomAlphaOfLength(10),
+            randomAlphaOfLength(10),
+            attributes);
+    }
+
+    @Override
+    protected NodeAttributes createTestInstance() {
+        return createRandom();
+    }
+
+    @Override
+    protected NodeAttributes doParseInstance(XContentParser parser) throws IOException {
+        return NodeAttributes.PARSER.parse(parser, null);
+    }
+
+    @Override
+    protected Predicate<String> getRandomFieldsExcludeFilter() {
+        return field -> !field.isEmpty();
+    }
+
+    @Override
+    protected boolean supportsUnknownFields() {
+        return true;
+    }
+}

+ 70 - 0
client/rest-high-level/src/test/java/org/elasticsearch/client/ml/job/stats/ForecastStatsTests.java

@@ -0,0 +1,70 @@
+/*
+ * Licensed to Elasticsearch under one or more contributor
+ * license agreements. See the NOTICE file distributed with
+ * this work for additional information regarding copyright
+ * ownership. Elasticsearch licenses this file to you under
+ * the Apache License, Version 2.0 (the "License"); you may
+ * not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ *    http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied.  See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+package org.elasticsearch.client.ml.job.stats;
+
+import org.elasticsearch.common.xcontent.XContentParser;
+import org.elasticsearch.test.AbstractXContentTestCase;
+
+import java.io.IOException;
+import java.util.HashMap;
+import java.util.Map;
+import java.util.function.Predicate;
+
+public class ForecastStatsTests extends AbstractXContentTestCase<ForecastStats> {
+
+    @Override
+    public ForecastStats createTestInstance() {
+        if (randomBoolean()) {
+            return createRandom(1, 22);
+        }
+        return new ForecastStats(0, null,null,null,null);
+    }
+
+    @Override
+    protected ForecastStats doParseInstance(XContentParser parser) throws IOException {
+        return ForecastStats.PARSER.parse(parser, null);
+    }
+
+    @Override
+    protected boolean supportsUnknownFields() {
+        return true;
+    }
+
+    @Override
+    protected Predicate<String> getRandomFieldsExcludeFilter() {
+        return field -> !field.isEmpty();
+    }
+
+    public static ForecastStats createRandom(long minTotal, long maxTotal) {
+        return new ForecastStats(
+            randomLongBetween(minTotal, maxTotal),
+            SimpleStatsTests.createRandom(),
+            SimpleStatsTests.createRandom(),
+            SimpleStatsTests.createRandom(),
+            createCountStats());
+    }
+
+    private static Map<String, Long> createCountStats() {
+        Map<String, Long> countStats = new HashMap<>();
+        for (int i = 0; i < randomInt(10); ++i) {
+            countStats.put(randomAlphaOfLengthBetween(1, 20), randomLongBetween(1L, 100L));
+        }
+        return countStats;
+    }
+}

+ 72 - 0
client/rest-high-level/src/test/java/org/elasticsearch/client/ml/job/stats/JobStatsTests.java

@@ -0,0 +1,72 @@
+/*
+ * Licensed to Elasticsearch under one or more contributor
+ * license agreements. See the NOTICE file distributed with
+ * this work for additional information regarding copyright
+ * ownership. Elasticsearch licenses this file to you under
+ * the Apache License, Version 2.0 (the "License"); you may
+ * not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ *    http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied.  See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+package org.elasticsearch.client.ml.job.stats;
+
+import org.elasticsearch.client.ml.NodeAttributes;
+import org.elasticsearch.client.ml.NodeAttributesTests;
+import org.elasticsearch.client.ml.job.process.DataCounts;
+import org.elasticsearch.client.ml.job.process.DataCountsTests;
+import org.elasticsearch.client.ml.job.process.ModelSizeStats;
+import org.elasticsearch.client.ml.job.process.ModelSizeStatsTests;
+import org.elasticsearch.common.unit.TimeValue;
+import org.elasticsearch.common.xcontent.XContentParser;
+import org.elasticsearch.client.ml.job.config.JobState;
+import org.elasticsearch.client.ml.job.config.JobTests;
+import org.elasticsearch.test.AbstractXContentTestCase;
+
+import java.io.IOException;
+import java.util.function.Predicate;
+
+
+public class JobStatsTests extends AbstractXContentTestCase<JobStats> {
+
+    public static JobStats createRandomInstance() {
+        String jobId = JobTests.randomValidJobId();
+        JobState state = randomFrom(JobState.CLOSING, JobState.CLOSED, JobState.OPENED, JobState.FAILED, JobState.OPENING);
+        DataCounts dataCounts = DataCountsTests.createTestInstance(jobId);
+
+        ModelSizeStats modelSizeStats = randomBoolean() ? ModelSizeStatsTests.createRandomized() : null;
+        ForecastStats forecastStats = randomBoolean() ? ForecastStatsTests.createRandom(1, 22) : null;
+        NodeAttributes nodeAttributes = randomBoolean() ? NodeAttributesTests.createRandom() : null;
+        String assigmentExplanation = randomBoolean() ? randomAlphaOfLength(10) : null;
+        TimeValue openTime = randomBoolean() ? TimeValue.timeValueMillis(randomIntBetween(1, 10000)) : null;
+
+        return new JobStats(jobId, dataCounts, state, modelSizeStats, forecastStats, nodeAttributes, assigmentExplanation, openTime);
+    }
+
+    @Override
+    protected JobStats createTestInstance() {
+        return createRandomInstance();
+    }
+
+    @Override
+    protected JobStats doParseInstance(XContentParser parser) throws IOException {
+        return JobStats.PARSER.parse(parser, null);
+    }
+
+    @Override
+    protected Predicate<String> getRandomFieldsExcludeFilter() {
+        return field -> !field.isEmpty();
+    }
+
+    @Override
+    protected boolean supportsUnknownFields() {
+        return true;
+    }
+}

+ 47 - 0
client/rest-high-level/src/test/java/org/elasticsearch/client/ml/job/stats/SimpleStatsTests.java

@@ -0,0 +1,47 @@
+/*
+ * Licensed to Elasticsearch under one or more contributor
+ * license agreements. See the NOTICE file distributed with
+ * this work for additional information regarding copyright
+ * ownership. Elasticsearch licenses this file to you under
+ * the Apache License, Version 2.0 (the "License"); you may
+ * not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ *    http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied.  See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+package org.elasticsearch.client.ml.job.stats;
+
+import org.elasticsearch.common.xcontent.XContentParser;
+import org.elasticsearch.test.AbstractXContentTestCase;
+
+import java.io.IOException;
+
+
+public class SimpleStatsTests extends AbstractXContentTestCase<SimpleStats> {
+
+    @Override
+    protected SimpleStats createTestInstance() {
+        return createRandom();
+    }
+
+    @Override
+    protected SimpleStats doParseInstance(XContentParser parser) throws IOException {
+        return SimpleStats.PARSER.parse(parser, null);
+    }
+
+    @Override
+    protected boolean supportsUnknownFields() {
+        return true;
+    }
+
+    public static SimpleStats createRandom() {
+        return new SimpleStats(randomDouble(), randomDouble(), randomDouble(), randomDouble());
+    }
+}

+ 67 - 0
docs/java-rest/high-level/ml/get-job-stats.asciidoc

@@ -0,0 +1,67 @@
+[[java-rest-high-x-pack-ml-get-job-stats]]
+=== Get Job Stats API
+
+The Get Job Stats API provides the ability to get any number of
+ {ml} job's statistics in the cluster.
+It accepts a `GetJobStatsRequest` object and responds
+with a `GetJobStatsResponse` object.
+
+[[java-rest-high-x-pack-ml-get-job-stats-request]]
+==== Get Job Stats Request
+
+A `GetJobsStatsRequest` object can have any number of `jobId`
+entries. However, they all must be non-null. An empty list is the same as
+requesting statistics for all jobs.
+
+["source","java",subs="attributes,callouts,macros"]
+--------------------------------------------------
+include-tagged::{doc-tests}/MlClientDocumentationIT.java[x-pack-ml-get-job-stats-request]
+--------------------------------------------------
+<1> Constructing a new request referencing existing `jobIds`, can contain wildcards
+<2> Whether to ignore if a wildcard expression matches no jobs.
+ (This includes `_all` string or when no jobs have been specified)
+
+[[java-rest-high-x-pack-ml-get-job-stats-execution]]
+==== Execution
+
+The request can be executed through the `MachineLearningClient` contained
+in the `RestHighLevelClient` object, accessed via the `machineLearningClient()` method.
+
+["source","java",subs="attributes,callouts,macros"]
+--------------------------------------------------
+include-tagged::{doc-tests}/MlClientDocumentationIT.java[x-pack-ml-get-job-stats-execute]
+--------------------------------------------------
+
+[[java-rest-high-x-pack-ml-get-job-stats-execution-async]]
+==== Asynchronous Execution
+
+The request can also be executed asynchronously:
+
+["source","java",subs="attributes,callouts,macros"]
+--------------------------------------------------
+include-tagged::{doc-tests}/MlClientDocumentationIT.java[x-pack-ml-get-job-stats-execute-async]
+--------------------------------------------------
+<1> The `GetJobsStatsRequest` to execute and the `ActionListener` to use when
+the execution completes
+
+The method does not block and returns immediately. The passed `ActionListener` is used
+to notify the caller of completion. A typical `ActionListener` for `GetJobsStatsResponse` may
+look like
+
+["source","java",subs="attributes,callouts,macros"]
+--------------------------------------------------
+include-tagged::{doc-tests}/MlClientDocumentationIT.java[x-pack-ml-get-job-stats-listener]
+--------------------------------------------------
+<1> `onResponse` is called back when the action is completed successfully
+<2> `onFailure` is called back when some unexpected error occurs
+
+[[java-rest-high-x-pack-ml-get-job-stats-response]]
+==== Get Job Stats Response
+The returned `GetJobStatsResponse` contains the requested job statistics:
+
+["source","java",subs="attributes,callouts,macros"]
+--------------------------------------------------
+include-tagged::{doc-tests}/MlClientDocumentationIT.java[x-pack-ml-get-job-stats-response]
+--------------------------------------------------
+<1> `getCount()` indicates the number of jobs statistics found
+<2> `getJobStats()` is the collection of {ml} `JobStats` objects found

+ 2 - 0
docs/java-rest/high-level/supported-apis.asciidoc

@@ -211,6 +211,7 @@ The Java High Level REST Client supports the following Machine Learning APIs:
 * <<java-rest-high-x-pack-ml-delete-job>>
 * <<java-rest-high-x-pack-ml-open-job>>
 * <<java-rest-high-x-pack-ml-close-job>>
+* <<java-rest-high-x-pack-ml-get-job-stats>>
 * <<java-rest-high-x-pack-ml-get-buckets>>
 * <<java-rest-high-x-pack-ml-get-records>>
 
@@ -219,6 +220,7 @@ include::ml/get-job.asciidoc[]
 include::ml/delete-job.asciidoc[]
 include::ml/open-job.asciidoc[]
 include::ml/close-job.asciidoc[]
+include::ml/get-job-stats.asciidoc[]
 include::ml/get-buckets.asciidoc[]
 include::ml/get-records.asciidoc[]