Search in sources :

Example 1 with Features

use of org.opensearch.ad.feature.Features in project anomaly-detection by opensearch-project.

the class PreviewAnomalyDetectorTransportActionTests method testPreviewTransportActionWithDetector.

@SuppressWarnings("unchecked")
@Test
public void testPreviewTransportActionWithDetector() throws IOException, InterruptedException {
    final CountDownLatch inProgressLatch = new CountDownLatch(1);
    CreateIndexResponse createResponse = TestHelpers.createIndex(client().admin(), AnomalyDetector.ANOMALY_DETECTORS_INDEX, AnomalyDetectionIndices.getAnomalyDetectorMappings());
    Assert.assertNotNull(createResponse);
    AnomalyDetector detector = TestHelpers.randomAnomalyDetector(ImmutableMap.of("testKey", "testValue"), Instant.now());
    IndexRequest indexRequest = new IndexRequest(AnomalyDetector.ANOMALY_DETECTORS_INDEX).setRefreshPolicy(WriteRequest.RefreshPolicy.IMMEDIATE).source(detector.toXContent(XContentFactory.jsonBuilder(), RestHandlerUtils.XCONTENT_WITH_TYPE));
    IndexResponse indexResponse = client().index(indexRequest).actionGet(5_000);
    assertEquals(RestStatus.CREATED, indexResponse.status());
    PreviewAnomalyDetectorRequest request = new PreviewAnomalyDetectorRequest(null, indexResponse.getId(), Instant.now(), Instant.now());
    ActionListener<PreviewAnomalyDetectorResponse> previewResponse = new ActionListener<PreviewAnomalyDetectorResponse>() {

        @Override
        public void onResponse(PreviewAnomalyDetectorResponse response) {
            try {
                XContentBuilder previewBuilder = response.toXContent(TestHelpers.builder(), ToXContent.EMPTY_PARAMS);
                Assert.assertNotNull(previewBuilder);
                Map<String, Object> map = TestHelpers.XContentBuilderToMap(previewBuilder);
                List<AnomalyResult> results = (List<AnomalyResult>) map.get("anomaly_result");
                Assert.assertNotNull(results);
                Assert.assertTrue(results.size() > 0);
                inProgressLatch.countDown();
            } catch (IOException e) {
                // Should not reach here
                Assert.assertTrue(false);
            }
        }

        @Override
        public void onFailure(Exception e) {
            // onFailure should not be called
            Assert.assertTrue(false);
        }
    };
    doReturn(TestHelpers.randomThresholdingResults()).when(modelManager).getPreviewResults(any(), anyInt());
    doAnswer(responseMock -> {
        Long startTime = responseMock.getArgument(1);
        ActionListener<Features> listener = responseMock.getArgument(3);
        listener.onResponse(TestHelpers.randomFeatures());
        return null;
    }).when(featureManager).getPreviewFeatures(anyObject(), anyLong(), anyLong(), any());
    action.doExecute(task, request, previewResponse);
    assertTrue(inProgressLatch.await(100, TimeUnit.SECONDS));
}
Also used : IOException(java.io.IOException) CountDownLatch(java.util.concurrent.CountDownLatch) CreateIndexRequest(org.opensearch.action.admin.indices.create.CreateIndexRequest) IndexRequest(org.opensearch.action.index.IndexRequest) AnomalyDetector(org.opensearch.ad.model.AnomalyDetector) OpenSearchStatusException(org.opensearch.OpenSearchStatusException) IOException(java.io.IOException) ActionListener(org.opensearch.action.ActionListener) IndexResponse(org.opensearch.action.index.IndexResponse) CreateIndexResponse(org.opensearch.action.admin.indices.create.CreateIndexResponse) ArgumentMatchers.anyLong(org.mockito.ArgumentMatchers.anyLong) ArgumentMatchers.anyObject(org.mockito.ArgumentMatchers.anyObject) AnomalyResult(org.opensearch.ad.model.AnomalyResult) List(java.util.List) Features(org.opensearch.ad.feature.Features) CreateIndexResponse(org.opensearch.action.admin.indices.create.CreateIndexResponse) XContentBuilder(org.opensearch.common.xcontent.XContentBuilder) Test(org.junit.Test)

Example 2 with Features

use of org.opensearch.ad.feature.Features in project anomaly-detection by opensearch-project.

the class PreviewAnomalyDetectorTransportActionTests method testPreviewTransportAction.

@SuppressWarnings("unchecked")
@Test
public void testPreviewTransportAction() throws IOException, InterruptedException {
    final CountDownLatch inProgressLatch = new CountDownLatch(1);
    AnomalyDetector detector = TestHelpers.randomAnomalyDetector(ImmutableMap.of("testKey", "testValue"), Instant.now());
    PreviewAnomalyDetectorRequest request = new PreviewAnomalyDetectorRequest(detector, detector.getDetectorId(), Instant.now(), Instant.now());
    ActionListener<PreviewAnomalyDetectorResponse> previewResponse = new ActionListener<PreviewAnomalyDetectorResponse>() {

        @Override
        public void onResponse(PreviewAnomalyDetectorResponse response) {
            try {
                XContentBuilder previewBuilder = response.toXContent(TestHelpers.builder(), ToXContent.EMPTY_PARAMS);
                Assert.assertNotNull(previewBuilder);
                Map<String, Object> map = TestHelpers.XContentBuilderToMap(previewBuilder);
                List<AnomalyResult> results = (List<AnomalyResult>) map.get("anomaly_result");
                Assert.assertNotNull(results);
                Assert.assertTrue(results.size() > 0);
                inProgressLatch.countDown();
            } catch (IOException e) {
                // Should not reach here
                Assert.assertTrue(false);
            }
        }

        @Override
        public void onFailure(Exception e) {
            // onFailure should not be called
            Assert.assertTrue(false);
        }
    };
    doReturn(TestHelpers.randomThresholdingResults()).when(modelManager).getPreviewResults(any(), anyInt());
    doAnswer(responseMock -> {
        Long startTime = responseMock.getArgument(1);
        ActionListener<Features> listener = responseMock.getArgument(3);
        listener.onResponse(TestHelpers.randomFeatures());
        return null;
    }).when(featureManager).getPreviewFeatures(anyObject(), anyLong(), anyLong(), any());
    action.doExecute(task, request, previewResponse);
    assertTrue(inProgressLatch.await(100, TimeUnit.SECONDS));
}
Also used : IOException(java.io.IOException) CountDownLatch(java.util.concurrent.CountDownLatch) AnomalyDetector(org.opensearch.ad.model.AnomalyDetector) OpenSearchStatusException(org.opensearch.OpenSearchStatusException) IOException(java.io.IOException) ActionListener(org.opensearch.action.ActionListener) ArgumentMatchers.anyLong(org.mockito.ArgumentMatchers.anyLong) ArgumentMatchers.anyObject(org.mockito.ArgumentMatchers.anyObject) AnomalyResult(org.opensearch.ad.model.AnomalyResult) List(java.util.List) Features(org.opensearch.ad.feature.Features) XContentBuilder(org.opensearch.common.xcontent.XContentBuilder) Test(org.junit.Test)

Example 3 with Features

use of org.opensearch.ad.feature.Features in project anomaly-detection by opensearch-project.

the class AnomalyDetectorRunner method executeDetector.

/**
 * run anomaly detector and return anomaly result.
 *
 * @param detector  anomaly detector instance
 * @param startTime detection period start time
 * @param endTime   detection period end time
 * @param context   stored thread context
 * @param listener handle anomaly result
 * @throws IOException - if a user gives wrong query input when defining a detector
 */
public void executeDetector(AnomalyDetector detector, Instant startTime, Instant endTime, ThreadContext.StoredContext context, ActionListener<List<AnomalyResult>> listener) throws IOException {
    context.restore();
    List<String> categoryField = detector.getCategoryField();
    if (categoryField != null && !categoryField.isEmpty()) {
        featureManager.getPreviewEntities(detector, startTime.toEpochMilli(), endTime.toEpochMilli(), ActionListener.wrap(entities -> {
            if (entities == null || entities.isEmpty()) {
                // TODO return exception like IllegalArgumentException to explain data is not enough for preview
                // This also requires front-end change to handle error message correspondingly
                // We return empty list for now to avoid breaking front-end
                listener.onResponse(Collections.emptyList());
                return;
            }
            ActionListener<EntityAnomalyResult> entityAnomalyResultListener = ActionListener.wrap(entityAnomalyResult -> {
                listener.onResponse(entityAnomalyResult.getAnomalyResults());
            }, e -> onFailure(e, listener, detector.getDetectorId()));
            MultiResponsesDelegateActionListener<EntityAnomalyResult> multiEntitiesResponseListener = new MultiResponsesDelegateActionListener<EntityAnomalyResult>(entityAnomalyResultListener, entities.size(), String.format(Locale.ROOT, "Fail to get preview result for multi entity detector %s", detector.getDetectorId()), true);
            for (Entity entity : entities) {
                featureManager.getPreviewFeaturesForEntity(detector, entity, startTime.toEpochMilli(), endTime.toEpochMilli(), ActionListener.wrap(features -> {
                    List<ThresholdingResult> entityResults = modelManager.getPreviewResults(features.getProcessedFeatures(), detector.getShingleSize());
                    List<AnomalyResult> sampledEntityResults = sample(parsePreviewResult(detector, features, entityResults, entity), maxPreviewResults);
                    multiEntitiesResponseListener.onResponse(new EntityAnomalyResult(sampledEntityResults));
                }, e -> multiEntitiesResponseListener.onFailure(e)));
            }
        }, e -> onFailure(e, listener, detector.getDetectorId())));
    } else {
        featureManager.getPreviewFeatures(detector, startTime.toEpochMilli(), endTime.toEpochMilli(), ActionListener.wrap(features -> {
            try {
                List<ThresholdingResult> results = modelManager.getPreviewResults(features.getProcessedFeatures(), detector.getShingleSize());
                listener.onResponse(sample(parsePreviewResult(detector, features, results, null), maxPreviewResults));
            } catch (Exception e) {
                onFailure(e, listener, detector.getDetectorId());
            }
        }, e -> onFailure(e, listener, detector.getDetectorId())));
    }
}
Also used : ModelManager(org.opensearch.ad.ml.ModelManager) ThresholdingResult(org.opensearch.ad.ml.ThresholdingResult) FeatureData(org.opensearch.ad.model.FeatureData) ThreadContext(org.opensearch.common.util.concurrent.ThreadContext) ArrayList(java.util.ArrayList) Locale(java.util.Locale) Map(java.util.Map) AnomalyDetector(org.opensearch.ad.model.AnomalyDetector) ActionListener(org.opensearch.action.ActionListener) CommonValue(org.opensearch.ad.constant.CommonValue) FeatureManager(org.opensearch.ad.feature.FeatureManager) Feature(org.opensearch.ad.model.Feature) IOException(java.io.IOException) Instant(java.time.Instant) Collectors(java.util.stream.Collectors) OpenSearchSecurityException(org.opensearch.OpenSearchSecurityException) Features(org.opensearch.ad.feature.Features) MultiResponsesDelegateActionListener(org.opensearch.ad.util.MultiResponsesDelegateActionListener) List(java.util.List) Logger(org.apache.logging.log4j.Logger) AnomalyResult(org.opensearch.ad.model.AnomalyResult) Entity(org.opensearch.ad.model.Entity) EntityAnomalyResult(org.opensearch.ad.model.EntityAnomalyResult) Collections(java.util.Collections) LogManager(org.apache.logging.log4j.LogManager) Entity(org.opensearch.ad.model.Entity) MultiResponsesDelegateActionListener(org.opensearch.ad.util.MultiResponsesDelegateActionListener) IOException(java.io.IOException) OpenSearchSecurityException(org.opensearch.OpenSearchSecurityException) ThresholdingResult(org.opensearch.ad.ml.ThresholdingResult) ActionListener(org.opensearch.action.ActionListener) MultiResponsesDelegateActionListener(org.opensearch.ad.util.MultiResponsesDelegateActionListener) AnomalyResult(org.opensearch.ad.model.AnomalyResult) EntityAnomalyResult(org.opensearch.ad.model.EntityAnomalyResult) ArrayList(java.util.ArrayList) List(java.util.List) EntityAnomalyResult(org.opensearch.ad.model.EntityAnomalyResult)

Aggregations

IOException (java.io.IOException)3 List (java.util.List)3 ActionListener (org.opensearch.action.ActionListener)3 Features (org.opensearch.ad.feature.Features)3 AnomalyDetector (org.opensearch.ad.model.AnomalyDetector)3 AnomalyResult (org.opensearch.ad.model.AnomalyResult)3 CountDownLatch (java.util.concurrent.CountDownLatch)2 Test (org.junit.Test)2 ArgumentMatchers.anyLong (org.mockito.ArgumentMatchers.anyLong)2 ArgumentMatchers.anyObject (org.mockito.ArgumentMatchers.anyObject)2 OpenSearchStatusException (org.opensearch.OpenSearchStatusException)2 XContentBuilder (org.opensearch.common.xcontent.XContentBuilder)2 Instant (java.time.Instant)1 ArrayList (java.util.ArrayList)1 Collections (java.util.Collections)1 Locale (java.util.Locale)1 Map (java.util.Map)1 Collectors (java.util.stream.Collectors)1 LogManager (org.apache.logging.log4j.LogManager)1 Logger (org.apache.logging.log4j.Logger)1