github.com/Ilhicas/nomad@v1.0.4-0.20210304152020-e86851182bc3/ui/app/controllers/topology.js (about)

     1  import Controller from '@ember/controller';
     2  import { computed, action } from '@ember/object';
     3  import { alias } from '@ember/object/computed';
     4  import { inject as service } from '@ember/service';
     5  import { tracked } from '@glimmer/tracking';
     6  import classic from 'ember-classic-decorator';
     7  import { reduceToLargestUnit } from 'nomad-ui/helpers/format-bytes';
     8  
     9  const sumAggregator = (sum, value) => sum + (value || 0);
    10  
    11  @classic
    12  export default class TopologyControllers extends Controller {
    13    @service userSettings;
    14  
    15    @alias('userSettings.showTopoVizPollingNotice') showPollingNotice;
    16  
    17    @tracked filteredNodes = null;
    18  
    19    @computed('model.nodes.@each.datacenter')
    20    get datacenters() {
    21      return Array.from(new Set(this.model.nodes.mapBy('datacenter'))).compact();
    22    }
    23  
    24    @computed('model.allocations.@each.isScheduled')
    25    get scheduledAllocations() {
    26      return this.model.allocations.filterBy('isScheduled');
    27    }
    28  
    29    @computed('model.nodes.@each.resources')
    30    get totalMemory() {
    31      const mibs = this.model.nodes.mapBy('resources.memory').reduce(sumAggregator, 0);
    32      return mibs * 1024 * 1024;
    33    }
    34  
    35    @computed('model.nodes.@each.resources')
    36    get totalCPU() {
    37      return this.model.nodes.mapBy('resources.cpu').reduce((sum, cpu) => sum + (cpu || 0), 0);
    38    }
    39  
    40    @computed('totalMemory')
    41    get totalMemoryFormatted() {
    42      return reduceToLargestUnit(this.totalMemory)[0].toFixed(2);
    43    }
    44  
    45    @computed('totalMemory')
    46    get totalMemoryUnits() {
    47      return reduceToLargestUnit(this.totalMemory)[1];
    48    }
    49  
    50    @computed('scheduledAllocations.@each.allocatedResources')
    51    get totalReservedMemory() {
    52      const mibs = this.scheduledAllocations
    53        .mapBy('allocatedResources.memory')
    54        .reduce(sumAggregator, 0);
    55      return mibs * 1024 * 1024;
    56    }
    57  
    58    @computed('scheduledAllocations.@each.allocatedResources')
    59    get totalReservedCPU() {
    60      return this.scheduledAllocations.mapBy('allocatedResources.cpu').reduce(sumAggregator, 0);
    61    }
    62  
    63    @computed('totalMemory', 'totalReservedMemory')
    64    get reservedMemoryPercent() {
    65      if (!this.totalReservedMemory || !this.totalMemory) return 0;
    66      return this.totalReservedMemory / this.totalMemory;
    67    }
    68  
    69    @computed('totalCPU', 'totalReservedCPU')
    70    get reservedCPUPercent() {
    71      if (!this.totalReservedCPU || !this.totalCPU) return 0;
    72      return this.totalReservedCPU / this.totalCPU;
    73    }
    74  
    75    @computed('activeAllocation.taskGroupName', 'scheduledAllocations.@each.{job,taskGroupName}')
    76    get siblingAllocations() {
    77      if (!this.activeAllocation) return [];
    78      const taskGroup = this.activeAllocation.taskGroupName;
    79      const jobId = this.activeAllocation.belongsTo('job').id();
    80  
    81      return this.scheduledAllocations.filter(allocation => {
    82        return allocation.taskGroupName === taskGroup && allocation.belongsTo('job').id() === jobId;
    83      });
    84    }
    85  
    86    @computed('activeNode')
    87    get nodeUtilization() {
    88      const node = this.activeNode;
    89      const [formattedMemory, memoryUnits] = reduceToLargestUnit(node.memory * 1024 * 1024);
    90      const totalReservedMemory = node.allocations.mapBy('memory').reduce(sumAggregator, 0);
    91      const totalReservedCPU = node.allocations.mapBy('cpu').reduce(sumAggregator, 0);
    92  
    93      return {
    94        totalMemoryFormatted: formattedMemory.toFixed(2),
    95        totalMemoryUnits: memoryUnits,
    96  
    97        totalMemory: node.memory * 1024 * 1024,
    98        totalReservedMemory: totalReservedMemory * 1024 * 1024,
    99        reservedMemoryPercent: totalReservedMemory / node.memory,
   100  
   101        totalCPU: node.cpu,
   102        totalReservedCPU,
   103        reservedCPUPercent: totalReservedCPU / node.cpu,
   104      };
   105    }
   106  
   107    @computed('siblingAllocations.@each.node')
   108    get uniqueActiveAllocationNodes() {
   109      return this.siblingAllocations.mapBy('node.id').uniq();
   110    }
   111  
   112    @action
   113    async setAllocation(allocation) {
   114      if (allocation) {
   115        await allocation.reload();
   116        await allocation.job.reload();
   117      }
   118      this.set('activeAllocation', allocation);
   119    }
   120  
   121    @action
   122    setNode(node) {
   123      this.set('activeNode', node);
   124    }
   125  
   126    @action
   127    handleTopoVizDataError(errors) {
   128      const filteredNodesError = errors.findBy('type', 'filtered-nodes');
   129      if (filteredNodesError) {
   130        this.filteredNodes = filteredNodesError.context;
   131      }
   132    }
   133  }