promise-toolbox: all to resolve promises in object

This commit is contained in:
Pierre 2016-02-10 12:25:54 +01:00
parent c274486057
commit 3687a230e1
2 changed files with 16 additions and 21 deletions

View File

@ -39,6 +39,7 @@
"mocha": "^2.3.4",
"must": "^0.13.1",
"nyc": "^5.2.0",
"promise-toolbox": "^0.1.1",
"source-map-support": "^0.4.0",
"standard": "^5.4.1",
"trace": "^2.0.2"

View File

@ -1,4 +1,5 @@
import forEach from 'lodash.foreach'
import { all } from 'promise-toolbox'
export const configurationSchema = {
type: 'object',
@ -135,45 +136,38 @@ class UsageReportPlugin {
return hostMean
}))
// Single host: get stats from its VMs.
// Returns { vm1_Id: vm1_Stats, vm2_Id: vm2_Stats, ... }
const _getHostVmsStats = async (machine, granularity) => {
const host = await this_._xo.getObject(machine)
const objects = await this_._xo.getObjects()
const promises = []
const vmIds = []
const promises = {}
forEach(objects, (obj) => {
if (obj.type === 'VM' && obj.power_state === 'Running' && obj.$poolId === host.$poolId) {
vmIds.push(obj.id)
promises.push(this_._xo.getXapiVmStats(obj, granularity))
promises[obj.id] = this_._xo.getXapiVmStats(obj, granularity)
}
})
const vmsStatsArray = await Promise.all(promises)
const vmStats = {}
forEach(vmsStatsArray, (stats, index) => {
vmStats[vmIds[index]] = stats
})
return vmStats
return promises::all()
}
this._unsets.push(this._xo.api.addMethod('generateHostVmsReport', async ({ machine, granularity }) => {
return _getHostVmsStats(machine, granularity)
}))
// Multiple hosts: get stats from all of their VMs
// Returns { host1_Id: { vm1_Id: vm1_Stats, vm2_Id: vm2_Stats }
// host2_Id: { vm3_Id: vm3_Stats } }
const _getHostsVmsStats = async (machines, granularity) => {
machines = machines.split(',')
const promises = []
forEach(machines, (machine) => {
promises.push(_getHostVmsStats(machine, granularity))
})
const reportArray = await Promise.all(promises)
const report = {}
forEach(reportArray, (hostReport) => {
forEach(hostReport, (value, key) => {
report[key] = value
})
const promises = {}
forEach(machines, (machine) => {
promises[machine] = _getHostVmsStats(machine, granularity)
})
return report
return promises::all()
}
this._unsets.push(this._xo.api.addMethod('generateHostsVmsReport', async ({ machines, granularity }) => {