use of org.opensearch.ad.transport.IndexAnomalyDetectorResponse in project anomaly-detection by opensearch-project.
the class RestIndexAnomalyDetectorAction method prepareRequest.
@Override
protected RestChannelConsumer prepareRequest(RestRequest request, NodeClient client) throws IOException {
if (!EnabledSetting.isADPluginEnabled()) {
throw new IllegalStateException(CommonErrorMessages.DISABLED_ERR_MSG);
}
String detectorId = request.param(DETECTOR_ID, AnomalyDetector.NO_ID);
logger.info("AnomalyDetector {} action for detectorId {}", request.method(), detectorId);
XContentParser parser = request.contentParser();
ensureExpectedToken(XContentParser.Token.START_OBJECT, parser.nextToken(), parser);
// TODO: check detection interval < modelTTL
AnomalyDetector detector = AnomalyDetector.parse(parser, detectorId, null, detectionInterval, detectionWindowDelay);
long seqNo = request.paramAsLong(IF_SEQ_NO, SequenceNumbers.UNASSIGNED_SEQ_NO);
long primaryTerm = request.paramAsLong(IF_PRIMARY_TERM, SequenceNumbers.UNASSIGNED_PRIMARY_TERM);
WriteRequest.RefreshPolicy refreshPolicy = request.hasParam(REFRESH) ? WriteRequest.RefreshPolicy.parse(request.param(REFRESH)) : WriteRequest.RefreshPolicy.IMMEDIATE;
RestRequest.Method method = request.getHttpRequest().method();
IndexAnomalyDetectorRequest indexAnomalyDetectorRequest = new IndexAnomalyDetectorRequest(detectorId, seqNo, primaryTerm, refreshPolicy, detector, method, requestTimeout, maxSingleEntityDetectors, maxMultiEntityDetectors, maxAnomalyFeatures);
return channel -> client.execute(IndexAnomalyDetectorAction.INSTANCE, indexAnomalyDetectorRequest, indexAnomalyDetectorResponse(channel, method));
}
use of org.opensearch.ad.transport.IndexAnomalyDetectorResponse in project anomaly-detection by opensearch-project.
the class AbstractAnomalyDetectorActionHandler method indexAnomalyDetector.
@SuppressWarnings("unchecked")
protected void indexAnomalyDetector(String detectorId) throws IOException {
AnomalyDetector detector = new AnomalyDetector(anomalyDetector.getDetectorId(), anomalyDetector.getVersion(), anomalyDetector.getName(), anomalyDetector.getDescription(), anomalyDetector.getTimeField(), anomalyDetector.getIndices(), anomalyDetector.getFeatureAttributes(), anomalyDetector.getFilterQuery(), anomalyDetector.getDetectionInterval(), anomalyDetector.getWindowDelay(), anomalyDetector.getShingleSize(), anomalyDetector.getUiMetadata(), anomalyDetector.getSchemaVersion(), Instant.now(), anomalyDetector.getCategoryField(), user, anomalyDetector.getResultIndex());
IndexRequest indexRequest = new IndexRequest(ANOMALY_DETECTORS_INDEX).setRefreshPolicy(refreshPolicy).source(detector.toXContent(XContentFactory.jsonBuilder(), XCONTENT_WITH_TYPE)).setIfSeqNo(seqNo).setIfPrimaryTerm(primaryTerm).timeout(requestTimeout);
if (StringUtils.isNotBlank(detectorId)) {
indexRequest.id(detectorId);
}
client.index(indexRequest, new ActionListener<IndexResponse>() {
@Override
public void onResponse(IndexResponse indexResponse) {
String errorMsg = checkShardsFailure(indexResponse);
if (errorMsg != null) {
listener.onFailure(new OpenSearchStatusException(errorMsg, indexResponse.status()));
return;
}
listener.onResponse((T) new IndexAnomalyDetectorResponse(indexResponse.getId(), indexResponse.getVersion(), indexResponse.getSeqNo(), indexResponse.getPrimaryTerm(), detector, RestStatus.CREATED));
}
@Override
public void onFailure(Exception e) {
logger.warn("Failed to update detector", e);
if (e.getMessage() != null && e.getMessage().contains("version conflict")) {
listener.onFailure(new IllegalArgumentException("There was a problem updating the historical detector:[" + detectorId + "]"));
} else {
listener.onFailure(e);
}
}
});
}
use of org.opensearch.ad.transport.IndexAnomalyDetectorResponse in project anomaly-detection by opensearch-project.
the class RestIndexAnomalyDetectorAction method indexAnomalyDetectorResponse.
private RestResponseListener<IndexAnomalyDetectorResponse> indexAnomalyDetectorResponse(RestChannel channel, RestRequest.Method method) {
return new RestResponseListener<IndexAnomalyDetectorResponse>(channel) {
@Override
public RestResponse buildResponse(IndexAnomalyDetectorResponse response) throws Exception {
RestStatus restStatus = RestStatus.CREATED;
if (method == RestRequest.Method.PUT) {
restStatus = RestStatus.OK;
}
BytesRestResponse bytesRestResponse = new BytesRestResponse(restStatus, response.toXContent(channel.newBuilder(), ToXContent.EMPTY_PARAMS));
if (restStatus == RestStatus.CREATED) {
String location = String.format(Locale.ROOT, "%s/%s", AnomalyDetectorPlugin.LEGACY_AD_BASE, response.getId());
bytesRestResponse.addHeader("Location", location);
}
return bytesRestResponse;
}
};
}
Aggregations