jihaozh commented on a change in pull request #3773: [TE] Queries for fetching 
detection & notification owner report
URL: https://github.com/apache/incubator-pinot/pull/3773#discussion_r252795204
 
 

 ##########
 File path: 
thirdeye/thirdeye-pinot/src/test/java/org/apache/pinot/thirdeye/tools/RunAdhocDatabaseQueriesTool.java
 ##########
 @@ -433,15 +454,138 @@ private void disableAllActiveFunction(Collection<Long> 
exception){
     }
   }
 
+  /**
+   * Generates a report of the status and owner of all the un-subscribed 
anomaly functions
+   */
+  private void unsubscribedDetections(){
+    List<ApplicationDTO> apps = applicationDAO.findAll();
+
+    List<AnomalyFunctionDTO> allAnomalyFuncs = anomalyFunctionDAO.findAll();
+    LOG.info("Total number of funcs: " + allAnomalyFuncs.size());
+
+    Set<Long> allAnomalyFuncsIds = new HashSet<>();
+    Set<Long> subscribedFuncsIds = new HashSet<>();
+    for (AnomalyFunctionDTO anom : allAnomalyFuncs) {
+      allAnomalyFuncsIds.add(anom.getId());
+    }
+
+    List<AnomalyFunctionDTO> subscribedFuncs = new ArrayList<>();
+    for (ApplicationDTO app : apps) {
+      List<AlertConfigDTO> alertConfigDTOS = 
alertConfigDAO.findByPredicate(Predicate.EQ("application", 
app.getApplication()));
+      if (alertConfigDTOS != null) {
+        for (AlertConfigDTO alertDTO : alertConfigDTOS) {
+          if (alertDTO.getEmailConfig() != null) {
+            if (alertDTO.getEmailConfig().getFunctionIds() != null) {
+              for (long id : alertDTO.getEmailConfig().getFunctionIds()) {
+                AnomalyFunctionDTO func = anomalyFunctionDAO.findById(id);
+                if (func != null) {
+                  subscribedFuncs.add(func);
+                }
+              }
+            }
+          }
+        }
+      }
+    }
+
+    for (AnomalyFunctionDTO anom : subscribedFuncs) {
+      subscribedFuncsIds.add(anom.getId());
+    }
+
+    allAnomalyFuncsIds.removeAll(subscribedFuncsIds);
+
+    for (long id : allAnomalyFuncsIds) {
+      AnomalyFunctionDTO func = anomalyFunctionDAO.findById(id);
+      System.out.println(String.format("%s\t%s\t%s\t%s\t%s", func.getId(), 
func.getFunctionName(), func.getIsActive(), func.getCreatedBy(), 
func.getUpdatedBy()));
+
+    }
+  }
+
+  /**
+   * Generates a report of the status, owner and recipients of all the 
subscription groups by application
+   */
+  private void notificationOwners(){
+    List<ApplicationDTO> apps = applicationDAO.findAll();
+    for (ApplicationDTO app : apps) {
+      List<AlertConfigDTO> alertConfigDTOS = 
alertConfigDAO.findByPredicate(Predicate.EQ("application", 
app.getApplication()));
+      for (AlertConfigDTO alertDTO : alertConfigDTOS) {
+        String recipients = fetchRecipients(alertDTO.getReceiverAddresses());
+        System.out.println(String.format("%s\t%s\t%s\t%s\t%s", 
app.getApplication(), alertDTO.getName(), alertDTO.isActive(), 
alertDTO.getCreatedBy(), recipients));
+      }
+    }
+  }
+
+  /**
+   * Generates a report of the status, owner and recipients of all the 
subscribed anomaly functions by application
+   */
+  private void detectionOwners(){
+    List<ApplicationDTO> apps = applicationDAO.findAll();
+
+    for (ApplicationDTO app : apps) {
+      List<AlertConfigDTO> alertConfigDTOS = 
alertConfigDAO.findByPredicate(Predicate.EQ("application", 
app.getApplication()));
+      if (alertConfigDTOS != null) {
+        for (AlertConfigDTO alertDTO : alertConfigDTOS) {
+          if (alertDTO.getEmailConfig() != null) {
+            if (alertDTO.getEmailConfig().getFunctionIds() != null) {
+              for (long id : alertDTO.getEmailConfig().getFunctionIds()) {
+                AnomalyFunctionDTO func = anomalyFunctionDAO.findById(id);
+                if (func != null) {
+                  String recipients = 
fetchRecipients(alertDTO.getReceiverAddresses());
+                  System.out.println(String.format("%s\t%s\t%s\t%s\t%s\t%s", 
app.getApplication(), func.getId(), func.getFunctionName(),
+                      func.getIsActive(), func.getCreatedBy(), 
String.join(",", recipients)));
+                }
+              }
+            }
+          }
+        }
+      }
+    }
+  }
+
+  private String fetchRecipients(DetectionAlertFilterRecipients receivers) {
+    String recipients = String.join(", ", receivers.getTo()).trim();
+    while (recipients.startsWith(",")) {
+      recipients = recipients.substring(1, recipients.length());
+    }
+    while (recipients.endsWith(",")) {
+      recipients = recipients.substring(0, recipients.length() - 1);
+    }
+    return recipients;
+  }
+
+  private void cleanup(){
+    List<DetectionConfigDTO> detectionConfigDTOS = 
detectionConfigDAO.findAll();
+
+    for (DetectionConfigDTO detFunction : detectionConfigDTOS) {
+      // Delete all the anomalies generated by the functions
+      List<AnomalyFunctionDTO> anomalies = 
anomalyFunctionDAO.findByPredicate(Predicate.EQ("baseId", detFunction.getId()));
+      for (AnomalyFunctionDTO anomaly : anomalies) {
+        anomalyFunctionDAO.delete(anomaly);
+      }
+      // Delete all the functions
+      detectionConfigDAO.delete(detFunction);
+    }
+
+    List<DetectionAlertConfigDTO> detectionAlertConfigDTOS = 
detectionAlertConfigDAO.findAll();
+    for (DetectionAlertConfigDTO alert : detectionAlertConfigDTOS) {
+      if (alert.getId() == 89049435 || alert.getId() == 89235978) {
+        continue;
+      }
+
+      detectionAlertConfigDAO.delete(alert);
+    }
+  }
+
   public static void main(String[] args) throws Exception {
 
-    File persistenceFile = new File(args[0]);
+    File persistenceFile = new File("/home/akrai/persistence.yml");
 
 Review comment:
   might want to change this path?

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


With regards,
Apache Git Services

---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]

Reply via email to