Search in sources :

Example 1 with DetectorValidationIssue

use of org.opensearch.ad.model.DetectorValidationIssue in project anomaly-detection by opensearch-project.

the class ValidateAnomalyDetectorResponseTests method testResponseSerialization.

@Test
public void testResponseSerialization() throws IOException {
    Map<String, String> subIssues = new HashMap<>();
    subIssues.put("a", "b");
    subIssues.put("c", "d");
    DetectorValidationIssue issue = TestHelpers.randomDetectorValidationIssueWithSubIssues(subIssues);
    ValidateAnomalyDetectorResponse response = new ValidateAnomalyDetectorResponse(issue);
    BytesStreamOutput output = new BytesStreamOutput();
    response.writeTo(output);
    StreamInput streamInput = output.bytes().streamInput();
    ValidateAnomalyDetectorResponse readResponse = ValidateAnomalyDetectorAction.INSTANCE.getResponseReader().read(streamInput);
    assertEquals("serialization has the wrong issue", issue, readResponse.getIssue());
}
Also used : HashMap(java.util.HashMap) StreamInput(org.opensearch.common.io.stream.StreamInput) DetectorValidationIssue(org.opensearch.ad.model.DetectorValidationIssue) BytesStreamOutput(org.opensearch.common.io.stream.BytesStreamOutput) AbstractADTest(org.opensearch.ad.AbstractADTest) Test(org.junit.Test)

Example 2 with DetectorValidationIssue

use of org.opensearch.ad.model.DetectorValidationIssue in project anomaly-detection by opensearch-project.

the class ValidateAnomalyDetectorResponseTests method testResponseToXContentWithSubIssues.

public void testResponseToXContentWithSubIssues() throws IOException {
    Map<String, String> subIssues = new HashMap<>();
    subIssues.put("a", "b");
    subIssues.put("c", "d");
    DetectorValidationIssue issue = TestHelpers.randomDetectorValidationIssueWithSubIssues(subIssues);
    ValidateAnomalyDetectorResponse response = new ValidateAnomalyDetectorResponse(issue);
    String validationResponse = TestHelpers.xContentBuilderToString(response.toXContent(TestHelpers.builder()));
    String message = issue.getMessage();
    assertEquals("{\"detector\":{\"name\":{\"message\":\"" + message + "\",\"sub_issues\":{\"a\":\"b\",\"c\":\"d\"}}}}", validationResponse);
}
Also used : HashMap(java.util.HashMap) DetectorValidationIssue(org.opensearch.ad.model.DetectorValidationIssue)

Example 3 with DetectorValidationIssue

use of org.opensearch.ad.model.DetectorValidationIssue in project anomaly-detection by opensearch-project.

the class ValidateAnomalyDetectorTransportAction method parseADValidationException.

protected DetectorValidationIssue parseADValidationException(ADValidationException exception) {
    String originalErrorMessage = exception.getMessage();
    String errorMessage = "";
    Map<String, String> subIssues = null;
    IntervalTimeConfiguration intervalSuggestion = exception.getIntervalSuggestion();
    switch(exception.getType()) {
        case FEATURE_ATTRIBUTES:
            int firstLeftBracketIndex = originalErrorMessage.indexOf("[");
            int lastRightBracketIndex = originalErrorMessage.lastIndexOf("]");
            if (firstLeftBracketIndex != -1) {
                // if feature issue messages are between square brackets like
                // [Feature has issue: A, Feature has issue: B]
                errorMessage = originalErrorMessage.substring(firstLeftBracketIndex + 1, lastRightBracketIndex);
                subIssues = getFeatureSubIssuesFromErrorMessage(errorMessage);
            } else {
                // features having issue like over max feature limit, duplicate feature name, etc.
                errorMessage = originalErrorMessage;
            }
            break;
        case NAME:
        case CATEGORY:
        case DETECTION_INTERVAL:
        case FILTER_QUERY:
        case TIMEFIELD_FIELD:
        case SHINGLE_SIZE_FIELD:
        case WINDOW_DELAY:
        case RESULT_INDEX:
        case GENERAL_SETTINGS:
        case AGGREGATION:
        case TIMEOUT:
        case INDICES:
            errorMessage = originalErrorMessage;
            break;
    }
    return new DetectorValidationIssue(exception.getAspect(), exception.getType(), errorMessage, subIssues, intervalSuggestion);
}
Also used : IntervalTimeConfiguration(org.opensearch.ad.model.IntervalTimeConfiguration) DetectorValidationIssue(org.opensearch.ad.model.DetectorValidationIssue)

Example 4 with DetectorValidationIssue

use of org.opensearch.ad.model.DetectorValidationIssue in project anomaly-detection by opensearch-project.

the class ValidateAnomalyDetectorTransportAction method checkIndicesAndExecute.

private void checkIndicesAndExecute(List<String> indices, AnomalyDetectorFunction function, ActionListener<ValidateAnomalyDetectorResponse> listener) {
    SearchRequest searchRequest = new SearchRequest().indices(indices.toArray(new String[0])).source(new SearchSourceBuilder().size(1).query(QueryBuilders.matchAllQuery()));
    client.search(searchRequest, ActionListener.wrap(r -> function.execute(), e -> {
        if (e instanceof IndexNotFoundException) {
            // IndexNotFoundException is converted to a ADValidationException that gets
            // parsed to a DetectorValidationIssue that is returned to
            // the user as a response indicating index doesn't exist
            DetectorValidationIssue issue = parseADValidationException(new ADValidationException(CommonErrorMessages.INDEX_NOT_FOUND, DetectorValidationIssueType.INDICES, ValidationAspect.DETECTOR));
            listener.onResponse(new ValidateAnomalyDetectorResponse(issue));
            return;
        }
        logger.error(e);
        listener.onFailure(e);
    }));
}
Also used : HandledTransportAction(org.opensearch.action.support.HandledTransportAction) HashMap(java.util.HashMap) ThreadContext(org.opensearch.common.util.concurrent.ThreadContext) AnomalyDetectorSettings(org.opensearch.ad.settings.AnomalyDetectorSettings) AnomalyDetectorFunction(org.opensearch.ad.rest.handler.AnomalyDetectorFunction) DetectorValidationIssue(org.opensearch.ad.model.DetectorValidationIssue) ParseUtils.getUserContext(org.opensearch.ad.util.ParseUtils.getUserContext) Locale(java.util.Locale) Map(java.util.Map) AnomalyDetector(org.opensearch.ad.model.AnomalyDetector) SearchRequest(org.opensearch.action.search.SearchRequest) Inject(org.opensearch.common.inject.Inject) ParseUtils.checkFilterByBackendRoles(org.opensearch.ad.util.ParseUtils.checkFilterByBackendRoles) ActionListener(org.opensearch.action.ActionListener) ValidationAspect(org.opensearch.ad.model.ValidationAspect) ValidateAnomalyDetectorActionHandler(org.opensearch.ad.rest.handler.ValidateAnomalyDetectorActionHandler) QueryBuilders(org.opensearch.index.query.QueryBuilders) Client(org.opensearch.client.Client) ADValidationException(org.opensearch.ad.common.exception.ADValidationException) RestRequest(org.opensearch.rest.RestRequest) IndexNotFoundException(org.opensearch.index.IndexNotFoundException) FILTER_BY_BACKEND_ROLES(org.opensearch.ad.settings.AnomalyDetectorSettings.FILTER_BY_BACKEND_ROLES) AnomalyDetectionIndices(org.opensearch.ad.indices.AnomalyDetectionIndices) Settings(org.opensearch.common.settings.Settings) Task(org.opensearch.tasks.Task) TransportService(org.opensearch.transport.TransportService) ActionFilters(org.opensearch.action.support.ActionFilters) List(java.util.List) CommonErrorMessages(org.opensearch.ad.constant.CommonErrorMessages) Logger(org.apache.logging.log4j.Logger) User(org.opensearch.commons.authuser.User) SearchSourceBuilder(org.opensearch.search.builder.SearchSourceBuilder) DetectorValidationIssueType(org.opensearch.ad.model.DetectorValidationIssueType) NamedXContentRegistry(org.opensearch.common.xcontent.NamedXContentRegistry) SearchFeatureDao(org.opensearch.ad.feature.SearchFeatureDao) ClusterService(org.opensearch.cluster.service.ClusterService) Clock(java.time.Clock) IntervalTimeConfiguration(org.opensearch.ad.model.IntervalTimeConfiguration) LogManager(org.apache.logging.log4j.LogManager) SearchRequest(org.opensearch.action.search.SearchRequest) IndexNotFoundException(org.opensearch.index.IndexNotFoundException) DetectorValidationIssue(org.opensearch.ad.model.DetectorValidationIssue) ADValidationException(org.opensearch.ad.common.exception.ADValidationException) SearchSourceBuilder(org.opensearch.search.builder.SearchSourceBuilder)

Example 5 with DetectorValidationIssue

use of org.opensearch.ad.model.DetectorValidationIssue in project anomaly-detection by opensearch-project.

the class ValidateAnomalyDetectorTransportAction method validateExecute.

private void validateExecute(ValidateAnomalyDetectorRequest request, User user, ThreadContext.StoredContext storedContext, ActionListener<ValidateAnomalyDetectorResponse> listener) {
    storedContext.restore();
    AnomalyDetector detector = request.getDetector();
    ActionListener<ValidateAnomalyDetectorResponse> validateListener = ActionListener.wrap(response -> {
        logger.debug("Result of validation process " + response);
        // forcing response to be empty
        listener.onResponse(new ValidateAnomalyDetectorResponse((DetectorValidationIssue) null));
    }, exception -> {
        if (exception instanceof ADValidationException) {
            // ADValidationException is converted as validation issues returned as response to user
            DetectorValidationIssue issue = parseADValidationException((ADValidationException) exception);
            listener.onResponse(new ValidateAnomalyDetectorResponse(issue));
            return;
        }
        logger.error(exception);
        listener.onFailure(exception);
    });
    checkIndicesAndExecute(detector.getIndices(), () -> {
        ValidateAnomalyDetectorActionHandler handler = new ValidateAnomalyDetectorActionHandler(clusterService, client, validateListener, anomalyDetectionIndices, detector, request.getRequestTimeout(), request.getMaxSingleEntityAnomalyDetectors(), request.getMaxMultiEntityAnomalyDetectors(), request.getMaxAnomalyFeatures(), RestRequest.Method.POST, xContentRegistry, user, searchFeatureDao, request.getValidationType(), clock);
        try {
            handler.start();
        } catch (Exception exception) {
            String errorMessage = String.format(Locale.ROOT, "Unknown exception caught while validating detector %s", request.getDetector());
            logger.error(errorMessage, exception);
            listener.onFailure(exception);
        }
    }, listener);
}
Also used : ValidateAnomalyDetectorActionHandler(org.opensearch.ad.rest.handler.ValidateAnomalyDetectorActionHandler) DetectorValidationIssue(org.opensearch.ad.model.DetectorValidationIssue) ADValidationException(org.opensearch.ad.common.exception.ADValidationException) AnomalyDetector(org.opensearch.ad.model.AnomalyDetector) ADValidationException(org.opensearch.ad.common.exception.ADValidationException) IndexNotFoundException(org.opensearch.index.IndexNotFoundException)

Aggregations

DetectorValidationIssue (org.opensearch.ad.model.DetectorValidationIssue)7 HashMap (java.util.HashMap)3 ADValidationException (org.opensearch.ad.common.exception.ADValidationException)3 AnomalyDetector (org.opensearch.ad.model.AnomalyDetector)3 List (java.util.List)2 Locale (java.util.Locale)2 CommonErrorMessages (org.opensearch.ad.constant.CommonErrorMessages)2 IntervalTimeConfiguration (org.opensearch.ad.model.IntervalTimeConfiguration)2 ValidationAspect (org.opensearch.ad.model.ValidationAspect)2 ValidateAnomalyDetectorActionHandler (org.opensearch.ad.rest.handler.ValidateAnomalyDetectorActionHandler)2 ClusterService (org.opensearch.cluster.service.ClusterService)2 Settings (org.opensearch.common.settings.Settings)2 IndexNotFoundException (org.opensearch.index.IndexNotFoundException)2 RestRequest (org.opensearch.rest.RestRequest)2 ImmutableList (com.google.common.collect.ImmutableList)1 IOException (java.io.IOException)1 Clock (java.time.Clock)1 Arrays (java.util.Arrays)1 Collections (java.util.Collections)1 HashSet (java.util.HashSet)1