This is an automated email from the ASF dual-hosted git repository.

albumenj pushed a commit to branch refactor-with-go
in repository https://gitbox.apache.org/repos/asf/dubbo-admin.git


The following commit(s) were added to refs/heads/refactor-with-go by this push:
     new 00b2d050 ADD: SearchService Feature (#1027)
00b2d050 is described below

commit 00b2d050d09b1aa39688c7711549477c08984547
Author: wudong5 <[email protected]>
AuthorDate: Tue Mar 14 11:34:31 2023 +0800

    ADD: SearchService Feature (#1027)
    
    * add ServiceDetail
    
    * gofumpt
    
    * gofumpt
---
 pkg/admin/handlers/service.go                      | 87 +++++++++++++++++++++-
 .../provider_service.go => model/consumer.go}      | 20 +++--
 .../service_detail.go}                             | 13 ++--
 pkg/admin/router/router.go                         |  4 +
 .../{provider_service.go => consumer_service.go}   |  6 +-
 pkg/admin/services/consumer_service_impl.go        | 45 +++++++++++
 pkg/admin/services/provider_service.go             |  2 +
 pkg/admin/services/provider_service_impl.go        | 10 +--
 pkg/admin/util/sync_utils.go                       | 29 ++++++++
 9 files changed, 196 insertions(+), 20 deletions(-)

diff --git a/pkg/admin/handlers/service.go b/pkg/admin/handlers/service.go
index d1f7857d..948bb52d 100644
--- a/pkg/admin/handlers/service.go
+++ b/pkg/admin/handlers/service.go
@@ -20,12 +20,20 @@ package handlers
 import (
        "net/http"
 
+       "dubbo.apache.org/dubbo-go/v3/metadata/identifier"
+       "github.com/apache/dubbo-admin/pkg/admin/config"
+       "github.com/apache/dubbo-admin/pkg/admin/model"
+       "github.com/apache/dubbo-admin/pkg/admin/util"
+
        "github.com/apache/dubbo-admin/pkg/admin/services"
        "github.com/apache/dubbo-admin/pkg/version"
        "github.com/gin-gonic/gin"
 )
 
-var providerService services.ProviderService = &services.ProviderServiceImpl{}
+var (
+       providerService services.ProviderService = 
&services.ProviderServiceImpl{}
+       consumerService services.ConsumerService = 
&services.ConsumerServiceImpl{}
+)
 
 func AllServices(c *gin.Context) {
        services, err := providerService.FindServices()
@@ -57,6 +65,83 @@ func SearchService(c *gin.Context) {
        })
 }
 
+func AllApplications(c *gin.Context) {
+       applications, err := providerService.FindApplications()
+       if err != nil {
+               c.JSON(http.StatusInternalServerError, gin.H{
+                       "error": err.Error(),
+               })
+               return
+       }
+       c.JSON(http.StatusOK, gin.H{
+               "code": 1,
+               "data": applications,
+       })
+}
+
+func AllConsumers(c *gin.Context) {
+       consumers, err := consumerService.FindAll()
+       if err != nil {
+               c.JSON(http.StatusInternalServerError, gin.H{
+                       "error": err.Error(),
+               })
+               return
+       }
+       c.JSON(http.StatusOK, gin.H{
+               "code": 1,
+               "data": consumers,
+       })
+}
+
+func ServiceDetail(c *gin.Context) {
+       service := c.Param("service")
+       group := util.GetGroup(service)
+       version := util.GetVersion(service)
+       interfaze := util.GetInterface(service)
+
+       providers, err := providerService.FindByService(service)
+       if err != nil {
+               c.JSON(http.StatusInternalServerError, gin.H{
+                       "error": err.Error(),
+               })
+               return
+       }
+       consumers, err := consumerService.FindByService(service)
+       if err != nil {
+               c.JSON(http.StatusInternalServerError, gin.H{
+                       "error": err.Error(),
+               })
+               return
+       }
+
+       application := ""
+       if len(providers) > 0 {
+               application = providers[0].Application
+       }
+       identifier := &identifier.MetadataIdentifier{
+               Application: application,
+               BaseMetadataIdentifier: identifier.BaseMetadataIdentifier{
+                       ServiceInterface: interfaze,
+                       Version:          version,
+                       Group:            group,
+                       Side:             "provider",
+               },
+       }
+       metadata, _ := 
config.MetadataReportCenter.GetServiceDefinition(identifier)
+
+       serviceDetail := &model.ServiceDetail{
+               Providers:   providers,
+               Consumers:   consumers,
+               Service:     service,
+               Application: application,
+               Metadata:    metadata,
+       }
+       c.JSON(http.StatusOK, gin.H{
+               "code": 1,
+               "data": serviceDetail,
+       })
+}
+
 func Version(c *gin.Context) {
        c.JSON(http.StatusOK, version.GetVersion())
 }
diff --git a/pkg/admin/services/provider_service.go 
b/pkg/admin/model/consumer.go
similarity index 73%
copy from pkg/admin/services/provider_service.go
copy to pkg/admin/model/consumer.go
index f87a75b7..95484e66 100644
--- a/pkg/admin/services/provider_service.go
+++ b/pkg/admin/model/consumer.go
@@ -13,11 +13,21 @@
 // See the License for the specific language governing permissions and
 // limitations under the License.
 
-package services
+package model
 
-import "github.com/apache/dubbo-admin/pkg/admin/model"
+import "time"
 
-type ProviderService interface {
-       FindServices() ([]string, error)
-       FindService(string, string) ([]*model.Provider, error)
+type Consumer struct {
+       Entity
+       Service     string
+       Parameters  string
+       Result      string
+       Address     string
+       Registry    string
+       Application string
+       Username    string
+       Statistics  string
+       Collected   time.Duration
+       Expired     time.Duration
+       Alived      int64
 }
diff --git a/pkg/admin/services/provider_service.go 
b/pkg/admin/model/service_detail.go
similarity index 80%
copy from pkg/admin/services/provider_service.go
copy to pkg/admin/model/service_detail.go
index f87a75b7..482b272e 100644
--- a/pkg/admin/services/provider_service.go
+++ b/pkg/admin/model/service_detail.go
@@ -13,11 +13,12 @@
 // See the License for the specific language governing permissions and
 // limitations under the License.
 
-package services
+package model
 
-import "github.com/apache/dubbo-admin/pkg/admin/model"
-
-type ProviderService interface {
-       FindServices() ([]string, error)
-       FindService(string, string) ([]*model.Provider, error)
+type ServiceDetail struct {
+       Providers   []*Provider
+       Consumers   []*Consumer
+       Service     string
+       Application string
+       Metadata    string
 }
diff --git a/pkg/admin/router/router.go b/pkg/admin/router/router.go
index 6b54d0ee..810551c8 100644
--- a/pkg/admin/router/router.go
+++ b/pkg/admin/router/router.go
@@ -27,6 +27,10 @@ func InitRouter() *gin.Engine {
 
        router.GET("/api/dev/services", handlers.AllServices)
        router.GET("/api/dev/service", handlers.SearchService)
+       router.GET("api/dev/applications", handlers.AllApplications)
+       router.GET("api/dev/consumers", handlers.AllConsumers)
+       router.GET("api/dev/service/:service", handlers.ServiceDetail)
        router.GET("/api/dev/version", handlers.Version)
+
        return router
 }
diff --git a/pkg/admin/services/provider_service.go 
b/pkg/admin/services/consumer_service.go
similarity index 87%
copy from pkg/admin/services/provider_service.go
copy to pkg/admin/services/consumer_service.go
index f87a75b7..4e7b6ec6 100644
--- a/pkg/admin/services/provider_service.go
+++ b/pkg/admin/services/consumer_service.go
@@ -17,7 +17,7 @@ package services
 
 import "github.com/apache/dubbo-admin/pkg/admin/model"
 
-type ProviderService interface {
-       FindServices() ([]string, error)
-       FindService(string, string) ([]*model.Provider, error)
+type ConsumerService interface {
+       FindAll() ([]*model.Consumer, error)
+       FindByService(string) ([]*model.Consumer, error)
 }
diff --git a/pkg/admin/services/consumer_service_impl.go 
b/pkg/admin/services/consumer_service_impl.go
new file mode 100644
index 00000000..3d35ad4e
--- /dev/null
+++ b/pkg/admin/services/consumer_service_impl.go
@@ -0,0 +1,45 @@
+// Licensed to the Apache Software Foundation (ASF) under one or more
+// contributor license agreements.  See the NOTICE file distributed with
+// this work for additional information regarding copyright ownership.
+// The ASF licenses this file to You 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.
+
+package services
+
+import (
+       "github.com/apache/dubbo-admin/pkg/admin/constant"
+       "github.com/apache/dubbo-admin/pkg/admin/model"
+       "github.com/apache/dubbo-admin/pkg/admin/util"
+)
+
+type ConsumerServiceImpl struct{}
+
+func (c *ConsumerServiceImpl) FindAll() ([]*model.Consumer, error) {
+       filter := make(map[string]string)
+       filter[constant.CategoryKey] = constant.ConsumersCategory
+       servicesMap, err := util.FilterFromCategory(filter)
+       if err != nil {
+               return nil, err
+       }
+       return util.URL2ConsumerList(servicesMap), nil
+}
+
+func (c *ConsumerServiceImpl) FindByService(service string) 
([]*model.Consumer, error) {
+       filter := make(map[string]string)
+       filter[constant.CategoryKey] = constant.ConsumersCategory
+       filter[util.ServiceFilterKey] = service
+       servicesMap, err := util.FilterFromCategory(filter)
+       if err != nil {
+               return nil, err
+       }
+       return util.URL2ConsumerList(servicesMap), nil
+}
diff --git a/pkg/admin/services/provider_service.go 
b/pkg/admin/services/provider_service.go
index f87a75b7..438be1e8 100644
--- a/pkg/admin/services/provider_service.go
+++ b/pkg/admin/services/provider_service.go
@@ -19,5 +19,7 @@ import "github.com/apache/dubbo-admin/pkg/admin/model"
 
 type ProviderService interface {
        FindServices() ([]string, error)
+       FindApplications() ([]string, error)
        FindService(string, string) ([]*model.Provider, error)
+       FindByService(string) ([]*model.Provider, error)
 }
diff --git a/pkg/admin/services/provider_service_impl.go 
b/pkg/admin/services/provider_service_impl.go
index 6a16f2e1..061110b1 100644
--- a/pkg/admin/services/provider_service_impl.go
+++ b/pkg/admin/services/provider_service_impl.go
@@ -48,7 +48,7 @@ func (p *ProviderServiceImpl) FindServices() ([]string, 
error) {
        return services, nil
 }
 
-func (p *ProviderServiceImpl) findApplications() ([]string, error) {
+func (p *ProviderServiceImpl) FindApplications() ([]string, error) {
        var (
                applications []string
                err          error
@@ -110,7 +110,7 @@ func (p *ProviderServiceImpl) findAddresses() ([]string, 
error) {
        return addresses, err
 }
 
-func (p *ProviderServiceImpl) findByService(providerService string) 
([]*model.Provider, error) {
+func (p *ProviderServiceImpl) FindByService(providerService string) 
([]*model.Provider, error) {
        filter := make(map[string]string)
        filter[constant.CategoryKey] = constant.ProvidersCategory
        filter[util.ServiceFilterKey] = providerService
@@ -156,7 +156,7 @@ func (p *ProviderServiceImpl) FindService(pattern string, 
filter string) ([]*mod
                                return nil, err
                        }
                } else if pattern == constant.Service {
-                       providers, err = p.findByService(filter)
+                       providers, err = p.FindByService(filter)
                        if err != nil {
                                return nil, err
                        }
@@ -181,7 +181,7 @@ func (p *ProviderServiceImpl) FindService(pattern string, 
filter string) ([]*mod
                                return nil, err
                        }
                } else if pattern == constant.ApplicationKey {
-                       candidates, err = p.findApplications()
+                       candidates, err = p.FindApplications()
                        if err != nil {
                                return nil, err
                        }
@@ -206,7 +206,7 @@ func (p *ProviderServiceImpl) FindService(pattern string, 
filter string) ([]*mod
                                                return nil, err
                                        }
                                } else if pattern == constant.Service {
-                                       providers, err = 
p.findByService(candidate)
+                                       providers, err = 
p.FindByService(candidate)
                                        if err != nil {
                                                return nil, err
                                        }
diff --git a/pkg/admin/util/sync_utils.go b/pkg/admin/util/sync_utils.go
index 0ebbd41f..3715e6dd 100644
--- a/pkg/admin/util/sync_utils.go
+++ b/pkg/admin/util/sync_utils.go
@@ -68,6 +68,35 @@ func URL2ProviderList(servicesMap map[string]*common.URL) 
[]*model.Provider {
        return providers
 }
 
+func URL2Consumer(id string, url *common.URL) *model.Consumer {
+       if url == nil {
+               return nil
+       }
+
+       return &model.Consumer{
+               Entity:      model.Entity{Hash: id},
+               Service:     url.ServiceKey(),
+               Address:     url.Location,
+               Application: url.GetParam(constant.ApplicationKey, ""),
+               Parameters:  url.String(),
+               Username:    url.GetParam(constant.OwnerKey, ""),
+       }
+}
+
+func URL2ConsumerList(servicesMap map[string]*common.URL) []*model.Consumer {
+       var consumers []*model.Consumer
+       if servicesMap == nil {
+               return consumers
+       }
+       for id, url := range servicesMap {
+               consumer := URL2Consumer(id, url)
+               if consumer != nil {
+                       consumers = append(consumers, consumer)
+               }
+       }
+       return consumers
+}
+
 func FilterFromCategory(filter map[string]string) (map[string]*common.URL, 
error) {
        c, ok := filter[constant.CategoryKey]
        if !ok {

Reply via email to