Search in sources :

Example 11 with ThresholdingResult

use of org.opensearch.ad.ml.ThresholdingResult in project anomaly-detection by opensearch-project.

the class AnomalyDetectorRunner method parsePreviewResult.

private List<AnomalyResult> parsePreviewResult(AnomalyDetector detector, Features features, List<ThresholdingResult> results, Entity entity) {
    // unprocessedFeatures[][], each row is for one date range.
    // For example, unprocessedFeatures[0][2] is for the first time range, the third feature
    double[][] unprocessedFeatures = features.getUnprocessedFeatures();
    List<Map.Entry<Long, Long>> timeRanges = features.getTimeRanges();
    List<Feature> featureAttributes = detector.getFeatureAttributes().stream().filter(Feature::getEnabled).collect(Collectors.toList());
    List<AnomalyResult> anomalyResults = new ArrayList<>();
    if (timeRanges != null && timeRanges.size() > 0) {
        for (int i = 0; i < timeRanges.size(); i++) {
            Map.Entry<Long, Long> timeRange = timeRanges.get(i);
            List<FeatureData> featureDatas = new ArrayList<>();
            int featureSize = featureAttributes.size();
            for (int j = 0; j < featureSize; j++) {
                double value = unprocessedFeatures[i][j];
                Feature feature = featureAttributes.get(j);
                FeatureData data = new FeatureData(feature.getId(), feature.getName(), value);
                featureDatas.add(data);
            }
            AnomalyResult result;
            if (results != null && results.size() > i) {
                ThresholdingResult thresholdingResult = results.get(i);
                result = thresholdingResult.toAnomalyResult(detector, Instant.ofEpochMilli(timeRange.getKey()), Instant.ofEpochMilli(timeRange.getValue()), null, null, featureDatas, entity, CommonValue.NO_SCHEMA_VERSION, null, null, null);
            } else {
                result = new AnomalyResult(detector.getDetectorId(), null, featureDatas, Instant.ofEpochMilli(timeRange.getKey()), Instant.ofEpochMilli(timeRange.getValue()), null, null, null, entity, detector.getUser(), CommonValue.NO_SCHEMA_VERSION, null);
            }
            anomalyResults.add(result);
        }
    }
    return anomalyResults;
}
Also used : ArrayList(java.util.ArrayList) FeatureData(org.opensearch.ad.model.FeatureData) Feature(org.opensearch.ad.model.Feature) ThresholdingResult(org.opensearch.ad.ml.ThresholdingResult) AnomalyResult(org.opensearch.ad.model.AnomalyResult) EntityAnomalyResult(org.opensearch.ad.model.EntityAnomalyResult) Map(java.util.Map)

Example 12 with ThresholdingResult

use of org.opensearch.ad.ml.ThresholdingResult 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())));
    }
}
Also used : ModelManager(org.opensearch.ad.ml.ModelManager) ThresholdingResult(org.opensearch.ad.ml.ThresholdingResult) FeatureData(org.opensearch.ad.model.FeatureData) ThreadContext(org.opensearch.common.util.concurrent.ThreadContext) ArrayList(java.util.ArrayList) Locale(java.util.Locale) Map(java.util.Map) AnomalyDetector(org.opensearch.ad.model.AnomalyDetector) ActionListener(org.opensearch.action.ActionListener) CommonValue(org.opensearch.ad.constant.CommonValue) FeatureManager(org.opensearch.ad.feature.FeatureManager) Feature(org.opensearch.ad.model.Feature) IOException(java.io.IOException) Instant(java.time.Instant) Collectors(java.util.stream.Collectors) OpenSearchSecurityException(org.opensearch.OpenSearchSecurityException) Features(org.opensearch.ad.feature.Features) MultiResponsesDelegateActionListener(org.opensearch.ad.util.MultiResponsesDelegateActionListener) List(java.util.List) Logger(org.apache.logging.log4j.Logger) AnomalyResult(org.opensearch.ad.model.AnomalyResult) Entity(org.opensearch.ad.model.Entity) EntityAnomalyResult(org.opensearch.ad.model.EntityAnomalyResult) Collections(java.util.Collections) LogManager(org.apache.logging.log4j.LogManager) Entity(org.opensearch.ad.model.Entity) MultiResponsesDelegateActionListener(org.opensearch.ad.util.MultiResponsesDelegateActionListener) IOException(java.io.IOException) OpenSearchSecurityException(org.opensearch.OpenSearchSecurityException) ThresholdingResult(org.opensearch.ad.ml.ThresholdingResult) ActionListener(org.opensearch.action.ActionListener) MultiResponsesDelegateActionListener(org.opensearch.ad.util.MultiResponsesDelegateActionListener) AnomalyResult(org.opensearch.ad.model.AnomalyResult) EntityAnomalyResult(org.opensearch.ad.model.EntityAnomalyResult) ArrayList(java.util.ArrayList) List(java.util.List) EntityAnomalyResult(org.opensearch.ad.model.EntityAnomalyResult)

Aggregations

ThresholdingResult (org.opensearch.ad.ml.ThresholdingResult)12 ModelManager (org.opensearch.ad.ml.ModelManager)7 ActionListener (org.opensearch.action.ActionListener)6 ArrayList (java.util.ArrayList)5 ActionFilters (org.opensearch.action.support.ActionFilters)5 AnomalyDetector (org.opensearch.ad.model.AnomalyDetector)5 PlainActionFuture (org.opensearch.action.support.PlainActionFuture)4 AnomalyResult (org.opensearch.ad.model.AnomalyResult)4 TransportService (org.opensearch.transport.TransportService)4 Instant (java.time.Instant)3 Map (java.util.Map)3 ADCircuitBreakerService (org.opensearch.ad.breaker.ADCircuitBreakerService)3 Task (org.opensearch.tasks.Task)3 Transport (org.opensearch.transport.Transport)3 HashMap (java.util.HashMap)2 List (java.util.List)2 Optional (java.util.Optional)2 ParameterizedMessage (org.apache.logging.log4j.message.ParameterizedMessage)2 Matchers.containsString (org.hamcrest.Matchers.containsString)2 ArgumentMatchers.anyString (org.mockito.ArgumentMatchers.anyString)2