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


##########
pkg/console/counter/manager.go:
##########
@@ -0,0 +1,317 @@
+/*
+ * 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 counter
+
+import (
+       "fmt"
+       "math"
+
+       "k8s.io/client-go/tools/cache"
+
+       "github.com/apache/dubbo-admin/pkg/core/events"
+       meshresource 
"github.com/apache/dubbo-admin/pkg/core/resource/apis/mesh/v1alpha1"
+       resmodel "github.com/apache/dubbo-admin/pkg/core/resource/model"
+)
+
+type CounterType string
+
+type FieldExtractor func(resmodel.Resource) string
+
+const (
+       ProtocolCounter  CounterType = "protocol"
+       ReleaseCounter   CounterType = "release"
+       DiscoveryCounter CounterType = "discovery"
+)
+
+type CounterManager interface {
+       RegisterSimpleCounter(kind resmodel.ResourceKind)
+       RegisterDistributionCounter(kind resmodel.ResourceKind, metric 
CounterType, extractor FieldExtractor)
+       Count(kind resmodel.ResourceKind) int
+       Distribution(metric CounterType) map[string]int
+       Reset()
+       Bind(bus events.EventBus) error
+}
+
+type distributionCounterConfig struct {
+       counterType CounterType
+       counter     *DistributionCounter
+       extractor   func(resmodel.Resource) string
+}
+
+type counterManager struct {
+       simpleCounters      map[resmodel.ResourceKind]*Counter
+       distributionConfigs 
map[resmodel.ResourceKind][]*distributionCounterConfig
+       distributionByType  map[CounterType]*DistributionCounter
+}
+
+func NewCounterManager() CounterManager {
+       return newCounterManager()
+}
+
+func newCounterManager() *counterManager {
+       cm := &counterManager{
+               simpleCounters:      make(map[resmodel.ResourceKind]*Counter),
+               distributionConfigs: 
make(map[resmodel.ResourceKind][]*distributionCounterConfig),
+               distributionByType:  make(map[CounterType]*DistributionCounter),
+       }
+
+       cm.RegisterSimpleCounter(meshresource.ApplicationKind)
+       cm.RegisterSimpleCounter(meshresource.ServiceKind)
+       cm.RegisterSimpleCounter(meshresource.InstanceKind)
+
+       cm.RegisterDistributionCounter(meshresource.InstanceKind, 
ProtocolCounter, instanceProtocolKey)
+       cm.RegisterDistributionCounter(meshresource.InstanceKind, 
ReleaseCounter, instanceReleaseKey)
+       cm.RegisterDistributionCounter(meshresource.InstanceKind, 
DiscoveryCounter, instanceMeshKey)
+
+       return cm
+}
+
+func (cm *counterManager) RegisterSimpleCounter(kind resmodel.ResourceKind) {
+       if kind == "" {
+               return
+       }
+       if _, exists := cm.simpleCounters[kind]; exists {
+               return
+       }
+       cm.simpleCounters[kind] = NewCounter(string(kind))
+}
+
+func (cm *counterManager) RegisterDistributionCounter(kind 
resmodel.ResourceKind, metric CounterType, extractor FieldExtractor) {
+       if kind == "" || metric == "" {
+               return
+       }
+       counter := cm.distributionByType[metric]
+       if counter == nil {
+               counter = NewDistributionCounter(string(metric))
+               cm.distributionByType[metric] = counter
+       }
+
+       configs := cm.distributionConfigs[kind]
+       for _, cfg := range configs {
+               if cfg.counterType == metric {
+                       cfg.counter = counter
+                       cfg.extractor = extractor
+                       return
+               }
+       }
+
+       cm.distributionConfigs[kind] = append(configs, 
&distributionCounterConfig{
+               counterType: metric,
+               counter:     counter,
+               extractor:   extractor,
+       })
+}
+
+func (cm *counterManager) Reset() {
+       for _, counter := range cm.simpleCounters {
+               counter.Reset()
+       }
+       for _, counter := range cm.distributionByType {
+               counter.Reset()
+       }
+}
+
+func (cm *counterManager) Count(kind resmodel.ResourceKind) int {
+       if counter, exists := cm.simpleCounters[kind]; exists {
+               return safeInt64ToInt(counter.Get())
+       }
+       return 0
+}
+
+func (cm *counterManager) Distribution(metric CounterType) map[string]int {
+       counter, exists := cm.distributionByType[metric]
+       if !exists {
+               return map[string]int{}
+       }
+       raw := counter.GetAll()
+       result := make(map[string]int, len(raw))
+       for k, v := range raw {
+               result[k] = safeInt64ToInt(v)

Review Comment:
   suggestion: 这里可以直接在接口层面返回int64,因为指标数量可能确实会超过int32



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