tianxiaoliang commented on a change in pull request #779:
URL:
https://github.com/apache/servicecomb-service-center/pull/779#discussion_r542056393
##########
File path: datasource/mongo/util.go
##########
@@ -30,3 +39,71 @@ func StringBuilder(data []string) string {
}
return str.String()
}
+
+func statistics(ctx context.Context, withShared bool) (*pb.Statistics, error) {
+ result := &pb.Statistics{
+ Services: &pb.StService{},
+ Instances: &pb.StInstance{},
+ Apps: &pb.StApp{},
+ }
+ domain := util.ParseDomain(ctx)
+ project := util.ParseProject(ctx)
+
+ filter := bson.M{ColumnDomain: domain, ColumnProject: project}
+
+ services, err := GetServices(ctx, filter)
+ if err != nil {
+ return nil, err
+ }
+ app := make(map[string]struct{}, len(services))
+ svcWithNonVersion := make(map[discovery.MicroServiceKey]struct{},
len(services))
+ serviceIDs := make(map[string]*discovery.MicroServiceKey, len(services))
+ for _, svc := range services {
+ key := TransServiceToKey(domain, project, svc)
+ if !withShared && core.IsGlobal(key) {
+ continue
+ }
+ if _, ok := app[key.AppId]; !ok {
+ app[key.AppId] = struct{}{}
+ }
+
+ key.Version = ""
+ if _, ok := svcWithNonVersion[*key]; !ok {
+ svcWithNonVersion[*key] = struct{}{}
+ }
+ serviceIDs[svc.ServiceId] = key
+ }
+ result.Services.Count = int64(len(svcWithNonVersion))
+ result.Apps.Count = int64(len(app))
+ for sid := range serviceIDs {
+ num, err := GetInstanceCountOfOneService(ctx, sid)
+ if err != nil {
+ return nil, err
+ }
+ result.Instances.CountByDomain = result.Instances.CountByDomain
+ num
+ }
+ instances, err := GetInstances(ctx, filter)
+ if err != nil {
+ return nil, err
+ }
+ var onlineServices int64
+ for _, ins := range instances {
+ _, ok := serviceIDs[ins.InstanceInfo.ServiceId]
+ if !ok {
+ continue
+ }
+ onlineServices++
+ }
+ result.Services.OnlineCount = onlineServices
+ return result, nil
+}
+
+func TransServiceToKey(domain, project string, service *pb.MicroService)
*discovery.MicroServiceKey {
Review comment:
这个方法etcd的实现和mongodb应该用一套。之前肯定有这个类似方法
##########
File path: datasource/mongo/mongo.go
##########
@@ -39,7 +44,7 @@ type DataSource struct {
func NewDataSource(opts datasource.Options) (datasource.DataSource, error) {
// TODO: construct a reasonable DataSource instance
- log.Warn("data source enable mongo mode")
+ log.Warn("dependency data source enable etcd mode")
Review comment:
为何mongo的实现写了etcd模式
##########
File path: datasource/mongo/util.go
##########
@@ -30,3 +39,71 @@ func StringBuilder(data []string) string {
}
return str.String()
}
+
+func statistics(ctx context.Context, withShared bool) (*pb.Statistics, error) {
Review comment:
和etcd一定能抽出公共部分,mongo,etcd应该只负责查询出数据,然后传给共同函数,61行开始的逻辑计算应该是共通的
----------------------------------------------------------------
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.
For queries about this service, please contact Infrastructure at:
[email protected]