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


##########
pkg/console/counter/manager.go:
##########
@@ -137,12 +140,28 @@ func (cm *counterManager) Distribution(metric 
CounterType) map[string]int64 {
        if !exists {
                return map[string]int64{}
        }
-       raw := counter.GetAll()
-       result := make(map[string]int64, len(raw))
-       for k, v := range raw {
-               result[k] = v
+       return counter.GetAll()
+}
+
+func (cm *counterManager) CountByMesh(kind resmodel.ResourceKind, mesh string) 
int64 {
+       if mesh == "" {
+               mesh = "default"
+       }
+       if counter, exists := cm.simpleCounters[kind]; exists {
+               return counter.GetByGroup(mesh)
+       }
+       return 0
+}
+
+func (cm *counterManager) DistributionByMesh(metric CounterType, mesh string) 
map[string]int64 {
+       if mesh == "" {
+               mesh = "default"
        }
-       return result
+       counter, exists := cm.distributionByType[metric]
+       if !exists {
+               return map[string]int64{}
+       }
+       return counter.GetByGroup(mesh)

Review Comment:
   The new mesh-based counting functionality introduced in CountByMesh and 
DistributionByMesh methods lacks test coverage. Given the complexity of the 
grouping logic and the presence of tests elsewhere in the repository, tests 
should be added to verify correct behavior for single mesh, multiple meshes, 
and edge cases like empty mesh names.



##########
pkg/console/counter/manager.go:
##########
@@ -224,25 +282,19 @@ func (cfg *distributionCounterConfig) extractFrom(res 
resmodel.Resource) string
        return cfg.extractor(res)
 }
 
-func (cfg *distributionCounterConfig) increment(key string) {
-       cfg.counter.Increment(normalizeDistributionKey(key))
-}
-
-func (cfg *distributionCounterConfig) decrement(key string) {
-       cfg.counter.Decrement(normalizeDistributionKey(key))
-}
-
 func (cfg *distributionCounterConfig) update(oldObj, newObj resmodel.Resource) 
{
        oldKey := normalizeDistributionKey(cfg.extractFrom(oldObj))
        newKey := normalizeDistributionKey(cfg.extractFrom(newObj))
        if oldKey == newKey {
                return
        }
+       oldMesh := extractMeshName(oldObj)
+       newMesh := extractMeshName(newObj)

Review Comment:
   The update method only updates counters when oldKey != newKey, but it 
doesn't handle the case where the mesh changes while the key stays the same. 
This means if a resource moves from one mesh to another without changing its 
protocol/release/discovery value, the counters won't be updated correctly. The 
condition should check if either the key or mesh has changed.
   ```suggestion
        oldMesh := extractMeshName(oldObj)
        newMesh := extractMeshName(newObj)
        if oldKey == newKey && oldMesh == newMesh {
                return
        }
   ```



##########
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:" {
+                                       if cfg := 
cm.getDistributionConfig(kind, ProtocolCounter); cfg != nil {
+                                               for i := int64(0); i < count; 
i++ {
+                                                       
cfg.counter.Increment(mesh, key[9:])
+                                               }
+                                       }
+                               } else if len(key) > 8 && key[:8] == "release:" 
{
+                                       if cfg := 
cm.getDistributionConfig(kind, ReleaseCounter); cfg != nil {
+                                               for i := int64(0); i < count; 
i++ {
+                                                       
cfg.counter.Increment(mesh, key[8:])
+                                               }
+                                       }
+                               } else if len(key) > 11 && key[:11] == 
"discovery:" {
+                                       if cfg := 
cm.getDistributionConfig(kind, DiscoveryCounter); cfg != nil {
+                                               for i := int64(0); i < count; 
i++ {
+                                                       
cfg.counter.Increment(mesh, key[11:])

Review Comment:
   Using a loop to call Increment multiple times is inefficient. Consider 
adding a bulk increment method like IncrementBy(group string, count int64) to 
the Counter struct to improve performance during initialization.
   ```suggestion
                // Use bulk increment when supported by the underlying counter 
implementation.
                type bulkCounter interface {
                        IncrementBy(group string, count int64)
                }
   
                if bc, ok := counter.(bulkCounter); ok {
                        for mesh, count := range meshCounts {
                                bc.IncrementBy(mesh, count)
                        }
                } else {
                        for mesh, count := range meshCounts {
                                for i := int64(0); i < count; i++ {
                                        counter.Increment(mesh)
                                }
                        }
                }
        }
   
        if kind == meshresource.InstanceKind {
                // Use bulk increment when supported by the underlying 
distribution counter implementation.
                type distBulkCounter interface {
                        IncrementBy(mesh, label string, count int64)
                }
   
                for mesh, distributions := range meshDistributions {
                        for key, count := range distributions {
                                if len(key) > 9 && key[:9] == "protocol:" {
                                        if cfg := 
cm.getDistributionConfig(kind, ProtocolCounter); cfg != nil {
                                                if bc, ok := 
cfg.counter.(distBulkCounter); ok {
                                                        bc.IncrementBy(mesh, 
key[9:], count)
                                                } else {
                                                        for i := int64(0); i < 
count; i++ {
                                                                
cfg.counter.Increment(mesh, key[9:])
                                                        }
                                                }
                                        }
                                } else if len(key) > 8 && key[:8] == "release:" 
{
                                        if cfg := 
cm.getDistributionConfig(kind, ReleaseCounter); cfg != nil {
                                                if bc, ok := 
cfg.counter.(distBulkCounter); ok {
                                                        bc.IncrementBy(mesh, 
key[8:], count)
                                                } else {
                                                        for i := int64(0); i < 
count; i++ {
                                                                
cfg.counter.Increment(mesh, key[8:])
                                                        }
                                                }
                                        }
                                } else if len(key) > 11 && key[:11] == 
"discovery:" {
                                        if cfg := 
cm.getDistributionConfig(kind, DiscoveryCounter); cfg != nil {
                                                if bc, ok := 
cfg.counter.(distBulkCounter); ok {
                                                        bc.IncrementBy(mesh, 
key[11:], count)
                                                } else {
                                                        for i := int64(0); i < 
count; i++ {
                                                                
cfg.counter.Increment(mesh, key[11:])
                                                        }
   ```



##########
pkg/console/counter/counter.go:
##########
@@ -19,79 +19,149 @@ package counter
 
 import (
        "sync"
-       "sync/atomic"
 )
 
 type Counter struct {
-       name  string
-       value atomic.Int64
+       name string
+       data map[string]int64
+       mu   sync.RWMutex
 }
 
 func NewCounter(name string) *Counter {
-       return &Counter{name: name}
+       return &Counter{
+               name: name,
+               data: make(map[string]int64),
+       }
 }
 
 func (c *Counter) Get() int64 {
-       return c.value.Load()
+       c.mu.RLock()
+       defer c.mu.RUnlock()
+       var sum int64
+       for _, v := range c.data {
+               sum += v
+       }
+       return sum
 }
 
-func (c *Counter) Increment() {
-       c.value.Add(1)
+func (c *Counter) GetByGroup(group string) int64 {
+       if group == "" {
+               group = "default"
+       }
+       c.mu.RLock()
+       defer c.mu.RUnlock()
+       return c.data[group]
 }
 
-func (c *Counter) Decrement() {
-       for {
-               current := c.value.Load()
-               if current == 0 {
-                       return
-               }
-               if c.value.CompareAndSwap(current, current-1) {
-                       return
+func (c *Counter) Increment(group string) {
+       if group == "" {
+               group = "default"
+       }
+       c.mu.Lock()
+       defer c.mu.Unlock()
+       c.data[group]++
+}
+
+func (c *Counter) Decrement(group string) {
+       if group == "" {
+               group = "default"
+       }
+       c.mu.Lock()
+       defer c.mu.Unlock()
+       if value, ok := c.data[group]; ok {
+               value--
+               if value <= 0 {
+                       delete(c.data, group)
+               } else {
+                       c.data[group] = value
                }
        }
 }

Review Comment:
   The Counter and DistributionCounter refactoring from atomic operations to 
map-based group tracking introduces significant complexity but lacks test 
coverage. Tests should verify thread safety, correct increment/decrement 
behavior per group, proper cleanup when counts reach zero, and the GetByGroup 
functionality.



##########
pkg/console/counter/component.go:
##########
@@ -64,6 +67,17 @@ func (c *managerComponent) Init(runtime.BuilderContext) 
error {
 }
 
 func (c *managerComponent) Start(rt runtime.Runtime, _ <-chan struct{}) error {
+       storeComponent, err := rt.GetComponent(runtime.ResourceStore)
+       if err != nil {
+               return err
+       }
+       storeRouter, ok := storeComponent.(store.Router)
+       if !ok {
+               return fmt.Errorf("component %s does not implement 
store.Router", runtime.ResourceStore)
+       }
+
+       c.initializeCountsFromStore(storeRouter)

Review Comment:
   The error returned by initializeCountsFromStore is being ignored. If 
initialization fails, the counter manager will start with incorrect counts but 
the application will continue running. This should either return the error to 
fail fast, or log a warning if partial initialization is acceptable.
   ```suggestion
        if err := c.initializeCountsFromStore(storeRouter); err != nil {
                fmt.Printf("warning: failed to initialize counter manager from 
store: %v\n", err)
        }
   ```



##########
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:" {
+                                       if cfg := 
cm.getDistributionConfig(kind, ProtocolCounter); cfg != nil {
+                                               for i := int64(0); i < count; 
i++ {
+                                                       
cfg.counter.Increment(mesh, key[9:])
+                                               }
+                                       }
+                               } else if len(key) > 8 && key[:8] == "release:" 
{
+                                       if cfg := 
cm.getDistributionConfig(kind, ReleaseCounter); cfg != nil {
+                                               for i := int64(0); i < count; 
i++ {
+                                                       
cfg.counter.Increment(mesh, key[8:])
+                                               }
+                                       }
+                               } else if len(key) > 11 && key[:11] == 
"discovery:" {
+                                       if cfg := 
cm.getDistributionConfig(kind, DiscoveryCounter); cfg != nil {
+                                               for i := int64(0); i < count; 
i++ {
+                                                       
cfg.counter.Increment(mesh, key[11:])
+                                               }
+                                       }
+                               }

Review Comment:
   Using magic numbers for string prefix lengths (9, 8, 11) makes the code 
fragile and error-prone. Consider using constants or the strings.HasPrefix 
function with explicit prefix strings to make the code more maintainable and 
less prone to errors if the prefixes change.



##########
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)
+                               }

Review Comment:
   The code repeatedly checks and initializes meshDistributions[mesh] on lines 
137, 145, and 151. This logic should be refactored to initialize the map once 
at the beginning of the instance processing block to improve code clarity and 
avoid redundant checks.
   ```suggestion
                                if meshDistributions[mesh] == nil {
                                        meshDistributions[mesh] = 
make(map[string]int64)
                                }
   
                                protocol := instance.Spec.GetProtocol()
                                if protocol != "" {
                                        
meshDistributions[mesh]["protocol:"+protocol]++
                                }
   
                                releaseVersion := 
instance.Spec.GetReleaseVersion()
                                if releaseVersion != "" {
                                        
meshDistributions[mesh]["release:"+releaseVersion]++
                                }
   ```



##########
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:" {
+                                       if cfg := 
cm.getDistributionConfig(kind, ProtocolCounter); cfg != nil {
+                                               for i := int64(0); i < count; 
i++ {
+                                                       
cfg.counter.Increment(mesh, key[9:])
+                                               }
+                                       }
+                               } else if len(key) > 8 && key[:8] == "release:" 
{
+                                       if cfg := 
cm.getDistributionConfig(kind, ReleaseCounter); cfg != nil {
+                                               for i := int64(0); i < count; 
i++ {
+                                                       
cfg.counter.Increment(mesh, key[8:])
+                                               }
+                                       }
+                               } else if len(key) > 11 && key[:11] == 
"discovery:" {
+                                       if cfg := 
cm.getDistributionConfig(kind, DiscoveryCounter); cfg != nil {
+                                               for i := int64(0); i < count; 
i++ {
+                                                       
cfg.counter.Increment(mesh, key[11:])
+                                               }
+                                       }
+                               }
+                       }
+               }
+       }
+
+       return nil
+}

Review Comment:
   The initializeCountsFromStore function, which is critical for ensuring 
correct counter initialization on startup, lacks test coverage. Tests should 
verify correct initialization from existing store data, proper handling of 
resources across multiple meshes, and correct distribution counter 
initialization for instances.



##########
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:" {
+                                       if cfg := 
cm.getDistributionConfig(kind, ProtocolCounter); cfg != nil {
+                                               for i := int64(0); i < count; 
i++ {
+                                                       
cfg.counter.Increment(mesh, key[9:])
+                                               }
+                                       }
+                               } else if len(key) > 8 && key[:8] == "release:" 
{
+                                       if cfg := 
cm.getDistributionConfig(kind, ReleaseCounter); cfg != nil {
+                                               for i := int64(0); i < count; 
i++ {
+                                                       
cfg.counter.Increment(mesh, key[8:])
+                                               }
+                                       }
+                               } else if len(key) > 11 && key[:11] == 
"discovery:" {
+                                       if cfg := 
cm.getDistributionConfig(kind, DiscoveryCounter); cfg != nil {
+                                               for i := int64(0); i < count; 
i++ {
+                                                       
cfg.counter.Increment(mesh, key[11:])
+                                               }
+                                       }

Review Comment:
   Using loops to call Increment multiple times is inefficient. Consider adding 
a bulk increment method to the DistributionCounter struct to improve 
performance during initialization, especially when dealing with large numbers 
of resources.



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