Skip to content

Commit

Permalink
Fix work bench issue data query (#3354)
Browse files Browse the repository at this point in the history
* Fix work bench issue data query

* update unit test

* Add comment

* Modify query
  • Loading branch information
shuofan authored Dec 15, 2021
1 parent 442f220 commit 8ed0350
Show file tree
Hide file tree
Showing 4 changed files with 43 additions and 20 deletions.
43 changes: 33 additions & 10 deletions modules/dop/dao/issue.go
Original file line number Diff line number Diff line change
Expand Up @@ -680,21 +680,44 @@ type IssueExpiryStatus struct {
ExpiryStatus ExpireType
}

func (client *DBClient) GetIssueExpiryStatusByProjects(req apistructs.WorkbenchRequest) ([]IssueExpiryStatus, error) {
sql := client.Table("dice_issues").Joins(joinState).Select("count(dice_issues.id) as issue_num, dice_issues.project_id, dice_issues.expiry_status")
sql = sql.Where("deleted = 0").Where("assignee = ? AND dice_issue_state.belong IN (?)", req.Assignees, req.StateBelongs)
if len(req.ProjectIDs) > 0 {
sql = sql.Where("dice_issues.project_id IN (?)", req.ProjectIDs)
func (client *DBClient) GetIssueExpiryStatusByProjects(req apistructs.WorkbenchRequest) ([]IssueExpiryStatus, int, error) {
sql := client.issueExpiryStatusQuery(req)
offset := (req.PageNo - 1) * req.PageSize
var res []IssueExpiryStatus
var total int
// paged projects with unfinished issues
if err := sql.Select("count(dice_issues.id) as issue_num, dice_issues.project_id, dice_issues.expiry_status").
Offset(offset).Limit(req.PageSize).Group("dice_issues.project_id").Find(&res).Error; err != nil {
return nil, 0, err
}
// total of matched projects
if err := sql.Select("count(distinct(dice_issues.project_id))").Count(&total).Error; err != nil {
return nil, 0, err
}
projectIDs := make([]uint64, 0, len(res))
for _, i := range res {
projectIDs = append(projectIDs, i.ProjectID)
}
req.ProjectIDs = projectIDs
sql = client.issueExpiryStatusQuery(req)
// query with matched projects
if err := sql.Select("count(dice_issues.id) as issue_num, dice_issues.project_id, dice_issues.expiry_status").
Group("dice_issues.project_id, dice_issues.expiry_status").Find(&res).Error; err != nil {
return nil, 0, err
}
return res, total, nil
}

func (client *DBClient) issueExpiryStatusQuery(req apistructs.WorkbenchRequest) *gorm.DB {
sql := client.Debug().Table("dice_issues").Joins(joinState)
sql = sql.Where("deleted = 0").Where("assignee IN (?) AND dice_issue_state.belong IN (?)", req.Assignees, req.StateBelongs)
if len(req.Type) > 0 {
sql = sql.Where("type IN (?)", req.Type)
}
offset := (req.PageNo - 1) * req.PageSize
var res []IssueExpiryStatus
if err := sql.Offset(offset).Limit(req.PageSize).Group("dice_issues.project_id, dice_issues.expiry_status").Find(&res).Error; err != nil {
return nil, err
if len(req.ProjectIDs) > 0 {
sql = sql.Where("dice_issues.project_id IN (?)", req.ProjectIDs)
}
return res, nil
return sql
}

func (client *DBClient) GetIssuesByProject(req apistructs.IssuePagingRequest) ([]Issue, uint64, error) {
Expand Down
12 changes: 6 additions & 6 deletions modules/dop/services/issue/issue.go
Original file line number Diff line number Diff line change
Expand Up @@ -1522,10 +1522,10 @@ func (svc *Issue) BatchUpdateIssuesSubscriber(req apistructs.IssueSubscriberBatc
return nil
}

func (svc *Issue) GetIssuesByStates(req apistructs.WorkbenchRequest) (map[uint64]*apistructs.WorkbenchProjectItem, error) {
stats, err := svc.db.GetIssueExpiryStatusByProjects(req)
func (svc *Issue) GetIssuesByStates(req apistructs.WorkbenchRequest) (map[uint64]*apistructs.WorkbenchProjectItem, int, error) {
stats, total, err := svc.db.GetIssueExpiryStatusByProjects(req)
if err != nil {
return nil, err
return nil, 0, err
}

projectMap := make(map[uint64]*apistructs.WorkbenchProjectItem)
Expand Down Expand Up @@ -1559,7 +1559,7 @@ func (svc *Issue) GetIssuesByStates(req apistructs.WorkbenchRequest) (map[uint64

pMap, err := svc.bdl.GetProjectsMap(projectIDs)
if err != nil {
return nil, err
return nil, 0, err
}

for i := range projectMap {
Expand All @@ -1568,7 +1568,7 @@ func (svc *Issue) GetIssuesByStates(req apistructs.WorkbenchRequest) (map[uint64
req.ProjectID = dto.ID
issues, total, err := svc.db.GetIssuesByProject(req.IssuePagingRequest)
if err != nil {
return nil, err
return nil, 0, err
}
issueList := make([]apistructs.Issue, 0, len(issues))
for _, v := range issues {
Expand All @@ -1584,7 +1584,7 @@ func (svc *Issue) GetIssuesByStates(req apistructs.WorkbenchRequest) (map[uint64
}
}

return projectMap, nil
return projectMap, total, nil
}

func (svc *Issue) GetAllIssuesByProject(req apistructs.IssueListRequest) ([]dao.IssueItem, error) {
Expand Down
4 changes: 2 additions & 2 deletions modules/dop/services/workbench/workbench.go
Original file line number Diff line number Diff line change
Expand Up @@ -176,13 +176,13 @@ func (w *Workbench) GetUndoneProjectItems(req apistructs.WorkbenchRequest, userI
Asc: true,
},
}
projectMap, err := w.issueSvc.GetIssuesByStates(req)
projectMap, total, err := w.issueSvc.GetIssuesByStates(req)
if err != nil {
return nil, err
}

res := &apistructs.WorkbenchResponse{}
res.Data.TotalProject = len(projectMap)
res.Data.TotalProject = total
res.Data.List = make([]*apistructs.WorkbenchProjectItem, 0)
for _, v := range projectMap {
res.Data.List = append(res.Data.List, v)
Expand Down
4 changes: 2 additions & 2 deletions modules/dop/services/workbench/workbench_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -72,10 +72,10 @@ func TestWorkbench_GetUndoneProjectItems(t *testing.T) {
},
}
m := monkey.PatchInstanceMethod(reflect.TypeOf(issueSvc), "GetIssuesByStates",
func(issueSvc *issue.Issue, req apistructs.WorkbenchRequest) (map[uint64]*apistructs.WorkbenchProjectItem, error) {
func(issueSvc *issue.Issue, req apistructs.WorkbenchRequest) (map[uint64]*apistructs.WorkbenchProjectItem, int, error) {
return map[uint64]*apistructs.WorkbenchProjectItem{
1: item,
}, nil
}, 1, nil
})
defer m.Unpatch()

Expand Down

0 comments on commit 8ed0350

Please sign in to comment.