diff --git a/modules/core-services/dao/application.go b/modules/core-services/dao/application.go index ca329f5ca4d..34f44297030 100644 --- a/modules/core-services/dao/application.go +++ b/modules/core-services/dao/application.go @@ -198,3 +198,16 @@ func (client *DBClient) GetRuntimeCountByAppIDS(appIDS []int64) ([]model.Applica } return counters, nil } + +// ListUnblockAppCountsByProjectIDS count project's unblock app nums by unblock time +func (client *DBClient) ListUnblockAppCountsByProjectIDS(projectIDS []uint64) ([]model.ProjectUnblockAppCount, error) { + counters := make([]model.ProjectUnblockAppCount, 0) + if err := client.Table("dice_app"). + Where("project_id in (?) and unblock_start < now() and unblock_end > now()", projectIDS). + Group("project_id"). + Select("count(*) as unblock_app_count, project_id"). + Scan(&counters).Error; err != nil { + return nil, err + } + return counters, nil +} diff --git a/modules/core-services/model/project.go b/modules/core-services/model/project.go index aae698133e5..3393c48a4d8 100644 --- a/modules/core-services/model/project.go +++ b/modules/core-services/model/project.go @@ -54,3 +54,8 @@ func (p *Project) GetClusterConfig() map[string]string { _ = json.Unmarshal([]byte(p.ClusterConfig), &clusterConfig) return clusterConfig } + +type ProjectUnblockAppCount struct { + UnblockAppCount int64 `json:"unblock_app_count"` + ProjectID int64 `json:"project_id"` +} diff --git a/modules/core-services/services/project/project.go b/modules/core-services/services/project/project.go index 4fbd66534a8..f466cd67a5a 100644 --- a/modules/core-services/services/project/project.go +++ b/modules/core-services/services/project/project.go @@ -1053,17 +1053,13 @@ func (p *Project) ListPublicProjects(userID string, params *apistructs.ProjectLi return nil, err } - // 获取每个项目的app信息 - now := time.Now() - apps, err := p.db.GetApplicationsByProjectIDs(projectIDs) + unblockAppCounts, err := p.ListUnblockAppCountsByProjectIDS(projectIDs) if err != nil { - return nil, err + return nil, errors.Errorf("failed to get unblock apps, err: %v", err) } - for _, app := range apps { - if app.UnblockStart != nil && app.UnblockEnd != nil && - now.Before(*app.UnblockEnd) && now.After(*app.UnblockStart) { - projectBlockStatus[uint64(app.ProjectID)] = "unblocked" - break + for _, counter := range unblockAppCounts { + if counter.UnblockAppCount > 0 { + projectBlockStatus[uint64(counter.ProjectID)] = "unblocked" } } @@ -1159,18 +1155,13 @@ func (p *Project) ListJoinedProjects(orgID int64, userID string, params *apistru if err != nil { return nil, errors.Errorf("failed to get projects, (%v)", err) } - now := time.Now() - for _, proj := range projects { - apps, err := p.db.GetProjectApplications(proj.ID) - if err != nil { - return nil, errors.Errorf("failed to get app, proj(%d): %v", proj.ID, err) - } - for _, app := range apps { - if app.UnblockStart != nil && app.UnblockEnd != nil && - now.Before(*app.UnblockEnd) && now.After(*app.UnblockStart) { - projectBlockStatus[uint64(proj.ID)] = "unblocked" - break - } + unblockAppCounts, err := p.ListUnblockAppCountsByProjectIDS(projectIDs) + if err != nil { + return nil, errors.Errorf("failed to get unblock apps, err: %v", err) + } + for _, counter := range unblockAppCounts { + if counter.UnblockAppCount > 0 { + projectBlockStatus[uint64(counter.ProjectID)] = "unblocked" } } @@ -1823,3 +1814,10 @@ func (p *Project) updateMemberCache(projectID uint64) (*memberCache, bool, error return member, true, nil } } + +func (p *Project) ListUnblockAppCountsByProjectIDS(projectIDS []uint64) ([]model.ProjectUnblockAppCount, error) { + if len(projectIDS) == 0 { + return nil, nil + } + return p.db.ListUnblockAppCountsByProjectIDS(projectIDS) +} diff --git a/modules/core-services/services/project/project_test.go b/modules/core-services/services/project/project_test.go index 86cd6c8892c..972a946fba6 100644 --- a/modules/core-services/services/project/project_test.go +++ b/modules/core-services/services/project/project_test.go @@ -395,3 +395,19 @@ func Test_defaultResourceConfig(t *testing.T) { t.Fatal("clusters names error in ResourceConfig") } } + +func TestListUnblockAppCountsByProjectIDS(t *testing.T) { + db := &dao.DBClient{} + m := monkey.PatchInstanceMethod(reflect.TypeOf(db), "ListUnblockAppCountsByProjectIDS", + func(db *dao.DBClient, projectIDS []uint64) ([]model.ProjectUnblockAppCount, error) { + return []model.ProjectUnblockAppCount{{ProjectID: 1, UnblockAppCount: 1}}, nil + }) + defer m.Unpatch() + p := Project{db: db} + emptyCounts, err := p.ListUnblockAppCountsByProjectIDS([]uint64{}) + assert.NoError(t, err) + assert.Equal(t, []model.ProjectUnblockAppCount(nil), emptyCounts) + counts, err := p.ListUnblockAppCountsByProjectIDS([]uint64{1}) + assert.NoError(t, err) + assert.Equal(t, counts[0].UnblockAppCount, int64(1)) +}