diff --git a/apm-collector/apm-collector-boot/src/main/resources/application.yml b/apm-collector/apm-collector-boot/src/main/resources/application.yml index 02d49d0944e1ae149c44c786f2755bde0b83d8a4..85021ee37e24d5d04eb3360c266d04031d531c73 100644 --- a/apm-collector/apm-collector-boot/src/main/resources/application.yml +++ b/apm-collector/apm-collector-boot/src/main/resources/application.yml @@ -27,16 +27,10 @@ collector_inside: grpc: host: localhost port: 11800 -storage: - elasticsearch: - cluster_name: CollectorDBCluster - cluster_transport_sniffer: true - cluster_nodes: localhost:9300 - index_shards_number: 2 - index_replicas_number: 0 -# uncomment to enable h2 storage #storage: -# h2: -# url: jdbc:h2:~/collector -# user_name: sa -# password: sa \ No newline at end of file +# elasticsearch: +# cluster_name: CollectorDBCluster +# cluster_transport_sniffer: true +# cluster_nodes: localhost:9300 +# index_shards_number: 2 +# index_replicas_number: 0 \ No newline at end of file diff --git a/apm-collector/apm-collector-client/src/main/java/org/skywalking/apm/collector/client/h2/H2Client.java b/apm-collector/apm-collector-client/src/main/java/org/skywalking/apm/collector/client/h2/H2Client.java index 8ab7c47d86e11e7a14c757b2bb7212fc1cc5e95c..0a28d0880b3f35bb580eba935668e44522507412 100644 --- a/apm-collector/apm-collector-client/src/main/java/org/skywalking/apm/collector/client/h2/H2Client.java +++ b/apm-collector/apm-collector-client/src/main/java/org/skywalking/apm/collector/client/h2/H2Client.java @@ -18,11 +18,15 @@ package org.skywalking.apm.collector.client.h2; -import java.sql.*; - +import java.sql.Connection; +import java.sql.PreparedStatement; +import java.sql.ResultSet; +import java.sql.SQLException; +import java.sql.Statement; import org.h2.jdbcx.JdbcConnectionPool; import org.h2.util.IOUtils; import org.skywalking.apm.collector.core.client.Client; +import org.skywalking.apm.collector.core.config.SystemConfig; import org.slf4j.Logger; import org.slf4j.LoggerFactory; @@ -40,21 +44,15 @@ public class H2Client implements Client { private String password; public H2Client() { - this.url = "jdbc:h2:mem:collector"; + this.url = "jdbc:h2:" + SystemConfig.DATA_PATH + "/h2"; this.userName = ""; this.password = ""; } - public H2Client(String url, String userName, String password) { - this.url = url; - this.userName = userName; - this.password = password; - } - @Override public void initialize() throws H2ClientException { try { cp = JdbcConnectionPool. - create(this.url, this.userName, this.password); + create(this.url, this.userName, this.password); conn = cp.getConnection(); } catch (Exception e) { throw new H2ClientException(e.getMessage(), e); diff --git a/apm-collector/apm-collector-core/src/main/java/org/skywalking/apm/collector/core/module/SingleModuleInstaller.java b/apm-collector/apm-collector-core/src/main/java/org/skywalking/apm/collector/core/module/SingleModuleInstaller.java index 4cc1e39f1189b2499bd1449ebea9603a40cdeca9..691f40e92caf8ef7ce02a4ad785cf685f2a02e60 100644 --- a/apm-collector/apm-collector-core/src/main/java/org/skywalking/apm/collector/core/module/SingleModuleInstaller.java +++ b/apm-collector/apm-collector-core/src/main/java/org/skywalking/apm/collector/core/module/SingleModuleInstaller.java @@ -76,7 +76,9 @@ public abstract class SingleModuleInstaller extends CommonModuleInstaller { throw new ClusterModuleException("single module, but configure multiple default module"); } this.moduleDefine = moduleDefineEntry.getValue(); - this.moduleDefine.configParser().parse(null); + if (this.moduleDefine.configParser() != null) { + this.moduleDefine.configParser().parse(null); + } hasDefaultModule = true; } } diff --git a/apm-collector/apm-collector-storage/src/main/java/org/skywalking/apm/collector/storage/StorageModuleDefine.java b/apm-collector/apm-collector-storage/src/main/java/org/skywalking/apm/collector/storage/StorageModuleDefine.java index 1fa83a89d1a8cbd5d5a1580297844e27c77c0a8b..6f22a30ae7fd7f0ec9d054422f4d26df55b8b0a3 100644 --- a/apm-collector/apm-collector-storage/src/main/java/org/skywalking/apm/collector/storage/StorageModuleDefine.java +++ b/apm-collector/apm-collector-storage/src/main/java/org/skywalking/apm/collector/storage/StorageModuleDefine.java @@ -68,10 +68,6 @@ public abstract class StorageModuleDefine extends ModuleDefine implements Cluste return null; } - @Override public final boolean defaultModule() { - return true; - } - public abstract StorageInstaller storageInstaller(); public abstract void injectClientIntoDAO(Client client) throws DefineException; diff --git a/apm-collector/apm-collector-storage/src/main/java/org/skywalking/apm/collector/storage/elasticsearch/StorageElasticSearchModuleDefine.java b/apm-collector/apm-collector-storage/src/main/java/org/skywalking/apm/collector/storage/elasticsearch/StorageElasticSearchModuleDefine.java index 06f8b2c816013b362bc7bd898bf6f29fa02b9f8f..c2e5e87f9571fd58f9fa0f6eb3038deae89636b4 100644 --- a/apm-collector/apm-collector-storage/src/main/java/org/skywalking/apm/collector/storage/elasticsearch/StorageElasticSearchModuleDefine.java +++ b/apm-collector/apm-collector-storage/src/main/java/org/skywalking/apm/collector/storage/elasticsearch/StorageElasticSearchModuleDefine.java @@ -46,6 +46,10 @@ public class StorageElasticSearchModuleDefine extends StorageModuleDefine { return MODULE_NAME; } + @Override public final boolean defaultModule() { + return false; + } + @Override protected ModuleConfigParser configParser() { return new StorageElasticSearchConfigParser(); } diff --git a/apm-collector/apm-collector-storage/src/main/java/org/skywalking/apm/collector/storage/h2/StorageH2Config.java b/apm-collector/apm-collector-storage/src/main/java/org/skywalking/apm/collector/storage/h2/StorageH2Config.java deleted file mode 100644 index a467b9cf4115ce4489ceb4691af1dc0f854d2063..0000000000000000000000000000000000000000 --- a/apm-collector/apm-collector-storage/src/main/java/org/skywalking/apm/collector/storage/h2/StorageH2Config.java +++ /dev/null @@ -1,28 +0,0 @@ -/* - * Copyright 2017, OpenSkywalking Organization All rights reserved. - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - * - * Project repository: https://github.com/OpenSkywalking/skywalking - */ - -package org.skywalking.apm.collector.storage.h2; - -/** - * @author clevertension - */ -public class StorageH2Config { - public static String URL; - public static String USER_NAME; - public static String PASSWORD; -} diff --git a/apm-collector/apm-collector-storage/src/main/java/org/skywalking/apm/collector/storage/h2/StorageH2ConfigParser.java b/apm-collector/apm-collector-storage/src/main/java/org/skywalking/apm/collector/storage/h2/StorageH2ConfigParser.java deleted file mode 100644 index 85a08c426d0d16657ed90f9d3a91fc19114ca6bb..0000000000000000000000000000000000000000 --- a/apm-collector/apm-collector-storage/src/main/java/org/skywalking/apm/collector/storage/h2/StorageH2ConfigParser.java +++ /dev/null @@ -1,45 +0,0 @@ -/* - * Copyright 2017, OpenSkywalking Organization All rights reserved. - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - * - * Project repository: https://github.com/OpenSkywalking/skywalking - */ - -package org.skywalking.apm.collector.storage.h2; - -import java.util.Map; -import org.skywalking.apm.collector.core.config.ConfigParseException; -import org.skywalking.apm.collector.core.module.ModuleConfigParser; -import org.skywalking.apm.collector.core.util.ObjectUtils; -import org.skywalking.apm.collector.core.util.StringUtils; - -/** - * @author pengys5 - */ -public class StorageH2ConfigParser implements ModuleConfigParser { - private static final String URL = "url"; - public static final String USER_NAME = "user_name"; - public static final String PASSWORD = "password"; - @Override public void parse(Map config) throws ConfigParseException { - if (ObjectUtils.isNotEmpty(config) && StringUtils.isNotEmpty(config.get(URL))) { - StorageH2Config.URL = (String)config.get(URL); - } - if (ObjectUtils.isNotEmpty(config) && StringUtils.isNotEmpty(config.get(USER_NAME))) { - StorageH2Config.USER_NAME = (String)config.get(USER_NAME); - } - if (ObjectUtils.isNotEmpty(config) && StringUtils.isNotEmpty(config.get(PASSWORD))) { - StorageH2Config.PASSWORD = (String)config.get(PASSWORD); - } - } -} diff --git a/apm-collector/apm-collector-storage/src/main/java/org/skywalking/apm/collector/storage/h2/StorageH2ModuleDefine.java b/apm-collector/apm-collector-storage/src/main/java/org/skywalking/apm/collector/storage/h2/StorageH2ModuleDefine.java index 9faae085feb315438b676c260a86fd634b6b0cb8..2de18e33a4a7edf759a5c40af4cafd5577a622a6 100644 --- a/apm-collector/apm-collector-storage/src/main/java/org/skywalking/apm/collector/storage/h2/StorageH2ModuleDefine.java +++ b/apm-collector/apm-collector-storage/src/main/java/org/skywalking/apm/collector/storage/h2/StorageH2ModuleDefine.java @@ -46,12 +46,16 @@ public class StorageH2ModuleDefine extends StorageModuleDefine { return MODULE_NAME; } + @Override public final boolean defaultModule() { + return true; + } + @Override protected ModuleConfigParser configParser() { - return new StorageH2ConfigParser(); + return null; } @Override protected Client createClient() { - return new H2Client(StorageH2Config.URL, StorageH2Config.USER_NAME, StorageH2Config.PASSWORD); + return new H2Client(); } @Override public StorageInstaller storageInstaller() { diff --git a/apm-collector/apm-collector-ui/src/main/java/org/skywalking/apm/collector/ui/dao/IServiceReferenceDAO.java b/apm-collector/apm-collector-ui/src/main/java/org/skywalking/apm/collector/ui/dao/IServiceReferenceDAO.java index f2bee52daacdb89ea7fbb9e01385df97014b55ce..cc67f7ac43c14a99d352bda4dd60141cb03bd3ba 100644 --- a/apm-collector/apm-collector-ui/src/main/java/org/skywalking/apm/collector/ui/dao/IServiceReferenceDAO.java +++ b/apm-collector/apm-collector-ui/src/main/java/org/skywalking/apm/collector/ui/dao/IServiceReferenceDAO.java @@ -18,13 +18,14 @@ package org.skywalking.apm.collector.ui.dao; -import com.google.gson.JsonArray; +import com.google.gson.JsonObject; +import java.util.Map; /** * @author pengys5 */ public interface IServiceReferenceDAO { - JsonArray load(int entryServiceId, long startTime, long endTime); + Map load(int entryServiceId, long startTime, long endTime); - JsonArray load(String entryServiceName, int entryApplicationId, long startTime, long endTime); + Map load(String entryServiceName, int entryApplicationId, long startTime, long endTime); } diff --git a/apm-collector/apm-collector-ui/src/main/java/org/skywalking/apm/collector/ui/dao/InstanceH2DAO.java b/apm-collector/apm-collector-ui/src/main/java/org/skywalking/apm/collector/ui/dao/InstanceH2DAO.java index 6b3c72fc0cbf43c243ef90476caaf4eddf8f23a7..f94c1f1efa1525dbc4e3a0df4d19ba4c6cedd1df 100644 --- a/apm-collector/apm-collector-ui/src/main/java/org/skywalking/apm/collector/ui/dao/InstanceH2DAO.java +++ b/apm-collector/apm-collector-ui/src/main/java/org/skywalking/apm/collector/ui/dao/InstanceH2DAO.java @@ -18,11 +18,13 @@ package org.skywalking.apm.collector.ui.dao; +import com.google.gson.JsonArray; +import com.google.gson.JsonObject; import java.sql.ResultSet; import java.sql.SQLException; import java.util.LinkedList; import java.util.List; - +import org.skywalking.apm.collector.cache.ApplicationCache; import org.skywalking.apm.collector.client.h2.H2Client; import org.skywalking.apm.collector.client.h2.H2ClientException; import org.skywalking.apm.collector.core.util.TimeBucketUtils; @@ -30,13 +32,9 @@ import org.skywalking.apm.collector.storage.define.register.InstanceDataDefine; import org.skywalking.apm.collector.storage.define.register.InstanceTable; import org.skywalking.apm.collector.storage.h2.SqlBuilder; import org.skywalking.apm.collector.storage.h2.dao.H2DAO; -import org.skywalking.apm.collector.ui.cache.ApplicationCache; import org.slf4j.Logger; import org.slf4j.LoggerFactory; -import com.google.gson.JsonArray; -import com.google.gson.JsonObject; - /** * @author pengys5, clevertension */ @@ -54,7 +52,7 @@ public class InstanceH2DAO extends H2DAO implements IInstanceDAO { long fiveMinuteBefore = System.currentTimeMillis() - 5 * 60 * 1000; fiveMinuteBefore = TimeBucketUtils.INSTANCE.getSecondTimeBucket(fiveMinuteBefore); String sql = SqlBuilder.buildSql(GET_LAST_HEARTBEAT_TIME_SQL, InstanceTable.COLUMN_HEARTBEAT_TIME, InstanceTable.TABLE, InstanceTable.COLUMN_HEARTBEAT_TIME); - Object[] params = new Object[]{fiveMinuteBefore}; + Object[] params = new Object[] {fiveMinuteBefore}; try (ResultSet rs = client.executeQuery(sql, params)) { if (rs.next()) { return rs.getLong(1); @@ -71,8 +69,8 @@ public class InstanceH2DAO extends H2DAO implements IInstanceDAO { long fiveMinuteBefore = System.currentTimeMillis() - 5 * 60 * 1000; fiveMinuteBefore = TimeBucketUtils.INSTANCE.getSecondTimeBucket(fiveMinuteBefore); String sql = SqlBuilder.buildSql(GET_INST_LAST_HEARTBEAT_TIME_SQL, InstanceTable.COLUMN_HEARTBEAT_TIME, InstanceTable.TABLE, - InstanceTable.COLUMN_HEARTBEAT_TIME, InstanceTable.COLUMN_INSTANCE_ID); - Object[] params = new Object[]{fiveMinuteBefore, applicationInstanceId}; + InstanceTable.COLUMN_HEARTBEAT_TIME, InstanceTable.COLUMN_INSTANCE_ID); + Object[] params = new Object[] {fiveMinuteBefore, applicationInstanceId}; try (ResultSet rs = client.executeQuery(sql, params)) { if (rs.next()) { return rs.getLong(1); @@ -88,15 +86,15 @@ public class InstanceH2DAO extends H2DAO implements IInstanceDAO { H2Client client = getClient(); JsonArray applications = new JsonArray(); String sql = SqlBuilder.buildSql(GET_APPLICATIONS_SQL, InstanceTable.COLUMN_INSTANCE_ID, - InstanceTable.TABLE, InstanceTable.COLUMN_HEARTBEAT_TIME, InstanceTable.COLUMN_APPLICATION_ID); - Object[] params = new Object[]{startTime, endTime}; + InstanceTable.TABLE, InstanceTable.COLUMN_HEARTBEAT_TIME, InstanceTable.COLUMN_APPLICATION_ID); + Object[] params = new Object[] {startTime, endTime}; try (ResultSet rs = client.executeQuery(sql, params)) { while (rs.next()) { Integer applicationId = rs.getInt(InstanceTable.COLUMN_APPLICATION_ID); logger.debug("applicationId: {}", applicationId); JsonObject application = new JsonObject(); application.addProperty("applicationId", applicationId); - application.addProperty("applicationCode", ApplicationCache.getForUI(applicationId)); + application.addProperty("applicationCode", ApplicationCache.get(applicationId)); application.addProperty("instanceCount", rs.getInt("cnt")); applications.add(application); } @@ -110,7 +108,7 @@ public class InstanceH2DAO extends H2DAO implements IInstanceDAO { public InstanceDataDefine.Instance getInstance(int instanceId) { H2Client client = getClient(); String sql = SqlBuilder.buildSql(GET_INSTANCE_SQL, InstanceTable.TABLE, InstanceTable.COLUMN_INSTANCE_ID); - Object[] params = new Object[]{instanceId}; + Object[] params = new Object[] {instanceId}; try (ResultSet rs = client.executeQuery(sql, params)) { if (rs.next()) { InstanceDataDefine.Instance instance = new InstanceDataDefine.Instance(); @@ -134,7 +132,7 @@ public class InstanceH2DAO extends H2DAO implements IInstanceDAO { List instanceList = new LinkedList<>(); H2Client client = getClient(); String sql = SqlBuilder.buildSql(GET_INSTANCES_SQL, InstanceTable.TABLE, InstanceTable.COLUMN_APPLICATION_ID, InstanceTable.COLUMN_HEARTBEAT_TIME); - Object[] params = new Object[]{applicationId, timeBucket}; + Object[] params = new Object[] {applicationId, timeBucket}; try (ResultSet rs = client.executeQuery(sql, params)) { while (rs.next()) { InstanceDataDefine.Instance instance = new InstanceDataDefine.Instance(); diff --git a/apm-collector/apm-collector-ui/src/main/java/org/skywalking/apm/collector/ui/dao/NodeComponentH2DAO.java b/apm-collector/apm-collector-ui/src/main/java/org/skywalking/apm/collector/ui/dao/NodeComponentH2DAO.java index 5fd855025d4f59bdcf73e9f4ce62b7892ebba833..54a86ca49b613782ed882616d32a62e0edab921d 100644 --- a/apm-collector/apm-collector-ui/src/main/java/org/skywalking/apm/collector/ui/dao/NodeComponentH2DAO.java +++ b/apm-collector/apm-collector-ui/src/main/java/org/skywalking/apm/collector/ui/dao/NodeComponentH2DAO.java @@ -18,49 +18,49 @@ package org.skywalking.apm.collector.ui.dao; +import com.google.gson.JsonArray; +import com.google.gson.JsonObject; import java.sql.ResultSet; import java.sql.SQLException; - +import org.skywalking.apm.collector.cache.ApplicationCache; import org.skywalking.apm.collector.client.h2.H2Client; import org.skywalking.apm.collector.client.h2.H2ClientException; import org.skywalking.apm.collector.core.util.StringUtils; import org.skywalking.apm.collector.storage.define.node.NodeComponentTable; import org.skywalking.apm.collector.storage.h2.SqlBuilder; import org.skywalking.apm.collector.storage.h2.dao.H2DAO; -import org.skywalking.apm.collector.ui.cache.ApplicationCache; import org.skywalking.apm.network.trace.component.ComponentsDefine; import org.slf4j.Logger; import org.slf4j.LoggerFactory; -import com.google.gson.JsonArray; -import com.google.gson.JsonObject; - /** * @author pengys5, clevertension */ public class NodeComponentH2DAO extends H2DAO implements INodeComponentDAO { private final Logger logger = LoggerFactory.getLogger(NodeComponentH2DAO.class); private static final String AGGREGATE_COMPONENT_SQL = "select {0}, {1}, {2} from {3} where {4} >= ? and {4} <= ? group by {0}, {1}, {2} limit 100"; + @Override public JsonArray load(long startTime, long endTime) { JsonArray nodeComponentArray = new JsonArray(); nodeComponentArray.addAll(aggregationComponent(startTime, endTime)); return nodeComponentArray; } + private JsonArray aggregationComponent(long startTime, long endTime) { H2Client client = getClient(); JsonArray nodeComponentArray = new JsonArray(); String sql = SqlBuilder.buildSql(AGGREGATE_COMPONENT_SQL, NodeComponentTable.COLUMN_COMPONENT_ID, - NodeComponentTable.COLUMN_PEER, NodeComponentTable.COLUMN_PEER_ID, - NodeComponentTable.TABLE, NodeComponentTable.COLUMN_TIME_BUCKET); - Object[] params = new Object[]{startTime, endTime}; + NodeComponentTable.COLUMN_PEER, NodeComponentTable.COLUMN_PEER_ID, + NodeComponentTable.TABLE, NodeComponentTable.COLUMN_TIME_BUCKET); + Object[] params = new Object[] {startTime, endTime}; try (ResultSet rs = client.executeQuery(sql, params)) { while (rs.next()) { int peerId = rs.getInt(NodeComponentTable.COLUMN_PEER_ID); int componentId = rs.getInt(NodeComponentTable.COLUMN_COMPONENT_ID); String componentName = ComponentsDefine.getInstance().getComponentName(componentId); if (peerId != 0) { - String peer = ApplicationCache.getForUI(peerId); + String peer = ApplicationCache.get(peerId); nodeComponentArray.add(buildNodeComponent(peer, componentName)); } String peer = rs.getString(NodeComponentTable.COLUMN_PEER); diff --git a/apm-collector/apm-collector-ui/src/main/java/org/skywalking/apm/collector/ui/dao/NodeMappingH2DAO.java b/apm-collector/apm-collector-ui/src/main/java/org/skywalking/apm/collector/ui/dao/NodeMappingH2DAO.java index 7febe8065a0db42768360514c6eac387675744ee..78ae8cf1c35d3de023fa8314a07ef47f7ce279f7 100644 --- a/apm-collector/apm-collector-ui/src/main/java/org/skywalking/apm/collector/ui/dao/NodeMappingH2DAO.java +++ b/apm-collector/apm-collector-ui/src/main/java/org/skywalking/apm/collector/ui/dao/NodeMappingH2DAO.java @@ -18,43 +18,42 @@ package org.skywalking.apm.collector.ui.dao; +import com.google.gson.JsonArray; +import com.google.gson.JsonObject; import java.sql.ResultSet; import java.sql.SQLException; - +import org.skywalking.apm.collector.cache.ApplicationCache; import org.skywalking.apm.collector.client.h2.H2Client; import org.skywalking.apm.collector.client.h2.H2ClientException; import org.skywalking.apm.collector.core.util.StringUtils; import org.skywalking.apm.collector.storage.define.node.NodeMappingTable; import org.skywalking.apm.collector.storage.h2.SqlBuilder; import org.skywalking.apm.collector.storage.h2.dao.H2DAO; -import org.skywalking.apm.collector.ui.cache.ApplicationCache; import org.slf4j.Logger; import org.slf4j.LoggerFactory; -import com.google.gson.JsonArray; -import com.google.gson.JsonObject; - /** * @author pengys5, clevertension */ public class NodeMappingH2DAO extends H2DAO implements INodeMappingDAO { private final Logger logger = LoggerFactory.getLogger(NodeMappingH2DAO.class); private static final String NODE_MAPPING_SQL = "select {0}, {1}, {2} from {3} where {4} >= ? and {4} <= ? group by {0}, {1}, {2} limit 100"; + @Override public JsonArray load(long startTime, long endTime) { H2Client client = getClient(); JsonArray nodeMappingArray = new JsonArray(); String sql = SqlBuilder.buildSql(NODE_MAPPING_SQL, NodeMappingTable.COLUMN_APPLICATION_ID, - NodeMappingTable.COLUMN_ADDRESS_ID, NodeMappingTable.COLUMN_ADDRESS, - NodeMappingTable.TABLE, NodeMappingTable.COLUMN_TIME_BUCKET); + NodeMappingTable.COLUMN_ADDRESS_ID, NodeMappingTable.COLUMN_ADDRESS, + NodeMappingTable.TABLE, NodeMappingTable.COLUMN_TIME_BUCKET); - Object[] params = new Object[]{startTime, endTime}; + Object[] params = new Object[] {startTime, endTime}; try (ResultSet rs = client.executeQuery(sql, params)) { while (rs.next()) { int applicationId = rs.getInt(NodeMappingTable.COLUMN_APPLICATION_ID); - String applicationCode = ApplicationCache.getForUI(applicationId); + String applicationCode = ApplicationCache.get(applicationId); int addressId = rs.getInt(NodeMappingTable.COLUMN_ADDRESS_ID); if (addressId != 0) { - String address = ApplicationCache.getForUI(addressId); + String address = ApplicationCache.get(addressId); JsonObject nodeMappingObj = new JsonObject(); nodeMappingObj.addProperty("applicationCode", applicationCode); nodeMappingObj.addProperty("address", address); diff --git a/apm-collector/apm-collector-ui/src/main/java/org/skywalking/apm/collector/ui/dao/NodeReferenceH2DAO.java b/apm-collector/apm-collector-ui/src/main/java/org/skywalking/apm/collector/ui/dao/NodeReferenceH2DAO.java index 52cdf9a410fb805243c17c302c1075d9867ad556..007882c44f9467d982e21a76593582dd90c6bc06 100644 --- a/apm-collector/apm-collector-ui/src/main/java/org/skywalking/apm/collector/ui/dao/NodeReferenceH2DAO.java +++ b/apm-collector/apm-collector-ui/src/main/java/org/skywalking/apm/collector/ui/dao/NodeReferenceH2DAO.java @@ -21,13 +21,13 @@ package org.skywalking.apm.collector.ui.dao; import java.sql.ResultSet; import java.sql.SQLException; +import org.skywalking.apm.collector.cache.ApplicationCache; import org.skywalking.apm.collector.client.h2.H2Client; import org.skywalking.apm.collector.client.h2.H2ClientException; import org.skywalking.apm.collector.core.util.StringUtils; import org.skywalking.apm.collector.storage.define.noderef.NodeReferenceTable; import org.skywalking.apm.collector.storage.h2.SqlBuilder; import org.skywalking.apm.collector.storage.h2.dao.H2DAO; -import org.skywalking.apm.collector.ui.cache.ApplicationCache; import org.slf4j.Logger; import org.slf4j.LoggerFactory; @@ -54,10 +54,10 @@ public class NodeReferenceH2DAO extends H2DAO implements INodeReferenceDAO { try (ResultSet rs = client.executeQuery(sql, params)) { while (rs.next()) { int applicationId = rs.getInt(NodeReferenceTable.COLUMN_FRONT_APPLICATION_ID); - String applicationCode = ApplicationCache.getForUI(applicationId); + String applicationCode = ApplicationCache.get(applicationId); int behindApplicationId = rs.getInt(NodeReferenceTable.COLUMN_BEHIND_APPLICATION_ID); if (behindApplicationId != 0) { - String behindApplicationCode = ApplicationCache.getForUI(behindApplicationId); + String behindApplicationCode = ApplicationCache.get(behindApplicationId); JsonObject nodeRefResSumObj = new JsonObject(); nodeRefResSumObj.addProperty("front", applicationCode); diff --git a/apm-collector/apm-collector-ui/src/main/java/org/skywalking/apm/collector/ui/dao/ServiceEntryH2DAO.java b/apm-collector/apm-collector-ui/src/main/java/org/skywalking/apm/collector/ui/dao/ServiceEntryH2DAO.java index dcf71a00a8898159d5fbd617732355ddefee12b4..664b9ca884ba3e73bbaa0fe610257325e4f27b4b 100644 --- a/apm-collector/apm-collector-ui/src/main/java/org/skywalking/apm/collector/ui/dao/ServiceEntryH2DAO.java +++ b/apm-collector/apm-collector-ui/src/main/java/org/skywalking/apm/collector/ui/dao/ServiceEntryH2DAO.java @@ -18,11 +18,13 @@ package org.skywalking.apm.collector.ui.dao; +import com.google.gson.JsonArray; +import com.google.gson.JsonObject; import java.sql.ResultSet; import java.sql.SQLException; import java.util.ArrayList; import java.util.List; - +import org.skywalking.apm.collector.cache.ApplicationCache; import org.skywalking.apm.collector.client.h2.H2Client; import org.skywalking.apm.collector.client.h2.H2ClientException; import org.skywalking.apm.collector.core.util.ColumnNameUtils; @@ -30,19 +32,16 @@ import org.skywalking.apm.collector.core.util.StringUtils; import org.skywalking.apm.collector.storage.define.service.ServiceEntryTable; import org.skywalking.apm.collector.storage.h2.SqlBuilder; import org.skywalking.apm.collector.storage.h2.dao.H2DAO; -import org.skywalking.apm.collector.ui.cache.ApplicationCache; import org.slf4j.Logger; import org.slf4j.LoggerFactory; -import com.google.gson.JsonArray; -import com.google.gson.JsonObject; - /** * @author pengys5 */ public class ServiceEntryH2DAO extends H2DAO implements IServiceEntryDAO { private final Logger logger = LoggerFactory.getLogger(SegmentH2DAO.class); private static final String GET_SERVICE_ENTRY_SQL = "select * from {0} where {1} >= ? and {2} <= ?"; + @Override public JsonObject load(int applicationId, String entryServiceName, long startTime, long endTime, int from, int size) { H2Client client = getClient(); @@ -77,7 +76,7 @@ public class ServiceEntryH2DAO extends H2DAO implements IServiceEntryDAO { while (rs.next()) { int appId = rs.getInt(ServiceEntryTable.COLUMN_APPLICATION_ID); int entryServiceId = rs.getInt(ServiceEntryTable.COLUMN_ENTRY_SERVICE_ID); - String applicationCode = ApplicationCache.getForUI(applicationId); + String applicationCode = ApplicationCache.get(applicationId); String entryServiceName1 = rs.getString(ServiceEntryTable.COLUMN_ENTRY_SERVICE_NAME); JsonObject row = new JsonObject(); diff --git a/apm-collector/apm-collector-ui/src/main/java/org/skywalking/apm/collector/ui/dao/ServiceReferenceEsDAO.java b/apm-collector/apm-collector-ui/src/main/java/org/skywalking/apm/collector/ui/dao/ServiceReferenceEsDAO.java index 75a4b3755ecc96a7f7f3b95c0b3f7ac68c26d08b..4a76bc84891dde17660ca9489e9f74ef52cb0088 100644 --- a/apm-collector/apm-collector-ui/src/main/java/org/skywalking/apm/collector/ui/dao/ServiceReferenceEsDAO.java +++ b/apm-collector/apm-collector-ui/src/main/java/org/skywalking/apm/collector/ui/dao/ServiceReferenceEsDAO.java @@ -18,9 +18,7 @@ package org.skywalking.apm.collector.ui.dao; -import com.google.gson.JsonArray; import com.google.gson.JsonObject; -import java.util.Iterator; import java.util.LinkedHashMap; import java.util.Map; import org.elasticsearch.action.search.SearchRequestBuilder; @@ -35,7 +33,6 @@ import org.skywalking.apm.collector.cache.ServiceIdCache; import org.skywalking.apm.collector.cache.ServiceNameCache; import org.skywalking.apm.collector.core.util.ColumnNameUtils; import org.skywalking.apm.collector.core.util.Const; -import org.skywalking.apm.collector.core.util.ObjectUtils; import org.skywalking.apm.collector.core.util.StringUtils; import org.skywalking.apm.collector.storage.define.serviceref.ServiceReferenceTable; import org.skywalking.apm.collector.storage.elasticsearch.dao.EsDAO; @@ -49,7 +46,8 @@ public class ServiceReferenceEsDAO extends EsDAO implements IServiceReferenceDAO private final Logger logger = LoggerFactory.getLogger(ServiceReferenceEsDAO.class); - @Override public JsonArray load(String entryServiceName, int entryApplicationId, long startTime, long endTime) { + @Override + public Map load(String entryServiceName, int entryApplicationId, long startTime, long endTime) { SearchRequestBuilder searchRequestBuilder = getClient().prepareSearch(ServiceReferenceTable.TABLE); searchRequestBuilder.setTypes(ServiceReferenceTable.TABLE_TYPE); searchRequestBuilder.setSearchType(SearchType.DFS_QUERY_THEN_FETCH); @@ -72,7 +70,7 @@ public class ServiceReferenceEsDAO extends EsDAO implements IServiceReferenceDAO return load(searchRequestBuilder); } - @Override public JsonArray load(int entryServiceId, long startTime, long endTime) { + @Override public Map load(int entryServiceId, long startTime, long endTime) { SearchRequestBuilder searchRequestBuilder = getClient().prepareSearch(ServiceReferenceTable.TABLE); searchRequestBuilder.setTypes(ServiceReferenceTable.TABLE_TYPE); searchRequestBuilder.setSearchType(SearchType.DFS_QUERY_THEN_FETCH); @@ -93,35 +91,9 @@ public class ServiceReferenceEsDAO extends EsDAO implements IServiceReferenceDAO return load(searchRequestBuilder); } - private JsonArray load(SearchRequestBuilder searchRequestBuilder) { + private Map load(SearchRequestBuilder searchRequestBuilder) { searchRequestBuilder.addAggregation(AggregationBuilders.terms(ServiceReferenceTable.COLUMN_FRONT_SERVICE_ID).field(ServiceReferenceTable.COLUMN_FRONT_SERVICE_ID).size(100) .subAggregation(AggregationBuilders.terms(ServiceReferenceTable.COLUMN_BEHIND_SERVICE_ID).field(ServiceReferenceTable.COLUMN_BEHIND_SERVICE_ID).size(100) - .subAggregation(AggregationBuilders.sum(ServiceReferenceTable.COLUMN_S1_LTE).field(ServiceReferenceTable.COLUMN_S1_LTE)) - .subAggregation(AggregationBuilders.sum(ServiceReferenceTable.COLUMN_S3_LTE).field(ServiceReferenceTable.COLUMN_S3_LTE)) - .subAggregation(AggregationBuilders.sum(ServiceReferenceTable.COLUMN_S5_LTE).field(ServiceReferenceTable.COLUMN_S5_LTE)) - .subAggregation(AggregationBuilders.sum(ServiceReferenceTable.COLUMN_S5_GT).field(ServiceReferenceTable.COLUMN_S5_GT)) - .subAggregation(AggregationBuilders.sum(ServiceReferenceTable.COLUMN_ERROR).field(ServiceReferenceTable.COLUMN_ERROR)) - .subAggregation(AggregationBuilders.sum(ServiceReferenceTable.COLUMN_SUMMARY).field(ServiceReferenceTable.COLUMN_SUMMARY)) - .subAggregation(AggregationBuilders.sum(ServiceReferenceTable.COLUMN_COST_SUMMARY).field(ServiceReferenceTable.COLUMN_COST_SUMMARY))) - .subAggregation(AggregationBuilders.terms(ServiceReferenceTable.COLUMN_BEHIND_SERVICE_NAME).field(ServiceReferenceTable.COLUMN_BEHIND_SERVICE_NAME).size(100) - .subAggregation(AggregationBuilders.sum(ServiceReferenceTable.COLUMN_S1_LTE).field(ServiceReferenceTable.COLUMN_S1_LTE)) - .subAggregation(AggregationBuilders.sum(ServiceReferenceTable.COLUMN_S3_LTE).field(ServiceReferenceTable.COLUMN_S3_LTE)) - .subAggregation(AggregationBuilders.sum(ServiceReferenceTable.COLUMN_S5_LTE).field(ServiceReferenceTable.COLUMN_S5_LTE)) - .subAggregation(AggregationBuilders.sum(ServiceReferenceTable.COLUMN_S5_GT).field(ServiceReferenceTable.COLUMN_S5_GT)) - .subAggregation(AggregationBuilders.sum(ServiceReferenceTable.COLUMN_ERROR).field(ServiceReferenceTable.COLUMN_ERROR)) - .subAggregation(AggregationBuilders.sum(ServiceReferenceTable.COLUMN_SUMMARY).field(ServiceReferenceTable.COLUMN_SUMMARY)) - .subAggregation(AggregationBuilders.sum(ServiceReferenceTable.COLUMN_COST_SUMMARY).field(ServiceReferenceTable.COLUMN_COST_SUMMARY)))); - - searchRequestBuilder.addAggregation(AggregationBuilders.terms(ServiceReferenceTable.COLUMN_FRONT_SERVICE_NAME).field(ServiceReferenceTable.COLUMN_FRONT_SERVICE_NAME).size(100) - .subAggregation(AggregationBuilders.terms(ServiceReferenceTable.COLUMN_BEHIND_SERVICE_ID).field(ServiceReferenceTable.COLUMN_BEHIND_SERVICE_ID).size(100) - .subAggregation(AggregationBuilders.sum(ServiceReferenceTable.COLUMN_S1_LTE).field(ServiceReferenceTable.COLUMN_S1_LTE)) - .subAggregation(AggregationBuilders.sum(ServiceReferenceTable.COLUMN_S3_LTE).field(ServiceReferenceTable.COLUMN_S3_LTE)) - .subAggregation(AggregationBuilders.sum(ServiceReferenceTable.COLUMN_S5_LTE).field(ServiceReferenceTable.COLUMN_S5_LTE)) - .subAggregation(AggregationBuilders.sum(ServiceReferenceTable.COLUMN_S5_GT).field(ServiceReferenceTable.COLUMN_S5_GT)) - .subAggregation(AggregationBuilders.sum(ServiceReferenceTable.COLUMN_ERROR).field(ServiceReferenceTable.COLUMN_ERROR)) - .subAggregation(AggregationBuilders.sum(ServiceReferenceTable.COLUMN_SUMMARY).field(ServiceReferenceTable.COLUMN_SUMMARY)) - .subAggregation(AggregationBuilders.sum(ServiceReferenceTable.COLUMN_COST_SUMMARY).field(ServiceReferenceTable.COLUMN_COST_SUMMARY))) - .subAggregation(AggregationBuilders.terms(ServiceReferenceTable.COLUMN_BEHIND_SERVICE_NAME).field(ServiceReferenceTable.COLUMN_BEHIND_SERVICE_NAME).size(100) .subAggregation(AggregationBuilders.sum(ServiceReferenceTable.COLUMN_S1_LTE).field(ServiceReferenceTable.COLUMN_S1_LTE)) .subAggregation(AggregationBuilders.sum(ServiceReferenceTable.COLUMN_S3_LTE).field(ServiceReferenceTable.COLUMN_S3_LTE)) .subAggregation(AggregationBuilders.sum(ServiceReferenceTable.COLUMN_S5_LTE).field(ServiceReferenceTable.COLUMN_S5_LTE)) @@ -141,27 +113,7 @@ public class ServiceReferenceEsDAO extends EsDAO implements IServiceReferenceDAO } } - Terms frontServiceNameTerms = searchResponse.getAggregations().get(ServiceReferenceTable.COLUMN_FRONT_SERVICE_NAME); - for (Terms.Bucket frontServiceBucket : frontServiceNameTerms.getBuckets()) { - String frontServiceName = frontServiceBucket.getKeyAsString(); - if (StringUtils.isNotEmpty(frontServiceName)) { - String[] serviceNames = frontServiceName.split(Const.ID_SPLIT); - int frontServiceId = ServiceIdCache.get(Integer.parseInt(serviceNames[0]), serviceNames[1]); - parseSubAggregate(serviceReferenceMap, frontServiceBucket, frontServiceId); - } - } - - JsonArray serviceReferenceArray = new JsonArray(); - JsonObject rootServiceReference = findRoot(serviceReferenceMap); - if (ObjectUtils.isNotEmpty(rootServiceReference)) { - serviceReferenceArray.add(rootServiceReference); - String id = rootServiceReference.get(ColumnNameUtils.INSTANCE.rename(ServiceReferenceTable.COLUMN_FRONT_SERVICE_ID)) + Const.ID_SPLIT + rootServiceReference.get(ColumnNameUtils.INSTANCE.rename(ServiceReferenceTable.COLUMN_BEHIND_SERVICE_ID)); - serviceReferenceMap.remove(id); - - int rootServiceId = rootServiceReference.get(ColumnNameUtils.INSTANCE.rename(ServiceReferenceTable.COLUMN_BEHIND_SERVICE_ID)).getAsInt(); - sortAsTree(rootServiceId, serviceReferenceArray, serviceReferenceMap); - } - return serviceReferenceArray; + return serviceReferenceMap; } private void parseSubAggregate(Map serviceReferenceMap, @@ -200,88 +152,9 @@ public class ServiceReferenceEsDAO extends EsDAO implements IServiceReferenceDAO serviceReference.addProperty(ColumnNameUtils.INSTANCE.rename(ServiceReferenceTable.COLUMN_ERROR), (long)error.getValue()); serviceReference.addProperty(ColumnNameUtils.INSTANCE.rename(ServiceReferenceTable.COLUMN_SUMMARY), (long)summary.getValue()); serviceReference.addProperty(ColumnNameUtils.INSTANCE.rename(ServiceReferenceTable.COLUMN_COST_SUMMARY), (long)costSum.getValue()); - merge(serviceReferenceMap, serviceReference); - } - } - - Terms behindServiceNameTerms = frontServiceBucket.getAggregations().get(ServiceReferenceTable.COLUMN_BEHIND_SERVICE_NAME); - for (Terms.Bucket behindServiceNameBucket : behindServiceNameTerms.getBuckets()) { - String behindServiceName = behindServiceNameBucket.getKeyAsString(); - if (StringUtils.isNotEmpty(behindServiceName)) { - Sum s1LteSum = behindServiceNameBucket.getAggregations().get(ServiceReferenceTable.COLUMN_S1_LTE); - Sum s3LteSum = behindServiceNameBucket.getAggregations().get(ServiceReferenceTable.COLUMN_S3_LTE); - Sum s5LteSum = behindServiceNameBucket.getAggregations().get(ServiceReferenceTable.COLUMN_S5_LTE); - Sum s5GtSum = behindServiceNameBucket.getAggregations().get(ServiceReferenceTable.COLUMN_S5_GT); - Sum error = behindServiceNameBucket.getAggregations().get(ServiceReferenceTable.COLUMN_ERROR); - Sum summary = behindServiceNameBucket.getAggregations().get(ServiceReferenceTable.COLUMN_SUMMARY); - Sum costSum = behindServiceNameBucket.getAggregations().get(ServiceReferenceTable.COLUMN_COST_SUMMARY); - - String frontServiceName = ServiceNameCache.get(frontServiceId); - String[] serviceNames = behindServiceName.split(Const.ID_SPLIT); - int behindServiceId = ServiceIdCache.get(Integer.parseInt(serviceNames[0]), serviceNames[1]); - behindServiceName = serviceNames[1]; - - JsonObject serviceReference = new JsonObject(); - serviceReference.addProperty(ColumnNameUtils.INSTANCE.rename(ServiceReferenceTable.COLUMN_FRONT_SERVICE_ID), frontServiceId); - serviceReference.addProperty(ColumnNameUtils.INSTANCE.rename(ServiceReferenceTable.COLUMN_FRONT_SERVICE_NAME), frontServiceName); - serviceReference.addProperty(ColumnNameUtils.INSTANCE.rename(ServiceReferenceTable.COLUMN_BEHIND_SERVICE_ID), behindServiceId); - serviceReference.addProperty(ColumnNameUtils.INSTANCE.rename(ServiceReferenceTable.COLUMN_BEHIND_SERVICE_NAME), behindServiceName); - serviceReference.addProperty(ColumnNameUtils.INSTANCE.rename(ServiceReferenceTable.COLUMN_S1_LTE), (long)s1LteSum.getValue()); - serviceReference.addProperty(ColumnNameUtils.INSTANCE.rename(ServiceReferenceTable.COLUMN_S3_LTE), (long)s3LteSum.getValue()); - serviceReference.addProperty(ColumnNameUtils.INSTANCE.rename(ServiceReferenceTable.COLUMN_S5_LTE), (long)s5LteSum.getValue()); - serviceReference.addProperty(ColumnNameUtils.INSTANCE.rename(ServiceReferenceTable.COLUMN_S5_GT), (long)s5GtSum.getValue()); - serviceReference.addProperty(ColumnNameUtils.INSTANCE.rename(ServiceReferenceTable.COLUMN_ERROR), (long)error.getValue()); - serviceReference.addProperty(ColumnNameUtils.INSTANCE.rename(ServiceReferenceTable.COLUMN_SUMMARY), (long)summary.getValue()); - serviceReference.addProperty(ColumnNameUtils.INSTANCE.rename(ServiceReferenceTable.COLUMN_COST_SUMMARY), (long)costSum.getValue()); - merge(serviceReferenceMap, serviceReference); - } - } - } - - private void merge(Map serviceReferenceMap, JsonObject serviceReference) { - String id = serviceReference.get(ColumnNameUtils.INSTANCE.rename(ServiceReferenceTable.COLUMN_FRONT_SERVICE_ID)) + Const.ID_SPLIT + serviceReference.get(ColumnNameUtils.INSTANCE.rename(ServiceReferenceTable.COLUMN_BEHIND_SERVICE_ID)); - - if (serviceReferenceMap.containsKey(id)) { - JsonObject reference = serviceReferenceMap.get(id); - add(reference, serviceReference, ColumnNameUtils.INSTANCE.rename(ServiceReferenceTable.COLUMN_S1_LTE)); - add(reference, serviceReference, ColumnNameUtils.INSTANCE.rename(ServiceReferenceTable.COLUMN_S3_LTE)); - add(reference, serviceReference, ColumnNameUtils.INSTANCE.rename(ServiceReferenceTable.COLUMN_S5_LTE)); - add(reference, serviceReference, ColumnNameUtils.INSTANCE.rename(ServiceReferenceTable.COLUMN_S5_GT)); - add(reference, serviceReference, ColumnNameUtils.INSTANCE.rename(ServiceReferenceTable.COLUMN_ERROR)); - add(reference, serviceReference, ColumnNameUtils.INSTANCE.rename(ServiceReferenceTable.COLUMN_SUMMARY)); - add(reference, serviceReference, ColumnNameUtils.INSTANCE.rename(ServiceReferenceTable.COLUMN_COST_SUMMARY)); - } else { - serviceReferenceMap.put(id, serviceReference); - } - } - - private void add(JsonObject oldReference, JsonObject newReference, String key) { - long oldValue = oldReference.get(key).getAsLong(); - long newValue = newReference.get(key).getAsLong(); - oldReference.addProperty(key, oldValue + newValue); - } - - private JsonObject findRoot(Map serviceReferenceMap) { - for (JsonObject serviceReference : serviceReferenceMap.values()) { - int frontServiceId = serviceReference.get(ColumnNameUtils.INSTANCE.rename(ServiceReferenceTable.COLUMN_FRONT_SERVICE_ID)).getAsInt(); - if (frontServiceId == 1) { - return serviceReference; - } - } - return null; - } - - private void sortAsTree(int serviceId, JsonArray serviceReferenceArray, - Map serviceReferenceMap) { - Iterator iterator = serviceReferenceMap.values().iterator(); - while (iterator.hasNext()) { - JsonObject serviceReference = iterator.next(); - int frontServiceId = serviceReference.get(ColumnNameUtils.INSTANCE.rename(ServiceReferenceTable.COLUMN_FRONT_SERVICE_ID)).getAsInt(); - if (serviceId == frontServiceId) { - serviceReferenceArray.add(serviceReference); - int behindServiceId = serviceReference.get(ColumnNameUtils.INSTANCE.rename(ServiceReferenceTable.COLUMN_BEHIND_SERVICE_ID)).getAsInt(); - sortAsTree(behindServiceId, serviceReferenceArray, serviceReferenceMap); + String id = serviceReference.get(ColumnNameUtils.INSTANCE.rename(ServiceReferenceTable.COLUMN_FRONT_SERVICE_ID)) + Const.ID_SPLIT + serviceReference.get(ColumnNameUtils.INSTANCE.rename(ServiceReferenceTable.COLUMN_BEHIND_SERVICE_ID)); + serviceReferenceMap.put(id, serviceReference); } } } diff --git a/apm-collector/apm-collector-ui/src/main/java/org/skywalking/apm/collector/ui/dao/ServiceReferenceH2DAO.java b/apm-collector/apm-collector-ui/src/main/java/org/skywalking/apm/collector/ui/dao/ServiceReferenceH2DAO.java index 37a818e0a12401898283efb2f7f75dc4a0ececa0..24b224594a30194bf1a61c681a7f2dca04702d47 100644 --- a/apm-collector/apm-collector-ui/src/main/java/org/skywalking/apm/collector/ui/dao/ServiceReferenceH2DAO.java +++ b/apm-collector/apm-collector-ui/src/main/java/org/skywalking/apm/collector/ui/dao/ServiceReferenceH2DAO.java @@ -18,29 +18,24 @@ package org.skywalking.apm.collector.ui.dao; +import com.google.gson.JsonObject; import java.sql.ResultSet; import java.sql.SQLException; -import java.util.Iterator; import java.util.LinkedHashMap; import java.util.Map; - +import org.skywalking.apm.collector.cache.ServiceIdCache; +import org.skywalking.apm.collector.cache.ServiceNameCache; import org.skywalking.apm.collector.client.h2.H2Client; import org.skywalking.apm.collector.client.h2.H2ClientException; import org.skywalking.apm.collector.core.util.ColumnNameUtils; import org.skywalking.apm.collector.core.util.Const; -import org.skywalking.apm.collector.core.util.ObjectUtils; import org.skywalking.apm.collector.core.util.StringUtils; import org.skywalking.apm.collector.storage.define.serviceref.ServiceReferenceTable; import org.skywalking.apm.collector.storage.h2.SqlBuilder; import org.skywalking.apm.collector.storage.h2.dao.H2DAO; -import org.skywalking.apm.collector.ui.cache.ServiceIdCache; -import org.skywalking.apm.collector.ui.cache.ServiceNameCache; import org.slf4j.Logger; import org.slf4j.LoggerFactory; -import com.google.gson.JsonArray; -import com.google.gson.JsonObject; - /** * @author pengys5, clevertension */ @@ -48,115 +43,69 @@ public class ServiceReferenceH2DAO extends H2DAO implements IServiceReferenceDAO private final Logger logger = LoggerFactory.getLogger(ServiceReferenceH2DAO.class); private static final String GET_SRV_REF_LOAD1 = "select {4}, {5}, {6}, {7}, sum({8}) as cnt1, sum({9}) as cnt2, sum({10}) as cnt3" + - ",sum({11}) as cnt4, sum({12}) cnt5, sum({13}) as cnt6, sum({14}) as cnt7 from {0} where {1} >= ? and {1} <= ? and {2} = ? and {3} = ? group by {4}, {5}, {6}, {7}"; + ",sum({11}) as cnt4, sum({12}) cnt5, sum({13}) as cnt6, sum({14}) as cnt7 from {0} where {1} >= ? and {1} <= ? and {2} = ? and {3} = ? group by {4}, {5}, {6}, {7}"; private static final String GET_SRV_REF_LOAD2 = "select {3}, {4}, {5}, {6}, sum({7}) as cnt1, sum({8}) as cnt2, sum({9}) as cnt3" + - ",sum({10}) as cnt4, sum({11}) cnt5, sum({12}) as cnt6, sum({13}) as cnt7 from {0} where {1} >= ? and {1} <= ? and {2} = ? group by {3}, {4}, {5}, {6}"; + ",sum({10}) as cnt4, sum({11}) cnt5, sum({12}) as cnt6, sum({13}) as cnt7 from {0} where {1} >= ? and {1} <= ? and {2} = ? group by {3}, {4}, {5}, {6}"; - @Override public JsonArray load(int entryServiceId, long startTime, long endTime) { + @Override + public Map load(String entryServiceName, int entryApplicationId, long startTime, long endTime) { H2Client client = getClient(); - String sql = SqlBuilder.buildSql(GET_SRV_REF_LOAD1, ServiceReferenceTable.TABLE, + String sql = SqlBuilder.buildSql(GET_SRV_REF_LOAD2, ServiceReferenceTable.TABLE, + ServiceReferenceTable.COLUMN_TIME_BUCKET, ServiceReferenceTable.COLUMN_ENTRY_SERVICE_NAME, + ServiceReferenceTable.COLUMN_FRONT_SERVICE_ID, ServiceReferenceTable.COLUMN_BEHIND_SERVICE_ID, + ServiceReferenceTable.COLUMN_FRONT_SERVICE_NAME, ServiceReferenceTable.COLUMN_BEHIND_SERVICE_NAME, + ServiceReferenceTable.COLUMN_S1_LTE, ServiceReferenceTable.COLUMN_S3_LTE, ServiceReferenceTable.COLUMN_S5_LTE, + ServiceReferenceTable.COLUMN_S5_GT, ServiceReferenceTable.COLUMN_ERROR, ServiceReferenceTable.COLUMN_SUMMARY, + ServiceReferenceTable.COLUMN_COST_SUMMARY); + + Object[] params = new Object[] {startTime, endTime, entryServiceName}; + entryServiceName = entryApplicationId + Const.ID_SPLIT + entryServiceName; + int entryServiceId = ServiceIdCache.get(entryApplicationId, entryServiceName); + if (entryServiceId != 0) { + sql = SqlBuilder.buildSql(GET_SRV_REF_LOAD1, ServiceReferenceTable.TABLE, ServiceReferenceTable.COLUMN_TIME_BUCKET, ServiceReferenceTable.COLUMN_ENTRY_SERVICE_ID, ServiceReferenceTable.COLUMN_ENTRY_SERVICE_NAME, ServiceReferenceTable.COLUMN_FRONT_SERVICE_ID, ServiceReferenceTable.COLUMN_BEHIND_SERVICE_ID, ServiceReferenceTable.COLUMN_FRONT_SERVICE_NAME, ServiceReferenceTable.COLUMN_BEHIND_SERVICE_NAME, ServiceReferenceTable.COLUMN_S1_LTE, ServiceReferenceTable.COLUMN_S3_LTE, ServiceReferenceTable.COLUMN_S5_LTE, ServiceReferenceTable.COLUMN_S5_GT, ServiceReferenceTable.COLUMN_ERROR, ServiceReferenceTable.COLUMN_SUMMARY, ServiceReferenceTable.COLUMN_COST_SUMMARY); - String entryServiceName = ServiceNameCache.get(entryServiceId); - Object[] params = new Object[]{startTime, endTime, entryServiceId, entryServiceName}; - try (ResultSet rs = client.executeQuery(sql, params)) { - if (rs.next()) { - Map serviceReferenceMap = new LinkedHashMap<>(); - - int frontServiceId = rs.getInt(ServiceReferenceTable.COLUMN_FRONT_SERVICE_ID); - if (frontServiceId != 0) { - parseSubAggregate(serviceReferenceMap, rs, frontServiceId); - } + params = new Object[] {startTime, endTime, entryServiceId, entryServiceName}; + } - String frontServiceName = rs.getString(ServiceReferenceTable.COLUMN_FRONT_SERVICE_NAME); - if (StringUtils.isNotEmpty(frontServiceName)) { - String[] serviceNames = frontServiceName.split(Const.ID_SPLIT); - int frontServiceId1 = ServiceIdCache.getForUI(Integer.parseInt(serviceNames[0]), serviceNames[1]); - parseSubAggregate(serviceReferenceMap, rs, frontServiceId1); - } + return load(client, params, sql); + } - JsonArray serviceReferenceArray = new JsonArray(); - JsonObject rootServiceReference = findRoot(serviceReferenceMap); - if (ObjectUtils.isNotEmpty(rootServiceReference)) { - serviceReferenceArray.add(rootServiceReference); - String id = rootServiceReference.get(ColumnNameUtils.INSTANCE.rename(ServiceReferenceTable.COLUMN_FRONT_SERVICE_ID)) + Const.ID_SPLIT + rootServiceReference.get(ColumnNameUtils.INSTANCE.rename(ServiceReferenceTable.COLUMN_BEHIND_SERVICE_ID)); - serviceReferenceMap.remove(id); + @Override public Map load(int entryServiceId, long startTime, long endTime) { + H2Client client = getClient(); + String sql = SqlBuilder.buildSql(GET_SRV_REF_LOAD1, ServiceReferenceTable.TABLE, + ServiceReferenceTable.COLUMN_TIME_BUCKET, ServiceReferenceTable.COLUMN_ENTRY_SERVICE_ID, ServiceReferenceTable.COLUMN_ENTRY_SERVICE_NAME, + ServiceReferenceTable.COLUMN_FRONT_SERVICE_ID, ServiceReferenceTable.COLUMN_BEHIND_SERVICE_ID, + ServiceReferenceTable.COLUMN_FRONT_SERVICE_NAME, ServiceReferenceTable.COLUMN_BEHIND_SERVICE_NAME, + ServiceReferenceTable.COLUMN_S1_LTE, ServiceReferenceTable.COLUMN_S3_LTE, ServiceReferenceTable.COLUMN_S5_LTE, + ServiceReferenceTable.COLUMN_S5_GT, ServiceReferenceTable.COLUMN_ERROR, ServiceReferenceTable.COLUMN_SUMMARY, + ServiceReferenceTable.COLUMN_COST_SUMMARY); + String entryServiceName = ServiceNameCache.get(entryServiceId); + Object[] params = new Object[] {startTime, endTime, entryServiceId, entryServiceName}; - int rootServiceId = rootServiceReference.get(ColumnNameUtils.INSTANCE.rename(ServiceReferenceTable.COLUMN_BEHIND_SERVICE_ID)).getAsInt(); - sortAsTree(rootServiceId, serviceReferenceArray, serviceReferenceMap); - } - return serviceReferenceArray; - } - } catch (SQLException | H2ClientException e) { - logger.error(e.getMessage(), e); - } - return null; + return load(client, params, sql); } - @Override public JsonArray load(String entryServiceName, int entryApplicationId, long startTime, long endTime) { - H2Client client = getClient(); - String sql = SqlBuilder.buildSql(GET_SRV_REF_LOAD2, ServiceReferenceTable.TABLE, - ServiceReferenceTable.COLUMN_TIME_BUCKET, ServiceReferenceTable.COLUMN_ENTRY_SERVICE_NAME, - ServiceReferenceTable.COLUMN_FRONT_SERVICE_ID, ServiceReferenceTable.COLUMN_BEHIND_SERVICE_ID, - ServiceReferenceTable.COLUMN_FRONT_SERVICE_NAME, ServiceReferenceTable.COLUMN_BEHIND_SERVICE_NAME, - ServiceReferenceTable.COLUMN_S1_LTE, ServiceReferenceTable.COLUMN_S3_LTE, ServiceReferenceTable.COLUMN_S5_LTE, - ServiceReferenceTable.COLUMN_S5_GT, ServiceReferenceTable.COLUMN_ERROR, ServiceReferenceTable.COLUMN_SUMMARY, - ServiceReferenceTable.COLUMN_COST_SUMMARY); - entryServiceName = entryApplicationId + Const.ID_SPLIT + entryServiceName; - Object[] params = new Object[]{startTime, endTime, entryServiceName}; - int entryServiceId = ServiceIdCache.get(entryApplicationId, entryServiceName); - if (entryServiceId != 0) { - sql = SqlBuilder.buildSql(GET_SRV_REF_LOAD1, ServiceReferenceTable.TABLE, - ServiceReferenceTable.COLUMN_TIME_BUCKET, ServiceReferenceTable.COLUMN_ENTRY_SERVICE_ID, ServiceReferenceTable.COLUMN_ENTRY_SERVICE_NAME, - ServiceReferenceTable.COLUMN_FRONT_SERVICE_ID, ServiceReferenceTable.COLUMN_BEHIND_SERVICE_ID, - ServiceReferenceTable.COLUMN_FRONT_SERVICE_NAME, ServiceReferenceTable.COLUMN_BEHIND_SERVICE_NAME, - ServiceReferenceTable.COLUMN_S1_LTE, ServiceReferenceTable.COLUMN_S3_LTE, ServiceReferenceTable.COLUMN_S5_LTE, - ServiceReferenceTable.COLUMN_S5_GT, ServiceReferenceTable.COLUMN_ERROR, ServiceReferenceTable.COLUMN_SUMMARY, - ServiceReferenceTable.COLUMN_COST_SUMMARY); - params = new Object[]{startTime, endTime, entryServiceId, entryServiceName}; - } + private Map load(H2Client client, Object[] params, String sql) { + Map serviceReferenceMap = new LinkedHashMap<>(); try (ResultSet rs = client.executeQuery(sql, params)) { if (rs.next()) { - Map serviceReferenceMap = new LinkedHashMap<>(); - int frontServiceId = rs.getInt(ServiceReferenceTable.COLUMN_FRONT_SERVICE_ID); - if (frontServiceId != 0) { - parseSubAggregate(serviceReferenceMap, rs, frontServiceId); - } - - String frontServiceName = rs.getString(ServiceReferenceTable.COLUMN_FRONT_SERVICE_NAME); - if (StringUtils.isNotEmpty(frontServiceName)) { - String[] serviceNames = frontServiceName.split(Const.ID_SPLIT); - int frontServiceId1 = ServiceIdCache.getForUI(Integer.parseInt(serviceNames[0]), serviceNames[1]); - parseSubAggregate(serviceReferenceMap, rs, frontServiceId1); - } - - JsonArray serviceReferenceArray = new JsonArray(); - JsonObject rootServiceReference = findRoot(serviceReferenceMap); - if (ObjectUtils.isNotEmpty(rootServiceReference)) { - serviceReferenceArray.add(rootServiceReference); - String id = rootServiceReference.get(ColumnNameUtils.INSTANCE.rename(ServiceReferenceTable.COLUMN_FRONT_SERVICE_ID)) + Const.ID_SPLIT + rootServiceReference.get(ColumnNameUtils.INSTANCE.rename(ServiceReferenceTable.COLUMN_BEHIND_SERVICE_ID)); - serviceReferenceMap.remove(id); - - int rootServiceId = rootServiceReference.get(ColumnNameUtils.INSTANCE.rename(ServiceReferenceTable.COLUMN_BEHIND_SERVICE_ID)).getAsInt(); - sortAsTree(rootServiceId, serviceReferenceArray, serviceReferenceMap); - } - return serviceReferenceArray; + parseSubAggregate(serviceReferenceMap, rs, frontServiceId); } } catch (SQLException | H2ClientException e) { logger.error(e.getMessage(), e); } - return null; + return serviceReferenceMap; } - private void parseSubAggregate(Map serviceReferenceMap, - ResultSet rs, - int frontServiceId) { + private void parseSubAggregate(Map serviceReferenceMap, ResultSet rs, + int frontServiceId) { try { int behindServiceId = rs.getInt(ServiceReferenceTable.COLUMN_BEHIND_SERVICE_ID); if (behindServiceId != 0) { @@ -168,11 +117,11 @@ public class ServiceReferenceH2DAO extends H2DAO implements IServiceReferenceDAO long summary = rs.getLong("cnt3"); long costSum = rs.getLong("cnt3"); - String frontServiceName = ServiceNameCache.getForUI(frontServiceId); + String frontServiceName = ServiceNameCache.get(frontServiceId); if (StringUtils.isNotEmpty(frontServiceName)) { frontServiceName = frontServiceName.split(Const.ID_SPLIT)[1]; } - String behindServiceName = ServiceNameCache.getForUI(behindServiceId); + String behindServiceName = ServiceNameCache.get(behindServiceId); if (StringUtils.isNotEmpty(frontServiceName)) { behindServiceName = behindServiceName.split(Const.ID_SPLIT)[1]; } @@ -189,92 +138,12 @@ public class ServiceReferenceH2DAO extends H2DAO implements IServiceReferenceDAO serviceReference.addProperty(ColumnNameUtils.INSTANCE.rename(ServiceReferenceTable.COLUMN_ERROR), error); serviceReference.addProperty(ColumnNameUtils.INSTANCE.rename(ServiceReferenceTable.COLUMN_SUMMARY), summary); serviceReference.addProperty(ColumnNameUtils.INSTANCE.rename(ServiceReferenceTable.COLUMN_COST_SUMMARY), costSum); - merge(serviceReferenceMap, serviceReference); - } - } catch (SQLException e) { - logger.error(e.getMessage(), e); - } - - try { - String behindServiceName = rs.getString(ServiceReferenceTable.COLUMN_BEHIND_SERVICE_NAME); - if (StringUtils.isNotEmpty(behindServiceName)) { - long s1LteSum = rs.getLong("cnt1"); - long s3LteSum = rs.getLong("cnt2"); - long s5LteSum = rs.getLong("cnt3"); - long s5GtSum = rs.getLong("cnt3"); - long error = rs.getLong("cnt3"); - long summary = rs.getLong("cnt3"); - long costSum = rs.getLong("cnt3"); - - String frontServiceName = ServiceNameCache.getForUI(frontServiceId); - String[] serviceNames = behindServiceName.split(Const.ID_SPLIT); - int behindServiceId = ServiceIdCache.getForUI(Integer.parseInt(serviceNames[0]), serviceNames[1]); - behindServiceName = serviceNames[1]; - JsonObject serviceReference = new JsonObject(); - serviceReference.addProperty(ColumnNameUtils.INSTANCE.rename(ServiceReferenceTable.COLUMN_FRONT_SERVICE_ID), frontServiceId); - serviceReference.addProperty(ColumnNameUtils.INSTANCE.rename(ServiceReferenceTable.COLUMN_FRONT_SERVICE_NAME), frontServiceName); - serviceReference.addProperty(ColumnNameUtils.INSTANCE.rename(ServiceReferenceTable.COLUMN_BEHIND_SERVICE_ID), behindServiceId); - serviceReference.addProperty(ColumnNameUtils.INSTANCE.rename(ServiceReferenceTable.COLUMN_BEHIND_SERVICE_NAME), behindServiceName); - serviceReference.addProperty(ColumnNameUtils.INSTANCE.rename(ServiceReferenceTable.COLUMN_S1_LTE), s1LteSum); - serviceReference.addProperty(ColumnNameUtils.INSTANCE.rename(ServiceReferenceTable.COLUMN_S3_LTE), s3LteSum); - serviceReference.addProperty(ColumnNameUtils.INSTANCE.rename(ServiceReferenceTable.COLUMN_S5_LTE), s5LteSum); - serviceReference.addProperty(ColumnNameUtils.INSTANCE.rename(ServiceReferenceTable.COLUMN_S5_GT), s5GtSum); - serviceReference.addProperty(ColumnNameUtils.INSTANCE.rename(ServiceReferenceTable.COLUMN_ERROR), error); - serviceReference.addProperty(ColumnNameUtils.INSTANCE.rename(ServiceReferenceTable.COLUMN_SUMMARY), summary); - serviceReference.addProperty(ColumnNameUtils.INSTANCE.rename(ServiceReferenceTable.COLUMN_COST_SUMMARY), costSum); - merge(serviceReferenceMap, serviceReference); + String id = serviceReference.get(ColumnNameUtils.INSTANCE.rename(ServiceReferenceTable.COLUMN_FRONT_SERVICE_ID)) + Const.ID_SPLIT + serviceReference.get(ColumnNameUtils.INSTANCE.rename(ServiceReferenceTable.COLUMN_BEHIND_SERVICE_ID)); + serviceReferenceMap.put(id, serviceReference); } } catch (SQLException e) { logger.error(e.getMessage(), e); } } - - private void merge(Map serviceReferenceMap, JsonObject serviceReference) { - String id = serviceReference.get(ColumnNameUtils.INSTANCE.rename(ServiceReferenceTable.COLUMN_FRONT_SERVICE_ID)) + Const.ID_SPLIT + serviceReference.get(ColumnNameUtils.INSTANCE.rename(ServiceReferenceTable.COLUMN_BEHIND_SERVICE_ID)); - - if (serviceReferenceMap.containsKey(id)) { - JsonObject reference = serviceReferenceMap.get(id); - add(reference, serviceReference, ColumnNameUtils.INSTANCE.rename(ServiceReferenceTable.COLUMN_S1_LTE)); - add(reference, serviceReference, ColumnNameUtils.INSTANCE.rename(ServiceReferenceTable.COLUMN_S3_LTE)); - add(reference, serviceReference, ColumnNameUtils.INSTANCE.rename(ServiceReferenceTable.COLUMN_S5_LTE)); - add(reference, serviceReference, ColumnNameUtils.INSTANCE.rename(ServiceReferenceTable.COLUMN_S5_GT)); - add(reference, serviceReference, ColumnNameUtils.INSTANCE.rename(ServiceReferenceTable.COLUMN_ERROR)); - add(reference, serviceReference, ColumnNameUtils.INSTANCE.rename(ServiceReferenceTable.COLUMN_SUMMARY)); - add(reference, serviceReference, ColumnNameUtils.INSTANCE.rename(ServiceReferenceTable.COLUMN_COST_SUMMARY)); - } else { - serviceReferenceMap.put(id, serviceReference); - } - } - - private void add(JsonObject oldReference, JsonObject newReference, String key) { - long oldValue = oldReference.get(key).getAsLong(); - long newValue = newReference.get(key).getAsLong(); - oldReference.addProperty(key, oldValue + newValue); - } - - private JsonObject findRoot(Map serviceReferenceMap) { - for (JsonObject serviceReference : serviceReferenceMap.values()) { - int frontServiceId = serviceReference.get(ColumnNameUtils.INSTANCE.rename(ServiceReferenceTable.COLUMN_FRONT_SERVICE_ID)).getAsInt(); - if (frontServiceId == 1) { - return serviceReference; - } - } - return null; - } - - private void sortAsTree(int serviceId, JsonArray serviceReferenceArray, - Map serviceReferenceMap) { - Iterator iterator = serviceReferenceMap.values().iterator(); - while (iterator.hasNext()) { - JsonObject serviceReference = iterator.next(); - int frontServiceId = serviceReference.get(ColumnNameUtils.INSTANCE.rename(ServiceReferenceTable.COLUMN_FRONT_SERVICE_ID)).getAsInt(); - if (serviceId == frontServiceId) { - serviceReferenceArray.add(serviceReference); - - int behindServiceId = serviceReference.get(ColumnNameUtils.INSTANCE.rename(ServiceReferenceTable.COLUMN_BEHIND_SERVICE_ID)).getAsInt(); - sortAsTree(behindServiceId, serviceReferenceArray, serviceReferenceMap); - } - } - } } diff --git a/apm-collector/apm-collector-ui/src/main/java/org/skywalking/apm/collector/ui/service/ServiceTreeService.java b/apm-collector/apm-collector-ui/src/main/java/org/skywalking/apm/collector/ui/service/ServiceTreeService.java index 53c09692a97297e37dae90b77298f3407cc4aeb5..5ca0938d47ad9a4ee54b6020fb3920aba5b56049 100644 --- a/apm-collector/apm-collector-ui/src/main/java/org/skywalking/apm/collector/ui/service/ServiceTreeService.java +++ b/apm-collector/apm-collector-ui/src/main/java/org/skywalking/apm/collector/ui/service/ServiceTreeService.java @@ -20,7 +20,13 @@ package org.skywalking.apm.collector.ui.service; import com.google.gson.JsonArray; import com.google.gson.JsonObject; +import java.util.Iterator; +import java.util.Map; +import org.skywalking.apm.collector.core.util.ColumnNameUtils; +import org.skywalking.apm.collector.core.util.Const; +import org.skywalking.apm.collector.core.util.ObjectUtils; import org.skywalking.apm.collector.storage.dao.DAOContainer; +import org.skywalking.apm.collector.storage.define.serviceref.ServiceReferenceTable; import org.skywalking.apm.collector.ui.dao.IServiceEntryDAO; import org.skywalking.apm.collector.ui.dao.IServiceReferenceDAO; @@ -37,11 +43,76 @@ public class ServiceTreeService { public JsonArray loadServiceTree(int entryServiceId, long startTime, long endTime) { IServiceReferenceDAO serviceReferenceDAO = (IServiceReferenceDAO)DAOContainer.INSTANCE.get(IServiceReferenceDAO.class.getName()); - return serviceReferenceDAO.load(entryServiceId, startTime, endTime); + Map serviceReferenceMap = serviceReferenceDAO.load(entryServiceId, startTime, endTime); + return buildTreeData(serviceReferenceMap); } public JsonArray loadServiceTree(String entryServiceName, int entryApplicationId, long startTime, long endTime) { IServiceReferenceDAO serviceReferenceDAO = (IServiceReferenceDAO)DAOContainer.INSTANCE.get(IServiceReferenceDAO.class.getName()); - return serviceReferenceDAO.load(entryServiceName, entryApplicationId, startTime, endTime); + Map serviceReferenceMap = serviceReferenceDAO.load(entryServiceName, entryApplicationId, startTime, endTime); + return buildTreeData(serviceReferenceMap); + } + + private JsonArray buildTreeData(Map serviceReferenceMap) { + JsonArray serviceReferenceArray = new JsonArray(); + JsonObject rootServiceReference = findRoot(serviceReferenceMap); + if (ObjectUtils.isNotEmpty(rootServiceReference)) { + serviceReferenceArray.add(rootServiceReference); + String id = rootServiceReference.get(ColumnNameUtils.INSTANCE.rename(ServiceReferenceTable.COLUMN_FRONT_SERVICE_ID)) + Const.ID_SPLIT + rootServiceReference.get(ColumnNameUtils.INSTANCE.rename(ServiceReferenceTable.COLUMN_BEHIND_SERVICE_ID)); + serviceReferenceMap.remove(id); + + int rootServiceId = rootServiceReference.get(ColumnNameUtils.INSTANCE.rename(ServiceReferenceTable.COLUMN_BEHIND_SERVICE_ID)).getAsInt(); + sortAsTree(rootServiceId, serviceReferenceArray, serviceReferenceMap); + } + + return serviceReferenceArray; + } + + private JsonObject findRoot(Map serviceReferenceMap) { + for (JsonObject serviceReference : serviceReferenceMap.values()) { + int frontServiceId = serviceReference.get(ColumnNameUtils.INSTANCE.rename(ServiceReferenceTable.COLUMN_FRONT_SERVICE_ID)).getAsInt(); + if (frontServiceId == 1) { + return serviceReference; + } + } + return null; + } + + private void sortAsTree(int serviceId, JsonArray serviceReferenceArray, + Map serviceReferenceMap) { + Iterator iterator = serviceReferenceMap.values().iterator(); + while (iterator.hasNext()) { + JsonObject serviceReference = iterator.next(); + int frontServiceId = serviceReference.get(ColumnNameUtils.INSTANCE.rename(ServiceReferenceTable.COLUMN_FRONT_SERVICE_ID)).getAsInt(); + if (serviceId == frontServiceId) { + serviceReferenceArray.add(serviceReference); + + int behindServiceId = serviceReference.get(ColumnNameUtils.INSTANCE.rename(ServiceReferenceTable.COLUMN_BEHIND_SERVICE_ID)).getAsInt(); + sortAsTree(behindServiceId, serviceReferenceArray, serviceReferenceMap); + } + } + } + + private void merge(Map serviceReferenceMap, JsonObject serviceReference) { + String id = serviceReference.get(ColumnNameUtils.INSTANCE.rename(ServiceReferenceTable.COLUMN_FRONT_SERVICE_ID)) + Const.ID_SPLIT + serviceReference.get(ColumnNameUtils.INSTANCE.rename(ServiceReferenceTable.COLUMN_BEHIND_SERVICE_ID)); + + if (serviceReferenceMap.containsKey(id)) { + JsonObject reference = serviceReferenceMap.get(id); + add(reference, serviceReference, ColumnNameUtils.INSTANCE.rename(ServiceReferenceTable.COLUMN_S1_LTE)); + add(reference, serviceReference, ColumnNameUtils.INSTANCE.rename(ServiceReferenceTable.COLUMN_S3_LTE)); + add(reference, serviceReference, ColumnNameUtils.INSTANCE.rename(ServiceReferenceTable.COLUMN_S5_LTE)); + add(reference, serviceReference, ColumnNameUtils.INSTANCE.rename(ServiceReferenceTable.COLUMN_S5_GT)); + add(reference, serviceReference, ColumnNameUtils.INSTANCE.rename(ServiceReferenceTable.COLUMN_ERROR)); + add(reference, serviceReference, ColumnNameUtils.INSTANCE.rename(ServiceReferenceTable.COLUMN_SUMMARY)); + add(reference, serviceReference, ColumnNameUtils.INSTANCE.rename(ServiceReferenceTable.COLUMN_COST_SUMMARY)); + } else { + serviceReferenceMap.put(id, serviceReference); + } + } + + private void add(JsonObject oldReference, JsonObject newReference, String key) { + long oldValue = oldReference.get(key).getAsLong(); + long newValue = newReference.get(key).getAsLong(); + oldReference.addProperty(key, oldValue + newValue); } } \ No newline at end of file