Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(dop): Optimize project list statistics #4015

Merged
merged 1 commit into from
Feb 11, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 11 additions & 2 deletions modules/core-services/services/project/project.go
Original file line number Diff line number Diff line change
Expand Up @@ -1255,7 +1255,15 @@ func (p *Project) convertToProjectDTO(joined bool, project *model.Project) apist
l.WithError(err).Errorln("failed to Unmarshal project.ClusterConfig")
}

total, _ := p.db.GetApplicationCountByProjectID(project.ID)
totalApp, err := p.db.GetApplicationCountByProjectID(project.ID)
if err != nil {
l.WithError(err).Errorln("failed to count app")
}

totalMember, _, err := p.db.GetMembersWithoutExtraByScope(apistructs.ProjectScope, project.ID)
if err != nil {
l.WithError(err).Errorln("failed to count member")
}

projectDto := apistructs.ProjectDTO{
ID: uint64(project.ID),
Expand All @@ -1268,7 +1276,8 @@ func (p *Project) convertToProjectDTO(joined bool, project *model.Project) apist
Creator: project.UserID,
DDHook: project.DDHook,
Stats: apistructs.ProjectStats{
CountApplications: int(total),
CountApplications: int(totalApp),
CountMembers: totalMember,
},
ClusterConfig: clusterConfig,
RollbackConfig: rollbackConfig,
Expand Down
21 changes: 0 additions & 21 deletions modules/dop/endpoints/project.go
Original file line number Diff line number Diff line change
Expand Up @@ -305,27 +305,6 @@ func (e *Endpoints) ListProject(ctx context.Context, r *http.Request, vars map[s
return apierrors.ErrListProject.InternalError(err).ToResp(), nil
}

// rich statistical data
if params.PageSize <= 15 {
Once.Do(func() {
ProjectStatsCache = &sync.Map{}
})
for i := range pagingProjects.List {
prjID := int64(pagingProjects.List[i].ID)
stats, ok := ProjectStatsCache.Load(prjID)
if !ok {
logrus.Infof("get a new project %v add in cache", prjID)
stats, err = e.getProjectStats(uint64(prjID))
if err != nil {
logrus.Errorf("fail to getProjectStats,%v", err)
continue
}
ProjectStatsCache.Store(prjID, stats)
}
pagingProjects.List[i].Stats = *stats.(*apistructs.ProjectStats)
}
}

var userIDs []string
for _, v := range pagingProjects.List {
userIDs = append(userIDs, v.Owners...)
Expand Down
2 changes: 0 additions & 2 deletions modules/dop/initialize.go
Original file line number Diff line number Diff line change
Expand Up @@ -123,8 +123,6 @@ func (p *provider) Initialize(ctx servicehub.Context) error {

registerWebHook(bdl.Bdl)

go endpoints.SetProjectStatsCache()

// 注册 hook
if err := ep.RegisterEvents(); err != nil {
return err
Expand Down