diff --git a/packages/xo-server-usage-report/src/index.js b/packages/xo-server-usage-report/src/index.js index c929a40ba..4738cc458 100644 --- a/packages/xo-server-usage-report/src/index.js +++ b/packages/xo-server-usage-report/src/index.js @@ -47,7 +47,7 @@ function computeMin (values) { return min } function computeCpuMax (cpus) { - return computeMax(cpus.map(computeMax)) + return sortArray(cpus.map(computeMax)) } function computeCpuMin (cpus) { return computeMin(cpus.map(computeMin)) @@ -55,6 +55,22 @@ function computeCpuMin (cpus) { function computeCpuMean (cpus) { return computeMean(cpus.map(computeMean)) } + +function compareNumbersDesc (a, b) { + if (a > b) { + return -1 + } + if (a < b) { + return 1 + } + return 0 +} +function sortArray (values) { + let n = 3 + let sort = values.sort(compareNumbersDesc) + return sort.slice(0, n) +} + // =================================================================== class UsageReportPlugin { @@ -67,10 +83,60 @@ class UsageReportPlugin { this.mailsReceivers = emails } load () { + const this_ = this + // TOP Max Cpu + this._unsets.push(this._xo.api.addMethod('generateCpu', async ({ machine, granularity }) => { + const machineStats = await this_._xo.getXapiHostStats(this_._xo.getObject(machine), granularity) + let maxCpu = computeCpuMax(machineStats.stats.cpus) + return { + 'max': maxCpu + } + })) + // TOP Max Load + // xo-cli generate machine=4a2dccec-83ff-4212-9e16-44fbc0527961 granularity=days + this._unsets.push(this._xo.api.addMethod('generateLoad', async ({ machine, granularity }) => { + const machineStats = await this_._xo.getXapiHostStats(this_._xo.getObject(machine), granularity) + let maxLoad = sortArray(machineStats.stats.load) + return { + 'max': maxLoad + } + })) + // TOP Max Memory + this._unsets.push(this._xo.api.addMethod('generateMemory', async ({ machine, granularity }) => { + const machineStats = await this_._xo.getXapiHostStats(this_._xo.getObject(machine), granularity) + let maxMemory = sortArray(machineStats.stats.memory) + return { + 'max': maxMemory + } + })) + // TOP Max MemoryUsed + this._unsets.push(this._xo.api.addMethod('generateMemoryUsed', async ({ machine, granularity }) => { + const machineStats = await this_._xo.getXapiHostStats(this_._xo.getObject(machine), granularity) + let maxMemoryUsed = sortArray(machineStats.stats.memoryUsed) + return { + 'max': maxMemoryUsed + } + })) +// ============================================================================= + // STATS min, max, mean + + // Cpus + this._unsets.push(this._xo.api.addMethod('generateCpuReport', async ({ machine, granularity }) => { + const machineStats = await this_._xo.getXapiHostStats(this_._xo.getObject(machine), granularity) + let maxCpu = computeCpuMax(machineStats.stats.cpus) + let minCpu = computeCpuMin(machineStats.stats.cpus) + let meanCpu = computeCpuMean(machineStats.stats.cpus) + + return { + 'max': maxCpu, + 'min': minCpu, + 'mean': meanCpu + } + })) + // Load // xo-cli generateLoadReport machine=4a2dccec-83ff-4212-9e16-44fbc0527961 granularity=days this._unsets.push(this._xo.api.addMethod('generateLoadReport', async ({ machine, granularity }) => { - const machineStats = await this._xo.getXapiHostStats(machine, granularity) - + const machineStats = await this_._xo.getXapiHostStats(this_._xo.getObject(machine), granularity) let maxLoad = computeMax(machineStats.stats.load) let minLoad = computeMin(machineStats.stats.load) let meanLoad = computeMean(machineStats.stats.load) @@ -81,43 +147,39 @@ class UsageReportPlugin { 'mean': meanLoad } })) - - // memory - this._unsets.push(this._xo.api.addMethod('generateMemoryReport', ({ machine, granularity }) => { - // TODO - - const machineStats = stats[`${machine}_${granularity}`] + // Memory + this._unsets.push(this._xo.api.addMethod('generateMemoryReport', async ({ machine, granularity }) => { + const machineStats = await this_._xo.getXapiHostStats(this_._xo.getObject(machine), granularity) let maxMemory = computeMax(machineStats.stats.memory) let minMemory = computeMin(machineStats.stats.memory) let meanMemory = computeMean(machineStats.stats.memory) + return { 'max': maxMemory, 'min': minMemory, 'mean': meanMemory } })) - // memoryUsed - this._unsets.push(this._xo.api.addMethod('generateMemoryUsedReport', ({ machine, granularity }) => { - // TODO - - const machineStats = stats[`${machine}_${granularity}`] + // MemoryUsed + this._unsets.push(this._xo.api.addMethod('generateMemoryUsedReport', async ({ machine, granularity }) => { + const machineStats = await this_._xo.getXapiHostStats(this_._xo.getObject(machine), granularity) let maxMemoryUsed = computeMax(machineStats.stats.memoryUsed) let minMemoryUsed = computeMin(machineStats.stats.memoryUsed) let meanMemoryUsed = computeMean(machineStats.stats.memoryUsed) + return { 'max': maxMemoryUsed, 'min': minMemoryUsed, 'mean': meanMemoryUsed } })) - // memoryFree - this._unsets.push(this._xo.api.addMethod('generateMemoryFreeReport', ({ machine, granularity }) => { - // TODO - - const machineStats = stats[`${machine}_${granularity}`] + // MemoryFree + this._unsets.push(this._xo.api.addMethod('generateMemoryFreeReport', async ({ machine, granularity }) => { + const machineStats = await this_._xo.getXapiHostStats(this_._xo.getObject(machine), granularity) let maxMemoryFree = computeMax(machineStats.stats.memoryFree) let minMemoryFree = computeMin(machineStats.stats.memoryFree) let meanMemoryFree = computeMean(machineStats.stats.memoryFree) + return { 'max': maxMemoryFree, 'min': minMemoryFree, @@ -134,6 +196,7 @@ class UsageReportPlugin { this._unsets.length = 0 } } + /* if (this._xo.sendEmail) { await this._xo.sendEmail({ to: this._mailsReceivers,