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