You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@pinot.apache.org by GitBox <gi...@apache.org> on 2019/06/25 18:31:08 UTC

[GitHub] [incubator-pinot] jihaozh commented on a change in pull request #4354: [TE] Trigger expression based grouping of anomalies - AND, OR and combinations

jihaozh commented on a change in pull request #4354: [TE] Trigger expression based grouping of anomalies - AND, OR and combinations
URL: https://github.com/apache/incubator-pinot/pull/4354#discussion_r297330539
 
 

 ##########
 File path: thirdeye/thirdeye-pinot/src/main/java/org/apache/pinot/thirdeye/detection/components/TriggerConditionGrouper.java
 ##########
 @@ -0,0 +1,205 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements.  See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership.  The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License.  You may obtain a copy of the License at
+ *
+ *   http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied.  See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+package org.apache.pinot.thirdeye.detection.components;
+
+import com.google.common.base.Preconditions;
+import java.util.ArrayList;
+import java.util.HashMap;
+import java.util.HashSet;
+import java.util.List;
+import java.util.Map;
+import java.util.Set;
+import org.apache.commons.collections4.MapUtils;
+import org.apache.pinot.thirdeye.datalayer.dto.MergedAnomalyResultDTO;
+import org.apache.pinot.thirdeye.detection.ConfigUtils;
+import org.apache.pinot.thirdeye.detection.InputDataFetcher;
+import org.apache.pinot.thirdeye.detection.annotation.Components;
+import org.apache.pinot.thirdeye.detection.annotation.DetectionTag;
+import org.apache.pinot.thirdeye.detection.spec.TriggerConditionGrouperSpec;
+import org.apache.pinot.thirdeye.detection.spi.components.Grouper;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+
+import static org.apache.pinot.thirdeye.detection.DetectionUtils.*;
+
+
+/**
+ * Expression based grouper - supports AND, OR and nested combinations of grouping
+ */
+@Components(title = "TriggerCondition", type = "GROUPER",
+    tags = {DetectionTag.GROUPER}, description = "An expression based grouper")
+public class TriggerConditionGrouper implements Grouper<TriggerConditionGrouperSpec> {
+  protected static final Logger LOG = LoggerFactory.getLogger(TriggerConditionGrouper.class);
+
+  private String expression;
+  private String operator;
+  private Map<String, Object> leftOp;
+  private Map<String, Object> rightOp;
+  private InputDataFetcher dataFetcher;
+
+  static final String PROP_DETECTOR_COMPONENT_NAME = "detectorComponentName";
+  static final String PROP_AND = "and";
+  static final String PROP_OR = "or";
+  static final String PROP_OPERATOR = "operator";
+  static final String PROP_LEFT_OP = "leftOp";
+  static final String PROP_RIGHT_OP = "rightOp";
+
+  /**
+   * Group based on 'AND' criteria - Entity has anomaly if both sub-entities A and B have anomalies
+   * at the same time. This means we find anomaly overlapping interval.
+   *
+   * Since the anomalies from the respective entities/metrics are merged
+   * before calling the grouper, we do not have to deal with overlapping
+   * anomalies within an entity/metric
+   *
+   * Sort anomalies and incrementally compare two anomalies for overlap criteria; break when no overlap
+   */
+  private List<MergedAnomalyResultDTO> andGrouping(
+      List<MergedAnomalyResultDTO> anomalyListA, List<MergedAnomalyResultDTO> anomalyListB) {
+    Set<MergedAnomalyResultDTO> groupedAnomalies = new HashSet<>();
+    List<MergedAnomalyResultDTO> anomalies = mergeAndSortAnomalies(anomalyListA, anomalyListB);
+    if (anomalies.isEmpty()) {
+      return anomalies;
+    }
+
+    for (int i = 0; i < anomalies.size(); i++) {
+      for (int j = i + 1; j < anomalies.size(); j++) {
+        // Check for overlap and output it
+        if (anomalies.get(j).getStartTime() <= anomalies.get(i).getEndTime()) {
+          MergedAnomalyResultDTO currentAnomaly = makeParentEntityAnomaly(anomalies.get(i));
+          currentAnomaly.setEndTime(Math.min(currentAnomaly.getEndTime(), anomalies.get(j). getEndTime()));
+          currentAnomaly.setStartTime(anomalies.get(j). getStartTime());
+          setEntityChildMapping(currentAnomaly, anomalies.get(j));
+
+          groupedAnomalies.add(currentAnomaly);
+        } else {
+          break;
+        }
+      }
+    }
+
+    return new ArrayList<>(groupedAnomalies);
+  }
+
+  /**
+   * Group based on 'OR' criteria - Entity has anomaly if either sub-entity A or B have anomalies.
+   * This means we find the total anomaly coverage.
+   *
+   * Since the anomalies from the respective entities/metrics are merged
+   * before calling the grouper, we do not have to deal with overlapping
+   * anomalies within an entity/metric
+   *
+   * Sort anomalies by start time and incrementally merge anomalies
+   */
+  private List<MergedAnomalyResultDTO> orGrouping(
+      List<MergedAnomalyResultDTO> anomalyListA, List<MergedAnomalyResultDTO> anomalyListB) {
+    Set<MergedAnomalyResultDTO> groupedAnomalies = new HashSet<>();
+    List<MergedAnomalyResultDTO> anomalies = mergeAndSortAnomalies(anomalyListA, anomalyListB);
+    if (anomalies.isEmpty()) {
+      return anomalies;
+    }
+
+    MergedAnomalyResultDTO currentAnomaly = makeParentEntityAnomaly(anomalies.get(0));
+    for (int i = 1; i < anomalies.size();  i++) {
+      if (anomalies.get(i).getStartTime() <= currentAnomaly.getEndTime()) {
+        // Partial or full overlap
+        currentAnomaly.setEndTime(Math.max(anomalies.get(i).getEndTime(), currentAnomaly.getEndTime()));
+        setEntityChildMapping(currentAnomaly, anomalies.get(i));
+      }  else {
+        // No overlap
+        groupedAnomalies.add(currentAnomaly);
+        currentAnomaly = makeParentEntityAnomaly(anomalies.get(i));
+      }
+    }
+    groupedAnomalies.add(currentAnomaly);
+
+    return new ArrayList<>(groupedAnomalies);
+  }
+
+  /**
+   * Groups the anomalies based on the parsed operator tree
+   */
+  private List<MergedAnomalyResultDTO> groupAnomaliesByOperator(Map<String, Object> operatorNode, List<MergedAnomalyResultDTO> anomalies) {
+    Preconditions.checkNotNull(operatorNode);
+
+    // Base condition - If reached leaf node, then return the anomalies corresponding to the entity/metric
+    String value = MapUtils.getString(operatorNode, "value");
+    if (value != null) {
+      List<MergedAnomalyResultDTO> filteredAnomalies = new ArrayList<>();
 
 Review comment:
   consider using the filter in the steam API.
   ```
   filteredAnomalies = anomalies.steam().filter(...).collect(Collector.toList());
   ```

----------------------------------------------------------------
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
 
For queries about this service, please contact Infrastructure at:
users@infra.apache.org


With regards,
Apache Git Services

---------------------------------------------------------------------
To unsubscribe, e-mail: commits-unsubscribe@pinot.apache.org
For additional commands, e-mail: commits-help@pinot.apache.org