Skip to content

Commit

Permalink
Factor out the common sum aggregator used in the topology controller
Browse files Browse the repository at this point in the history
  • Loading branch information
DingoEatingFuzz committed Oct 15, 2020
1 parent 2cf4b5d commit 0d920ae
Showing 1 changed file with 7 additions and 15 deletions.
22 changes: 7 additions & 15 deletions ui/app/controllers/topology.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@ import { computed, action } from '@ember/object';
import classic from 'ember-classic-decorator';
import { reduceToLargestUnit } from 'nomad-ui/helpers/format-bytes';

const sumAggregator = (sum, value) => sum + (value || 0);

@classic
export default class TopologyControllers extends Controller {
@computed('[email protected]')
Expand All @@ -17,9 +19,7 @@ export default class TopologyControllers extends Controller {

@computed('[email protected]')
get totalMemory() {
const mibs = this.model.nodes
.mapBy('resources.memory')
.reduce((sum, memory) => sum + (memory || 0), 0);
const mibs = this.model.nodes.mapBy('resources.memory').reduce(sumAggregator, 0);
return mibs * 1024 * 1024;
}

Expand All @@ -40,17 +40,13 @@ export default class TopologyControllers extends Controller {

@computed('[email protected]')
get totalReservedMemory() {
const mibs = this.model.allocations
.mapBy('allocatedResources.memory')
.reduce((sum, memory) => sum + (memory || 0), 0);
const mibs = this.model.allocations.mapBy('allocatedResources.memory').reduce(sumAggregator, 0);
return mibs * 1024 * 1024;
}

@computed('[email protected]')
get totalReservedCPU() {
return this.model.allocations
.mapBy('allocatedResources.cpu')
.reduce((sum, cpu) => sum + (cpu || 0), 0);
return this.model.allocations.mapBy('allocatedResources.cpu').reduce(sumAggregator, 0);
}

@computed('totalMemory', 'totalReservedMemory')
Expand Down Expand Up @@ -80,12 +76,8 @@ export default class TopologyControllers extends Controller {
get nodeUtilization() {
const node = this.activeNode;
const [formattedMemory, memoryUnits] = reduceToLargestUnit(node.memory * 1024 * 1024);
const totalReservedMemory = node.allocations
.mapBy('memory')
.reduce((sum, memory) => sum + (memory || 0), 0);
const totalReservedCPU = node.allocations
.mapBy('cpu')
.reduce((sum, cpu) => sum + (cpu || 0), 0);
const totalReservedMemory = node.allocations.mapBy('memory').reduce(sumAggregator, 0);
const totalReservedCPU = node.allocations.mapBy('cpu').reduce(sumAggregator, 0);

return {
totalMemoryFormatted: formattedMemory.toFixed(2),
Expand Down

0 comments on commit 0d920ae

Please sign in to comment.