Skip to content

Commit

Permalink
feat(xo-server/rest-api/dashboard): add S3 backup repositories inform…
Browse files Browse the repository at this point in the history
…ation
  • Loading branch information
MathieuRA committed Sep 9, 2024
1 parent 6c8d41d commit 9437476
Show file tree
Hide file tree
Showing 3 changed files with 49 additions and 21 deletions.
12 changes: 12 additions & 0 deletions @xen-orchestra/backups/RemoteAdapter.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ import fromEvent from 'promise-toolbox/fromEvent'
import groupBy from 'lodash/groupBy.js'
import pDefer from 'promise-toolbox/defer'
import pickBy from 'lodash/pickBy.js'
import reduce from 'lodash/reduce.js'
import tar from 'tar'
import zlib from 'zlib'

Expand Down Expand Up @@ -826,6 +827,17 @@ export class RemoteAdapter {
}
return metadata
}

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

// @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)
}
}

Object.assign(RemoteAdapter.prototype, {
Expand Down
7 changes: 7 additions & 0 deletions packages/xo-server/src/xo-mixins/remotes.mjs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import asyncMapSettled from '@xen-orchestra/async-map/legacy.js'
import Disposable from 'promise-toolbox/Disposable'
import Obfuscate from '@vates/obfuscate'
import { basename } from 'path'
import { createLogger } from '@xen-orchestra/log'
Expand All @@ -9,6 +10,7 @@ import { invalidParameters, noSuchObject } from 'xo-common/api-errors.js'
import { synchronized } from 'decorator-synchronized'

import patch from '../patch.mjs'
import { noop } from '../utils.mjs'
import { Remotes } from '../models/remote.mjs'

// ===================================================================
Expand Down Expand Up @@ -173,10 +175,15 @@ export default class {
: this.getRemoteHandler(remote.id).then(handler => handler.getInfo())

try {
const sizeUsedForBackup = await Disposable.use(this._app.getBackupsRemoteAdapter(remote), adapter =>
adapter.getTotalBackupSize()
).catch(noop)

await timeout.call(
promise.then(info => {
remotesInfo[remote.id] = {
...info,
sizeUsedForBackup,
encryption,
}
}),
Expand Down
51 changes: 30 additions & 21 deletions packages/xo-server/src/xo-mixins/rest-api.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -226,34 +226,43 @@ async function _getDashboardStats(app) {
}

try {
const remotes = await app.getAllRemotes()
const remotesInfo = await app.getAllRemotesInfo()

const backupRepositoriesSize = remotes.reduce(
(prev, remote) => {
const { type } = parse(remote.url)
const remoteInfo = remotesInfo[remote.id]

if (!remote.enabled || type === 's3' || remoteInfo === undefined) {
return prev
}

return {
available: prev.available + remoteInfo.available,
backups: 0, // @TODO: compute the space used by backups
other: 0, // @TODO: compute the space used by everything that is not a backup
total: prev.total + remoteInfo.size,
used: prev.used + remoteInfo.used,
}
const backupRepositoriesSize = {
s3: {
backups: 0,
},
{
other: {
available: 0,
backups: 0,
other: 0,
total: 0,
used: 0,
},
}

const remotes = await app.getAllRemotes()
const remotesInfo = await app.getAllRemotesInfo()

for (const remote of remotes) {
const { type } = parse(remote.url)
const remoteInfo = remotesInfo[remote.id]

if (!remote.enabled || remoteInfo === undefined) {
continue
}
)

const sizeUsedForBackup = remoteInfo.sizeUsedForBackup ?? 0
const isS3 = type === 's3'
const target = isS3 ? backupRepositoriesSize.s3 : backupRepositoriesSize.other

target.backups += sizeUsedForBackup
if (!isS3) {
target.available += remoteInfo.available
target.other += remoteInfo.used - sizeUsedForBackup
target.total += remoteInfo.size
target.used += remoteInfo.used
}
}

dashboard.backupRepositories = { size: backupRepositoriesSize }
} catch (error) {
console.error(error)
Expand Down

0 comments on commit 9437476

Please sign in to comment.