This repository has been archived by the owner on Aug 2, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 36
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Adding new Search Detector Info API (#286)
* Adding new Search Info API
- Loading branch information
1 parent
2f18231
commit 6138f78
Showing
11 changed files
with
547 additions
and
2 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
79 changes: 79 additions & 0 deletions
79
...va/com/amazon/opendistroforelasticsearch/ad/rest/RestSearchAnomalyDetectorInfoAction.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,79 @@ | ||
/* | ||
* Copyright 2020 Amazon.com, Inc. or its affiliates. All Rights Reserved. | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"). | ||
* You may not use this file except in compliance with the License. | ||
* A copy of the License is located at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* or in the "license" file accompanying this file. This file 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 com.amazon.opendistroforelasticsearch.ad.rest; | ||
|
||
import static com.amazon.opendistroforelasticsearch.ad.util.RestHandlerUtils.COUNT; | ||
import static com.amazon.opendistroforelasticsearch.ad.util.RestHandlerUtils.MATCH; | ||
|
||
import java.io.IOException; | ||
import java.util.List; | ||
import java.util.Locale; | ||
|
||
import org.apache.logging.log4j.LogManager; | ||
import org.apache.logging.log4j.Logger; | ||
import org.elasticsearch.rest.BaseRestHandler; | ||
import org.elasticsearch.rest.RestHandler; | ||
import org.elasticsearch.rest.RestRequest; | ||
import org.elasticsearch.rest.action.RestToXContentListener; | ||
|
||
import com.amazon.opendistroforelasticsearch.ad.AnomalyDetectorPlugin; | ||
import com.amazon.opendistroforelasticsearch.ad.constant.CommonErrorMessages; | ||
import com.amazon.opendistroforelasticsearch.ad.settings.EnabledSetting; | ||
import com.amazon.opendistroforelasticsearch.ad.transport.SearchAnomalyDetectorInfoAction; | ||
import com.amazon.opendistroforelasticsearch.ad.transport.SearchAnomalyDetectorInfoRequest; | ||
import com.google.common.collect.ImmutableList; | ||
|
||
public class RestSearchAnomalyDetectorInfoAction extends BaseRestHandler { | ||
|
||
public static final String SEARCH_ANOMALY_DETECTOR_INFO_ACTION = "search_anomaly_detector_info"; | ||
|
||
private static final Logger logger = LogManager.getLogger(RestSearchAnomalyDetectorInfoAction.class); | ||
|
||
public RestSearchAnomalyDetectorInfoAction() {} | ||
|
||
@Override | ||
public String getName() { | ||
return SEARCH_ANOMALY_DETECTOR_INFO_ACTION; | ||
} | ||
|
||
@Override | ||
protected RestChannelConsumer prepareRequest(RestRequest request, org.elasticsearch.client.node.NodeClient client) throws IOException { | ||
if (!EnabledSetting.isADPluginEnabled()) { | ||
throw new IllegalStateException(CommonErrorMessages.DISABLED_ERR_MSG); | ||
} | ||
|
||
String detectorName = request.param("name", null); | ||
String rawPath = request.rawPath(); | ||
|
||
SearchAnomalyDetectorInfoRequest searchAnomalyDetectorInfoRequest = new SearchAnomalyDetectorInfoRequest(detectorName, rawPath); | ||
return channel -> client | ||
.execute(SearchAnomalyDetectorInfoAction.INSTANCE, searchAnomalyDetectorInfoRequest, new RestToXContentListener<>(channel)); | ||
} | ||
|
||
@Override | ||
public List<RestHandler.Route> routes() { | ||
return ImmutableList | ||
.of( | ||
// get the count of number of detectors | ||
new RestHandler.Route( | ||
RestRequest.Method.GET, | ||
String.format(Locale.ROOT, "%s/%s", AnomalyDetectorPlugin.AD_BASE_DETECTORS_URI, COUNT) | ||
), | ||
// get if a detector name exists with name | ||
new RestHandler.Route(RestRequest.Method.GET, String.format("%s/%s", AnomalyDetectorPlugin.AD_BASE_DETECTORS_URI, MATCH)) | ||
); | ||
} | ||
} |
31 changes: 31 additions & 0 deletions
31
...a/com/amazon/opendistroforelasticsearch/ad/transport/SearchAnomalyDetectorInfoAction.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,31 @@ | ||
/* | ||
* Copyright 2020 Amazon.com, Inc. or its affiliates. All Rights Reserved. | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"). | ||
* You may not use this file except in compliance with the License. | ||
* A copy of the License is located at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* or in the "license" file accompanying this file. This file 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 com.amazon.opendistroforelasticsearch.ad.transport; | ||
|
||
import org.elasticsearch.action.ActionType; | ||
|
||
import com.amazon.opendistroforelasticsearch.ad.constant.CommonValue; | ||
|
||
public class SearchAnomalyDetectorInfoAction extends ActionType<SearchAnomalyDetectorInfoResponse> { | ||
// External Action which used for public facing RestAPIs. | ||
public static final String NAME = CommonValue.EXTERNAL_ACTION_PREFIX + "detector/info"; | ||
public static final SearchAnomalyDetectorInfoAction INSTANCE = new SearchAnomalyDetectorInfoAction(); | ||
|
||
private SearchAnomalyDetectorInfoAction() { | ||
super(NAME, SearchAnomalyDetectorInfoResponse::new); | ||
} | ||
|
||
} |
61 changes: 61 additions & 0 deletions
61
.../com/amazon/opendistroforelasticsearch/ad/transport/SearchAnomalyDetectorInfoRequest.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,61 @@ | ||
/* | ||
* Copyright 2020 Amazon.com, Inc. or its affiliates. All Rights Reserved. | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"). | ||
* You may not use this file except in compliance with the License. | ||
* A copy of the License is located at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* or in the "license" file accompanying this file. This file 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 com.amazon.opendistroforelasticsearch.ad.transport; | ||
|
||
import java.io.IOException; | ||
|
||
import org.elasticsearch.action.ActionRequest; | ||
import org.elasticsearch.action.ActionRequestValidationException; | ||
import org.elasticsearch.common.io.stream.StreamInput; | ||
import org.elasticsearch.common.io.stream.StreamOutput; | ||
|
||
public class SearchAnomalyDetectorInfoRequest extends ActionRequest { | ||
|
||
private String name; | ||
private String rawPath; | ||
|
||
public SearchAnomalyDetectorInfoRequest(StreamInput in) throws IOException { | ||
super(in); | ||
name = in.readOptionalString(); | ||
rawPath = in.readString(); | ||
} | ||
|
||
public SearchAnomalyDetectorInfoRequest(String name, String rawPath) throws IOException { | ||
super(); | ||
this.name = name; | ||
this.rawPath = rawPath; | ||
} | ||
|
||
public String getName() { | ||
return name; | ||
} | ||
|
||
public String getRawPath() { | ||
return rawPath; | ||
} | ||
|
||
@Override | ||
public void writeTo(StreamOutput out) throws IOException { | ||
super.writeTo(out); | ||
out.writeOptionalString(name); | ||
out.writeString(rawPath); | ||
} | ||
|
||
@Override | ||
public ActionRequestValidationException validate() { | ||
return null; | ||
} | ||
} |
65 changes: 65 additions & 0 deletions
65
...com/amazon/opendistroforelasticsearch/ad/transport/SearchAnomalyDetectorInfoResponse.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,65 @@ | ||
/* | ||
* Copyright 2020 Amazon.com, Inc. or its affiliates. All Rights Reserved. | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"). | ||
* You may not use this file except in compliance with the License. | ||
* A copy of the License is located at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* or in the "license" file accompanying this file. This file 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 com.amazon.opendistroforelasticsearch.ad.transport; | ||
|
||
import java.io.IOException; | ||
|
||
import org.elasticsearch.action.ActionResponse; | ||
import org.elasticsearch.common.io.stream.StreamInput; | ||
import org.elasticsearch.common.io.stream.StreamOutput; | ||
import org.elasticsearch.common.xcontent.ToXContentObject; | ||
import org.elasticsearch.common.xcontent.XContentBuilder; | ||
|
||
import com.amazon.opendistroforelasticsearch.ad.util.RestHandlerUtils; | ||
|
||
public class SearchAnomalyDetectorInfoResponse extends ActionResponse implements ToXContentObject { | ||
private long count; | ||
private boolean nameExists; | ||
|
||
public SearchAnomalyDetectorInfoResponse(StreamInput in) throws IOException { | ||
super(in); | ||
count = in.readLong(); | ||
nameExists = in.readBoolean(); | ||
} | ||
|
||
public SearchAnomalyDetectorInfoResponse(long count, boolean nameExists) { | ||
this.count = count; | ||
this.nameExists = nameExists; | ||
} | ||
|
||
public long getCount() { | ||
return count; | ||
} | ||
|
||
public boolean isNameExists() { | ||
return nameExists; | ||
} | ||
|
||
@Override | ||
public void writeTo(StreamOutput out) throws IOException { | ||
out.writeLong(count); | ||
out.writeBoolean(nameExists); | ||
} | ||
|
||
@Override | ||
public XContentBuilder toXContent(XContentBuilder builder, Params params) throws IOException { | ||
builder.startObject(); | ||
builder.field(RestHandlerUtils.COUNT, count); | ||
builder.field(RestHandlerUtils.MATCH, nameExists); | ||
builder.endObject(); | ||
return builder; | ||
} | ||
} |
Oops, something went wrong.