Factor out the common sum aggregator used in the topology controller

This commit is contained in:
Michael Lange 2020-10-15 10:40:32 -07:00
parent f3300bcbf0
commit 2309baf080
1 changed files with 7 additions and 15 deletions

View File

@ -3,6 +3,8 @@ import { computed, action } from '@ember/object';
import classic from 'ember-classic-decorator'; import classic from 'ember-classic-decorator';
import { reduceToLargestUnit } from 'nomad-ui/helpers/format-bytes'; import { reduceToLargestUnit } from 'nomad-ui/helpers/format-bytes';
const sumAggregator = (sum, value) => sum + (value || 0);
@classic @classic
export default class TopologyControllers extends Controller { export default class TopologyControllers extends Controller {
@computed('model.nodes.@each.datacenter') @computed('model.nodes.@each.datacenter')
@ -17,9 +19,7 @@ export default class TopologyControllers extends Controller {
@computed('model.nodes.@each.resources') @computed('model.nodes.@each.resources')
get totalMemory() { get totalMemory() {
const mibs = this.model.nodes const mibs = this.model.nodes.mapBy('resources.memory').reduce(sumAggregator, 0);
.mapBy('resources.memory')
.reduce((sum, memory) => sum + (memory || 0), 0);
return mibs * 1024 * 1024; return mibs * 1024 * 1024;
} }
@ -40,17 +40,13 @@ export default class TopologyControllers extends Controller {
@computed('model.allocations.@each.allocatedResources') @computed('model.allocations.@each.allocatedResources')
get totalReservedMemory() { get totalReservedMemory() {
const mibs = this.model.allocations const mibs = this.model.allocations.mapBy('allocatedResources.memory').reduce(sumAggregator, 0);
.mapBy('allocatedResources.memory')
.reduce((sum, memory) => sum + (memory || 0), 0);
return mibs * 1024 * 1024; return mibs * 1024 * 1024;
} }
@computed('model.allocations.@each.allocatedResources') @computed('model.allocations.@each.allocatedResources')
get totalReservedCPU() { get totalReservedCPU() {
return this.model.allocations return this.model.allocations.mapBy('allocatedResources.cpu').reduce(sumAggregator, 0);
.mapBy('allocatedResources.cpu')
.reduce((sum, cpu) => sum + (cpu || 0), 0);
} }
@computed('totalMemory', 'totalReservedMemory') @computed('totalMemory', 'totalReservedMemory')
@ -80,12 +76,8 @@ export default class TopologyControllers extends Controller {
get nodeUtilization() { get nodeUtilization() {
const node = this.activeNode; const node = this.activeNode;
const [formattedMemory, memoryUnits] = reduceToLargestUnit(node.memory * 1024 * 1024); const [formattedMemory, memoryUnits] = reduceToLargestUnit(node.memory * 1024 * 1024);
const totalReservedMemory = node.allocations const totalReservedMemory = node.allocations.mapBy('memory').reduce(sumAggregator, 0);
.mapBy('memory') const totalReservedCPU = node.allocations.mapBy('cpu').reduce(sumAggregator, 0);
.reduce((sum, memory) => sum + (memory || 0), 0);
const totalReservedCPU = node.allocations
.mapBy('cpu')
.reduce((sum, cpu) => sum + (cpu || 0), 0);
return { return {
totalMemoryFormatted: formattedMemory.toFixed(2), totalMemoryFormatted: formattedMemory.toFixed(2),