Skip to content

Commit

Permalink
add recusrive fn to compute backup instead code duplication
Browse files Browse the repository at this point in the history
  • Loading branch information
MathieuRA committed Sep 12, 2024
1 parent bcad776 commit 25fe0c7
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 9 deletions.
19 changes: 15 additions & 4 deletions @xen-orchestra/backups/RemoteAdapter.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -828,15 +828,26 @@ export class RemoteAdapter {
return metadata
}

#computeTotalBackupSizeRecursively(backups) {
return reduce(
backups,
(prev, backup) => {
const _backup = Array.isArray(backup) ? this.#computeTotalBackupSizeRecursively(backup) : backup
return {
onDisk: prev.onDisk + (_backup.onDisk ?? _backup.size),
}
},
{ onDisk: 0 }
)
}

async getTotalVmBackupSize() {
const vmBackups = await this.listAllVmBackups()
return reduce(vmBackups, (sum, backups) => sum + backups.reduce((sum, backup) => sum + backup.size, 0), 0)
return this.#computeTotalBackupSizeRecursively(await this.listAllVmBackups())
}

// @TODO: add `getTotalXoBackupSize` and `getTotalPoolBackupSize` once `size` is implemented
async getTotalBackupSize() {
const backupsSize = await Promise.all([this.getTotalVmBackupSize()])
return backupsSize.reduce((sum, backupSize) => sum + backupSize)
return this.#computeTotalBackupSizeRecursively(await Promise.all([this.getTotalVmBackupSize()]))
}
}

Expand Down
4 changes: 2 additions & 2 deletions packages/xo-server/src/xo-mixins/remotes.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -174,7 +174,7 @@ export default class {
})
: this.getRemoteHandler(remote.id).then(handler => handler.getInfo())

const sizeUsedForBackups = await Disposable.use(this._app.getBackupsRemoteAdapter(remote), adapter =>
const totalBackupSize = await Disposable.use(this._app.getBackupsRemoteAdapter(remote), adapter =>
adapter.getTotalBackupSize()
).catch(noop)

Expand All @@ -183,7 +183,7 @@ export default class {
promise.then(info => {
remotesInfo[remote.id] = {
...info,
sizeUsedForBackups,
totalBackupSize,
encryption,
}
}),
Expand Down
7 changes: 4 additions & 3 deletions packages/xo-server/src/xo-mixins/rest-api.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -240,14 +240,15 @@ async function _getDashboardStats(app) {
continue
}

const { available, size, sizeUsedForBackups = 0, used } = backupRepositoryInfo
const { available, size, totalBackupSize, used } = backupRepositoryInfo

const isS3 = type === 's3'
const target = isS3 ? s3Brsize : otherBrSize

target.backups += sizeUsedForBackups
target.backups += totalBackupSize?.onDisk ?? 0
if (!isS3) {
target.available += available
target.other += used - sizeUsedForBackups
target.other += used - (totalBackupSize?.onDisk ?? 0)
target.total += size
target.used += used
}
Expand Down

0 comments on commit 25fe0c7

Please sign in to comment.