use of org.opensearch.ad.model.EntityAnomalyResult in project anomaly-detection by opensearch-project.
the class MultiResponsesDelegateActionListenerTests method testForceResponse.
@SuppressWarnings("unchecked")
public void testForceResponse() {
AnomalyResult anomalyResult1 = randomHCADAnomalyDetectResult(0.25, 0.25, "error");
AnomalyResult anomalyResult2 = randomHCADAnomalyDetectResult(0.5, 0.5, "error");
EntityAnomalyResult entityAnomalyResult1 = new EntityAnomalyResult(new ArrayList<AnomalyResult>() {
{
add(anomalyResult1);
}
});
EntityAnomalyResult entityAnomalyResult2 = new EntityAnomalyResult(new ArrayList<AnomalyResult>() {
{
add(anomalyResult2);
}
});
ActionListener<EntityAnomalyResult> actualListener = mock(ActionListener.class);
MultiResponsesDelegateActionListener<EntityAnomalyResult> multiListener = new MultiResponsesDelegateActionListener<EntityAnomalyResult>(actualListener, 3, "blah", true);
multiListener.onResponse(entityAnomalyResult1);
multiListener.onResponse(entityAnomalyResult2);
multiListener.onFailure(new RuntimeException());
entityAnomalyResult1.merge(entityAnomalyResult2);
verify(actualListener).onResponse(entityAnomalyResult1);
}
use of org.opensearch.ad.model.EntityAnomalyResult in project anomaly-detection by opensearch-project.
the class AnomalyDetectorRunner method executeDetector.
/**
* run anomaly detector and return anomaly result.
*
* @param detector anomaly detector instance
* @param startTime detection period start time
* @param endTime detection period end time
* @param context stored thread context
* @param listener handle anomaly result
* @throws IOException - if a user gives wrong query input when defining a detector
*/
public void executeDetector(AnomalyDetector detector, Instant startTime, Instant endTime, ThreadContext.StoredContext context, ActionListener<List<AnomalyResult>> listener) throws IOException {
context.restore();
List<String> categoryField = detector.getCategoryField();
if (categoryField != null && !categoryField.isEmpty()) {
featureManager.getPreviewEntities(detector, startTime.toEpochMilli(), endTime.toEpochMilli(), ActionListener.wrap(entities -> {
if (entities == null || entities.isEmpty()) {
// TODO return exception like IllegalArgumentException to explain data is not enough for preview
// This also requires front-end change to handle error message correspondingly
// We return empty list for now to avoid breaking front-end
listener.onResponse(Collections.emptyList());
return;
}
ActionListener<EntityAnomalyResult> entityAnomalyResultListener = ActionListener.wrap(entityAnomalyResult -> {
listener.onResponse(entityAnomalyResult.getAnomalyResults());
}, e -> onFailure(e, listener, detector.getDetectorId()));
MultiResponsesDelegateActionListener<EntityAnomalyResult> multiEntitiesResponseListener = new MultiResponsesDelegateActionListener<EntityAnomalyResult>(entityAnomalyResultListener, entities.size(), String.format(Locale.ROOT, "Fail to get preview result for multi entity detector %s", detector.getDetectorId()), true);
for (Entity entity : entities) {
featureManager.getPreviewFeaturesForEntity(detector, entity, startTime.toEpochMilli(), endTime.toEpochMilli(), ActionListener.wrap(features -> {
List<ThresholdingResult> entityResults = modelManager.getPreviewResults(features.getProcessedFeatures(), detector.getShingleSize());
List<AnomalyResult> sampledEntityResults = sample(parsePreviewResult(detector, features, entityResults, entity), maxPreviewResults);
multiEntitiesResponseListener.onResponse(new EntityAnomalyResult(sampledEntityResults));
}, e -> multiEntitiesResponseListener.onFailure(e)));
}
}, e -> onFailure(e, listener, detector.getDetectorId())));
} else {
featureManager.getPreviewFeatures(detector, startTime.toEpochMilli(), endTime.toEpochMilli(), ActionListener.wrap(features -> {
try {
List<ThresholdingResult> results = modelManager.getPreviewResults(features.getProcessedFeatures(), detector.getShingleSize());
listener.onResponse(sample(parsePreviewResult(detector, features, results, null), maxPreviewResults));
} catch (Exception e) {
onFailure(e, listener, detector.getDetectorId());
}
}, e -> onFailure(e, listener, detector.getDetectorId())));
}
}
Aggregations