Skip to content

HLRC: ML Adding get datafeed stats API #34271

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 6 commits into from
Oct 4, 2018
Merged
Show file tree
Hide file tree
Changes from 5 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@
import org.elasticsearch.client.ml.GetCalendarsRequest;
import org.elasticsearch.client.ml.GetCategoriesRequest;
import org.elasticsearch.client.ml.GetDatafeedRequest;
import org.elasticsearch.client.ml.GetDatafeedStatsRequest;
import org.elasticsearch.client.ml.GetInfluencersRequest;
import org.elasticsearch.client.ml.GetJobRequest;
import org.elasticsearch.client.ml.GetJobStatsRequest;
Expand Down Expand Up @@ -259,6 +260,23 @@ static Request stopDatafeed(StopDatafeedRequest stopDatafeedRequest) throws IOEx
return request;
}

static Request getDatafeedStats(GetDatafeedStatsRequest getDatafeedStatsRequest) {
String endpoint = new EndpointBuilder()
.addPathPartAsIs("_xpack")
.addPathPartAsIs("ml")
.addPathPartAsIs("datafeeds")
.addPathPart(Strings.collectionToCommaDelimitedString(getDatafeedStatsRequest.getDatafeedIds()))
.addPathPartAsIs("_stats")
.build();
Request request = new Request(HttpGet.METHOD_NAME, endpoint);

RequestConverters.Params params = new RequestConverters.Params(request);
if (getDatafeedStatsRequest.isAllowNoDatafeeds() != null) {
params.putParam("allow_no_datafeeds", Boolean.toString(getDatafeedStatsRequest.isAllowNoDatafeeds()));
}
return request;
}

static Request deleteForecast(DeleteForecastRequest deleteForecastRequest) {
String endpoint = new EndpointBuilder()
.addPathPartAsIs("_xpack")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,8 @@
import org.elasticsearch.client.ml.GetCategoriesResponse;
import org.elasticsearch.client.ml.GetDatafeedRequest;
import org.elasticsearch.client.ml.GetDatafeedResponse;
import org.elasticsearch.client.ml.GetDatafeedStatsRequest;
import org.elasticsearch.client.ml.GetDatafeedStatsResponse;
import org.elasticsearch.client.ml.GetInfluencersRequest;
import org.elasticsearch.client.ml.GetInfluencersResponse;
import org.elasticsearch.client.ml.GetJobRequest;
Expand Down Expand Up @@ -181,7 +183,7 @@ public GetJobStatsResponse getJobStats(GetJobStatsRequest request, RequestOption
}

/**
* Gets one or more Machine Learning job configuration info, asynchronously.
* Gets usage statistics for one or more Machine Learning jobs, 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>
Expand Down Expand Up @@ -649,6 +651,47 @@ public void stopDatafeedAsync(StopDatafeedRequest request, RequestOptions option
Collections.emptySet());
}

/**
* Gets statistics for one or more Machine Learning datafeeds
* <p>
* For additional info
* see <a href="https://www.elastic.co/guide/en/elasticsearch/reference/current/ml-get-datafeed-stats.html">Get datafeed stats docs</a>
*
* @param request {@link GetDatafeedStatsRequest} Request containing a list of datafeedId(s) and additional options
* @param options Additional request options (e.g. headers), use {@link RequestOptions#DEFAULT} if nothing needs to be customized
* @return {@link GetDatafeedStatsResponse} response object containing
* the {@link org.elasticsearch.client.ml.datafeed.DatafeedStats} objects and the number of datafeeds found
* @throws IOException when there is a serialization issue sending the request or receiving the response
*/
public GetDatafeedStatsResponse getDatafeedStats(GetDatafeedStatsRequest request, RequestOptions options) throws IOException {
return restHighLevelClient.performRequestAndParseEntity(request,
MLRequestConverters::getDatafeedStats,
options,
GetDatafeedStatsResponse::fromXContent,
Collections.emptySet());
}

/**
* Gets statistics for one or more Machine Learning datafeeds, asynchronously.
* <p>
* For additional info
* see <a href="https://www.elastic.co/guide/en/elasticsearch/reference/current/ml-get-datafeed-stats.html">Get datafeed stats docs</a>
*
* @param request {@link GetDatafeedStatsRequest} Request containing a list of datafeedId(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 GetDatafeedStatsResponse} upon request completion
*/
public void getDatafeedStatsAsync(GetDatafeedStatsRequest request,
RequestOptions options,
ActionListener<GetDatafeedStatsResponse> listener) {
restHighLevelClient.performRequestAsyncAndParseEntity(request,
MLRequestConverters::getDatafeedStats,
options,
GetDatafeedStatsResponse::fromXContent,
listener,
Collections.emptySet());
}

/**
* Updates a Machine Learning {@link org.elasticsearch.client.ml.job.config.Job}
* <p>
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,147 @@
/*
* 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.datafeed.DatafeedConfig;
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.ToXContent;
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.datafeed.DatafeedStats} by their respective datafeedIds
*
* {@code _all} explicitly gets all the datafeeds' statistics in the cluster
* An empty request (no {@code datafeedId}s) implicitly gets all the datafeeds' statistics in the cluster
*/
public class GetDatafeedStatsRequest extends ActionRequest implements ToXContentObject {

public static final ParseField ALLOW_NO_DATAFEEDS = new ParseField("allow_no_datafeeds");

@SuppressWarnings("unchecked")
public static final ConstructingObjectParser<GetDatafeedStatsRequest, Void> PARSER = new ConstructingObjectParser<>(
"get_datafeed_stats_request", a -> new GetDatafeedStatsRequest((List<String>) a[0]));

static {
PARSER.declareField(ConstructingObjectParser.constructorArg(),
p -> Arrays.asList(Strings.commaDelimitedListToStringArray(p.text())),
DatafeedConfig.ID, ObjectParser.ValueType.STRING_ARRAY);
PARSER.declareBoolean(GetDatafeedStatsRequest::setAllowNoDatafeeds, ALLOW_NO_DATAFEEDS);
}

private static final String ALL_DATAFEEDS = "_all";

private final List<String> datafeedIds;
private Boolean allowNoDatafeeds;

/**
* Explicitly gets all datafeeds statistics
*
* @return a {@link GetDatafeedStatsRequest} for all existing datafeeds
*/
public static GetDatafeedStatsRequest getAllDatafeedStatsRequest(){
return new GetDatafeedStatsRequest(ALL_DATAFEEDS);
}

GetDatafeedStatsRequest(List<String> datafeedIds) {
if (datafeedIds.stream().anyMatch(Objects::isNull)) {
throw new NullPointerException("datafeedIds must not contain null values");
}
this.datafeedIds = new ArrayList<>(datafeedIds);
}

/**
* Get the specified Datafeed's statistics via their unique datafeedIds
*
* @param datafeedIds must be non-null and each datafeedId must be non-null
*/
public GetDatafeedStatsRequest(String... datafeedIds) {
this(Arrays.asList(datafeedIds));
}

/**
* All the datafeedIds for which to get statistics
*/
public List<String> getDatafeedIds() {
return datafeedIds;
}

public Boolean isAllowNoDatafeeds() {
return this.allowNoDatafeeds;
}

/**
* Whether to ignore if a wildcard expression matches no datafeeds.
*
* This includes {@code _all} string or when no datafeeds have been specified
*
* @param allowNoDatafeeds When {@code true} ignore if wildcard or {@code _all} matches no datafeeds. Defaults to {@code true}
*/
public void setAllowNoDatafeeds(boolean allowNoDatafeeds) {
this.allowNoDatafeeds = allowNoDatafeeds;
}

@Override
public int hashCode() {
return Objects.hash(datafeedIds, allowNoDatafeeds);
}

@Override
public boolean equals(Object other) {
if (this == other) {
return true;
}

if (other == null || getClass() != other.getClass()) {
return false;
}

GetDatafeedStatsRequest that = (GetDatafeedStatsRequest) other;
return Objects.equals(datafeedIds, that.datafeedIds) &&
Objects.equals(allowNoDatafeeds, that.allowNoDatafeeds);
}

@Override
public ActionRequestValidationException validate() {
return null;
}

@Override
public XContentBuilder toXContent(XContentBuilder builder, ToXContent.Params params) throws IOException {
builder.startObject();
builder.field(DatafeedConfig.ID.getPreferredName(), Strings.collectionToCommaDelimitedString(datafeedIds));
if (allowNoDatafeeds != null) {
builder.field(ALLOW_NO_DATAFEEDS.getPreferredName(), allowNoDatafeeds);
}
builder.endObject();
return builder;
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,89 @@
/*
* 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.client.ml.datafeed.DatafeedStats;
import org.elasticsearch.common.ParseField;
import org.elasticsearch.common.Strings;
import org.elasticsearch.common.xcontent.ConstructingObjectParser;
import org.elasticsearch.common.xcontent.XContentParser;

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 DatafeedStats} objects and the total count found
*/
public class GetDatafeedStatsResponse extends AbstractResultResponse<DatafeedStats> {

public static final ParseField RESULTS_FIELD = new ParseField("datafeeds");

@SuppressWarnings("unchecked")
public static final ConstructingObjectParser<GetDatafeedStatsResponse, Void> PARSER =
new ConstructingObjectParser<>("get_datafeed_stats_response",
true,
a -> new GetDatafeedStatsResponse((List<DatafeedStats>) a[0], (long) a[1]));

static {
PARSER.declareObjectArray(constructorArg(), DatafeedStats.PARSER, RESULTS_FIELD);
PARSER.declareLong(constructorArg(), COUNT);
}

GetDatafeedStatsResponse(List<DatafeedStats> results, long count) {
super(RESULTS_FIELD, results, count);
}

/**
* The collection of {@link DatafeedStats} objects found in the query
*/
public List<DatafeedStats> datafeedStats() {
return results;
}

public static GetDatafeedStatsResponse fromXContent(XContentParser parser) throws IOException {
return PARSER.parse(parser, null);
}

@Override
public int hashCode() {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can hashCode() be moved into the parent class AbstractResultResponse?
Not an issue for this PR, just wondering

return Objects.hash(results, count);
}

@Override
public boolean equals(Object obj) {
if (this == obj) {
return true;
}

if (obj == null || getClass() != obj.getClass()) {
return false;
}

GetDatafeedStatsResponse other = (GetDatafeedStatsResponse) obj;
return Objects.equals(results, other.results) && count == other.count;
}

@Override
public final String toString() {
return Strings.toString(this);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
/*
* 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.datafeed;

import org.elasticsearch.common.ParseField;

import java.util.Locale;

/**
* Datafeed State POJO
*/
public enum DatafeedState {

STARTED, STOPPED, STARTING, STOPPING;

public static final ParseField STATE = new ParseField("state");

public static DatafeedState fromString(String name) {
return valueOf(name.trim().toUpperCase(Locale.ROOT));
}

@Override
public String toString() {
return name().toLowerCase(Locale.ROOT);
}
}
Loading