-
Notifications
You must be signed in to change notification settings - Fork 2.2k
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
feat: Support for asynchronously obtaining the backup file size #7660
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -8,7 +8,6 @@ import ( | |
"fmt" | ||
"os" | ||
"path" | ||
"sort" | ||
"strings" | ||
"sync" | ||
"time" | ||
|
@@ -30,7 +29,9 @@ type BackupService struct{} | |
type IBackupService interface { | ||
List() ([]dto.BackupInfo, error) | ||
SearchRecordsWithPage(search dto.RecordSearch) (int64, []dto.BackupRecords, error) | ||
LoadSize(req dto.RecordSearch) ([]dto.BackupFile, error) | ||
SearchRecordsByCronjobWithPage(search dto.RecordSearchByCronjob) (int64, []dto.BackupRecords, error) | ||
LoadSizeByCronjob(req dto.RecordSearchByCronjob) ([]dto.BackupFile, error) | ||
LoadOneDriveInfo() (dto.OneDriveInfo, error) | ||
DownloadRecord(info dto.DownloadRecord) (string, error) | ||
Create(backupDto dto.BackupOperate) error | ||
|
@@ -94,11 +95,29 @@ func (u *BackupService) SearchRecordsWithPage(search dto.RecordSearch) (int64, [ | |
return 0, nil, err | ||
} | ||
|
||
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 | ||
var list []dto.BackupRecords | ||
for _, item := range records { | ||
var itemRecord dto.BackupRecords | ||
if err := copier.Copy(&itemRecord, &item); err != nil { | ||
continue | ||
} | ||
list = append(list, itemRecord) | ||
} | ||
return total, list, err | ||
} | ||
|
||
func (u *BackupService) LoadSize(req dto.RecordSearch) ([]dto.BackupFile, error) { | ||
_, records, err := backupRepo.PageRecord( | ||
req.Page, req.PageSize, | ||
commonRepo.WithOrderBy("created_at desc"), | ||
commonRepo.WithByName(req.Name), | ||
commonRepo.WithByType(req.Type), | ||
backupRepo.WithByDetailName(req.DetailName), | ||
) | ||
if err != nil { | ||
return nil, err | ||
} | ||
return u.loadRecordSize(records) | ||
} | ||
|
||
func (u *BackupService) ListAppRecords(name, detailName, fileName string) ([]model.BackupRecord, error) { | ||
|
@@ -125,11 +144,27 @@ func (u *BackupService) SearchRecordsByCronjobWithPage(search dto.RecordSearchBy | |
return 0, nil, err | ||
} | ||
|
||
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 | ||
var list []dto.BackupRecords | ||
for _, item := range records { | ||
var itemRecord dto.BackupRecords | ||
if err := copier.Copy(&itemRecord, &item); err != nil { | ||
continue | ||
} | ||
list = append(list, itemRecord) | ||
} | ||
return total, list, err | ||
} | ||
|
||
func (u *BackupService) LoadSizeByCronjob(req dto.RecordSearchByCronjob) ([]dto.BackupFile, error) { | ||
_, records, err := backupRepo.PageRecord( | ||
req.Page, req.PageSize, | ||
commonRepo.WithOrderBy("created_at desc"), | ||
backupRepo.WithByCronID(req.CronjobID), | ||
) | ||
if err != nil { | ||
return nil, err | ||
} | ||
return u.loadRecordSize(records) | ||
} | ||
|
||
type loadSizeHelper struct { | ||
|
@@ -482,15 +517,14 @@ func (u *BackupService) loadAccessToken(backup *model.BackupAccount) error { | |
return nil | ||
} | ||
|
||
func (u *BackupService) loadRecordSize(records []model.BackupRecord) ([]dto.BackupRecords, error) { | ||
var datas []dto.BackupRecords | ||
func (u *BackupService) loadRecordSize(records []model.BackupRecord) ([]dto.BackupFile, error) { | ||
var datas []dto.BackupFile | ||
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 { | ||
return nil, errors.WithMessage(constant.ErrStructTransform, err.Error()) | ||
} | ||
var item dto.BackupFile | ||
item.ID = records[i].ID | ||
item.Name = records[i].FileName | ||
itemPath := path.Join(records[i].FileDir, records[i].FileName) | ||
if _, ok := clientMap[records[i].Source]; !ok { | ||
backup, err := backupRepo.Get(commonRepo.WithByType(records[i].Source)) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Here are the key aspects of the differences between the two versions: Differences in the
|
||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -48,10 +48,15 @@ | |
<el-table-column :label="$t('commons.table.name')" prop="fileName" show-overflow-tooltip /> | ||
<el-table-column :label="$t('file.size')" prop="size" show-overflow-tooltip> | ||
<template #default="{ row }"> | ||
<span v-if="row.size"> | ||
{{ computeSize(row.size) }} | ||
</span> | ||
<span v-else>-</span> | ||
<div v-if="row.hasLoad"> | ||
<span v-if="row.size"> | ||
{{ computeSize(row.size) }} | ||
</span> | ||
<span v-else>-</span> | ||
</div> | ||
<div v-if="!row.hasLoad"> | ||
<el-button link loading></el-button> | ||
</div> | ||
</template> | ||
</el-table-column> | ||
<el-table-column :label="$t('database.source')" prop="backupType"> | ||
|
@@ -110,7 +115,7 @@ import { computeSize, dateFormat, downloadFile } from '@/utils/util'; | |
import { getBackupList, handleBackup, handleRecover } from '@/api/modules/setting'; | ||
import i18n from '@/lang'; | ||
import DrawerHeader from '@/components/drawer-header/index.vue'; | ||
import { deleteBackupRecord, downloadBackupRecord, searchBackupRecords } from '@/api/modules/setting'; | ||
import { deleteBackupRecord, downloadBackupRecord, searchBackupRecords, loadBackupSize } from '@/api/modules/setting'; | ||
import { Backup } from '@/api/interface/backup'; | ||
import router from '@/routers'; | ||
import { MsgSuccess } from '@/utils/message'; | ||
|
@@ -197,6 +202,31 @@ const search = async () => { | |
loading.value = false; | ||
data.value = res.data.items || []; | ||
paginationConfig.total = res.data.total; | ||
if (paginationConfig.total !== 0) { | ||
loadSize(params); | ||
} | ||
}) | ||
.catch(() => { | ||
loading.value = false; | ||
}); | ||
}; | ||
|
||
const loadSize = async (params: any) => { | ||
await loadBackupSize(params) | ||
.then((res) => { | ||
let stats = res.data || []; | ||
if (stats.length === 0) { | ||
return; | ||
} | ||
for (const backup of data.value) { | ||
for (const item of stats) { | ||
if (backup.id === item.id) { | ||
backup.hasLoad = true; | ||
backup.size = item.size; | ||
break; | ||
} | ||
} | ||
} | ||
}) | ||
.catch(() => { | ||
loading.value = false; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The provided code includes some improvements that address potential issues and optimize the functionality:
Here's the optimized version of the relevant changes: @@ -110,7 +115,7 @@ import { computeSize, dateFormat, downloadFile } from '@/utils/util';
import { getBackupList, handleBackup, handleRecover } from '@/api/modules/setting';
import i18n from '@/lang';
import DrawerHeader from '@/components/drawer-header/index.vue';
-import { deleteBackupRecord, downloadBackupRecord, searchBackupRecords } from '@/api/modules/setting';
+import { deleteBackupRecord, downloadBackupRecord, searchBackupRecords, loadBackupSize } from '@/api/modules/setting';
const search = async () => {
try { Explanation: Removed redundant imports at the top and added a type hint in the |
||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
There are some minor changes in the comments and variable names across the files but no significant code differences that require immediate action. However, here are a few general optimizations:
@Description
,@Summary
headers have consistent formatting.Specific Changes
BaseApi.go
Updated
LoadBackupSize
andLoadBackupSizeByCronjob
functions to include comments on each request parameter.These updates ensure clarity and consistency in the API documentation while maintaining functionality.