Similarityoung commented on code in PR #1429:
URL: https://github.com/apache/dubbo-admin/pull/1429#discussion_r2901714423


##########
pkg/console/service/service.go:
##########
@@ -160,6 +162,421 @@ func ToServiceSearchRespByConsumer(res 
*meshresource.ServiceConsumerMetadataReso
        }
 }
 
+type serviceMethodCandidate struct {
+       detail    *model.ServiceMethodDetailResp
+       signature string
+       method    *meshproto.Method
+}
+
+func GetServiceMethodNames(ctx consolectx.Context, req 
model.ServiceMethodsReq) ([]model.ServiceMethodSummaryResp, error) {
+       metadataList, err := listServiceProviderMetadata(ctx, req)
+       if err != nil {
+               return nil, err
+       }
+
+       return buildServiceMethodSummaries(metadataList), nil
+}
+
+func GetServiceMethodDetail(ctx consolectx.Context, req 
model.ServiceMethodDetailReq) (*model.ServiceMethodDetailResp, error) {
+       metadataList, err := listServiceProviderMetadata(ctx, 
req.ServiceMethodsReq)
+       if err != nil {
+               return nil, err
+       }
+
+       candidate, err := 
resolveStructuredServiceMethodCandidate(buildServiceMethodCandidates(metadataList),
 req)
+       if err != nil {
+               return nil, err
+       }
+
+       detail := cloneServiceMethodDetailResp(candidate.detail)
+       detail.Types = buildServiceMethodRelatedTypes(metadataList, 
candidate.method)
+       return detail, nil
+}
+
+func listServiceProviderMetadata(ctx consolectx.Context, req 
model.ServiceMethodsReq) ([]*meshresource.ServiceProviderMetadataResource, 
error) {
+       indexes := map[string]string{
+               index.ByMeshIndex:                  req.Mesh,
+               index.ByServiceProviderServiceName: req.ServiceName,
+       }
+       if req.ProviderAppName != "" {
+               indexes[index.ByServiceProviderAppName] = req.ProviderAppName
+       }
+
+       metadataList, err := 
manager.ListByIndexes[*meshresource.ServiceProviderMetadataResource](
+               ctx.ResourceManager(),
+               meshresource.ServiceProviderMetadataKind,
+               indexes,
+       )
+       if err != nil {
+               return nil, err
+       }
+
+       filtered := make([]*meshresource.ServiceProviderMetadataResource, 0, 
len(metadataList))
+       for _, metadata := range metadataList {
+               if !matchesServiceMethodsReq(metadata, req) {
+                       continue
+               }
+               filtered = append(filtered, metadata)
+       }
+
+       return filtered, nil
+}
+
+func matchesServiceMethodsReq(metadata 
*meshresource.ServiceProviderMetadataResource, req model.ServiceMethodsReq) 
bool {
+       if metadata == nil || metadata.Spec == nil {
+               return false
+       }
+       if req.Group != "" && metadata.Spec.Group != req.Group {
+               return false
+       }
+       if req.Version != "" && metadata.Spec.Version != req.Version {
+               return false
+       }
+       if req.ProviderAppName != "" && metadata.Spec.ProviderAppName != 
req.ProviderAppName {
+               return false
+       }
+       return true
+}
+
+func buildServiceMethodSummaries(metadataList 
[]*meshresource.ServiceProviderMetadataResource) 
[]model.ServiceMethodSummaryResp {
+       candidates := buildServiceMethodCandidates(metadataList)
+       summaries := make([]model.ServiceMethodSummaryResp, 0, len(candidates))
+       for _, candidate := range candidates {
+               summaries = append(summaries, model.ServiceMethodSummaryResp{
+                       MethodName:     candidate.detail.MethodName,
+                       ParameterTypes: append([]string{}, 
candidate.detail.ParameterTypes...),
+                       Signature:      candidate.signature,
+               })
+       }
+       return summaries
+}
+
+func buildServiceMethodCandidates(metadataList 
[]*meshresource.ServiceProviderMetadataResource) []*serviceMethodCandidate {
+       candidateByKey := make(map[string]*serviceMethodCandidate)
+       structuredMethodNames := make(map[string]struct{})
+       fallbackMethodNames := make(map[string]struct{})
+
+       for _, metadata := range metadataList {
+               for _, method := range metadata.Spec.Methods {
+                       candidate, ok := 
newStructuredServiceMethodCandidate(method)
+                       if !ok {
+                               continue
+                       }
+                       
candidateByKey[serviceMethodKey(candidate.detail.MethodName, 
candidate.signature)] = candidate
+                       structuredMethodNames[candidate.detail.MethodName] = 
struct{}{}
+               }
+
+               if len(metadata.Spec.Methods) > 0 {
+                       continue
+               }
+               for _, methodName := range 
methodNamesFromMetadataParameters(metadata.Spec.Parameters) {
+                       fallbackMethodNames[methodName] = struct{}{}
+               }
+       }
+
+       for methodName := range fallbackMethodNames {
+               if _, ok := structuredMethodNames[methodName]; ok {
+                       continue
+               }
+               candidateByKey[serviceMethodKey(methodName, "")] = 
newFallbackServiceMethodCandidate(methodName)
+       }
+
+       candidates := make([]*serviceMethodCandidate, 0, len(candidateByKey))
+       for _, candidate := range candidateByKey {
+               candidates = append(candidates, candidate)
+       }
+       sort.Slice(candidates, func(i, j int) bool {
+               if candidates[i].detail.MethodName != 
candidates[j].detail.MethodName {
+                       return candidates[i].detail.MethodName < 
candidates[j].detail.MethodName
+               }
+               return candidates[i].signature < candidates[j].signature
+       })
+       return candidates
+}
+
+func findServiceMethodCandidate(candidates []*serviceMethodCandidate, req 
model.ServiceMethodDetailReq) (*serviceMethodCandidate, bool) {
+       if req.Signature != "" {
+               for _, candidate := range candidates {
+                       if candidate.detail.MethodName == req.MethodName && 
candidate.signature == req.Signature {
+                               return candidate, false
+                       }
+               }
+               return nil, false
+       }

Review Comment:
   no need



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: [email protected]

For queries about this service, please contact Infrastructure at:
[email protected]


---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]

Reply via email to