github.com/Ilhicas/nomad@v1.0.4-0.20210304152020-e86851182bc3/ui/app/utils/resources-diffs.js (about) 1 import d3Format from 'd3-format'; 2 3 import { reduceToLargestUnit } from 'nomad-ui/helpers/format-bytes'; 4 5 const formatPercent = d3Format.format('+.0%'); 6 const sumAggregate = (total, val) => total + val; 7 8 export default class ResourcesDiffs { 9 constructor(model, multiplier, recommendations, excludedRecommendations) { 10 this.model = model; 11 this.multiplier = multiplier; 12 this.recommendations = recommendations; 13 this.excludedRecommendations = excludedRecommendations.filter(r => recommendations.includes(r)); 14 } 15 16 get cpu() { 17 const included = this.includedRecommendations.filterBy('resource', 'CPU'); 18 const excluded = this.excludedRecommendations.filterBy('resource', 'CPU'); 19 20 return new ResourceDiffs( 21 this.model.reservedCPU, 22 'reservedCPU', 23 'MHz', 24 this.multiplier, 25 included, 26 excluded 27 ); 28 } 29 30 get memory() { 31 const included = this.includedRecommendations.filterBy('resource', 'MemoryMB'); 32 const excluded = this.excludedRecommendations.filterBy('resource', 'MemoryMB'); 33 34 return new ResourceDiffs( 35 this.model.reservedMemory, 36 'reservedMemory', 37 'MiB', 38 this.multiplier, 39 included, 40 excluded 41 ); 42 } 43 44 get includedRecommendations() { 45 return this.recommendations.reject(r => this.excludedRecommendations.includes(r)); 46 } 47 } 48 49 class ResourceDiffs { 50 constructor( 51 base, 52 baseTaskPropertyName, 53 units, 54 multiplier, 55 includedRecommendations, 56 excludedRecommendations 57 ) { 58 this.base = base; 59 this.baseTaskPropertyName = baseTaskPropertyName; 60 this.units = units; 61 this.multiplier = multiplier; 62 this.included = includedRecommendations; 63 this.excluded = excludedRecommendations; 64 } 65 66 get recommended() { 67 if (this.included.length) { 68 return ( 69 this.included.mapBy('value').reduce(sumAggregate, 0) + 70 this.excluded.mapBy(`task.${this.baseTaskPropertyName}`).reduce(sumAggregate, 0) 71 ); 72 } else { 73 return this.base; 74 } 75 } 76 77 get delta() { 78 return this.recommended - this.base; 79 } 80 81 get aggregateDiff() { 82 return this.delta * this.multiplier; 83 } 84 85 get absoluteAggregateDiff() { 86 const delta = Math.abs(this.aggregateDiff); 87 88 if (this.units === 'MiB') { 89 if (delta === 0) { 90 return '0 MiB'; 91 } 92 93 const [memory, units] = reduceToLargestUnit(delta * 1024 * 1024); 94 const formattedMemory = Number.isInteger(memory) ? memory : memory.toFixed(2); 95 96 return `${formattedMemory} ${units}`; 97 } else { 98 return `${delta} ${this.units}`; 99 } 100 } 101 102 get signedDiff() { 103 const delta = this.aggregateDiff; 104 return `${signForDelta(delta)}${this.absoluteAggregateDiff}`; 105 } 106 107 get percentDiff() { 108 return formatPercent(this.delta / this.base); 109 } 110 } 111 112 function signForDelta(delta) { 113 if (delta > 0) { 114 return '+'; 115 } else if (delta < 0) { 116 return '-'; 117 } 118 119 return ''; 120 }