From 10eef260d412dbff4300e85b31dbf7e82557c1c8 Mon Sep 17 00:00:00 2001 From: ssongliu <73214554+ssongliu@users.noreply.github.com> Date: Mon, 29 Jan 2024 16:34:49 +0800 Subject: [PATCH] =?UTF-8?q?perf:=20=E4=BC=98=E5=8C=96=E8=AE=A1=E5=88=92?= =?UTF-8?q?=E4=BB=BB=E5=8A=A1=E5=A4=87=E4=BB=BD=E5=88=97=E8=A1=A8=E6=89=93?= =?UTF-8?q?=E5=BC=80=E9=80=9F=E5=BA=A6=20(#3740)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- backend/app/service/backup.go | 20 ++++++++++++++++---- 1 file changed, 16 insertions(+), 4 deletions(-) diff --git a/backend/app/service/backup.go b/backend/app/service/backup.go index 934634b13..d3765c735 100644 --- a/backend/app/service/backup.go +++ b/backend/app/service/backup.go @@ -8,7 +8,9 @@ import ( "fmt" "os" "path" + "sort" "strings" + "sync" "time" "github.com/1Panel-dev/1Panel/backend/app/dto" @@ -81,7 +83,6 @@ func (u *BackupService) List() ([]dto.BackupInfo, error) { func (u *BackupService) SearchRecordsWithPage(search dto.RecordSearch) (int64, []dto.BackupRecords, error) { total, records, err := backupRepo.PageRecord( search.Page, search.PageSize, - commonRepo.WithOrderBy("created_at desc"), commonRepo.WithByName(search.Name), commonRepo.WithByType(search.Type), backupRepo.WithByDetailName(search.DetailName), @@ -91,13 +92,15 @@ func (u *BackupService) SearchRecordsWithPage(search dto.RecordSearch) (int64, [ } datas, err := u.loadRecordSize(records) + sort.Slice(datas, func(i, j int) bool { + return datas[i].CreatedAt.After(datas[j].CreatedAt) + }) return total, datas, err } func (u *BackupService) SearchRecordsByCronjobWithPage(search dto.RecordSearchByCronjob) (int64, []dto.BackupRecords, error) { total, records, err := backupRepo.PageRecord( search.Page, search.PageSize, - commonRepo.WithOrderBy("created_at desc"), backupRepo.WithByCronID(search.CronjobID), ) if err != nil { @@ -105,6 +108,9 @@ func (u *BackupService) SearchRecordsByCronjobWithPage(search dto.RecordSearchBy } datas, err := u.loadRecordSize(records) + sort.Slice(datas, func(i, j int) bool { + return datas[i].CreatedAt.After(datas[j].CreatedAt) + }) return total, datas, err } @@ -437,6 +443,7 @@ func (u *BackupService) loadAccessToken(backup *model.BackupAccount) error { func (u *BackupService) loadRecordSize(records []model.BackupRecord) ([]dto.BackupRecords, error) { var datas []dto.BackupRecords clientMap := make(map[string]loadSizeHelper) + var wg sync.WaitGroup for i := 0; i < len(records); i++ { var item dto.BackupRecords if err := copier.Copy(&item, &records[i]); err != nil { @@ -464,10 +471,15 @@ func (u *BackupService) loadRecordSize(records []model.BackupRecord) ([]dto.Back continue } if clientMap[records[i].Source].isOk { - item.Size, _ = clientMap[records[i].Source].client.Size(path.Join(clientMap[records[i].Source].backupPath, itemPath)) + wg.Add(1) + go func(index int) { + item.Size, _ = clientMap[records[index].Source].client.Size(path.Join(clientMap[records[index].Source].backupPath, itemPath)) + datas = append(datas, item) + wg.Done() + }(i) } - datas = append(datas, item) } + wg.Wait() return datas, nil }