robocanic commented on code in PR #1390:
URL: https://github.com/apache/dubbo-admin/pull/1390#discussion_r2701986706


##########
pkg/console/counter/component.go:
##########
@@ -75,6 +89,112 @@ func (c *managerComponent) Start(rt runtime.Runtime, _ 
<-chan struct{}) error {
        return c.manager.Bind(bus)
 }
 
+func (c *managerComponent) initializeCountsFromStore(storeRouter store.Router) 
error {
+       if err := c.initializeResourceCount(storeRouter, 
meshresource.InstanceKind); err != nil {
+               return fmt.Errorf("failed to initialize instance count: %w", 
err)
+       }
+
+       if err := c.initializeResourceCount(storeRouter, 
meshresource.ApplicationKind); err != nil {
+               return fmt.Errorf("failed to initialize application count: %w", 
err)
+       }
+
+       if err := c.initializeResourceCount(storeRouter, 
meshresource.ServiceProviderMetadataKind); err != nil {
+               return fmt.Errorf("failed to initialize service provider 
metadata count: %w", err)
+       }
+
+       return nil
+}
+
+func (c *managerComponent) initializeResourceCount(storeRouter store.Router, 
kind resmodel.ResourceKind) error {
+       resourceStore, err := storeRouter.ResourceKindRoute(kind)
+       if err != nil {
+               return err
+       }
+
+       allResources := resourceStore.List()
+
+       meshCounts := make(map[string]int64)
+       meshDistributions := make(map[string]map[string]int64)
+
+       for _, obj := range allResources {
+               resource, ok := obj.(resmodel.Resource)
+               if !ok {
+                       continue
+               }
+
+               mesh := resource.ResourceMesh()
+               if mesh == "" {
+                       mesh = "default"
+               }
+
+               meshCounts[mesh]++
+
+               if kind == meshresource.InstanceKind {
+                       instance, ok := 
resource.(*meshresource.InstanceResource)
+                       if ok && instance.Spec != nil {
+                               protocol := instance.Spec.GetProtocol()
+                               if protocol != "" {
+                                       if meshDistributions[mesh] == nil {
+                                               meshDistributions[mesh] = 
make(map[string]int64)
+                                       }
+                                       
meshDistributions[mesh]["protocol:"+protocol]++
+                               }
+
+                               releaseVersion := 
instance.Spec.GetReleaseVersion()
+                               if releaseVersion != "" {
+                                       if meshDistributions[mesh] == nil {
+                                               meshDistributions[mesh] = 
make(map[string]int64)
+                                       }
+                                       
meshDistributions[mesh]["release:"+releaseVersion]++
+                               }
+
+                               if meshDistributions[mesh] == nil {
+                                       meshDistributions[mesh] = 
make(map[string]int64)
+                               }
+                               meshDistributions[mesh]["discovery:"+mesh]++
+                       }
+               }
+       }
+
+       cm := c.manager.(*counterManager)
+
+       if counter, exists := cm.simpleCounters[kind]; exists {
+               for mesh, count := range meshCounts {
+                       for i := int64(0); i < count; i++ {
+                               counter.Increment(mesh)
+                       }
+               }
+       }
+
+       if kind == meshresource.InstanceKind {
+               for mesh, distributions := range meshDistributions {
+                       for key, count := range distributions {
+                               if len(key) > 9 && key[:9] == "protocol:" {

Review Comment:
   Suggestion:这里的逻辑是不是能直接放到上面的for-loop里面,将对应的子类型increment,不需要再来一个map中转



-- 
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