Search in sources :

Example 21 with AnomalyDetectionException

use of org.opensearch.ad.common.exception.AnomalyDetectionException in project anomaly-detection by opensearch-project.

the class ParseUtils method batchFeatureQuery.

/**
 * Generate batch query request for feature aggregation on given date range.
 *
 * @param detector anomaly detector
 * @param entity entity
 * @param startTime start time
 * @param endTime end time
 * @param xContentRegistry content registry
 * @return search source builder
 * @throws IOException throw IO exception if fail to parse feature aggregation
 * @throws AnomalyDetectionException throw AD exception if no enabled feature
 */
public static SearchSourceBuilder batchFeatureQuery(AnomalyDetector detector, Entity entity, long startTime, long endTime, NamedXContentRegistry xContentRegistry) throws IOException {
    RangeQueryBuilder rangeQuery = new RangeQueryBuilder(detector.getTimeField()).from(startTime).to(endTime).format(EPOCH_MILLIS_FORMAT).includeLower(true).includeUpper(false);
    BoolQueryBuilder internalFilterQuery = QueryBuilders.boolQuery().must(rangeQuery).must(detector.getFilterQuery());
    if (detector.isMultientityDetector() && entity != null && entity.getAttributes().size() > 0) {
        entity.getAttributes().entrySet().forEach(attr -> {
            internalFilterQuery.filter(new TermQueryBuilder(attr.getKey(), attr.getValue()));
        });
    }
    long intervalSeconds = ((IntervalTimeConfiguration) detector.getDetectionInterval()).toDuration().getSeconds();
    List<CompositeValuesSourceBuilder<?>> sources = new ArrayList<>();
    sources.add(new DateHistogramValuesSourceBuilder(DATE_HISTOGRAM).field(detector.getTimeField()).fixedInterval(DateHistogramInterval.seconds((int) intervalSeconds)));
    CompositeAggregationBuilder aggregationBuilder = new CompositeAggregationBuilder(FEATURE_AGGS, sources).size(MAX_BATCH_TASK_PIECE_SIZE);
    if (detector.getEnabledFeatureIds().size() == 0) {
        throw new AnomalyDetectionException("No enabled feature configured").countedInStats(false);
    }
    for (Feature feature : detector.getFeatureAttributes()) {
        if (feature.getEnabled()) {
            AggregatorFactories.Builder internalAgg = parseAggregators(feature.getAggregation().toString(), xContentRegistry, feature.getId());
            aggregationBuilder.subAggregation(internalAgg.getAggregatorFactories().iterator().next());
        }
    }
    SearchSourceBuilder searchSourceBuilder = new SearchSourceBuilder();
    searchSourceBuilder.aggregation(aggregationBuilder);
    searchSourceBuilder.query(internalFilterQuery);
    searchSourceBuilder.size(0);
    return searchSourceBuilder;
}
Also used : AnomalyDetectionException(org.opensearch.ad.common.exception.AnomalyDetectionException) DoubleArrayList(com.carrotsearch.hppc.DoubleArrayList) ArrayList(java.util.ArrayList) CompositeAggregationBuilder(org.opensearch.search.aggregations.bucket.composite.CompositeAggregationBuilder) TermQueryBuilder(org.opensearch.index.query.TermQueryBuilder) RangeQueryBuilder(org.opensearch.index.query.RangeQueryBuilder) Feature(org.opensearch.ad.model.Feature) SearchSourceBuilder(org.opensearch.search.builder.SearchSourceBuilder) BoolQueryBuilder(org.opensearch.index.query.BoolQueryBuilder) DateHistogramValuesSourceBuilder(org.opensearch.search.aggregations.bucket.composite.DateHistogramValuesSourceBuilder) AggregatorFactories(org.opensearch.search.aggregations.AggregatorFactories) CompositeValuesSourceBuilder(org.opensearch.search.aggregations.bucket.composite.CompositeValuesSourceBuilder)

Example 22 with AnomalyDetectionException

use of org.opensearch.ad.common.exception.AnomalyDetectionException in project anomaly-detection by opensearch-project.

the class MultiEntityResultHandlerTests method testNothingToSave.

@Test
public void testNothingToSave() throws IOException, InterruptedException {
    setUpSavingAnomalyResultIndex(false);
    CountDownLatch verified = new CountDownLatch(1);
    handler.flush(new ADResultBulkRequest(), ActionListener.wrap(response -> {
        assertTrue("Should not reach here ", false);
        verified.countDown();
    }, exception -> {
        assertTrue(exception instanceof AnomalyDetectionException);
        verified.countDown();
    }));
    assertTrue(verified.await(100, TimeUnit.SECONDS));
}
Also used : ArgumentMatchers.any(org.mockito.ArgumentMatchers.any) ADResultBulkRequest(org.opensearch.ad.transport.ADResultBulkRequest) ArgumentMatchers(org.mockito.ArgumentMatchers) ArgumentMatchers.eq(org.mockito.ArgumentMatchers.eq) RequestPriority(org.opensearch.ad.ratelimit.RequestPriority) IOException(java.io.IOException) Test(org.junit.Test) ADResultBulkAction(org.opensearch.ad.transport.ADResultBulkAction) Instant(java.time.Instant) AnomalyDetectionException(org.opensearch.ad.common.exception.AnomalyDetectionException) TimeUnit(java.util.concurrent.TimeUnit) CountDownLatch(java.util.concurrent.CountDownLatch) ChronoUnit(java.time.temporal.ChronoUnit) ADResultBulkResponse(org.opensearch.ad.transport.ADResultBulkResponse) TestHelpers(org.opensearch.ad.TestHelpers) ResultWriteRequest(org.opensearch.ad.ratelimit.ResultWriteRequest) Mockito.doAnswer(org.mockito.Mockito.doAnswer) ActionListener(org.opensearch.action.ActionListener) AnomalyDetectionException(org.opensearch.ad.common.exception.AnomalyDetectionException) ADResultBulkRequest(org.opensearch.ad.transport.ADResultBulkRequest) CountDownLatch(java.util.concurrent.CountDownLatch) Test(org.junit.Test)

Example 23 with AnomalyDetectionException

use of org.opensearch.ad.common.exception.AnomalyDetectionException in project anomaly-detection by opensearch-project.

the class MultiEntityResultHandlerTests method testCreateUnAcked.

@Test
public void testCreateUnAcked() throws IOException, InterruptedException {
    setUpSavingAnomalyResultIndex(false, IndexCreation.NOT_ACKED);
    CountDownLatch verified = new CountDownLatch(1);
    handler.flush(request, ActionListener.wrap(response -> {
        assertTrue("Should not reach here ", false);
        verified.countDown();
    }, exception -> {
        assertTrue(exception instanceof AnomalyDetectionException);
        verified.countDown();
    }));
    assertTrue(verified.await(100, TimeUnit.SECONDS));
}
Also used : ArgumentMatchers.any(org.mockito.ArgumentMatchers.any) ADResultBulkRequest(org.opensearch.ad.transport.ADResultBulkRequest) ArgumentMatchers(org.mockito.ArgumentMatchers) ArgumentMatchers.eq(org.mockito.ArgumentMatchers.eq) RequestPriority(org.opensearch.ad.ratelimit.RequestPriority) IOException(java.io.IOException) Test(org.junit.Test) ADResultBulkAction(org.opensearch.ad.transport.ADResultBulkAction) Instant(java.time.Instant) AnomalyDetectionException(org.opensearch.ad.common.exception.AnomalyDetectionException) TimeUnit(java.util.concurrent.TimeUnit) CountDownLatch(java.util.concurrent.CountDownLatch) ChronoUnit(java.time.temporal.ChronoUnit) ADResultBulkResponse(org.opensearch.ad.transport.ADResultBulkResponse) TestHelpers(org.opensearch.ad.TestHelpers) ResultWriteRequest(org.opensearch.ad.ratelimit.ResultWriteRequest) Mockito.doAnswer(org.mockito.Mockito.doAnswer) ActionListener(org.opensearch.action.ActionListener) AnomalyDetectionException(org.opensearch.ad.common.exception.AnomalyDetectionException) CountDownLatch(java.util.concurrent.CountDownLatch) Test(org.junit.Test)

Example 24 with AnomalyDetectionException

use of org.opensearch.ad.common.exception.AnomalyDetectionException in project anomaly-detection by opensearch-project.

the class ExceptionUtilsTests method testCountInStats.

public void testCountInStats() {
    assertTrue(ExceptionUtil.countInStats(new AnomalyDetectionException("test")));
    assertFalse(ExceptionUtil.countInStats(new AnomalyDetectionException("test").countedInStats(false)));
    assertTrue(ExceptionUtil.countInStats(new RuntimeException("test")));
}
Also used : AnomalyDetectionException(org.opensearch.ad.common.exception.AnomalyDetectionException)

Example 25 with AnomalyDetectionException

use of org.opensearch.ad.common.exception.AnomalyDetectionException in project anomaly-detection by opensearch-project.

the class ParseUtilsTests method testBatchFeatureQueryWithoutFeature.

public void testBatchFeatureQueryWithoutFeature() throws IOException {
    String index = randomAlphaOfLength(5);
    Instant now = Instant.now().truncatedTo(ChronoUnit.SECONDS);
    AnomalyDetector detector = TestHelpers.randomAnomalyDetector(ImmutableList.of(index), ImmutableList.of(), null, now, 1, false, null);
    long startTime = now.minus(10, ChronoUnit.DAYS).toEpochMilli();
    long endTime = now.plus(10, ChronoUnit.DAYS).toEpochMilli();
    AnomalyDetectionException exception = expectThrows(AnomalyDetectionException.class, () -> ParseUtils.batchFeatureQuery(detector, null, startTime, endTime, TestHelpers.xContentRegistry()));
    assertEquals("No enabled feature configured", exception.getMessage());
}
Also used : AnomalyDetectionException(org.opensearch.ad.common.exception.AnomalyDetectionException) Instant(java.time.Instant) AnomalyDetector(org.opensearch.ad.model.AnomalyDetector)

Aggregations

AnomalyDetectionException (org.opensearch.ad.common.exception.AnomalyDetectionException)28 ActionListener (org.opensearch.action.ActionListener)10 IOException (java.io.IOException)9 Optional (java.util.Optional)8 EndRunException (org.opensearch.ad.common.exception.EndRunException)7 AnomalyDetector (org.opensearch.ad.model.AnomalyDetector)7 LogManager (org.apache.logging.log4j.LogManager)6 Logger (org.apache.logging.log4j.Logger)6 DiscoveryNode (org.opensearch.cluster.node.DiscoveryNode)6 ClusterService (org.opensearch.cluster.service.ClusterService)6 Settings (org.opensearch.common.settings.Settings)6 ArrayList (java.util.ArrayList)5 List (java.util.List)5 ModelManager (org.opensearch.ad.ml.ModelManager)5 Instant (java.time.Instant)4 Map (java.util.Map)4 ThreadedActionListener (org.opensearch.action.support.ThreadedActionListener)4 LimitExceededException (org.opensearch.ad.common.exception.LimitExceededException)4 ResourceNotFoundException (org.opensearch.ad.common.exception.ResourceNotFoundException)4 Entity (org.opensearch.ad.model.Entity)4