use of org.opensearch.ad.model.AnomalyDetector.ANOMALY_DETECTORS_INDEX in project anomaly-detection by opensearch-project.
the class EntityProfileRunnerTests method testNotMultiEntityDetector.
@SuppressWarnings("unchecked")
public void testNotMultiEntityDetector() throws IOException, InterruptedException {
detector = TestHelpers.randomAnomalyDetectorWithInterval(new IntervalTimeConfiguration(detectorIntervalMin, ChronoUnit.MINUTES));
doAnswer(invocation -> {
Object[] args = invocation.getArguments();
GetRequest request = (GetRequest) args[0];
ActionListener<GetResponse> listener = (ActionListener<GetResponse>) args[1];
String indexName = request.index();
if (indexName.equals(ANOMALY_DETECTORS_INDEX)) {
listener.onResponse(TestHelpers.createGetResponse(detector, detector.getDetectorId(), AnomalyDetector.ANOMALY_DETECTORS_INDEX));
}
return null;
}).when(client).get(any(), any());
final CountDownLatch inProgressLatch = new CountDownLatch(1);
runner.profile(detectorId, entity, state, ActionListener.wrap(response -> {
assertTrue("Should not reach here", false);
inProgressLatch.countDown();
}, exception -> {
assertTrue(exception.getMessage().contains(EntityProfileRunner.NOT_HC_DETECTOR_ERR_MSG));
inProgressLatch.countDown();
}));
assertTrue(inProgressLatch.await(100, TimeUnit.SECONDS));
}
use of org.opensearch.ad.model.AnomalyDetector.ANOMALY_DETECTORS_INDEX in project anomaly-detection by opensearch-project.
the class EntityProfileRunnerTests method testJobIndexNotFound.
@SuppressWarnings("unchecked")
public void testJobIndexNotFound() throws InterruptedException {
setUpExecuteEntityProfileAction(InittedEverResultStatus.INITTED);
final CountDownLatch inProgressLatch = new CountDownLatch(1);
doAnswer(invocation -> {
Object[] args = invocation.getArguments();
GetRequest request = (GetRequest) args[0];
ActionListener<GetResponse> listener = (ActionListener<GetResponse>) args[1];
String indexName = request.index();
if (indexName.equals(ANOMALY_DETECTORS_INDEX)) {
listener.onResponse(TestHelpers.createGetResponse(detector, detector.getDetectorId(), AnomalyDetector.ANOMALY_DETECTORS_INDEX));
} else if (indexName.equals(ANOMALY_DETECTOR_JOB_INDEX)) {
listener.onFailure(new IndexNotFoundException(ANOMALY_DETECTOR_JOB_INDEX));
}
return null;
}).when(client).get(any(), any());
EntityProfile expectedProfile = new EntityProfile.Builder().build();
runner.profile(detectorId, entity, initNInfo, ActionListener.wrap(response -> {
assertEquals(expectedProfile, response);
inProgressLatch.countDown();
}, exception -> {
LOG.error("Unexpected error", exception);
assertTrue("Should not reach here", false);
inProgressLatch.countDown();
}));
assertTrue(inProgressLatch.await(100, TimeUnit.SECONDS));
}
use of org.opensearch.ad.model.AnomalyDetector.ANOMALY_DETECTORS_INDEX in project anomaly-detection by opensearch-project.
the class AbstractAnomalyDetectorActionHandler method updateAnomalyDetector.
protected void updateAnomalyDetector(String detectorId, boolean indexingDryRun) {
GetRequest request = new GetRequest(ANOMALY_DETECTORS_INDEX, detectorId);
client.get(request, ActionListener.wrap(response -> onGetAnomalyDetectorResponse(response, indexingDryRun, detectorId), exception -> listener.onFailure(exception)));
}
use of org.opensearch.ad.model.AnomalyDetector.ANOMALY_DETECTORS_INDEX in project anomaly-detection by opensearch-project.
the class AbstractAnomalyDetectorActionHandler method validateAgainstExistingMultiEntityAnomalyDetector.
protected void validateAgainstExistingMultiEntityAnomalyDetector(String detectorId, boolean indexingDryRun) {
if (anomalyDetectionIndices.doesAnomalyDetectorIndexExist()) {
QueryBuilder query = QueryBuilders.boolQuery().filter(QueryBuilders.existsQuery(AnomalyDetector.CATEGORY_FIELD));
SearchSourceBuilder searchSourceBuilder = new SearchSourceBuilder().query(query).size(0).timeout(requestTimeout);
SearchRequest searchRequest = new SearchRequest(ANOMALY_DETECTORS_INDEX).source(searchSourceBuilder);
client.search(searchRequest, ActionListener.wrap(response -> onSearchMultiEntityAdResponse(response, detectorId, indexingDryRun), exception -> listener.onFailure(exception)));
} else {
validateCategoricalField(detectorId, indexingDryRun);
}
}
use of org.opensearch.ad.model.AnomalyDetector.ANOMALY_DETECTORS_INDEX in project anomaly-detection by opensearch-project.
the class ADTaskManager method getDetector.
/**
* Get anomaly detector and execute consumer function.
* [Important!] Make sure listener returns in function
*
* @param detectorId detector id
* @param function consumer function
* @param listener action listener
* @param <T> action listener response type
*/
public <T> void getDetector(String detectorId, Consumer<Optional<AnomalyDetector>> function, ActionListener<T> listener) {
GetRequest getRequest = new GetRequest(ANOMALY_DETECTORS_INDEX, detectorId);
client.get(getRequest, ActionListener.wrap(response -> {
if (!response.isExists()) {
function.accept(Optional.empty());
return;
}
try (XContentParser parser = createXContentParserFromRegistry(xContentRegistry, response.getSourceAsBytesRef())) {
ensureExpectedToken(XContentParser.Token.START_OBJECT, parser.nextToken(), parser);
AnomalyDetector detector = AnomalyDetector.parse(parser, response.getId(), response.getVersion());
function.accept(Optional.of(detector));
} catch (Exception e) {
String message = "Failed to parse anomaly detector " + detectorId;
logger.error(message, e);
listener.onFailure(new OpenSearchStatusException(message, RestStatus.INTERNAL_SERVER_ERROR));
}
}, exception -> {
logger.error("Failed to get detector " + detectorId, exception);
listener.onFailure(exception);
}));
}
Aggregations