-
Notifications
You must be signed in to change notification settings - Fork 24.8k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[HLRC][ML] Add ML get datafeed API to HLRC (#33715)
Relates #29827
- Loading branch information
1 parent
73417bf
commit db40315
Showing
19 changed files
with
736 additions
and
97 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
144 changes: 144 additions & 0 deletions
144
client/rest-high-level/src/main/java/org/elasticsearch/client/ml/GetDatafeedRequest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,144 @@ | ||
/* | ||
* 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.xcontent.ConstructingObjectParser; | ||
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 DatafeedConfig} objects with the matching {@code datafeedId}s. | ||
* | ||
* {@code _all} explicitly gets all the datafeeds in the cluster | ||
* An empty request (no {@code datafeedId}s) implicitly gets all the datafeeds in the cluster | ||
*/ | ||
public class GetDatafeedRequest extends ActionRequest implements ToXContentObject { | ||
|
||
public static final ParseField DATAFEED_IDS = new ParseField("datafeed_ids"); | ||
public static final ParseField ALLOW_NO_DATAFEEDS = new ParseField("allow_no_datafeeds"); | ||
|
||
private static final String ALL_DATAFEEDS = "_all"; | ||
private final List<String> datafeedIds; | ||
private Boolean allowNoDatafeeds; | ||
|
||
@SuppressWarnings("unchecked") | ||
public static final ConstructingObjectParser<GetDatafeedRequest, Void> PARSER = new ConstructingObjectParser<>( | ||
"get_datafeed_request", | ||
true, a -> new GetDatafeedRequest(a[0] == null ? new ArrayList<>() : (List<String>) a[0])); | ||
|
||
static { | ||
PARSER.declareStringArray(ConstructingObjectParser.optionalConstructorArg(), DATAFEED_IDS); | ||
PARSER.declareBoolean(GetDatafeedRequest::setAllowNoDatafeeds, ALLOW_NO_DATAFEEDS); | ||
} | ||
|
||
/** | ||
* Helper method to create a query that will get ALL datafeeds | ||
* @return new {@link GetDatafeedRequest} object searching for the datafeedId "_all" | ||
*/ | ||
public static GetDatafeedRequest getAllDatafeedsRequest() { | ||
return new GetDatafeedRequest(ALL_DATAFEEDS); | ||
} | ||
|
||
/** | ||
* Get the specified {@link DatafeedConfig} configurations via their unique datafeedIds | ||
* @param datafeedIds must not contain any null values | ||
*/ | ||
public GetDatafeedRequest(String... datafeedIds) { | ||
this(Arrays.asList(datafeedIds)); | ||
} | ||
|
||
GetDatafeedRequest(List<String> datafeedIds) { | ||
if (datafeedIds.stream().anyMatch(Objects::isNull)) { | ||
throw new NullPointerException("datafeedIds must not contain null values"); | ||
} | ||
this.datafeedIds = new ArrayList<>(datafeedIds); | ||
} | ||
|
||
/** | ||
* All the datafeedIds for which to get configuration information | ||
*/ | ||
public List<String> getDatafeedIds() { | ||
return datafeedIds; | ||
} | ||
|
||
/** | ||
* Whether to ignore if a wildcard expression matches no datafeeds. | ||
* | ||
* @param allowNoDatafeeds If this is {@code false}, then an error is returned when a wildcard (or {@code _all}) | ||
* does not match any datafeeds | ||
*/ | ||
public void setAllowNoDatafeeds(boolean allowNoDatafeeds) { | ||
this.allowNoDatafeeds = allowNoDatafeeds; | ||
} | ||
|
||
public Boolean isAllowNoDatafeeds() { | ||
return allowNoDatafeeds; | ||
} | ||
|
||
@Override | ||
public ActionRequestValidationException validate() { | ||
return null; | ||
} | ||
|
||
@Override | ||
public int hashCode() { | ||
return Objects.hash(datafeedIds, allowNoDatafeeds); | ||
} | ||
|
||
@Override | ||
public boolean equals(Object other) { | ||
if (this == other) { | ||
return true; | ||
} | ||
|
||
if (other == null || other.getClass() != getClass()) { | ||
return false; | ||
} | ||
|
||
GetDatafeedRequest that = (GetDatafeedRequest) other; | ||
return Objects.equals(datafeedIds, that.datafeedIds) && | ||
Objects.equals(allowNoDatafeeds, that.allowNoDatafeeds); | ||
} | ||
|
||
@Override | ||
public XContentBuilder toXContent(XContentBuilder builder, Params params) throws IOException { | ||
builder.startObject(); | ||
|
||
if (datafeedIds.isEmpty() == false) { | ||
builder.field(DATAFEED_IDS.getPreferredName(), datafeedIds); | ||
} | ||
|
||
if (allowNoDatafeeds != null) { | ||
builder.field(ALLOW_NO_DATAFEEDS.getPreferredName(), allowNoDatafeeds); | ||
} | ||
|
||
builder.endObject(); | ||
return builder; | ||
} | ||
} |
89 changes: 89 additions & 0 deletions
89
client/rest-high-level/src/main/java/org/elasticsearch/client/ml/GetDatafeedResponse.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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.DatafeedConfig; | ||
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 java.util.stream.Collectors; | ||
|
||
import static org.elasticsearch.common.xcontent.ConstructingObjectParser.constructorArg; | ||
|
||
/** | ||
* Contains a {@link List} of the found {@link DatafeedConfig} objects and the total count found | ||
*/ | ||
public class GetDatafeedResponse extends AbstractResultResponse<DatafeedConfig> { | ||
|
||
public static final ParseField RESULTS_FIELD = new ParseField("datafeeds"); | ||
|
||
@SuppressWarnings("unchecked") | ||
public static final ConstructingObjectParser<GetDatafeedResponse, Void> PARSER = | ||
new ConstructingObjectParser<>("get_datafeed_response", true, | ||
a -> new GetDatafeedResponse((List<DatafeedConfig.Builder>) a[0], (long) a[1])); | ||
|
||
static { | ||
PARSER.declareObjectArray(constructorArg(), DatafeedConfig.PARSER, RESULTS_FIELD); | ||
PARSER.declareLong(constructorArg(), AbstractResultResponse.COUNT); | ||
} | ||
|
||
GetDatafeedResponse(List<DatafeedConfig.Builder> datafeedBuilders, long count) { | ||
super(RESULTS_FIELD, datafeedBuilders.stream().map(DatafeedConfig.Builder::build).collect(Collectors.toList()), count); | ||
} | ||
|
||
/** | ||
* The collection of {@link DatafeedConfig} objects found in the query | ||
*/ | ||
public List<DatafeedConfig> datafeeds() { | ||
return results; | ||
} | ||
|
||
public static GetDatafeedResponse 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; | ||
} | ||
|
||
GetDatafeedResponse other = (GetDatafeedResponse) obj; | ||
return Objects.equals(results, other.results) && count == other.count; | ||
} | ||
|
||
@Override | ||
public final String toString() { | ||
return Strings.toString(this); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.