github.com/anth0d/nomad@v0.0.0-20221214183521-ae3a0a2cad06/ui/app/utils/resources-diffs.js (about)

     1  import d3Format from 'd3-format';
     2  
     3  import { formatBytes, formatHertz } from 'nomad-ui/utils/units';
     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) =>
    14        recommendations.includes(r)
    15      );
    16    }
    17  
    18    get cpu() {
    19      const included = this.includedRecommendations.filterBy('resource', 'CPU');
    20      const excluded = this.excludedRecommendations.filterBy('resource', 'CPU');
    21  
    22      return new ResourceDiffs(
    23        this.model.reservedCPU,
    24        'reservedCPU',
    25        'MHz',
    26        this.multiplier,
    27        included,
    28        excluded
    29      );
    30    }
    31  
    32    get memory() {
    33      const included = this.includedRecommendations.filterBy(
    34        'resource',
    35        'MemoryMB'
    36      );
    37      const excluded = this.excludedRecommendations.filterBy(
    38        'resource',
    39        'MemoryMB'
    40      );
    41  
    42      return new ResourceDiffs(
    43        this.model.reservedMemory,
    44        'reservedMemory',
    45        'MiB',
    46        this.multiplier,
    47        included,
    48        excluded
    49      );
    50    }
    51  
    52    get includedRecommendations() {
    53      return this.recommendations.reject((r) =>
    54        this.excludedRecommendations.includes(r)
    55      );
    56    }
    57  }
    58  
    59  class ResourceDiffs {
    60    constructor(
    61      base,
    62      baseTaskPropertyName,
    63      units,
    64      multiplier,
    65      includedRecommendations,
    66      excludedRecommendations
    67    ) {
    68      this.base = base;
    69      this.baseTaskPropertyName = baseTaskPropertyName;
    70      this.units = units;
    71      this.multiplier = multiplier;
    72      this.included = includedRecommendations;
    73      this.excluded = excludedRecommendations;
    74    }
    75  
    76    get recommended() {
    77      if (this.included.length) {
    78        return (
    79          this.included.mapBy('value').reduce(sumAggregate, 0) +
    80          this.excluded
    81            .mapBy(`task.${this.baseTaskPropertyName}`)
    82            .reduce(sumAggregate, 0)
    83        );
    84      } else {
    85        return this.base;
    86      }
    87    }
    88  
    89    get delta() {
    90      return this.recommended - this.base;
    91    }
    92  
    93    get aggregateDiff() {
    94      return this.delta * this.multiplier;
    95    }
    96  
    97    get absoluteAggregateDiff() {
    98      const delta = Math.abs(this.aggregateDiff);
    99  
   100      if (this.units === 'MiB') {
   101        return formatBytes(delta, 'MiB');
   102      } else if (this.units === 'MHz') {
   103        return formatHertz(delta, 'MHz');
   104      } else {
   105        return `${delta} ${this.units}`;
   106      }
   107    }
   108  
   109    get signedDiff() {
   110      const delta = this.aggregateDiff;
   111      return `${signForDelta(delta)}${this.absoluteAggregateDiff}`;
   112    }
   113  
   114    get percentDiff() {
   115      return formatPercent(this.delta / this.base);
   116    }
   117  }
   118  
   119  function signForDelta(delta) {
   120    if (delta > 0) {
   121      return '+';
   122    } else if (delta < 0) {
   123      return '-';
   124    }
   125  
   126    return '';
   127  }