wu-sheng commented on a change in pull request #6888:
URL: https://github.com/apache/skywalking/pull/6888#discussion_r632951894



##########
File path: 
oap-server/server-query-plugin/query-graphql-plugin/src/main/java/org/apache/skywalking/oap/query/graphql/resolver/AlarmQuery.java
##########
@@ -50,23 +64,167 @@ private AlarmQueryService getQueryService() {
         return queryService;
     }
 
+    private EventQueryService getEventQueryService() {
+        if (eventQueryService == null) {
+            this.eventQueryService = 
moduleManager.find(CoreModule.NAME).provider().getService(EventQueryService.class);
+        }
+        return eventQueryService;
+    }
+
+    private ForkJoinPool getForkJoinPool() {
+        if (forkJoinPool == null) {
+            this.forkJoinPool = new 
ForkJoinPool(Runtime.getRuntime().availableProcessors());
+        }
+        return forkJoinPool;
+    }
+
     public AlarmTrend getAlarmTrend(final Duration duration) {
         return new AlarmTrend();
     }
 
     public Alarms getAlarm(final Duration duration, final Scope scope, final 
String keyword,
-                           final Pagination paging, final List<Tag> tags) 
throws IOException {
+                           final Pagination paging, final List<Tag> tags) 
throws Throwable {
         Integer scopeId = null;
         if (scope != null) {
             scopeId = scope.getScopeId();
         }
         long startSecondTB = 0;
         long endSecondTB = 0;
+        EventQueryCondition condition = new EventQueryCondition();
         if (nonNull(duration)) {
             startSecondTB = duration.getStartTimeBucketInSec();
             endSecondTB = duration.getEndTimeBucketInSec();
+            condition.setTime(duration);
+        }
+        Alarms alarms = getQueryService().getAlarm(
+                scopeId, keyword, paging, startSecondTB, endSecondTB, tags);
+        return includeEvents2AlarmsByCondition(alarms, condition);
+    }
+
+    private Alarms includeEvents2AlarmsByCondition(Alarms alarms, 
EventQueryCondition condition) throws Exception {
+        if (alarms.getTotal() < 1) {
+            return alarms;
         }
-        return getQueryService().getAlarm(
-            scopeId, keyword, paging, startSecondTB, endSecondTB, tags);
+
+        final List<EventQueryCondition> allConditions = new 
ArrayList<>(alarms.getTotal());
+        alarms.getMsgs().stream().forEach(m -> {
+            constructCurrentSource(m).stream().forEach(c -> {
+                final EventQueryCondition currentCondition = 
constructNewEventQueryCondition(condition);
+                currentCondition.setSource(c);
+                allConditions.add(currentCondition);
+            });
+        });
+
+        List<Event> events = 
getEventQueryService().queryEvents(allConditions).getEvents();
+        Map<String, List<Event>> mappingEvents = 
events.stream().collect(Collectors.toMap(Event::getSourcesString, e -> {
+            final List<Event> allEvents = new ArrayList<>();
+            allEvents.add(e);
+            return allEvents;
+        }, (List<Event> firstEvents, List<Event> secondEvents) -> {
+                if (CollectionUtils.isNotEmpty(firstEvents)) {
+                    firstEvents.addAll(secondEvents);
+                }
+                return firstEvents;
+        }));
+        alarms.getMsgs().stream().forEach(a -> {
+            if 
(CollectionUtils.isNotEmpty(mappingEvents.get(a.getId0SourcesStr()))) {
+                a.getEvents().addAll(mappingEvents.get(a.getId0SourcesStr()));
+            }
+            if (Boolean.TRUE.equals(a.getId0LinkId1Flag()) && 
CollectionUtils.isNotEmpty(mappingEvents.get(a.getId1SourcesStr()))) {
+                a.getEvents().addAll(mappingEvents.get(a.getId0SourcesStr()));
+            }
+        });
+        return alarms;
+    }
+
+    private List<Source> constructCurrentSource(AlarmMessage msg) {

Review comment:
       ```suggestion
       private List<Source> buildEventSources(AlarmMessage msg) {
   ```




-- 
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:
[email protected]


Reply via email to