use of org.opensearch.ad.model.AnomalyDetector in project anomaly-detection by opensearch-project.
the class EntityProfileRunner method prepareEntityProfile.
private void prepareEntityProfile(ActionListener<EntityProfile> listener, String detectorId, Entity entityValue, Set<EntityProfileName> profilesToCollect, AnomalyDetector detector, String categoryField) {
EntityProfileRequest request = new EntityProfileRequest(detectorId, entityValue, profilesToCollect);
client.execute(EntityProfileAction.INSTANCE, request, ActionListener.wrap(r -> getJob(detectorId, entityValue, profilesToCollect, detector, r, listener), listener::onFailure));
}
use of org.opensearch.ad.model.AnomalyDetector in project anomaly-detection by opensearch-project.
the class NodeStateManager method onGetDetectorResponse.
private ActionListener<GetResponse> onGetDetectorResponse(String adID, ActionListener<Optional<AnomalyDetector>> listener) {
return ActionListener.wrap(response -> {
if (response == null || !response.isExists()) {
listener.onResponse(Optional.empty());
return;
}
String xc = response.getSourceAsString();
LOG.debug("Fetched anomaly detector: {}", xc);
try (XContentParser parser = XContentType.JSON.xContent().createParser(xContentRegistry, LoggingDeprecationHandler.INSTANCE, xc)) {
ensureExpectedToken(XContentParser.Token.START_OBJECT, parser.nextToken(), parser);
AnomalyDetector detector = AnomalyDetector.parse(parser, response.getId());
// end execution if all features are disabled
if (detector.getEnabledFeatureIds().isEmpty()) {
listener.onFailure(new EndRunException(adID, CommonErrorMessages.ALL_FEATURES_DISABLED_ERR_MSG, true).countedInStats(false));
return;
}
NodeState state = states.computeIfAbsent(adID, id -> new NodeState(id, clock));
state.setDetectorDef(detector);
listener.onResponse(Optional.of(detector));
} catch (Exception t) {
LOG.error("Fail to parse detector {}", adID);
LOG.error("Stack trace:", t);
listener.onResponse(Optional.empty());
}
}, listener::onFailure);
}
use of org.opensearch.ad.model.AnomalyDetector in project anomaly-detection by opensearch-project.
the class ADTaskManagerTests method testStartHistoricalAnalysisWithNoOwningNode.
@SuppressWarnings("unchecked")
public void testStartHistoricalAnalysisWithNoOwningNode() throws IOException {
AnomalyDetector detector = TestHelpers.randomAnomalyDetector(ImmutableList.of());
DetectionDateRange detectionDateRange = TestHelpers.randomDetectionDateRange();
User user = null;
int availableTaskSlots = randomIntBetween(1, 10);
ActionListener<AnomalyDetectorJobResponse> listener = mock(ActionListener.class);
doAnswer(invocation -> {
Consumer<Optional<DiscoveryNode>> function = invocation.getArgument(1);
function.accept(Optional.empty());
return null;
}).when(hashRing).buildAndGetOwningNodeWithSameLocalAdVersion(anyString(), any(), any());
adTaskManager.startHistoricalAnalysis(detector, detectionDateRange, user, availableTaskSlots, transportService, listener);
verify(listener, times(1)).onFailure(any());
}
use of org.opensearch.ad.model.AnomalyDetector in project anomaly-detection by opensearch-project.
the class ADTaskManagerTests method testStopDetectorWithNonExistingTask.
@SuppressWarnings("unchecked")
public void testStopDetectorWithNonExistingTask() {
String detectorId = randomAlphaOfLength(5);
boolean historical = true;
ActionListener<AnomalyDetectorJobResponse> listener = mock(ActionListener.class);
doAnswer(invocation -> {
Consumer<Optional<AnomalyDetector>> function = invocation.getArgument(1);
AnomalyDetector detector = randomAnomalyDetector(ImmutableList.of(randomFeature(true)));
function.accept(Optional.of(detector));
return null;
}).when(adTaskManager).getDetector(anyString(), any(), any());
doAnswer(invocation -> {
ActionListener<SearchResponse> actionListener = invocation.getArgument(1);
actionListener.onResponse(null);
return null;
}).when(client).search(any(), any());
adTaskManager.stopDetector(detectorId, historical, indexAnomalyDetectorJobActionHandler, null, transportService, listener);
verify(listener, times(1)).onFailure(any());
}
use of org.opensearch.ad.model.AnomalyDetector in project anomaly-detection by opensearch-project.
the class ADTaskManagerTests method testStartDetectorWithNoEnabledFeature.
public void testStartDetectorWithNoEnabledFeature() throws IOException {
AnomalyDetector detector = randomDetector(ImmutableList.of(randomFeature(false)), randomAlphaOfLength(5), 1, randomAlphaOfLength(5));
setupGetDetector(detector);
adTaskManager.startDetector(detector.getDetectorId(), detectionDateRange, indexAnomalyDetectorJobActionHandler, randomUser(), transportService, context, listener);
verify(listener, times(1)).onFailure(exceptionCaptor.capture());
}
Aggregations