Skip to content

Commit

Permalink
Enhance(dashboard): optimize get policies request
Browse files Browse the repository at this point in the history
  • Loading branch information
topjohncian committed Nov 24, 2022
1 parent 4519dc0 commit e1c6e3f
Showing 1 changed file with 12 additions and 4 deletions.
16 changes: 12 additions & 4 deletions service/admin/policy.go
Original file line number Diff line number Diff line change
Expand Up @@ -318,12 +318,20 @@ func (service *AdminListService) Policies() serializer.Response {

// 统计每个策略的文件使用
statics := make(map[uint][2]int, len(res))
policyIds := make([]uint, 0, len(res))
for i := 0; i < len(res); i++ {
policyIds = append(policyIds, res[i].ID)
}

rows, _ := model.DB.Model(&model.File{}).Where("policy_id in (?)", policyIds).
Select("policy_id,count(id),sum(size)").Group("policy_id").Rows()

for rows.Next() {
policyId := uint(0)
total := [2]int{}
row := model.DB.Model(&model.File{}).Where("policy_id = ?", res[i].ID).
Select("count(id),sum(size)").Row()
row.Scan(&total[0], &total[1])
statics[res[i].ID] = total
rows.Scan(&policyId, &total[0], &total[1])

statics[policyId] = total
}

return serializer.Response{Data: map[string]interface{}{
Expand Down

0 comments on commit e1c6e3f

Please sign in to comment.