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



##########
File path: 
oap-server/server-query-plugin/query-graphql-plugin/src/main/java/org/apache/skywalking/oap/query/graphql/resolver/AlarmQuery.java
##########
@@ -62,11 +83,111 @@ public Alarms getAlarm(final Duration duration, final 
Scope scope, final String
         }
         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);
+        Events events = null;
+        try {
+            events = getEventQueryService().queryEvents(condition);
+        } catch (Throwable e) {
+            LOGGER.error(e.getMessage(), e);
+            return alarms;
+        }
+        return includeEvents2Alarms(alarms, events);
+    }
+
+    private Alarms includeEvents2Alarms(Alarms alarms, Events events) {
+        if (alarms.getTotal() < 1 || events.getTotal() < 1) {
+            return alarms;
         }
-        return getQueryService().getAlarm(
-            scopeId, keyword, paging, startSecondTB, endSecondTB, tags);
+        Map<String, List<Event>> mappingMap = 
events.getEvents().stream().collect(Collectors.groupingBy(Event::getServiceInSource));
+        alarms.getMsgs().forEach(a -> {
+            switch (a.getScopeId()) {
+                case DefaultScopeDefine.SERVICE :
+                    List<Event> serviceEvent = 
mappingMap.get(IDManager.ServiceID.analysisId(a.getId()).getName());
+                    if (CollectionUtils.isNotEmpty(serviceEvent)) {
+                        a.setEvents(serviceEvent);
+                    }
+                    break;
+                case DefaultScopeDefine.SERVICE_RELATION :
+                    List<Event> sourceServiceEvent = 
mappingMap.get(IDManager.ServiceID.analysisId(a.getId()));
+                    List<Event> destServiceEvent = 
mappingMap.get(IDManager.ServiceID.analysisId(a.getId1()));
+                    if (CollectionUtils.isNotEmpty(sourceServiceEvent)) {
+                        a.setEvents(sourceServiceEvent);
+                    }
+                    if (CollectionUtils.isNotEmpty(destServiceEvent)) {
+                        a.getEvents().addAll(destServiceEvent);
+                    }
+                    break;
+                case DefaultScopeDefine.SERVICE_INSTANCE :
+                    IDManager.ServiceInstanceID.InstanceIDDefinition 
instanceIDDefinition = IDManager.ServiceInstanceID.analysisId(a.getId());
+                    String serviceInstanceName = 
instanceIDDefinition.getName();
+                    String serviceName = 
IDManager.ServiceID.analysisId(instanceIDDefinition.getServiceId()).getName();
+                    List<Event> serviceInstanceEvent = 
mappingMap.get(serviceName);
+                    if (CollectionUtils.isNotEmpty(serviceInstanceEvent)) {
+                        List<Event> filterEvents = 
serviceInstanceEvent.stream().filter(e -> 
StringUtils.equals(e.getSource().getServiceInstance(), 
serviceInstanceName)).collect(Collectors.toList());
+                        a.setEvents(filterEvents);
+                    }
+                    break;
+                case DefaultScopeDefine.SERVICE_INSTANCE_RELATION :
+                    IDManager.ServiceInstanceID.InstanceIDDefinition 
sourceInstanceIDDefinition = IDManager.ServiceInstanceID.analysisId(a.getId());
+                    String sourceServiceInstanceName = 
sourceInstanceIDDefinition.getName();
+                    String sourceServiceName = 
IDManager.ServiceID.analysisId(sourceInstanceIDDefinition.getServiceId()).getName();
+
+                    IDManager.ServiceInstanceID.InstanceIDDefinition 
destInstanceIDDefinition = IDManager.ServiceInstanceID.analysisId(a.getId1());
+                    String destServiceInstanceName = 
destInstanceIDDefinition.getName();
+                    String destServiceName = 
IDManager.ServiceID.analysisId(destInstanceIDDefinition.getServiceId()).getName();
+
+                    List<Event> sourceInstanceEvent = 
mappingMap.get(sourceServiceName);
+                    List<Event> destInstanceEvent = 
mappingMap.get(destServiceName);
+
+                    if (CollectionUtils.isNotEmpty(sourceInstanceEvent)) {
+                        List<Event> filterEvents = 
sourceInstanceEvent.stream().filter(e -> 
StringUtils.equals(e.getSource().getServiceInstance(), 
sourceServiceInstanceName)).collect(Collectors.toList());
+                        a.setEvents(filterEvents);
+                    }
+                    if (CollectionUtils.isNotEmpty(destInstanceEvent)) {
+                        List<Event> filterEvents = 
destInstanceEvent.stream().filter(e -> 
StringUtils.equals(e.getSource().getServiceInstance(), 
destServiceInstanceName)).collect(Collectors.toList());
+                        a.getEvents().addAll(filterEvents);
+                    }
+                    break;
+                case DefaultScopeDefine.ENDPOINT :

Review comment:
       > we can't get instance-endpoint mapping
   
   No, we can't. I prefer a service level event should be enough for the 
endpoint, after all, the endpoint belongs to a service. The unstable of a 
single instance can't impact the performance of an endpoint in most cases 
unless there is a load balance issue.




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