Skip to content

Commit

Permalink
batch query project's unblock app (#3592)
Browse files Browse the repository at this point in the history
  • Loading branch information
chengjoey authored Dec 31, 2021
1 parent 5497e9e commit 4b70b6a
Show file tree
Hide file tree
Showing 4 changed files with 53 additions and 21 deletions.
13 changes: 13 additions & 0 deletions modules/core-services/dao/application.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
5 changes: 5 additions & 0 deletions modules/core-services/model/project.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"`
}
40 changes: 19 additions & 21 deletions modules/core-services/services/project/project.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
}
}

Expand Down Expand Up @@ -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"
}
}

Expand Down Expand Up @@ -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)
}
16 changes: 16 additions & 0 deletions modules/core-services/services/project/project_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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))
}

0 comments on commit 4b70b6a

Please sign in to comment.