From 00a1b7e811ebb62c4da7f7e949da1ded2d49f668 Mon Sep 17 00:00:00 2001 From: mathieuRA Date: Wed, 25 Sep 2024 11:05:37 +0200 Subject: [PATCH] feat(xo-server/rest-api/dashboard): avoid iterating over all XAPI objects --- packages/xo-server/src/xo-mixins/rest-api.mjs | 35 +++++++------------ packages/xo-server/src/xo.mjs | 2 ++ 2 files changed, 14 insertions(+), 23 deletions(-) diff --git a/packages/xo-server/src/xo-mixins/rest-api.mjs b/packages/xo-server/src/xo-mixins/rest-api.mjs index 78996abaf19..ac6e879afef 100644 --- a/packages/xo-server/src/xo-mixins/rest-api.mjs +++ b/packages/xo-server/src/xo-mixins/rest-api.mjs @@ -174,34 +174,23 @@ async function _getDashboardStats(app) { } } - const poolIds = new Set() - const hosts = [] - const writableSrs = [] - const nonReplicaVms = [] + const pools = Object.values(app.objects.indexes.type.pool) + const hosts = Object.values(app.objects.indexes.type.host) + const srs = Object.values(app.objects.indexes.type.SR) + const vms = Object.values(app.objects.indexes.type.VM) + + const writableSrs = srs.filter(isSrWritable) + const nonReplicaVms = vms.filter(vm => !isReplicaVm(vm)) const vmIdsProtected = new Set() const vmIdsUnprotected = new Set() - for (const obj of app.objects.values()) { - if (obj.type === 'host') { - hosts.push(obj) - poolIds.add(obj.$pool) - if (hvSupportedVersions !== undefined && !semver.satisfies(obj.version, hvSupportedVersions[obj.productBrand])) { - nHostsEol++ - } - } - - if (obj.type === 'SR') { - if (isSrWritable(obj)) { - writableSrs.push(obj) - } - } - - if (obj.type === 'VM' && !isReplicaVm(obj)) { - nonReplicaVms.push(obj) + hosts.forEach(host => { + if (hvSupportedVersions !== undefined && !semver.satisfies(host.version, hvSupportedVersions[host.productBrand])) { + nHostsEol++ } - } + }) - dashboard.nPools = poolIds.size + dashboard.nPools = pools.length dashboard.nHosts = hosts.length dashboard.nHostsEol = nHostsEol diff --git a/packages/xo-server/src/xo.mjs b/packages/xo-server/src/xo.mjs index a1a3da41578..ddaf911659b 100644 --- a/packages/xo-server/src/xo.mjs +++ b/packages/xo-server/src/xo.mjs @@ -12,6 +12,7 @@ import stubTrue from 'lodash/stubTrue.js' import SslCertificate from '@xen-orchestra/mixins/SslCertificate.mjs' import Tasks from '@xen-orchestra/mixins/Tasks.mjs' import { Collection as XoCollection } from 'xo-collection' +import { Index } from 'xo-collection/index.js' import { createClient as createRedisClient } from 'redis' import { createDebounceResource } from '@vates/disposable/debounceResource.js' import { createLogger } from '@xen-orchestra/log' @@ -41,6 +42,7 @@ export default class Xo extends EventEmitter { this._objects = new XoCollection() this._objects.createIndex('byRef', new XoUniqueIndex('_xapiRef')) + this._objects.createIndex('type', new Index('type')) this._httpRequestWatchers = { __proto__: null }