Search in sources :

Example 1 with RelationDTO

use of org.apache.dubbo.admin.model.dto.RelationDTO in project incubator-dubbo-ops by apache.

the class MetricsServiceImpl method getApplicationRelation.

@Override
public RelationDTO getApplicationRelation() {
    List<Consumer> consumerList = consumerService.findAll();
    List<Provider> providerList = providerService.findAll();
    int index = 0;
    // collect all service
    Set<String> serviceSet = new HashSet<>();
    // collect consumer's nodes map <application, node>
    Map<String, RelationDTO.Node> consumerNodeMap = new HashMap<>();
    // collect consumer's service and applications map <service, set<application>>
    Map<String, Set<String>> consumerServiceApplicationMap = new HashMap<>();
    for (Consumer consumer : consumerList) {
        String application = consumer.getApplication();
        if (!consumerNodeMap.keySet().contains(application)) {
            RelationDTO.Node node = new RelationDTO.Node(index, application, RelationDTO.CONSUMER_CATEGORIES.getIndex());
            consumerNodeMap.put(application, node);
            index++;
        }
        String service = consumer.getService();
        serviceSet.add(service);
        consumerServiceApplicationMap.computeIfAbsent(service, s -> new HashSet<>());
        consumerServiceApplicationMap.get(service).add(application);
    }
    // collect provider's nodes
    Map<String, RelationDTO.Node> providerNodeMap = new HashMap<>();
    // collect provider's service and applications map <service, set<application>>
    Map<String, Set<String>> providerServiceApplicationMap = new HashMap<>();
    for (Provider provider : providerList) {
        String application = provider.getApplication();
        if (!providerNodeMap.keySet().contains(application)) {
            RelationDTO.Node node = new RelationDTO.Node(index, application, RelationDTO.PROVIDER_CATEGORIES.getIndex());
            providerNodeMap.put(application, node);
            index++;
        }
        String service = provider.getService();
        serviceSet.add(service);
        providerServiceApplicationMap.computeIfAbsent(service, s -> new HashSet<>());
        providerServiceApplicationMap.get(service).add(application);
    }
    // merge provider's nodes and consumer's nodes
    Map<String, RelationDTO.Node> nodeMap = new HashMap<>(consumerNodeMap);
    for (Map.Entry<String, RelationDTO.Node> entry : providerNodeMap.entrySet()) {
        if (nodeMap.get(entry.getKey()) != null) {
            nodeMap.get(entry.getKey()).setCategory(RelationDTO.CONSUMER_AND_PROVIDER_CATEGORIES.getIndex());
        } else {
            nodeMap.put(entry.getKey(), entry.getValue());
        }
    }
    // build link by same service
    Set<RelationDTO.Link> linkSet = new HashSet<>();
    for (String service : serviceSet) {
        Set<String> consumerApplicationSet = consumerServiceApplicationMap.get(service);
        Set<String> providerApplicationSet = providerServiceApplicationMap.get(service);
        if (CollectionUtils.isNotEmpty(consumerApplicationSet) && CollectionUtils.isNotEmpty(providerApplicationSet)) {
            for (String providerApplication : providerApplicationSet) {
                for (String consumerApplication : consumerApplicationSet) {
                    if (nodeMap.get(consumerApplication) != null && nodeMap.get(providerApplication) != null) {
                        Integer consumerIndex = nodeMap.get(consumerApplication).getIndex();
                        Integer providerIndex = nodeMap.get(providerApplication).getIndex();
                        linkSet.add(new RelationDTO.Link(consumerIndex, providerIndex));
                    }
                }
            }
        }
    }
    // sort node by index
    List<RelationDTO.Node> nodeList = nodeMap.values().stream().sorted(Comparator.comparingInt(RelationDTO.Node::getIndex)).collect(Collectors.toList());
    return new RelationDTO(nodeList, new ArrayList<>(linkSet));
}
Also used : Set(java.util.Set) HashSet(java.util.HashSet) HashMap(java.util.HashMap) Provider(org.apache.dubbo.admin.model.domain.Provider) Consumer(org.apache.dubbo.admin.model.domain.Consumer) RelationDTO(org.apache.dubbo.admin.model.dto.RelationDTO) HashMap(java.util.HashMap) Map(java.util.Map) HashSet(java.util.HashSet)

Aggregations

HashMap (java.util.HashMap)1 HashSet (java.util.HashSet)1 Map (java.util.Map)1 Set (java.util.Set)1 Consumer (org.apache.dubbo.admin.model.domain.Consumer)1 Provider (org.apache.dubbo.admin.model.domain.Provider)1 RelationDTO (org.apache.dubbo.admin.model.dto.RelationDTO)1