From b513024a85aeceaf2604235c3b673ba0905517f3 Mon Sep 17 00:00:00 2001 From: peng-yongsheng <8082209@qq.com> Date: Mon, 5 Feb 2018 10:26:54 +0800 Subject: [PATCH] Provide the searchService query. --- .../dao/ui/IServiceNameServiceUIDAO.java | 4 ++ .../storage/ui/service/ServiceInfo.java | 18 ++++++++- .../storage/ui/service/ServiceNode.java | 39 +++++++++++++++++-- .../es/dao/ui/ServiceNameServiceEsUIDAO.java | 26 +++++++++++++ .../register/ServiceNameEsTableDefine.java | 2 +- .../h2/dao/ui/ServiceNameServiceH2UIDAO.java | 22 +++++++++++ .../ui/jetty/handler/GraphQLHandler.java | 5 ++- .../apm/collector/ui/query/ServiceQuery.java | 27 +++++++++++-- .../ui/service/ServiceNameService.java | 6 +++ .../ui-graphql/service-layer.graphqls | 7 +++- 10 files changed, 143 insertions(+), 13 deletions(-) diff --git a/apm-collector/apm-collector-storage/collector-storage-define/src/main/java/org/apache/skywalking/apm/collector/storage/dao/ui/IServiceNameServiceUIDAO.java b/apm-collector/apm-collector-storage/collector-storage-define/src/main/java/org/apache/skywalking/apm/collector/storage/dao/ui/IServiceNameServiceUIDAO.java index 8f39dda9a..0d8fe4859 100644 --- a/apm-collector/apm-collector-storage/collector-storage-define/src/main/java/org/apache/skywalking/apm/collector/storage/dao/ui/IServiceNameServiceUIDAO.java +++ b/apm-collector/apm-collector-storage/collector-storage-define/src/main/java/org/apache/skywalking/apm/collector/storage/dao/ui/IServiceNameServiceUIDAO.java @@ -18,11 +18,15 @@ package org.apache.skywalking.apm.collector.storage.dao.ui; +import java.util.List; import org.apache.skywalking.apm.collector.storage.base.dao.DAO; +import org.apache.skywalking.apm.collector.storage.ui.service.ServiceInfo; /** * @author peng-yongsheng */ public interface IServiceNameServiceUIDAO extends DAO { int getCount(); + + List searchService(String keyword, int topN); } diff --git a/apm-collector/apm-collector-storage/collector-storage-define/src/main/java/org/apache/skywalking/apm/collector/storage/ui/service/ServiceInfo.java b/apm-collector/apm-collector-storage/collector-storage-define/src/main/java/org/apache/skywalking/apm/collector/storage/ui/service/ServiceInfo.java index f33fbd469..808fe25f3 100644 --- a/apm-collector/apm-collector-storage/collector-storage-define/src/main/java/org/apache/skywalking/apm/collector/storage/ui/service/ServiceInfo.java +++ b/apm-collector/apm-collector-storage/collector-storage-define/src/main/java/org/apache/skywalking/apm/collector/storage/ui/service/ServiceInfo.java @@ -24,6 +24,20 @@ package org.apache.skywalking.apm.collector.storage.ui.service; public class ServiceInfo { private int id; private String name; - private int avgResponseTime; - private int tps; + + public int getId() { + return id; + } + + public void setId(int id) { + this.id = id; + } + + public String getName() { + return name; + } + + public void setName(String name) { + this.name = name; + } } diff --git a/apm-collector/apm-collector-storage/collector-storage-define/src/main/java/org/apache/skywalking/apm/collector/storage/ui/service/ServiceNode.java b/apm-collector/apm-collector-storage/collector-storage-define/src/main/java/org/apache/skywalking/apm/collector/storage/ui/service/ServiceNode.java index 5d2b0cc95..4a15a5e5a 100644 --- a/apm-collector/apm-collector-storage/collector-storage-define/src/main/java/org/apache/skywalking/apm/collector/storage/ui/service/ServiceNode.java +++ b/apm-collector/apm-collector-storage/collector-storage-define/src/main/java/org/apache/skywalking/apm/collector/storage/ui/service/ServiceNode.java @@ -24,7 +24,40 @@ import org.apache.skywalking.apm.collector.storage.ui.common.Node; * @author peng-yongsheng */ public class ServiceNode extends Node { - private Float sla; - private Long calls; - private Integer numOfServiceAlarm; + private float sla; + private long calls; + private int numOfServiceAlarm; + private int applicationId; + + public float getSla() { + return sla; + } + + public void setSla(float sla) { + this.sla = sla; + } + + public long getCalls() { + return calls; + } + + public void setCalls(long calls) { + this.calls = calls; + } + + public int getNumOfServiceAlarm() { + return numOfServiceAlarm; + } + + public void setNumOfServiceAlarm(int numOfServiceAlarm) { + this.numOfServiceAlarm = numOfServiceAlarm; + } + + public int getApplicationId() { + return applicationId; + } + + public void setApplicationId(int applicationId) { + this.applicationId = applicationId; + } } diff --git a/apm-collector/apm-collector-storage/collector-storage-es-provider/src/main/java/org/apache/skywalking/apm/collector/storage/es/dao/ui/ServiceNameServiceEsUIDAO.java b/apm-collector/apm-collector-storage/collector-storage-es-provider/src/main/java/org/apache/skywalking/apm/collector/storage/es/dao/ui/ServiceNameServiceEsUIDAO.java index 67654d03d..f050e0a16 100644 --- a/apm-collector/apm-collector-storage/collector-storage-es-provider/src/main/java/org/apache/skywalking/apm/collector/storage/es/dao/ui/ServiceNameServiceEsUIDAO.java +++ b/apm-collector/apm-collector-storage/collector-storage-es-provider/src/main/java/org/apache/skywalking/apm/collector/storage/es/dao/ui/ServiceNameServiceEsUIDAO.java @@ -18,13 +18,18 @@ package org.apache.skywalking.apm.collector.storage.es.dao.ui; +import java.util.LinkedList; +import java.util.List; import org.apache.skywalking.apm.collector.client.elasticsearch.ElasticSearchClient; import org.apache.skywalking.apm.collector.storage.dao.ui.IServiceNameServiceUIDAO; import org.apache.skywalking.apm.collector.storage.es.base.dao.EsDAO; import org.apache.skywalking.apm.collector.storage.table.register.ServiceNameTable; +import org.apache.skywalking.apm.collector.storage.ui.service.ServiceInfo; import org.elasticsearch.action.search.SearchRequestBuilder; import org.elasticsearch.action.search.SearchResponse; import org.elasticsearch.action.search.SearchType; +import org.elasticsearch.index.query.QueryBuilders; +import org.elasticsearch.search.SearchHit; /** * @author peng-yongsheng @@ -44,4 +49,25 @@ public class ServiceNameServiceEsUIDAO extends EsDAO implements IServiceNameServ SearchResponse searchResponse = searchRequestBuilder.execute().actionGet(); return (int)searchResponse.getHits().getTotalHits(); } + + @Override public List searchService(String keyword, int topN) { + SearchRequestBuilder searchRequestBuilder = getClient().prepareSearch(ServiceNameTable.TABLE); + searchRequestBuilder.setTypes(ServiceNameTable.TABLE_TYPE); + searchRequestBuilder.setSearchType(SearchType.DFS_QUERY_THEN_FETCH); + searchRequestBuilder.setSize(topN); + + searchRequestBuilder.setQuery(QueryBuilders.matchQuery(ServiceNameTable.COLUMN_SERVICE_NAME, keyword)); + + SearchResponse searchResponse = searchRequestBuilder.execute().actionGet(); + SearchHit[] searchHits = searchResponse.getHits().getHits(); + + List serviceInfos = new LinkedList<>(); + for (SearchHit searchHit : searchHits) { + ServiceInfo serviceInfo = new ServiceInfo(); + serviceInfo.setId(((Number)searchHit.getSource().get(ServiceNameTable.COLUMN_SERVICE_ID)).intValue()); + serviceInfo.setName((String)searchHit.getSource().get(ServiceNameTable.COLUMN_SERVICE_NAME)); + serviceInfos.add(serviceInfo); + } + return serviceInfos; + } } diff --git a/apm-collector/apm-collector-storage/collector-storage-es-provider/src/main/java/org/apache/skywalking/apm/collector/storage/es/define/register/ServiceNameEsTableDefine.java b/apm-collector/apm-collector-storage/collector-storage-es-provider/src/main/java/org/apache/skywalking/apm/collector/storage/es/define/register/ServiceNameEsTableDefine.java index f15707bec..2a5dc39c0 100644 --- a/apm-collector/apm-collector-storage/collector-storage-es-provider/src/main/java/org/apache/skywalking/apm/collector/storage/es/define/register/ServiceNameEsTableDefine.java +++ b/apm-collector/apm-collector-storage/collector-storage-es-provider/src/main/java/org/apache/skywalking/apm/collector/storage/es/define/register/ServiceNameEsTableDefine.java @@ -37,7 +37,7 @@ public class ServiceNameEsTableDefine extends ElasticSearchTableDefine { @Override public void initialize() { addColumn(new ElasticSearchColumnDefine(ServiceNameTable.COLUMN_APPLICATION_ID, ElasticSearchColumnDefine.Type.Integer.name())); - addColumn(new ElasticSearchColumnDefine(ServiceNameTable.COLUMN_SERVICE_NAME, ElasticSearchColumnDefine.Type.Keyword.name())); + addColumn(new ElasticSearchColumnDefine(ServiceNameTable.COLUMN_SERVICE_NAME, ElasticSearchColumnDefine.Type.Text.name())); addColumn(new ElasticSearchColumnDefine(ServiceNameTable.COLUMN_SERVICE_ID, ElasticSearchColumnDefine.Type.Integer.name())); } } diff --git a/apm-collector/apm-collector-storage/collector-storage-h2-provider/src/main/java/org/apache/skywalking/apm/collector/storage/h2/dao/ui/ServiceNameServiceH2UIDAO.java b/apm-collector/apm-collector-storage/collector-storage-h2-provider/src/main/java/org/apache/skywalking/apm/collector/storage/h2/dao/ui/ServiceNameServiceH2UIDAO.java index 1c694a59e..29470eefb 100644 --- a/apm-collector/apm-collector-storage/collector-storage-h2-provider/src/main/java/org/apache/skywalking/apm/collector/storage/h2/dao/ui/ServiceNameServiceH2UIDAO.java +++ b/apm-collector/apm-collector-storage/collector-storage-h2-provider/src/main/java/org/apache/skywalking/apm/collector/storage/h2/dao/ui/ServiceNameServiceH2UIDAO.java @@ -20,12 +20,15 @@ package org.apache.skywalking.apm.collector.storage.h2.dao.ui; import java.sql.ResultSet; import java.sql.SQLException; +import java.util.LinkedList; +import java.util.List; import org.apache.skywalking.apm.collector.client.h2.H2Client; import org.apache.skywalking.apm.collector.client.h2.H2ClientException; import org.apache.skywalking.apm.collector.storage.base.sql.SqlBuilder; import org.apache.skywalking.apm.collector.storage.dao.ui.IServiceNameServiceUIDAO; import org.apache.skywalking.apm.collector.storage.h2.base.dao.H2DAO; import org.apache.skywalking.apm.collector.storage.table.register.ServiceNameTable; +import org.apache.skywalking.apm.collector.storage.ui.service.ServiceInfo; import org.slf4j.Logger; import org.slf4j.LoggerFactory; @@ -54,4 +57,23 @@ public class ServiceNameServiceH2UIDAO extends H2DAO implements IServiceNameServ } return 0; } + + @Override public List searchService(String keyword, int topN) { + String dynamicSql = "select {0},{1} from {2} where {3} like ? limit ?"; + String sql = SqlBuilder.buildSql(dynamicSql, ServiceNameTable.COLUMN_SERVICE_ID, ServiceNameTable.COLUMN_SERVICE_NAME, ServiceNameTable.TABLE, ServiceNameTable.COLUMN_SERVICE_NAME); + Object[] params = new Object[] {keyword, topN}; + + List serviceInfos = new LinkedList<>(); + try (ResultSet rs = getClient().executeQuery(sql, params)) { + while (rs.next()) { + ServiceInfo serviceInfo = new ServiceInfo(); + serviceInfo.setId(rs.getInt(ServiceNameTable.COLUMN_SERVICE_ID)); + serviceInfo.setName(rs.getString(ServiceNameTable.COLUMN_SERVICE_NAME)); + serviceInfos.add(serviceInfo); + } + } catch (SQLException | H2ClientException e) { + logger.error(e.getMessage(), e); + } + return serviceInfos; + } } diff --git a/apm-collector/apm-collector-ui/collector-ui-jetty-provider/src/main/java/org/apache/skywalking/apm/collector/ui/jetty/handler/GraphQLHandler.java b/apm-collector/apm-collector-ui/collector-ui-jetty-provider/src/main/java/org/apache/skywalking/apm/collector/ui/jetty/handler/GraphQLHandler.java index 7c30f793d..81cedadbf 100644 --- a/apm-collector/apm-collector-ui/collector-ui-jetty-provider/src/main/java/org/apache/skywalking/apm/collector/ui/jetty/handler/GraphQLHandler.java +++ b/apm-collector/apm-collector-ui/collector-ui-jetty-provider/src/main/java/org/apache/skywalking/apm/collector/ui/jetty/handler/GraphQLHandler.java @@ -43,6 +43,7 @@ import org.apache.skywalking.apm.collector.server.jetty.JettyHandler; import org.apache.skywalking.apm.collector.storage.ui.application.ApplicationNode; import org.apache.skywalking.apm.collector.storage.ui.application.ConjecturalNode; import org.apache.skywalking.apm.collector.storage.ui.common.VisualUserNode; +import org.apache.skywalking.apm.collector.storage.ui.service.ServiceNode; import org.apache.skywalking.apm.collector.ui.graphql.VersionMutation; import org.apache.skywalking.apm.collector.ui.graphql.VersionQuery; import org.apache.skywalking.apm.collector.ui.mutation.ConfigMutation; @@ -82,9 +83,9 @@ public class GraphQLHandler extends JettyHandler { .file("ui-graphql/service-layer.graphqls") .file("ui-graphql/trace.graphqls") .resolvers(new VersionQuery(), new VersionMutation(), new AlarmQuery(), new ApplicationQuery(moduleManager)) - .resolvers(new OverViewLayerQuery(moduleManager), new ServerQuery(moduleManager), new ServiceQuery(), new TraceQuery(moduleManager)) + .resolvers(new OverViewLayerQuery(moduleManager), new ServerQuery(moduleManager), new ServiceQuery(moduleManager), new TraceQuery(moduleManager)) .resolvers(new ConfigQuery(), new ConfigMutation()) - .dictionary(ConjecturalNode.class, VisualUserNode.class, ApplicationNode.class) + .dictionary(ConjecturalNode.class, VisualUserNode.class, ApplicationNode.class, ServiceNode.class) .build() .makeExecutableSchema(); diff --git a/apm-collector/apm-collector-ui/collector-ui-jetty-provider/src/main/java/org/apache/skywalking/apm/collector/ui/query/ServiceQuery.java b/apm-collector/apm-collector-ui/collector-ui-jetty-provider/src/main/java/org/apache/skywalking/apm/collector/ui/query/ServiceQuery.java index ef11c1066..600f36e50 100644 --- a/apm-collector/apm-collector-ui/collector-ui-jetty-provider/src/main/java/org/apache/skywalking/apm/collector/ui/query/ServiceQuery.java +++ b/apm-collector/apm-collector-ui/collector-ui-jetty-provider/src/main/java/org/apache/skywalking/apm/collector/ui/query/ServiceQuery.java @@ -18,21 +18,40 @@ package org.apache.skywalking.apm.collector.ui.query; +import java.text.ParseException; import java.util.List; -import org.apache.skywalking.apm.collector.ui.graphql.Query; +import org.apache.skywalking.apm.collector.core.module.ModuleManager; +import org.apache.skywalking.apm.collector.core.util.ObjectUtils; import org.apache.skywalking.apm.collector.storage.ui.common.Duration; import org.apache.skywalking.apm.collector.storage.ui.common.ResponseTimeTrend; import org.apache.skywalking.apm.collector.storage.ui.common.SLATrend; import org.apache.skywalking.apm.collector.storage.ui.common.ThroughputTrend; import org.apache.skywalking.apm.collector.storage.ui.common.Topology; -import org.apache.skywalking.apm.collector.storage.ui.service.ServiceNode; +import org.apache.skywalking.apm.collector.storage.ui.service.ServiceInfo; +import org.apache.skywalking.apm.collector.ui.graphql.Query; +import org.apache.skywalking.apm.collector.ui.service.ServiceNameService; /** * @author peng-yongsheng */ public class ServiceQuery implements Query { - public List searchService(String keyword, Duration duration, Integer topN) { - return null; + + private final ModuleManager moduleManager; + private ServiceNameService serviceNameService; + + public ServiceQuery(ModuleManager moduleManager) { + this.moduleManager = moduleManager; + } + + private ServiceNameService getServiceNameService() { + if (ObjectUtils.isEmpty(serviceNameService)) { + this.serviceNameService = new ServiceNameService(moduleManager); + } + return serviceNameService; + } + + public List searchService(String keyword, int topN) throws ParseException { + return getServiceNameService().searchService(keyword, topN); } public ResponseTimeTrend getServiceResponseTimeTrend(int serviceId, Duration duration) { diff --git a/apm-collector/apm-collector-ui/collector-ui-jetty-provider/src/main/java/org/apache/skywalking/apm/collector/ui/service/ServiceNameService.java b/apm-collector/apm-collector-ui/collector-ui-jetty-provider/src/main/java/org/apache/skywalking/apm/collector/ui/service/ServiceNameService.java index ebe66d0b2..673461352 100644 --- a/apm-collector/apm-collector-ui/collector-ui-jetty-provider/src/main/java/org/apache/skywalking/apm/collector/ui/service/ServiceNameService.java +++ b/apm-collector/apm-collector-ui/collector-ui-jetty-provider/src/main/java/org/apache/skywalking/apm/collector/ui/service/ServiceNameService.java @@ -18,9 +18,11 @@ package org.apache.skywalking.apm.collector.ui.service; +import java.util.List; import org.apache.skywalking.apm.collector.core.module.ModuleManager; import org.apache.skywalking.apm.collector.storage.StorageModule; import org.apache.skywalking.apm.collector.storage.dao.ui.IServiceNameServiceUIDAO; +import org.apache.skywalking.apm.collector.storage.ui.service.ServiceInfo; /** * @author peng-yongsheng @@ -36,4 +38,8 @@ public class ServiceNameService { public int getCount() { return serviceNameServiceUIDAO.getCount(); } + + public List searchService(String keyword, int topN) { + return serviceNameServiceUIDAO.searchService(keyword, topN); + } } diff --git a/apm-protocol/apm-ui-protocol/src/main/resources/ui-graphql/service-layer.graphqls b/apm-protocol/apm-ui-protocol/src/main/resources/ui-graphql/service-layer.graphqls index a5b44557a..2c14b8be2 100644 --- a/apm-protocol/apm-ui-protocol/src/main/resources/ui-graphql/service-layer.graphqls +++ b/apm-protocol/apm-ui-protocol/src/main/resources/ui-graphql/service-layer.graphqls @@ -15,6 +15,11 @@ type ServiceNode implements Node { type ServiceInfo { id: ID! name: String +} + +type ServiceMetric { + id: ID! + name: String # The unit is millisecond. avgResponseTime: Int! tps: Int! @@ -27,7 +32,7 @@ type TraceItem { } extend type Query { - searchService(keyword: String!, duration: Duration!, topN: Int!): [ServiceNode!]! + searchService(keyword: String!, topN: Int!): [ServiceInfo!]! getServiceResponseTimeTrend(serviceId: ID!, duration: Duration!): ResponseTimeTrend getServiceTPSTrend(serviceId: ID!, duration: Duration!): ThroughputTrend getServiceSLATrend(serviceId: ID!, duration: Duration!): SLATrend