use of org.opensearch.ad.model.AnomalyDetector in project anomaly-detection by opensearch-project.
the class ValidateAnomalyDetectorTransportActionTests method testValidateAnomalyDetectorWithDuplicateName.
@Test
public void testValidateAnomalyDetectorWithDuplicateName() throws IOException {
AnomalyDetector anomalyDetector = TestHelpers.randomAnomalyDetector(timeField, "index-test");
ingestTestDataValidate(anomalyDetector.getIndices().get(0), Instant.now().minus(1, ChronoUnit.DAYS), 1, "error");
createDetectorIndex();
createDetector(anomalyDetector);
ValidateAnomalyDetectorRequest request = new ValidateAnomalyDetectorRequest(anomalyDetector, ValidationAspect.DETECTOR.getName(), 5, 5, 5, new TimeValue(5_000L));
ValidateAnomalyDetectorResponse response = client().execute(ValidateAnomalyDetectorAction.INSTANCE, request).actionGet(5_000);
assertNotNull(response.getIssue());
assertEquals(DetectorValidationIssueType.NAME, response.getIssue().getType());
assertEquals(ValidationAspect.DETECTOR, response.getIssue().getAspect());
}
use of org.opensearch.ad.model.AnomalyDetector in project anomaly-detection by opensearch-project.
the class ValidateAnomalyDetectorTransportActionTests method testValidateAnomalyDetectorWithDuplicateFeatureNamesAndDuplicateAggregationNames.
@Test
public void testValidateAnomalyDetectorWithDuplicateFeatureNamesAndDuplicateAggregationNames() throws IOException {
Feature maxFeature = maxValueFeature(nameField, categoryField, nameField);
Feature maxFeatureTwo = maxValueFeature(nameField, categoryField, nameField);
AnomalyDetector anomalyDetector = TestHelpers.randomAnomalyDetector(timeField, "test-index", ImmutableList.of(maxFeature, maxFeatureTwo));
ingestTestDataValidate(anomalyDetector.getIndices().get(0), Instant.now().minus(1, ChronoUnit.DAYS), 1, "error");
ValidateAnomalyDetectorRequest request = new ValidateAnomalyDetectorRequest(anomalyDetector, ValidationAspect.DETECTOR.getName(), 5, 5, 5, new TimeValue(5_000L));
ValidateAnomalyDetectorResponse response = client().execute(ValidateAnomalyDetectorAction.INSTANCE, request).actionGet(5_000);
assertNotNull(response.getIssue());
assertTrue(response.getIssue().getMessage().contains("Detector has duplicate feature aggregation query names:"));
assertTrue(response.getIssue().getMessage().contains("Detector has duplicate feature names:"));
assertEquals(DetectorValidationIssueType.FEATURE_ATTRIBUTES, response.getIssue().getType());
assertEquals(ValidationAspect.DETECTOR, response.getIssue().getAspect());
}
use of org.opensearch.ad.model.AnomalyDetector in project anomaly-detection by opensearch-project.
the class ValidateAnomalyDetectorTransportActionTests method testValidateAnomalyDetectorWithCustomResultIndex.
@Test
public void testValidateAnomalyDetectorWithCustomResultIndex() throws IOException {
String resultIndex = CommonName.CUSTOM_RESULT_INDEX_PREFIX + "test";
createCustomADResultIndex(resultIndex);
AnomalyDetector anomalyDetector = TestHelpers.randomDetector(ImmutableList.of(TestHelpers.randomFeature()), randomAlphaOfLength(5).toLowerCase(Locale.ROOT), randomIntBetween(1, 5), timeField, null, resultIndex);
ingestTestDataValidate(anomalyDetector.getIndices().get(0), Instant.now().minus(1, ChronoUnit.DAYS), 1, "error");
ValidateAnomalyDetectorRequest request = new ValidateAnomalyDetectorRequest(anomalyDetector, ValidationAspect.DETECTOR.getName(), 5, 5, 5, new TimeValue(5_000L));
ValidateAnomalyDetectorResponse response = client().execute(ValidateAnomalyDetectorAction.INSTANCE, request).actionGet(5_000);
assertNull(response.getIssue());
}
use of org.opensearch.ad.model.AnomalyDetector in project anomaly-detection by opensearch-project.
the class ValidateAnomalyDetectorTransportActionTests method testValidateAnomalyDetectorWithNonExistingFeatureField.
@Test
public void testValidateAnomalyDetectorWithNonExistingFeatureField() throws IOException {
Feature maxFeature = maxValueFeature(nameField, "non_existing_field", nameField);
AnomalyDetector anomalyDetector = TestHelpers.randomAnomalyDetector(timeField, "test-index", ImmutableList.of(maxFeature));
ingestTestDataValidate(anomalyDetector.getIndices().get(0), Instant.now().minus(1, ChronoUnit.DAYS), 1, "error");
ValidateAnomalyDetectorRequest request = new ValidateAnomalyDetectorRequest(anomalyDetector, ValidationAspect.DETECTOR.getName(), 5, 5, 5, new TimeValue(5_000L));
ValidateAnomalyDetectorResponse response = client().execute(ValidateAnomalyDetectorAction.INSTANCE, request).actionGet(5_000);
assertNotNull(response.getIssue());
assertEquals(DetectorValidationIssueType.FEATURE_ATTRIBUTES, response.getIssue().getType());
assertEquals(ValidationAspect.DETECTOR, response.getIssue().getAspect());
assertTrue(response.getIssue().getMessage().contains(CommonErrorMessages.FEATURE_WITH_EMPTY_DATA_MSG));
assertTrue(response.getIssue().getSubIssues().containsKey(maxFeature.getName()));
assertTrue(CommonErrorMessages.FEATURE_WITH_EMPTY_DATA_MSG.contains(response.getIssue().getSubIssues().get(maxFeature.getName())));
}
use of org.opensearch.ad.model.AnomalyDetector in project anomaly-detection by opensearch-project.
the class EntityProfileRunner method validateEntity.
/**
* Verify if the input entity exists or not in case of typos.
*
* If a user deletes the entity after job start, then we will not be able to
* get this entity in the index. For this case, we will not return a profile
* for this entity even if it's running on some data node. the entity's model
* will be deleted by another entity or by maintenance due to long inactivity.
*
* @param entity Entity accessor
* @param categoryFields category fields defined for a detector
* @param detectorId Detector Id
* @param profilesToCollect Profile to collect from the input
* @param detector Detector config accessor
* @param listener Callback to send responses.
*/
private void validateEntity(Entity entity, List<String> categoryFields, String detectorId, Set<EntityProfileName> profilesToCollect, AnomalyDetector detector, ActionListener<EntityProfile> listener) {
Map<String, String> attributes = entity.getAttributes();
if (attributes == null || attributes.size() != categoryFields.size()) {
listener.onFailure(new IllegalArgumentException(EMPTY_ENTITY_ATTRIBUTES));
return;
}
for (String field : categoryFields) {
if (false == attributes.containsKey(field)) {
listener.onFailure(new IllegalArgumentException("Cannot find " + field));
return;
}
}
BoolQueryBuilder internalFilterQuery = QueryBuilders.boolQuery().filter(detector.getFilterQuery());
for (TermQueryBuilder term : entity.getTermQueryBuilders()) {
internalFilterQuery.filter(term);
}
SearchSourceBuilder searchSourceBuilder = new SearchSourceBuilder().query(internalFilterQuery).size(1);
SearchRequest searchRequest = new SearchRequest(detector.getIndices().toArray(new String[0]), searchSourceBuilder).preference(Preference.LOCAL.toString());
client.search(searchRequest, ActionListener.wrap(searchResponse -> {
try {
if (searchResponse.getHits().getHits().length == 0) {
listener.onFailure(new IllegalArgumentException(NO_ENTITY));
return;
}
prepareEntityProfile(listener, detectorId, entity, profilesToCollect, detector, categoryFields.get(0));
} catch (Exception e) {
listener.onFailure(new IllegalArgumentException(NO_ENTITY));
return;
}
}, e -> listener.onFailure(new IllegalArgumentException(NO_ENTITY))));
}
Aggregations