Search in sources :

Example 11 with AnomalyDetector

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());
}
Also used : AnomalyDetector(org.opensearch.ad.model.AnomalyDetector) TimeValue(org.opensearch.common.unit.TimeValue) Test(org.junit.Test)

Example 12 with AnomalyDetector

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());
}
Also used : Feature(org.opensearch.ad.model.Feature) AnomalyDetector(org.opensearch.ad.model.AnomalyDetector) TimeValue(org.opensearch.common.unit.TimeValue) Test(org.junit.Test)

Example 13 with AnomalyDetector

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());
}
Also used : AnomalyDetector(org.opensearch.ad.model.AnomalyDetector) TimeValue(org.opensearch.common.unit.TimeValue) Test(org.junit.Test)

Example 14 with AnomalyDetector

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())));
}
Also used : Feature(org.opensearch.ad.model.Feature) AnomalyDetector(org.opensearch.ad.model.AnomalyDetector) TimeValue(org.opensearch.common.unit.TimeValue) Test(org.junit.Test)

Example 15 with AnomalyDetector

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))));
}
Also used : EntityProfileRequest(org.opensearch.ad.transport.EntityProfileRequest) EntityProfileName(org.opensearch.ad.model.EntityProfileName) XContentParser(org.opensearch.common.xcontent.XContentParser) ANOMALY_DETECTOR_JOB_INDEX(org.opensearch.ad.model.AnomalyDetectorJob.ANOMALY_DETECTOR_JOB_INDEX) EntityProfileResponse(org.opensearch.ad.transport.EntityProfileResponse) Map(java.util.Map) AnomalyDetector(org.opensearch.ad.model.AnomalyDetector) SearchRequest(org.opensearch.action.search.SearchRequest) ActionListener(org.opensearch.action.ActionListener) EntityProfile(org.opensearch.ad.model.EntityProfile) NestedQueryBuilder(org.opensearch.index.query.NestedQueryBuilder) QueryBuilders(org.opensearch.index.query.QueryBuilders) Client(org.opensearch.client.Client) CommonName(org.opensearch.ad.constant.CommonName) NumericSetting(org.opensearch.ad.settings.NumericSetting) Preference(org.opensearch.cluster.routing.Preference) InitProgressProfile(org.opensearch.ad.model.InitProgressProfile) IndexNotFoundException(org.opensearch.index.IndexNotFoundException) ANOMALY_DETECTORS_INDEX(org.opensearch.ad.model.AnomalyDetector.ANOMALY_DETECTORS_INDEX) GetRequest(org.opensearch.action.get.GetRequest) LoggingDeprecationHandler(org.opensearch.common.xcontent.LoggingDeprecationHandler) Set(java.util.Set) TermQueryBuilder(org.opensearch.index.query.TermQueryBuilder) XContentParserUtils.ensureExpectedToken(org.opensearch.common.xcontent.XContentParserUtils.ensureExpectedToken) ScoreMode(org.apache.lucene.search.join.ScoreMode) MultiResponsesDelegateActionListener(org.opensearch.ad.util.MultiResponsesDelegateActionListener) AggregationBuilders(org.opensearch.search.aggregations.AggregationBuilders) List(java.util.List) CommonErrorMessages(org.opensearch.ad.constant.CommonErrorMessages) Logger(org.apache.logging.log4j.Logger) AnomalyResult(org.opensearch.ad.model.AnomalyResult) Entity(org.opensearch.ad.model.Entity) EntityState(org.opensearch.ad.model.EntityState) SearchSourceBuilder(org.opensearch.search.builder.SearchSourceBuilder) AnomalyDetectorJob(org.opensearch.ad.model.AnomalyDetectorJob) NamedXContentRegistry(org.opensearch.common.xcontent.NamedXContentRegistry) Optional(java.util.Optional) XContentType(org.opensearch.common.xcontent.XContentType) IntervalTimeConfiguration(org.opensearch.ad.model.IntervalTimeConfiguration) LogManager(org.apache.logging.log4j.LogManager) EntityProfileAction(org.opensearch.ad.transport.EntityProfileAction) ParseUtils(org.opensearch.ad.util.ParseUtils) BoolQueryBuilder(org.opensearch.index.query.BoolQueryBuilder) SearchRequest(org.opensearch.action.search.SearchRequest) BoolQueryBuilder(org.opensearch.index.query.BoolQueryBuilder) TermQueryBuilder(org.opensearch.index.query.TermQueryBuilder) IndexNotFoundException(org.opensearch.index.IndexNotFoundException) SearchSourceBuilder(org.opensearch.search.builder.SearchSourceBuilder)

Aggregations

AnomalyDetector (org.opensearch.ad.model.AnomalyDetector)226 IOException (java.io.IOException)71 ActionListener (org.opensearch.action.ActionListener)54 Response (org.opensearch.client.Response)45 Map (java.util.Map)43 List (java.util.List)41 Client (org.opensearch.client.Client)38 SearchRequest (org.opensearch.action.search.SearchRequest)37 LogManager (org.apache.logging.log4j.LogManager)35 Logger (org.apache.logging.log4j.Logger)35 ArrayList (java.util.ArrayList)33 Optional (java.util.Optional)33 Test (org.junit.Test)32 SearchResponse (org.opensearch.action.search.SearchResponse)32 Instant (java.time.Instant)30 TimeValue (org.opensearch.common.unit.TimeValue)30 HashMap (java.util.HashMap)29 Entity (org.opensearch.ad.model.Entity)29 NamedXContentRegistry (org.opensearch.common.xcontent.NamedXContentRegistry)29 SearchSourceBuilder (org.opensearch.search.builder.SearchSourceBuilder)29