promise-toolbox
: all
to resolve promises in object
This commit is contained in:
parent
c274486057
commit
3687a230e1
@ -39,6 +39,7 @@
|
|||||||
"mocha": "^2.3.4",
|
"mocha": "^2.3.4",
|
||||||
"must": "^0.13.1",
|
"must": "^0.13.1",
|
||||||
"nyc": "^5.2.0",
|
"nyc": "^5.2.0",
|
||||||
|
"promise-toolbox": "^0.1.1",
|
||||||
"source-map-support": "^0.4.0",
|
"source-map-support": "^0.4.0",
|
||||||
"standard": "^5.4.1",
|
"standard": "^5.4.1",
|
||||||
"trace": "^2.0.2"
|
"trace": "^2.0.2"
|
||||||
|
@ -1,4 +1,5 @@
|
|||||||
import forEach from 'lodash.foreach'
|
import forEach from 'lodash.foreach'
|
||||||
|
import { all } from 'promise-toolbox'
|
||||||
|
|
||||||
export const configurationSchema = {
|
export const configurationSchema = {
|
||||||
type: 'object',
|
type: 'object',
|
||||||
@ -135,45 +136,38 @@ class UsageReportPlugin {
|
|||||||
return hostMean
|
return hostMean
|
||||||
}))
|
}))
|
||||||
|
|
||||||
|
// Single host: get stats from its VMs.
|
||||||
|
// Returns { vm1_Id: vm1_Stats, vm2_Id: vm2_Stats, ... }
|
||||||
const _getHostVmsStats = async (machine, granularity) => {
|
const _getHostVmsStats = async (machine, granularity) => {
|
||||||
const host = await this_._xo.getObject(machine)
|
const host = await this_._xo.getObject(machine)
|
||||||
const objects = await this_._xo.getObjects()
|
const objects = await this_._xo.getObjects()
|
||||||
const promises = []
|
|
||||||
const vmIds = []
|
const promises = {}
|
||||||
forEach(objects, (obj) => {
|
forEach(objects, (obj) => {
|
||||||
if (obj.type === 'VM' && obj.power_state === 'Running' && obj.$poolId === host.$poolId) {
|
if (obj.type === 'VM' && obj.power_state === 'Running' && obj.$poolId === host.$poolId) {
|
||||||
vmIds.push(obj.id)
|
promises[obj.id] = this_._xo.getXapiVmStats(obj, granularity)
|
||||||
promises.push(this_._xo.getXapiVmStats(obj, granularity))
|
|
||||||
}
|
}
|
||||||
})
|
})
|
||||||
const vmsStatsArray = await Promise.all(promises)
|
|
||||||
|
|
||||||
const vmStats = {}
|
return promises::all()
|
||||||
forEach(vmsStatsArray, (stats, index) => {
|
|
||||||
vmStats[vmIds[index]] = stats
|
|
||||||
})
|
|
||||||
return vmStats
|
|
||||||
}
|
}
|
||||||
|
|
||||||
this._unsets.push(this._xo.api.addMethod('generateHostVmsReport', async ({ machine, granularity }) => {
|
this._unsets.push(this._xo.api.addMethod('generateHostVmsReport', async ({ machine, granularity }) => {
|
||||||
return _getHostVmsStats(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) => {
|
const _getHostsVmsStats = async (machines, granularity) => {
|
||||||
machines = machines.split(',')
|
machines = machines.split(',')
|
||||||
const promises = []
|
|
||||||
forEach(machines, (machine) => {
|
|
||||||
promises.push(_getHostVmsStats(machine, granularity))
|
|
||||||
})
|
|
||||||
const reportArray = await Promise.all(promises)
|
|
||||||
|
|
||||||
const report = {}
|
const promises = {}
|
||||||
forEach(reportArray, (hostReport) => {
|
forEach(machines, (machine) => {
|
||||||
forEach(hostReport, (value, key) => {
|
promises[machine] = _getHostVmsStats(machine, granularity)
|
||||||
report[key] = value
|
|
||||||
})
|
|
||||||
})
|
})
|
||||||
return report
|
|
||||||
|
return promises::all()
|
||||||
}
|
}
|
||||||
|
|
||||||
this._unsets.push(this._xo.api.addMethod('generateHostsVmsReport', async ({ machines, granularity }) => {
|
this._unsets.push(this._xo.api.addMethod('generateHostsVmsReport', async ({ machines, granularity }) => {
|
||||||
|
Loading…
Reference in New Issue
Block a user