use of org.opensearch.ad.model.AnomalyDetector in project anomaly-detection by opensearch-project.
the class ParseUtils method getDetector.
/**
* If filterByEnabled is true, get detector and check if the user has permissions to access the detector,
* then execute function; otherwise, get detector and execute function
* @param requestUser user from request
* @param detectorId detector id
* @param listener action listener
* @param function consumer function
* @param client client
* @param clusterService cluster service
* @param xContentRegistry XContent registry
* @param filterByBackendRole filter by backend role or not
*/
public static void getDetector(User requestUser, String detectorId, ActionListener listener, Consumer<AnomalyDetector> function, Client client, ClusterService clusterService, NamedXContentRegistry xContentRegistry, boolean filterByBackendRole) {
if (clusterService.state().metadata().indices().containsKey(AnomalyDetector.ANOMALY_DETECTORS_INDEX)) {
GetRequest request = new GetRequest(AnomalyDetector.ANOMALY_DETECTORS_INDEX).id(detectorId);
client.get(request, ActionListener.wrap(response -> onGetAdResponse(response, requestUser, detectorId, listener, function, xContentRegistry, filterByBackendRole), exception -> {
logger.error("Failed to get anomaly detector: " + detectorId, exception);
listener.onFailure(exception);
}));
} else {
listener.onFailure(new IndexNotFoundException(AnomalyDetector.ANOMALY_DETECTORS_INDEX));
}
}
use of org.opensearch.ad.model.AnomalyDetector in project anomaly-detection by opensearch-project.
the class IndexAnomalyDetectorActionHandlerTests method testMoreThanTenMultiEntityDetectors.
@SuppressWarnings("unchecked")
public void testMoreThanTenMultiEntityDetectors() throws IOException {
String field = "a";
AnomalyDetector detector = TestHelpers.randomAnomalyDetectorUsingCategoryFields(detectorId, Arrays.asList(field));
SearchResponse detectorResponse = mock(SearchResponse.class);
int totalHits = 11;
when(detectorResponse.getHits()).thenReturn(TestHelpers.createSearchHits(totalHits));
SearchResponse userIndexResponse = mock(SearchResponse.class);
int userIndexHits = 0;
when(userIndexResponse.getHits()).thenReturn(TestHelpers.createSearchHits(userIndexHits));
// extend NodeClient since its execute method is final and mockito does not allow to mock final methods
// we can also use spy to overstep the final methods
NodeClient client = getCustomNodeClient(detectorResponse, userIndexResponse, detector, threadPool);
NodeClient clientSpy = spy(client);
handler = new IndexAnomalyDetectorActionHandler(clusterService, clientSpy, transportService, channel, anomalyDetectionIndices, detectorId, seqNo, primaryTerm, refreshPolicy, detector, requestTimeout, maxSingleEntityAnomalyDetectors, maxMultiEntityAnomalyDetectors, maxAnomalyFeatures, method, xContentRegistry(), null, adTaskManager, searchFeatureDao);
handler.start();
ArgumentCaptor<Exception> response = ArgumentCaptor.forClass(Exception.class);
verify(clientSpy, times(1)).search(any(SearchRequest.class), any());
verify(channel).onFailure(response.capture());
Exception value = response.getValue();
assertTrue(value instanceof IllegalArgumentException);
String errorMsg = String.format(Locale.ROOT, IndexAnomalyDetectorActionHandler.EXCEEDED_MAX_MULTI_ENTITY_DETECTORS_PREFIX_MSG, maxMultiEntityAnomalyDetectors);
assertTrue(value.getMessage().contains(errorMsg));
}
use of org.opensearch.ad.model.AnomalyDetector in project anomaly-detection by opensearch-project.
the class IndexAnomalyDetectorActionHandlerTests method testTextField.
@SuppressWarnings("unchecked")
public void testTextField() throws IOException {
String field = "a";
AnomalyDetector detector = TestHelpers.randomAnomalyDetectorUsingCategoryFields(detectorId, Arrays.asList(field));
SearchResponse detectorResponse = mock(SearchResponse.class);
int totalHits = 9;
when(detectorResponse.getHits()).thenReturn(TestHelpers.createSearchHits(totalHits));
// extend NodeClient since its execute method is final and mockito does not allow to mock final methods
// we can also use spy to overstep the final methods
NodeClient client = new NodeClient(Settings.EMPTY, threadPool) {
@Override
public <Request extends ActionRequest, Response extends ActionResponse> void doExecute(ActionType<Response> action, Request request, ActionListener<Response> listener) {
try {
if (action.equals(SearchAction.INSTANCE)) {
listener.onResponse((Response) detectorResponse);
} else {
// passes first get field mapping call where timestamp has to be of type date
// fails on second call where categorical field is checked to be type keyword or IP
// we need to put the test in the same package of GetFieldMappingsResponse since its constructor is package private
GetFieldMappingsResponse response = new GetFieldMappingsResponse(TestHelpers.createFieldMappings(detector.getIndices().get(0), field, "date"));
listener.onResponse((Response) response);
}
} catch (IOException e) {
e.printStackTrace();
}
}
};
handler = new IndexAnomalyDetectorActionHandler(clusterService, client, transportService, channel, anomalyDetectionIndices, detectorId, seqNo, primaryTerm, refreshPolicy, detector, requestTimeout, maxSingleEntityAnomalyDetectors, maxMultiEntityAnomalyDetectors, maxAnomalyFeatures, method, xContentRegistry(), null, adTaskManager, searchFeatureDao);
ArgumentCaptor<Exception> response = ArgumentCaptor.forClass(Exception.class);
handler.start();
verify(channel).onFailure(response.capture());
Exception value = response.getValue();
assertTrue(value instanceof Exception);
assertTrue(value.getMessage().contains(IndexAnomalyDetectorActionHandler.CATEGORICAL_FIELD_TYPE_ERR_MSG));
}
use of org.opensearch.ad.model.AnomalyDetector in project anomaly-detection by opensearch-project.
the class IndexAnomalyDetectorActionHandlerTests method testUpdateTemplate.
@SuppressWarnings("unchecked")
private void testUpdateTemplate(String fieldTypeName) throws IOException {
String field = "a";
AnomalyDetector detector = TestHelpers.randomAnomalyDetectorUsingCategoryFields(detectorId, Arrays.asList(field));
SearchResponse detectorResponse = mock(SearchResponse.class);
int totalHits = 9;
when(detectorResponse.getHits()).thenReturn(TestHelpers.createSearchHits(totalHits));
GetResponse getDetectorResponse = TestHelpers.createGetResponse(detector, detector.getDetectorId(), AnomalyDetector.ANOMALY_DETECTORS_INDEX);
SearchResponse userIndexResponse = mock(SearchResponse.class);
int userIndexHits = 0;
when(userIndexResponse.getHits()).thenReturn(TestHelpers.createSearchHits(userIndexHits));
// extend NodeClient since its execute method is final and mockito does not allow to mock final methods
// we can also use spy to overstep the final methods
NodeClient client = new NodeClient(Settings.EMPTY, threadPool) {
@Override
public <Request extends ActionRequest, Response extends ActionResponse> void doExecute(ActionType<Response> action, Request request, ActionListener<Response> listener) {
try {
if (action.equals(SearchAction.INSTANCE)) {
assertTrue(request instanceof SearchRequest);
SearchRequest searchRequest = (SearchRequest) request;
if (searchRequest.indices()[0].equals(ANOMALY_DETECTORS_INDEX)) {
listener.onResponse((Response) detectorResponse);
} else {
listener.onResponse((Response) userIndexResponse);
}
} else if (action.equals(GetAction.INSTANCE)) {
assertTrue(request instanceof GetRequest);
listener.onResponse((Response) getDetectorResponse);
} else {
GetFieldMappingsResponse response = new GetFieldMappingsResponse(TestHelpers.createFieldMappings(detector.getIndices().get(0), field, fieldTypeName));
listener.onResponse((Response) response);
}
} catch (IOException e) {
e.printStackTrace();
}
}
};
NodeClient clientSpy = spy(client);
ClusterName clusterName = new ClusterName("test");
ClusterState clusterState = ClusterState.builder(clusterName).metadata(Metadata.builder().build()).build();
when(clusterService.state()).thenReturn(clusterState);
handler = new IndexAnomalyDetectorActionHandler(clusterService, clientSpy, transportService, channel, anomalyDetectionIndices, detectorId, seqNo, primaryTerm, refreshPolicy, detector, requestTimeout, maxSingleEntityAnomalyDetectors, maxMultiEntityAnomalyDetectors, maxAnomalyFeatures, RestRequest.Method.PUT, xContentRegistry(), null, adTaskManager, searchFeatureDao);
ArgumentCaptor<Exception> response = ArgumentCaptor.forClass(Exception.class);
handler.start();
verify(clientSpy, times(1)).execute(eq(GetFieldMappingsAction.INSTANCE), any(), any());
verify(channel).onFailure(response.capture());
Exception value = response.getValue();
if (fieldTypeName.equals(CommonName.IP_TYPE) || fieldTypeName.equals(CommonName.KEYWORD_TYPE)) {
assertTrue(value.getMessage().contains(IndexAnomalyDetectorActionHandler.NO_DOCS_IN_USER_INDEX_MSG));
} else {
assertTrue(value.getMessage().contains(IndexAnomalyDetectorActionHandler.CATEGORICAL_FIELD_TYPE_ERR_MSG));
}
}
use of org.opensearch.ad.model.AnomalyDetector in project anomaly-detection by opensearch-project.
the class AnomalyDetectorRestApiIT method testCreateAnomalyDetectorWithNotExistingIndices.
public void testCreateAnomalyDetectorWithNotExistingIndices() throws Exception {
AnomalyDetector detector = TestHelpers.randomAnomalyDetector(TestHelpers.randomUiMetadata(), null);
TestHelpers.assertFailWith(ResponseException.class, "index_not_found_exception", () -> TestHelpers.makeRequest(client(), "POST", TestHelpers.AD_BASE_DETECTORS_URI, ImmutableMap.of(), TestHelpers.toHttpEntity(detector), null));
}
Aggregations