Search in sources :

Example 1 with IF_PRIMARY_TERM

use of org.opensearch.ad.util.RestHandlerUtils.IF_PRIMARY_TERM 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 IF_PRIMARY_TERM

use of org.opensearch.ad.util.RestHandlerUtils.IF_PRIMARY_TERM in project anomaly-detection by opensearch-project.

the class RestAnomalyDetectorJobAction 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);
    long seqNo = request.paramAsLong(IF_SEQ_NO, SequenceNumbers.UNASSIGNED_SEQ_NO);
    long primaryTerm = request.paramAsLong(IF_PRIMARY_TERM, SequenceNumbers.UNASSIGNED_PRIMARY_TERM);
    boolean historical = request.paramAsBoolean("historical", false);
    String rawPath = request.rawPath();
    DetectionDateRange detectionDateRange = parseDetectionDateRange(request);
    AnomalyDetectorJobRequest anomalyDetectorJobRequest = new AnomalyDetectorJobRequest(detectorId, detectionDateRange, historical, seqNo, primaryTerm, rawPath);
    return channel -> client.execute(AnomalyDetectorJobAction.INSTANCE, anomalyDetectorJobRequest, new RestToXContentListener<>(channel));
}
Also used : IF_SEQ_NO(org.opensearch.ad.util.RestHandlerUtils.IF_SEQ_NO) SequenceNumbers(org.opensearch.index.seqno.SequenceNumbers) REQUEST_TIMEOUT(org.opensearch.ad.settings.AnomalyDetectorSettings.REQUEST_TIMEOUT) EnabledSetting(org.opensearch.ad.settings.EnabledSetting) XContentParser(org.opensearch.common.xcontent.XContentParser) RestToXContentListener(org.opensearch.rest.action.RestToXContentListener) ImmutableList(com.google.common.collect.ImmutableList) IF_PRIMARY_TERM(org.opensearch.ad.util.RestHandlerUtils.IF_PRIMARY_TERM) Locale(java.util.Locale) STOP_JOB(org.opensearch.ad.util.RestHandlerUtils.STOP_JOB) BaseRestHandler(org.opensearch.rest.BaseRestHandler) AnomalyDetectorJobRequest(org.opensearch.ad.transport.AnomalyDetectorJobRequest) AnomalyDetectorJobAction(org.opensearch.ad.transport.AnomalyDetectorJobAction) TimeValue(org.opensearch.common.unit.TimeValue) NodeClient(org.opensearch.client.node.NodeClient) RestRequest(org.opensearch.rest.RestRequest) START_JOB(org.opensearch.ad.util.RestHandlerUtils.START_JOB) XContentParserUtils.ensureExpectedToken(org.opensearch.common.xcontent.XContentParserUtils.ensureExpectedToken) IOException(java.io.IOException) Settings(org.opensearch.common.settings.Settings) List(java.util.List) CommonErrorMessages(org.opensearch.ad.constant.CommonErrorMessages) DetectionDateRange(org.opensearch.ad.model.DetectionDateRange) ClusterService(org.opensearch.cluster.service.ClusterService) AnomalyDetectorPlugin(org.opensearch.ad.AnomalyDetectorPlugin) DETECTOR_ID(org.opensearch.ad.util.RestHandlerUtils.DETECTOR_ID) AnomalyDetectorJobRequest(org.opensearch.ad.transport.AnomalyDetectorJobRequest) DetectionDateRange(org.opensearch.ad.model.DetectionDateRange)

Aggregations

ImmutableList (com.google.common.collect.ImmutableList)2 IOException (java.io.IOException)2 List (java.util.List)2 Locale (java.util.Locale)2 AnomalyDetectorPlugin (org.opensearch.ad.AnomalyDetectorPlugin)2 CommonErrorMessages (org.opensearch.ad.constant.CommonErrorMessages)2 EnabledSetting (org.opensearch.ad.settings.EnabledSetting)2 DETECTOR_ID (org.opensearch.ad.util.RestHandlerUtils.DETECTOR_ID)2 IF_PRIMARY_TERM (org.opensearch.ad.util.RestHandlerUtils.IF_PRIMARY_TERM)2 IF_SEQ_NO (org.opensearch.ad.util.RestHandlerUtils.IF_SEQ_NO)2 NodeClient (org.opensearch.client.node.NodeClient)2 ClusterService (org.opensearch.cluster.service.ClusterService)2 Settings (org.opensearch.common.settings.Settings)2 XContentParser (org.opensearch.common.xcontent.XContentParser)2 XContentParserUtils.ensureExpectedToken (org.opensearch.common.xcontent.XContentParserUtils.ensureExpectedToken)2 SequenceNumbers (org.opensearch.index.seqno.SequenceNumbers)2 RestRequest (org.opensearch.rest.RestRequest)2 LogManager (org.apache.logging.log4j.LogManager)1 Logger (org.apache.logging.log4j.Logger)1 WriteRequest (org.opensearch.action.support.WriteRequest)1