From e606114f35d724d25a6136062b89f99c6a79cdee Mon Sep 17 00:00:00 2001 From: little-cui Date: Sat, 24 Feb 2018 14:17:05 +0800 Subject: [PATCH] SCB-339 Exclude the version in online services calculation --- server/govern/service.go | 28 +++++++++++++++++----------- 1 file changed, 17 insertions(+), 11 deletions(-) diff --git a/server/govern/service.go b/server/govern/service.go index 73780e54e..4a0abe875 100644 --- a/server/govern/service.go +++ b/server/govern/service.go @@ -365,25 +365,27 @@ func statistics(ctx context.Context) (*pb.Statistics, error) { key := apt.GetServiceIndexRootKey(domainProject) + "/" svcOpts := append(opts, registry.WithStrKey(key), - registry.WithPrefix(), - registry.WithKeyOnly()) + registry.WithPrefix()) respSvc, err := store.Store().ServiceIndex().Search(ctx, svcOpts...) if err != nil { return nil, err } - app := make(map[string]interface{}, respSvc.Count) - svcWithNonVersion := make(map[string]interface{}, respSvc.Count) + app := make(map[string]struct{}, respSvc.Count) + svcWithNonVersion := make(map[string]struct{}, respSvc.Count) + svcIdToNonVerKey := make(map[string]string, respSvc.Count) for _, kv := range respSvc.Kvs { - key, _ := pb.GetInfoFromSvcIndexKV(kv) - + key, val := pb.GetInfoFromSvcIndexKV(kv) if _, ok := app[key.AppId]; !ok { - app[key.AppId] = nil + app[key.AppId] = struct{}{} } key.Version = "" svcWithNonVersionKey := apt.GenerateServiceIndexKey(key) - svcWithNonVersion[svcWithNonVersionKey] = nil + if _, ok := svcWithNonVersion[svcWithNonVersionKey]; !ok { + svcWithNonVersion[svcWithNonVersionKey] = struct{}{} + } + svcIdToNonVerKey[util.BytesToStringWithNoCopy(val)] = svcWithNonVersionKey } result.Services.Count = int64(len(svcWithNonVersion)) @@ -400,11 +402,15 @@ func statistics(ctx context.Context) (*pb.Statistics, error) { return nil, err } - onlineServices := make(map[string]interface{}, respSvc.Count) + onlineServices := make(map[string]struct{}, respSvc.Count) for _, kv := range respIns.Kvs { serviceId, _, _, _ := pb.GetInfoFromInstKV(kv) - if _, ok := onlineServices[serviceId]; !ok { - onlineServices[serviceId] = nil + key, ok := svcIdToNonVerKey[serviceId] + if !ok { + continue + } + if _, ok := onlineServices[key]; !ok { + onlineServices[key] = struct{}{} } } result.Instances.Count = respIns.Count