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/27 22:50:39 UTC

[GitHub] [incubator-pinot] jihaozh commented on a change in pull request #4371: [TE] Support Anomaly Hierarchy for Composite alerts

jihaozh commented on a change in pull request #4371: [TE] Support Anomaly Hierarchy for Composite alerts
URL: https://github.com/apache/incubator-pinot/pull/4371#discussion_r298399227
 
 

 ##########
 File path: thirdeye/thirdeye-pinot/src/main/java/org/apache/pinot/thirdeye/datalayer/bao/jdbc/MergedAnomalyResultManagerImpl.java
 ##########
 @@ -98,24 +99,82 @@ public Long save(MergedAnomalyResultDTO mergedAnomalyResultDTO) {
       update(mergedAnomalyResultDTO);
       return mergedAnomalyResultDTO.getId();
     }
+    return saveAnomaly(mergedAnomalyResultDTO, new HashSet<>());
+  }
+
+  public int update(MergedAnomalyResultDTO mergedAnomalyResultDTO) {
+    if (mergedAnomalyResultDTO.getId() == null) {
+      Long id = save(mergedAnomalyResultDTO);
+      if (id > 0) {
+        return 1;
+      } else {
+        return 0;
+      }
+    } else {
+      return updateAnomaly(mergedAnomalyResultDTO, new HashSet<>());
+    }
+  }
+
+  private Long saveAnomaly(MergedAnomalyResultDTO mergedAnomalyResultDTO, Set<MergedAnomalyResultDTO> visitedAnomalies) {
+    Preconditions.checkNotNull(mergedAnomalyResultDTO);
+    Preconditions.checkNotNull(visitedAnomalies);
+
+    visitedAnomalies.add(mergedAnomalyResultDTO);
+
+    if (mergedAnomalyResultDTO.getId() != null) {
+      updateAnomaly(mergedAnomalyResultDTO, visitedAnomalies);
+      return mergedAnomalyResultDTO.getId();
+    }
+
     MergedAnomalyResultBean mergeAnomalyBean = convertMergeAnomalyDTO2Bean(mergedAnomalyResultDTO);
+    Set<Long> childAnomalyIds = saveChildAnomalies(mergedAnomalyResultDTO, visitedAnomalies);
+    mergeAnomalyBean.setChildIds(childAnomalyIds);
+
     Long id = genericPojoDao.put(mergeAnomalyBean);
     mergedAnomalyResultDTO.setId(id);
     return id;
   }
 
-  public int update(MergedAnomalyResultDTO mergedAnomalyResultDTO) {
+  private int updateAnomaly(MergedAnomalyResultDTO mergedAnomalyResultDTO, Set<MergedAnomalyResultDTO> visitedAnomalies) {
+    visitedAnomalies.add(mergedAnomalyResultDTO);
+
     if (mergedAnomalyResultDTO.getId() == null) {
-      Long id = save(mergedAnomalyResultDTO);
+      Long id = saveAnomaly(mergedAnomalyResultDTO, visitedAnomalies);
       if (id > 0) {
         return 1;
       } else {
         return 0;
       }
-    } else {
-      MergedAnomalyResultBean mergeAnomalyBean = convertMergeAnomalyDTO2Bean(mergedAnomalyResultDTO);
-      return genericPojoDao.update(mergeAnomalyBean);
     }
+
+    MergedAnomalyResultBean mergeAnomalyBean = convertMergeAnomalyDTO2Bean(mergedAnomalyResultDTO);
+    Set<Long> childAnomalyIds = saveChildAnomalies(mergedAnomalyResultDTO, visitedAnomalies);
+    mergeAnomalyBean.setChildIds(childAnomalyIds);
+
+    return genericPojoDao.update(mergeAnomalyBean);
+  }
+
+  private Set<Long> saveChildAnomalies(MergedAnomalyResultDTO parentAnomaly,
+      Set<MergedAnomalyResultDTO> visitedAnomalies) {
+    Set<Long> childIds = new HashSet<>();
+    Set<MergedAnomalyResultDTO> childAnomalies = parentAnomaly.getChildren();
+    if (childAnomalies == null || childAnomalies.isEmpty()) {
+      // No child anomalies to save
+      return null;
 
 Review comment:
   How about return an empty set instead of null to prevent NullPointerException. 

----------------------------------------------------------------
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