Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Enhance(dashboard): optimize get policies request #1539

Merged
merged 1 commit into from
Dec 16, 2022
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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