Search in sources :

Example 1 with IndexAnomalyDetectorResponse

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));
}
Also used : IF_SEQ_NO(org.opensearch.ad.util.RestHandlerUtils.IF_SEQ_NO) SequenceNumbers(org.opensearch.index.seqno.SequenceNumbers) ToXContent(org.opensearch.common.xcontent.ToXContent) EnabledSetting(org.opensearch.ad.settings.EnabledSetting) XContentParser(org.opensearch.common.xcontent.XContentParser) WriteRequest(org.opensearch.action.support.WriteRequest) ImmutableList(com.google.common.collect.ImmutableList) IF_PRIMARY_TERM(org.opensearch.ad.util.RestHandlerUtils.IF_PRIMARY_TERM) IndexAnomalyDetectorResponse(org.opensearch.ad.transport.IndexAnomalyDetectorResponse) Locale(java.util.Locale) AnomalyDetector(org.opensearch.ad.model.AnomalyDetector) IndexAnomalyDetectorAction(org.opensearch.ad.transport.IndexAnomalyDetectorAction) RestResponseListener(org.opensearch.rest.action.RestResponseListener) IndexAnomalyDetectorRequest(org.opensearch.ad.transport.IndexAnomalyDetectorRequest) NodeClient(org.opensearch.client.node.NodeClient) RestRequest(org.opensearch.rest.RestRequest) REFRESH(org.opensearch.ad.util.RestHandlerUtils.REFRESH) XContentParserUtils.ensureExpectedToken(org.opensearch.common.xcontent.XContentParserUtils.ensureExpectedToken) IOException(java.io.IOException) Settings(org.opensearch.common.settings.Settings) RestStatus(org.opensearch.rest.RestStatus) BytesRestResponse(org.opensearch.rest.BytesRestResponse) RestResponse(org.opensearch.rest.RestResponse) List(java.util.List) CommonErrorMessages(org.opensearch.ad.constant.CommonErrorMessages) Logger(org.apache.logging.log4j.Logger) RestChannel(org.opensearch.rest.RestChannel) ClusterService(org.opensearch.cluster.service.ClusterService) AnomalyDetectorPlugin(org.opensearch.ad.AnomalyDetectorPlugin) LogManager(org.apache.logging.log4j.LogManager) DETECTOR_ID(org.opensearch.ad.util.RestHandlerUtils.DETECTOR_ID) RestRequest(org.opensearch.rest.RestRequest) WriteRequest(org.opensearch.action.support.WriteRequest) IndexAnomalyDetectorRequest(org.opensearch.ad.transport.IndexAnomalyDetectorRequest) XContentParser(org.opensearch.common.xcontent.XContentParser) AnomalyDetector(org.opensearch.ad.model.AnomalyDetector)

Example 2 with IndexAnomalyDetectorResponse

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);
            }
        }
    });
}
Also used : IndexResponse(org.opensearch.action.index.IndexResponse) CreateIndexResponse(org.opensearch.action.admin.indices.create.CreateIndexResponse) IndexAnomalyDetectorResponse(org.opensearch.ad.transport.IndexAnomalyDetectorResponse) IndexRequest(org.opensearch.action.index.IndexRequest) AnomalyDetector(org.opensearch.ad.model.AnomalyDetector) OpenSearchStatusException(org.opensearch.OpenSearchStatusException) ADValidationException(org.opensearch.ad.common.exception.ADValidationException) IOException(java.io.IOException) OpenSearchStatusException(org.opensearch.OpenSearchStatusException)

Example 3 with IndexAnomalyDetectorResponse

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;
        }
    };
}
Also used : RestStatus(org.opensearch.rest.RestStatus) RestResponseListener(org.opensearch.rest.action.RestResponseListener) BytesRestResponse(org.opensearch.rest.BytesRestResponse) IndexAnomalyDetectorResponse(org.opensearch.ad.transport.IndexAnomalyDetectorResponse)

Aggregations

IndexAnomalyDetectorResponse (org.opensearch.ad.transport.IndexAnomalyDetectorResponse)3 IOException (java.io.IOException)2 AnomalyDetector (org.opensearch.ad.model.AnomalyDetector)2 BytesRestResponse (org.opensearch.rest.BytesRestResponse)2 RestStatus (org.opensearch.rest.RestStatus)2 RestResponseListener (org.opensearch.rest.action.RestResponseListener)2 ImmutableList (com.google.common.collect.ImmutableList)1 List (java.util.List)1 Locale (java.util.Locale)1 LogManager (org.apache.logging.log4j.LogManager)1 Logger (org.apache.logging.log4j.Logger)1 OpenSearchStatusException (org.opensearch.OpenSearchStatusException)1 CreateIndexResponse (org.opensearch.action.admin.indices.create.CreateIndexResponse)1 IndexRequest (org.opensearch.action.index.IndexRequest)1 IndexResponse (org.opensearch.action.index.IndexResponse)1 WriteRequest (org.opensearch.action.support.WriteRequest)1 AnomalyDetectorPlugin (org.opensearch.ad.AnomalyDetectorPlugin)1 ADValidationException (org.opensearch.ad.common.exception.ADValidationException)1 CommonErrorMessages (org.opensearch.ad.constant.CommonErrorMessages)1 EnabledSetting (org.opensearch.ad.settings.EnabledSetting)1