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());
}
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);
}
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);
}
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);
}));
}
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);
}
Aggregations