Skip to content

Commit

Permalink
perf: Optimize project list statistics
Browse files Browse the repository at this point in the history
  • Loading branch information
littlejiancc committed Feb 11, 2022
1 parent a62e2a7 commit 9ce89e0
Show file tree
Hide file tree
Showing 3 changed files with 11 additions and 25 deletions.
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

0 comments on commit 9ce89e0

Please sign in to comment.