|
15 | 15 |
|
16 | 16 | package com.navercorp.pinpoint.web.vo;
|
17 | 17 |
|
18 |
| -import com.fasterxml.jackson.databind.annotation.JsonSerialize; |
19 |
| -import com.navercorp.pinpoint.web.view.ApplicationAgentHostListSerializer; |
| 18 | +import com.fasterxml.jackson.annotation.JsonProperty; |
| 19 | +import com.navercorp.pinpoint.common.util.StringUtils; |
| 20 | +import com.navercorp.pinpoint.loader.service.ServiceTypeRegistryService; |
20 | 21 |
|
21 | 22 | import java.util.ArrayList;
|
22 |
| -import java.util.LinkedHashMap; |
| 23 | +import java.util.Comparator; |
| 24 | +import java.util.HashMap; |
23 | 25 | import java.util.List;
|
24 | 26 | import java.util.Map;
|
| 27 | +import java.util.Objects; |
| 28 | +import java.util.stream.Collectors; |
25 | 29 |
|
26 | 30 | /**
|
27 | 31 | * @author Taejin Koo
|
28 | 32 | */
|
29 |
| -@JsonSerialize(using = ApplicationAgentHostListSerializer.class) |
30 | 33 | public class ApplicationAgentHostList {
|
31 | 34 |
|
32 |
| - private final Map<String, List<AgentInfo>> map = new LinkedHashMap<>(); |
33 |
| - |
34 | 35 | private final int startApplicationIndex;
|
35 | 36 | private final int endApplicationIndex;
|
36 | 37 | private final int totalApplications;
|
37 | 38 |
|
38 |
| - public ApplicationAgentHostList(int startApplicationIndex, int endApplicationIndex, int totalApplications) { |
| 39 | + private final List<ApplicationInfo> applications; |
| 40 | + |
| 41 | + public ApplicationAgentHostList(int startApplicationIndex, int endApplicationIndex, int totalApplications, |
| 42 | + List<ApplicationInfo> applications) { |
39 | 43 | this.startApplicationIndex = startApplicationIndex;
|
40 | 44 | this.endApplicationIndex = endApplicationIndex;
|
41 | 45 | this.totalApplications = totalApplications;
|
| 46 | + this.applications = Objects.requireNonNull(applications, "applications"); |
42 | 47 | }
|
43 | 48 |
|
| 49 | + @JsonProperty("startIndex") |
44 | 50 | public int getStartApplicationIndex() {
|
45 | 51 | return startApplicationIndex;
|
46 | 52 | }
|
47 | 53 |
|
| 54 | + @JsonProperty("endIndex") |
48 | 55 | public int getEndApplicationIndex() {
|
49 | 56 | return endApplicationIndex;
|
50 | 57 | }
|
51 | 58 |
|
| 59 | + @JsonProperty("totalApplications") |
52 | 60 | public int getTotalApplications() {
|
53 | 61 | return totalApplications;
|
54 | 62 | }
|
55 | 63 |
|
56 |
| - public Map<String, List<AgentInfo>> getMap() { |
57 |
| - return map; |
| 64 | + public List<ApplicationInfo> getApplications() { |
| 65 | + return applications; |
58 | 66 | }
|
59 | 67 |
|
60 |
| - public void put(String applicationName, List<AgentInfo> agentInfoList) { |
61 |
| - if (applicationName == null) { |
62 |
| - return; |
63 |
| - } |
| 68 | + @Override |
| 69 | + public String toString() { |
| 70 | + return "ApplicationAgentHostList{" + |
| 71 | + "startApplicationIndex=" + startApplicationIndex + |
| 72 | + ", endApplicationIndex=" + endApplicationIndex + |
| 73 | + ", totalApplications=" + totalApplications + |
| 74 | + ", applications=" + applications + |
| 75 | + '}'; |
| 76 | + } |
| 77 | + |
| 78 | + public static Builder newBuilder(ServiceTypeRegistryService serviceTypeRegistryService, |
| 79 | + int startApplicationIndex, int endApplicationIndex, int totalApplications) { |
| 80 | + return new Builder(startApplicationIndex, endApplicationIndex, totalApplications, serviceTypeRegistryService); |
| 81 | + } |
64 | 82 |
|
65 |
| - List<AgentInfo> value = map.computeIfAbsent(applicationName, k -> new ArrayList<>()); |
| 83 | + public static class Builder { |
| 84 | + private final int startApplicationIndex; |
| 85 | + private final int endApplicationIndex; |
| 86 | + private final int totalApplications; |
| 87 | + private final ServiceTypeRegistryService serviceTypeRegistryService; |
66 | 88 |
|
67 |
| - if (agentInfoList == null) { |
68 |
| - return; |
| 89 | + private final Map<String, List<AgentHost>> map = new HashMap<>(); |
| 90 | + |
| 91 | + public Builder(int startApplicationIndex, int endApplicationIndex, int totalApplications, ServiceTypeRegistryService serviceTypeRegistryService) { |
| 92 | + this.startApplicationIndex = startApplicationIndex; |
| 93 | + this.endApplicationIndex = endApplicationIndex; |
| 94 | + this.totalApplications = totalApplications; |
| 95 | + this.serviceTypeRegistryService = Objects.requireNonNull(serviceTypeRegistryService, "serviceTypeRegistryService"); |
69 | 96 | }
|
70 | 97 |
|
71 |
| - for (AgentInfo agentInfo : agentInfoList) { |
72 |
| - if (agentInfo != null) { |
73 |
| - value.add(agentInfo); |
| 98 | + |
| 99 | + public void addAgentInfo(String applicationName, List<AgentInfo> agentInfoList) { |
| 100 | + if (applicationName == null) { |
| 101 | + return; |
| 102 | + } |
| 103 | + |
| 104 | + List<AgentHost> value = map.computeIfAbsent(applicationName, k -> new ArrayList<>()); |
| 105 | + |
| 106 | + if (agentInfoList == null) { |
| 107 | + return; |
74 | 108 | }
|
| 109 | + |
| 110 | + for (AgentInfo agentInfo : agentInfoList) { |
| 111 | + if (agentInfo != null) { |
| 112 | + value.add(newAgentHost(agentInfo)); |
| 113 | + } |
| 114 | + } |
| 115 | + } |
| 116 | + |
| 117 | + private AgentHost newAgentHost(AgentInfo agentInfo) { |
| 118 | + String agentId = StringUtils.defaultString(agentInfo.getAgentId(), ""); |
| 119 | + String hostName = StringUtils.defaultString(agentInfo.getHostName(), ""); |
| 120 | + String ip = StringUtils.defaultString(agentInfo.getIp(), ""); |
| 121 | + String serviceType = serviceTypeRegistryService.findServiceType(agentInfo.getServiceTypeCode()).getDesc(); |
| 122 | + return new AgentHost(agentId, hostName, ip, serviceType); |
| 123 | + } |
| 124 | + |
| 125 | + public ApplicationAgentHostList build() { |
| 126 | + List<ApplicationInfo> applicationInfos = buildApplicationInfo(this.map); |
| 127 | + ApplicationAgentHostList agents = new ApplicationAgentHostList(startApplicationIndex, endApplicationIndex, totalApplications, |
| 128 | + applicationInfos); |
| 129 | + return agents; |
| 130 | + } |
| 131 | + |
| 132 | + private List<ApplicationInfo> buildApplicationInfo(Map<String, List<AgentHost>> map) { |
| 133 | + List<ApplicationInfo> applications = map.entrySet().stream() |
| 134 | + .map(Builder::newApplication) |
| 135 | + .sorted(Comparator.comparing(ApplicationInfo::getApplicationName)) |
| 136 | + .collect(Collectors.toList()); |
| 137 | + return applications; |
| 138 | + } |
| 139 | + |
| 140 | + |
| 141 | + private static ApplicationInfo newApplication(Map.Entry<String, List<AgentHost>> entry) { |
| 142 | + String applicationName = entry.getKey(); |
| 143 | + |
| 144 | + List<AgentHost> agentHosts = entry.getValue(); |
| 145 | + agentHosts.sort(Comparator.comparing(AgentHost::getAgentId)); |
| 146 | + |
| 147 | + return new ApplicationInfo(applicationName, agentHosts); |
| 148 | + } |
| 149 | + } |
| 150 | + |
| 151 | + public static class ApplicationInfo { |
| 152 | + private final String applicationName; |
| 153 | + private final List<AgentHost> agents; |
| 154 | + |
| 155 | + public ApplicationInfo(String applicationName, List<AgentHost> agents) { |
| 156 | + this.applicationName = Objects.requireNonNull(applicationName, "applicationName"); |
| 157 | + this.agents = Objects.requireNonNull(agents, "agents"); |
| 158 | + } |
| 159 | + |
| 160 | + public String getApplicationName() { |
| 161 | + return applicationName; |
| 162 | + } |
| 163 | + |
| 164 | + public List<AgentHost> getAgents() { |
| 165 | + return agents; |
| 166 | + } |
| 167 | + } |
| 168 | + |
| 169 | + public static class AgentHost { |
| 170 | + private final String agentId; |
| 171 | + private final String hostName; |
| 172 | + private final String ip; |
| 173 | + private final String serviceType; |
| 174 | + |
| 175 | + public AgentHost(String agentId, String hostName, String ip, String serviceType) { |
| 176 | + this.agentId = Objects.requireNonNull(agentId, "agentId"); |
| 177 | + this.hostName = Objects.requireNonNull(hostName, "hostName"); |
| 178 | + this.ip = Objects.requireNonNull(ip, "ip"); |
| 179 | + this.serviceType = Objects.requireNonNull(serviceType, "serviceType"); |
| 180 | + } |
| 181 | + |
| 182 | + public String getAgentId() { |
| 183 | + return agentId; |
| 184 | + } |
| 185 | + |
| 186 | + public String getHostName() { |
| 187 | + return hostName; |
| 188 | + } |
| 189 | + |
| 190 | + public String getIp() { |
| 191 | + return ip; |
| 192 | + } |
| 193 | + |
| 194 | + public String getServiceType() { |
| 195 | + return serviceType; |
75 | 196 | }
|
76 | 197 | }
|
77 | 198 |
|
|
0 commit comments