@Test public void testSearch() { AnomalySearcher anomalySearcher = new AnomalySearcher(); Map<String, Object> result = anomalySearcher.search(new AnomalySearchFilter(1L, 3L), 10, 0); Assert.assertEquals(result.get("count"), 2L); Assert.assertEquals(result.get("limit"), 10); Assert.assertEquals(result.get("offset"), 0); List<MergedAnomalyResultDTO> anomalies = ConfigUtils.getList(result.get("elements")); Assert.assertEquals(anomalies.size(), 2); Assert.assertEquals(anomalies.get(0), anomalyDAO.findById(1L)); Assert.assertEquals(anomalies.get(1), anomalyDAO.findById(2L)); }
public Map<String, Object> search(AnomalySearchFilter searchFilter, int limit, int offset) { Predicate predicate = Predicate.NEQ("baseId", 0); if (searchFilter.getAnomalyIds().isEmpty()) { predicate = Predicate.EQ("child", false); } if (searchFilter.getStartTime() != null) { predicate = Predicate.AND(predicate, Predicate.LT("startTime", searchFilter.getEndTime())); } if (searchFilter.getEndTime() != null) { predicate = Predicate.AND(predicate, Predicate.GT("endTime", searchFilter.getStartTime())); } Set<Long> detectionConfigIds = new HashSet<>(); Set<Long> subscribedDetectionConfigIds = new HashSet<>(); if (!searchFilter.getDetectionNames().isEmpty()) { detectionConfigIds = this.detectionConfigDAO.findByPredicate(Predicate.IN("name", searchFilter.getDetectionNames().toArray())) .stream() .map(DetectionConfigBean::getId) .collect(Collectors.toSet()); } if (!searchFilter.getSubscriptionGroups().isEmpty()) { subscribedDetectionConfigIds = this.detectionAlertConfigDAO.findByPredicate( Predicate.IN("name", searchFilter.getSubscriptionGroups().toArray())) .stream() .map(detectionAlertConfigDTO -> detectionAlertConfigDTO.getVectorClocks().keySet()) .flatMap(Collection::stream) .collect(Collectors.toSet()); } if (!searchFilter.getDetectionNames().isEmpty() && !searchFilter.getSubscriptionGroups().isEmpty()) { detectionConfigIds.retainAll(subscribedDetectionConfigIds); } else { detectionConfigIds.addAll(subscribedDetectionConfigIds); } if (!searchFilter.getDetectionNames().isEmpty() || !searchFilter.getSubscriptionGroups().isEmpty()) { if (detectionConfigIds.isEmpty()) { return ImmutableMap.of("count", 0, "limit", limit, "offset", offset, "elements", Collections.emptyList()); } predicate = Predicate.AND(predicate, Predicate.IN("detectionConfigId", detectionConfigIds.toArray())); } if (!searchFilter.getDatasets().isEmpty()) { List<Predicate> datasetPredicates = new ArrayList<>(); for (String dataset : searchFilter.getDatasets()) { datasetPredicates.add(Predicate.LIKE("collection", "%" + dataset + "%")); } predicate = Predicate.AND(predicate, Predicate.OR(datasetPredicates.toArray(new Predicate[0]))); } if (!searchFilter.getMetrics().isEmpty()) { predicate = Predicate.AND(predicate, Predicate.IN("metric", searchFilter.getMetrics().toArray())); } if (!searchFilter.getAnomalyIds().isEmpty()) { predicate = Predicate.AND(predicate, Predicate.IN("baseId", searchFilter.getAnomalyIds().toArray())); } long count; List<MergedAnomalyResultDTO> results; if (searchFilter.getFeedbacks().isEmpty()) { List<Long> anomalyIds = this.anomalyDAO.findIdsByPredicate(predicate) .stream() .sorted(Comparator.reverseOrder()) .collect(Collectors.toList()); count = anomalyIds.size(); results = anomalyIds.isEmpty() ? Collections.emptyList() : this.anomalyDAO.findByIds(ResourceUtils.paginateResults(anomalyIds, offset, limit)); } else { List<MergedAnomalyResultDTO> anomalies = this.anomalyDAO.findByPredicate(predicate); Set<AnomalyFeedbackType> feedbackFilters = searchFilter.getFeedbacks().stream().map(AnomalyFeedbackType::valueOf).collect(Collectors.toSet()); results = anomalies.stream() .filter(anomaly -> (anomaly.getFeedback() == null && feedbackFilters.contains(NO_FEEDBACK)) || ( anomaly.getFeedback() != null && feedbackFilters.contains(anomaly.getFeedback().getFeedbackType()))) .sorted(Comparator.comparingLong(AbstractDTO::getId).reversed()) .collect(Collectors.toList()); count = results.size(); results = ResourceUtils.paginateResults(results, offset, limit); } return ImmutableMap.of("count", count, "limit", limit, "offset", offset, "elements", results); }
AnomalySearcher { public Map<String, Object> search(AnomalySearchFilter searchFilter, int limit, int offset) { Predicate predicate = Predicate.NEQ("baseId", 0); if (searchFilter.getAnomalyIds().isEmpty()) { predicate = Predicate.EQ("child", false); } if (searchFilter.getStartTime() != null) { predicate = Predicate.AND(predicate, Predicate.LT("startTime", searchFilter.getEndTime())); } if (searchFilter.getEndTime() != null) { predicate = Predicate.AND(predicate, Predicate.GT("endTime", searchFilter.getStartTime())); } Set<Long> detectionConfigIds = new HashSet<>(); Set<Long> subscribedDetectionConfigIds = new HashSet<>(); if (!searchFilter.getDetectionNames().isEmpty()) { detectionConfigIds = this.detectionConfigDAO.findByPredicate(Predicate.IN("name", searchFilter.getDetectionNames().toArray())) .stream() .map(DetectionConfigBean::getId) .collect(Collectors.toSet()); } if (!searchFilter.getSubscriptionGroups().isEmpty()) { subscribedDetectionConfigIds = this.detectionAlertConfigDAO.findByPredicate( Predicate.IN("name", searchFilter.getSubscriptionGroups().toArray())) .stream() .map(detectionAlertConfigDTO -> detectionAlertConfigDTO.getVectorClocks().keySet()) .flatMap(Collection::stream) .collect(Collectors.toSet()); } if (!searchFilter.getDetectionNames().isEmpty() && !searchFilter.getSubscriptionGroups().isEmpty()) { detectionConfigIds.retainAll(subscribedDetectionConfigIds); } else { detectionConfigIds.addAll(subscribedDetectionConfigIds); } if (!searchFilter.getDetectionNames().isEmpty() || !searchFilter.getSubscriptionGroups().isEmpty()) { if (detectionConfigIds.isEmpty()) { return ImmutableMap.of("count", 0, "limit", limit, "offset", offset, "elements", Collections.emptyList()); } predicate = Predicate.AND(predicate, Predicate.IN("detectionConfigId", detectionConfigIds.toArray())); } if (!searchFilter.getDatasets().isEmpty()) { List<Predicate> datasetPredicates = new ArrayList<>(); for (String dataset : searchFilter.getDatasets()) { datasetPredicates.add(Predicate.LIKE("collection", "%" + dataset + "%")); } predicate = Predicate.AND(predicate, Predicate.OR(datasetPredicates.toArray(new Predicate[0]))); } if (!searchFilter.getMetrics().isEmpty()) { predicate = Predicate.AND(predicate, Predicate.IN("metric", searchFilter.getMetrics().toArray())); } if (!searchFilter.getAnomalyIds().isEmpty()) { predicate = Predicate.AND(predicate, Predicate.IN("baseId", searchFilter.getAnomalyIds().toArray())); } long count; List<MergedAnomalyResultDTO> results; if (searchFilter.getFeedbacks().isEmpty()) { List<Long> anomalyIds = this.anomalyDAO.findIdsByPredicate(predicate) .stream() .sorted(Comparator.reverseOrder()) .collect(Collectors.toList()); count = anomalyIds.size(); results = anomalyIds.isEmpty() ? Collections.emptyList() : this.anomalyDAO.findByIds(ResourceUtils.paginateResults(anomalyIds, offset, limit)); } else { List<MergedAnomalyResultDTO> anomalies = this.anomalyDAO.findByPredicate(predicate); Set<AnomalyFeedbackType> feedbackFilters = searchFilter.getFeedbacks().stream().map(AnomalyFeedbackType::valueOf).collect(Collectors.toSet()); results = anomalies.stream() .filter(anomaly -> (anomaly.getFeedback() == null && feedbackFilters.contains(NO_FEEDBACK)) || ( anomaly.getFeedback() != null && feedbackFilters.contains(anomaly.getFeedback().getFeedbackType()))) .sorted(Comparator.comparingLong(AbstractDTO::getId).reversed()) .collect(Collectors.toList()); count = results.size(); results = ResourceUtils.paginateResults(results, offset, limit); } return ImmutableMap.of("count", count, "limit", limit, "offset", offset, "elements", results); } }
AnomalySearcher { public Map<String, Object> search(AnomalySearchFilter searchFilter, int limit, int offset) { Predicate predicate = Predicate.NEQ("baseId", 0); if (searchFilter.getAnomalyIds().isEmpty()) { predicate = Predicate.EQ("child", false); } if (searchFilter.getStartTime() != null) { predicate = Predicate.AND(predicate, Predicate.LT("startTime", searchFilter.getEndTime())); } if (searchFilter.getEndTime() != null) { predicate = Predicate.AND(predicate, Predicate.GT("endTime", searchFilter.getStartTime())); } Set<Long> detectionConfigIds = new HashSet<>(); Set<Long> subscribedDetectionConfigIds = new HashSet<>(); if (!searchFilter.getDetectionNames().isEmpty()) { detectionConfigIds = this.detectionConfigDAO.findByPredicate(Predicate.IN("name", searchFilter.getDetectionNames().toArray())) .stream() .map(DetectionConfigBean::getId) .collect(Collectors.toSet()); } if (!searchFilter.getSubscriptionGroups().isEmpty()) { subscribedDetectionConfigIds = this.detectionAlertConfigDAO.findByPredicate( Predicate.IN("name", searchFilter.getSubscriptionGroups().toArray())) .stream() .map(detectionAlertConfigDTO -> detectionAlertConfigDTO.getVectorClocks().keySet()) .flatMap(Collection::stream) .collect(Collectors.toSet()); } if (!searchFilter.getDetectionNames().isEmpty() && !searchFilter.getSubscriptionGroups().isEmpty()) { detectionConfigIds.retainAll(subscribedDetectionConfigIds); } else { detectionConfigIds.addAll(subscribedDetectionConfigIds); } if (!searchFilter.getDetectionNames().isEmpty() || !searchFilter.getSubscriptionGroups().isEmpty()) { if (detectionConfigIds.isEmpty()) { return ImmutableMap.of("count", 0, "limit", limit, "offset", offset, "elements", Collections.emptyList()); } predicate = Predicate.AND(predicate, Predicate.IN("detectionConfigId", detectionConfigIds.toArray())); } if (!searchFilter.getDatasets().isEmpty()) { List<Predicate> datasetPredicates = new ArrayList<>(); for (String dataset : searchFilter.getDatasets()) { datasetPredicates.add(Predicate.LIKE("collection", "%" + dataset + "%")); } predicate = Predicate.AND(predicate, Predicate.OR(datasetPredicates.toArray(new Predicate[0]))); } if (!searchFilter.getMetrics().isEmpty()) { predicate = Predicate.AND(predicate, Predicate.IN("metric", searchFilter.getMetrics().toArray())); } if (!searchFilter.getAnomalyIds().isEmpty()) { predicate = Predicate.AND(predicate, Predicate.IN("baseId", searchFilter.getAnomalyIds().toArray())); } long count; List<MergedAnomalyResultDTO> results; if (searchFilter.getFeedbacks().isEmpty()) { List<Long> anomalyIds = this.anomalyDAO.findIdsByPredicate(predicate) .stream() .sorted(Comparator.reverseOrder()) .collect(Collectors.toList()); count = anomalyIds.size(); results = anomalyIds.isEmpty() ? Collections.emptyList() : this.anomalyDAO.findByIds(ResourceUtils.paginateResults(anomalyIds, offset, limit)); } else { List<MergedAnomalyResultDTO> anomalies = this.anomalyDAO.findByPredicate(predicate); Set<AnomalyFeedbackType> feedbackFilters = searchFilter.getFeedbacks().stream().map(AnomalyFeedbackType::valueOf).collect(Collectors.toSet()); results = anomalies.stream() .filter(anomaly -> (anomaly.getFeedback() == null && feedbackFilters.contains(NO_FEEDBACK)) || ( anomaly.getFeedback() != null && feedbackFilters.contains(anomaly.getFeedback().getFeedbackType()))) .sorted(Comparator.comparingLong(AbstractDTO::getId).reversed()) .collect(Collectors.toList()); count = results.size(); results = ResourceUtils.paginateResults(results, offset, limit); } return ImmutableMap.of("count", count, "limit", limit, "offset", offset, "elements", results); } AnomalySearcher(); }
AnomalySearcher { public Map<String, Object> search(AnomalySearchFilter searchFilter, int limit, int offset) { Predicate predicate = Predicate.NEQ("baseId", 0); if (searchFilter.getAnomalyIds().isEmpty()) { predicate = Predicate.EQ("child", false); } if (searchFilter.getStartTime() != null) { predicate = Predicate.AND(predicate, Predicate.LT("startTime", searchFilter.getEndTime())); } if (searchFilter.getEndTime() != null) { predicate = Predicate.AND(predicate, Predicate.GT("endTime", searchFilter.getStartTime())); } Set<Long> detectionConfigIds = new HashSet<>(); Set<Long> subscribedDetectionConfigIds = new HashSet<>(); if (!searchFilter.getDetectionNames().isEmpty()) { detectionConfigIds = this.detectionConfigDAO.findByPredicate(Predicate.IN("name", searchFilter.getDetectionNames().toArray())) .stream() .map(DetectionConfigBean::getId) .collect(Collectors.toSet()); } if (!searchFilter.getSubscriptionGroups().isEmpty()) { subscribedDetectionConfigIds = this.detectionAlertConfigDAO.findByPredicate( Predicate.IN("name", searchFilter.getSubscriptionGroups().toArray())) .stream() .map(detectionAlertConfigDTO -> detectionAlertConfigDTO.getVectorClocks().keySet()) .flatMap(Collection::stream) .collect(Collectors.toSet()); } if (!searchFilter.getDetectionNames().isEmpty() && !searchFilter.getSubscriptionGroups().isEmpty()) { detectionConfigIds.retainAll(subscribedDetectionConfigIds); } else { detectionConfigIds.addAll(subscribedDetectionConfigIds); } if (!searchFilter.getDetectionNames().isEmpty() || !searchFilter.getSubscriptionGroups().isEmpty()) { if (detectionConfigIds.isEmpty()) { return ImmutableMap.of("count", 0, "limit", limit, "offset", offset, "elements", Collections.emptyList()); } predicate = Predicate.AND(predicate, Predicate.IN("detectionConfigId", detectionConfigIds.toArray())); } if (!searchFilter.getDatasets().isEmpty()) { List<Predicate> datasetPredicates = new ArrayList<>(); for (String dataset : searchFilter.getDatasets()) { datasetPredicates.add(Predicate.LIKE("collection", "%" + dataset + "%")); } predicate = Predicate.AND(predicate, Predicate.OR(datasetPredicates.toArray(new Predicate[0]))); } if (!searchFilter.getMetrics().isEmpty()) { predicate = Predicate.AND(predicate, Predicate.IN("metric", searchFilter.getMetrics().toArray())); } if (!searchFilter.getAnomalyIds().isEmpty()) { predicate = Predicate.AND(predicate, Predicate.IN("baseId", searchFilter.getAnomalyIds().toArray())); } long count; List<MergedAnomalyResultDTO> results; if (searchFilter.getFeedbacks().isEmpty()) { List<Long> anomalyIds = this.anomalyDAO.findIdsByPredicate(predicate) .stream() .sorted(Comparator.reverseOrder()) .collect(Collectors.toList()); count = anomalyIds.size(); results = anomalyIds.isEmpty() ? Collections.emptyList() : this.anomalyDAO.findByIds(ResourceUtils.paginateResults(anomalyIds, offset, limit)); } else { List<MergedAnomalyResultDTO> anomalies = this.anomalyDAO.findByPredicate(predicate); Set<AnomalyFeedbackType> feedbackFilters = searchFilter.getFeedbacks().stream().map(AnomalyFeedbackType::valueOf).collect(Collectors.toSet()); results = anomalies.stream() .filter(anomaly -> (anomaly.getFeedback() == null && feedbackFilters.contains(NO_FEEDBACK)) || ( anomaly.getFeedback() != null && feedbackFilters.contains(anomaly.getFeedback().getFeedbackType()))) .sorted(Comparator.comparingLong(AbstractDTO::getId).reversed()) .collect(Collectors.toList()); count = results.size(); results = ResourceUtils.paginateResults(results, offset, limit); } return ImmutableMap.of("count", count, "limit", limit, "offset", offset, "elements", results); } AnomalySearcher(); Map<String, Object> search(AnomalySearchFilter searchFilter, int limit, int offset); }
AnomalySearcher { public Map<String, Object> search(AnomalySearchFilter searchFilter, int limit, int offset) { Predicate predicate = Predicate.NEQ("baseId", 0); if (searchFilter.getAnomalyIds().isEmpty()) { predicate = Predicate.EQ("child", false); } if (searchFilter.getStartTime() != null) { predicate = Predicate.AND(predicate, Predicate.LT("startTime", searchFilter.getEndTime())); } if (searchFilter.getEndTime() != null) { predicate = Predicate.AND(predicate, Predicate.GT("endTime", searchFilter.getStartTime())); } Set<Long> detectionConfigIds = new HashSet<>(); Set<Long> subscribedDetectionConfigIds = new HashSet<>(); if (!searchFilter.getDetectionNames().isEmpty()) { detectionConfigIds = this.detectionConfigDAO.findByPredicate(Predicate.IN("name", searchFilter.getDetectionNames().toArray())) .stream() .map(DetectionConfigBean::getId) .collect(Collectors.toSet()); } if (!searchFilter.getSubscriptionGroups().isEmpty()) { subscribedDetectionConfigIds = this.detectionAlertConfigDAO.findByPredicate( Predicate.IN("name", searchFilter.getSubscriptionGroups().toArray())) .stream() .map(detectionAlertConfigDTO -> detectionAlertConfigDTO.getVectorClocks().keySet()) .flatMap(Collection::stream) .collect(Collectors.toSet()); } if (!searchFilter.getDetectionNames().isEmpty() && !searchFilter.getSubscriptionGroups().isEmpty()) { detectionConfigIds.retainAll(subscribedDetectionConfigIds); } else { detectionConfigIds.addAll(subscribedDetectionConfigIds); } if (!searchFilter.getDetectionNames().isEmpty() || !searchFilter.getSubscriptionGroups().isEmpty()) { if (detectionConfigIds.isEmpty()) { return ImmutableMap.of("count", 0, "limit", limit, "offset", offset, "elements", Collections.emptyList()); } predicate = Predicate.AND(predicate, Predicate.IN("detectionConfigId", detectionConfigIds.toArray())); } if (!searchFilter.getDatasets().isEmpty()) { List<Predicate> datasetPredicates = new ArrayList<>(); for (String dataset : searchFilter.getDatasets()) { datasetPredicates.add(Predicate.LIKE("collection", "%" + dataset + "%")); } predicate = Predicate.AND(predicate, Predicate.OR(datasetPredicates.toArray(new Predicate[0]))); } if (!searchFilter.getMetrics().isEmpty()) { predicate = Predicate.AND(predicate, Predicate.IN("metric", searchFilter.getMetrics().toArray())); } if (!searchFilter.getAnomalyIds().isEmpty()) { predicate = Predicate.AND(predicate, Predicate.IN("baseId", searchFilter.getAnomalyIds().toArray())); } long count; List<MergedAnomalyResultDTO> results; if (searchFilter.getFeedbacks().isEmpty()) { List<Long> anomalyIds = this.anomalyDAO.findIdsByPredicate(predicate) .stream() .sorted(Comparator.reverseOrder()) .collect(Collectors.toList()); count = anomalyIds.size(); results = anomalyIds.isEmpty() ? Collections.emptyList() : this.anomalyDAO.findByIds(ResourceUtils.paginateResults(anomalyIds, offset, limit)); } else { List<MergedAnomalyResultDTO> anomalies = this.anomalyDAO.findByPredicate(predicate); Set<AnomalyFeedbackType> feedbackFilters = searchFilter.getFeedbacks().stream().map(AnomalyFeedbackType::valueOf).collect(Collectors.toSet()); results = anomalies.stream() .filter(anomaly -> (anomaly.getFeedback() == null && feedbackFilters.contains(NO_FEEDBACK)) || ( anomaly.getFeedback() != null && feedbackFilters.contains(anomaly.getFeedback().getFeedbackType()))) .sorted(Comparator.comparingLong(AbstractDTO::getId).reversed()) .collect(Collectors.toList()); count = results.size(); results = ResourceUtils.paginateResults(results, offset, limit); } return ImmutableMap.of("count", count, "limit", limit, "offset", offset, "elements", results); } AnomalySearcher(); Map<String, Object> search(AnomalySearchFilter searchFilter, int limit, int offset);  }