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.
Moving Preview Anomaly Detectors to Transport layer (#321)
* Moving Preview Anomaly Detectors to Transport layer * Removing powermock for tests * Adding tests for Preview Transport Action * Adding test coverage for Preview Transport Action
- Loading branch information
1 parent
44f238c
commit 43a28b7
Showing
10 changed files
with
923 additions
and
136 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
129 changes: 129 additions & 0 deletions
129
.../java/com/amazon/opendistroforelasticsearch/ad/rest/RestPreviewAnomalyDetectorAction.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,129 @@ | ||
/* | ||
* 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.PREVIEW; | ||
import static org.elasticsearch.common.xcontent.XContentParserUtils.ensureExpectedToken; | ||
|
||
import java.io.IOException; | ||
import java.util.List; | ||
import java.util.Locale; | ||
|
||
import org.apache.commons.lang.StringUtils; | ||
import org.apache.logging.log4j.LogManager; | ||
import org.apache.logging.log4j.Logger; | ||
import org.elasticsearch.common.xcontent.XContentParser; | ||
import org.elasticsearch.rest.BaseRestHandler; | ||
import org.elasticsearch.rest.BytesRestResponse; | ||
import org.elasticsearch.rest.RestHandler; | ||
import org.elasticsearch.rest.RestRequest; | ||
import org.elasticsearch.rest.RestStatus; | ||
import org.elasticsearch.rest.action.RestToXContentListener; | ||
|
||
import com.amazon.opendistroforelasticsearch.ad.AnomalyDetectorPlugin; | ||
import com.amazon.opendistroforelasticsearch.ad.constant.CommonErrorMessages; | ||
import com.amazon.opendistroforelasticsearch.ad.model.AnomalyDetectorExecutionInput; | ||
import com.amazon.opendistroforelasticsearch.ad.settings.EnabledSetting; | ||
import com.amazon.opendistroforelasticsearch.ad.transport.PreviewAnomalyDetectorAction; | ||
import com.amazon.opendistroforelasticsearch.ad.transport.PreviewAnomalyDetectorRequest; | ||
import com.amazon.opendistroforelasticsearch.ad.util.RestHandlerUtils; | ||
import com.google.common.collect.ImmutableList; | ||
|
||
public class RestPreviewAnomalyDetectorAction extends BaseRestHandler { | ||
|
||
public static final String PREVIEW_ANOMALY_DETECTOR_ACTION = "preview_anomaly_detector"; | ||
|
||
private static final Logger logger = LogManager.getLogger(RestPreviewAnomalyDetectorAction.class); | ||
|
||
public RestPreviewAnomalyDetectorAction() {} | ||
|
||
@Override | ||
public String getName() { | ||
return PREVIEW_ANOMALY_DETECTOR_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); | ||
} | ||
|
||
AnomalyDetectorExecutionInput input = getAnomalyDetectorExecutionInput(request); | ||
|
||
return channel -> { | ||
String rawPath = request.rawPath(); | ||
String error = validateAdExecutionInput(input); | ||
if (StringUtils.isNotBlank(error)) { | ||
channel.sendResponse(new BytesRestResponse(RestStatus.BAD_REQUEST, error)); | ||
return; | ||
} | ||
PreviewAnomalyDetectorRequest previewRequest = new PreviewAnomalyDetectorRequest( | ||
input.getDetector(), | ||
input.getDetectorId(), | ||
input.getPeriodStart(), | ||
input.getPeriodEnd() | ||
); | ||
client.execute(PreviewAnomalyDetectorAction.INSTANCE, previewRequest, new RestToXContentListener<>(channel)); | ||
}; | ||
} | ||
|
||
private AnomalyDetectorExecutionInput getAnomalyDetectorExecutionInput(RestRequest request) throws IOException { | ||
String detectorId = null; | ||
if (request.hasParam(RestHandlerUtils.DETECTOR_ID)) { | ||
detectorId = request.param(RestHandlerUtils.DETECTOR_ID); | ||
} | ||
|
||
XContentParser parser = request.contentParser(); | ||
ensureExpectedToken(XContentParser.Token.START_OBJECT, parser.nextToken(), parser); | ||
AnomalyDetectorExecutionInput input = AnomalyDetectorExecutionInput.parse(parser, detectorId); | ||
if (detectorId != null) { | ||
input.setDetectorId(detectorId); | ||
} | ||
return input; | ||
} | ||
|
||
private String validateAdExecutionInput(AnomalyDetectorExecutionInput input) { | ||
if (StringUtils.isBlank(input.getDetectorId())) { | ||
return "Must set anomaly detector id"; | ||
} | ||
if (input.getPeriodStart() == null || input.getPeriodEnd() == null) { | ||
return "Must set both period start and end date with epoch of milliseconds"; | ||
} | ||
if (!input.getPeriodStart().isBefore(input.getPeriodEnd())) { | ||
return "Period start date should be before end date"; | ||
} | ||
return null; | ||
} | ||
|
||
@Override | ||
public List<RestHandler.Route> routes() { | ||
return ImmutableList | ||
.of( | ||
// preview detector | ||
new Route( | ||
RestRequest.Method.POST, | ||
String | ||
.format( | ||
Locale.ROOT, | ||
"%s/{%s}/%s", | ||
AnomalyDetectorPlugin.AD_BASE_DETECTORS_URI, | ||
RestHandlerUtils.DETECTOR_ID, | ||
PREVIEW | ||
) | ||
) | ||
); | ||
} | ||
} |
Oops, something went wrong.