github.com/Ilhicas/nomad@v1.0.4-0.20210304152020-e86851182bc3/ui/app/components/primary-metric.js (about)

     1  import Ember from 'ember';
     2  import Component from '@ember/component';
     3  import { inject as service } from '@ember/service';
     4  import { computed } from '@ember/object';
     5  import { task, timeout } from 'ember-concurrency';
     6  import { classNames } from '@ember-decorators/component';
     7  import classic from 'ember-classic-decorator';
     8  
     9  @classic
    10  @classNames('primary-metric')
    11  export default class PrimaryMetric extends Component {
    12    @service token;
    13    @service('stats-trackers-registry') statsTrackersRegistry;
    14  
    15    // One of Node, Allocation, or TaskState
    16    resource = null;
    17  
    18    // cpu or memory
    19    metric = null;
    20  
    21    'data-test-primary-metric' = true;
    22  
    23    // An instance of a StatsTracker. An alternative interface to resource
    24    @computed('trackedResource', 'type')
    25    get tracker() {
    26      const resource = this.trackedResource;
    27      return this.statsTrackersRegistry.getTracker(resource);
    28    }
    29  
    30    @computed('resource')
    31    get type() {
    32      const resource = this.resource;
    33      return resource && resource.constructor.modelName;
    34    }
    35  
    36    @computed('resource.allocation', 'type')
    37    get trackedResource() {
    38      // TaskStates use the allocation stats tracker
    39      return this.type === 'task-state' ? this.get('resource.allocation') : this.resource;
    40    }
    41  
    42    @computed('metric')
    43    get metricLabel() {
    44      const metric = this.metric;
    45      const mappings = {
    46        cpu: 'CPU',
    47        memory: 'Memory',
    48      };
    49      return mappings[metric] || metric;
    50    }
    51  
    52    @computed('metric', 'resource.name', 'tracker.tasks', 'type')
    53    get data() {
    54      if (!this.tracker) return [];
    55  
    56      const metric = this.metric;
    57      if (this.type === 'task-state') {
    58        // handle getting the right task out of the tracker
    59        const task = this.get('tracker.tasks').findBy('task', this.get('resource.name'));
    60        return task && task[metric];
    61      }
    62  
    63      return this.get(`tracker.${metric}`);
    64    }
    65  
    66    @computed('metric', 'resource.name', 'tracker.tasks', 'type')
    67    get reservedAmount() {
    68      const metricProperty = this.metric === 'cpu' ? 'reservedCPU' : 'reservedMemory';
    69  
    70      if (this.type === 'task-state') {
    71        const task = this.get('tracker.tasks').findBy('task', this.get('resource.name'));
    72        return task[metricProperty];
    73      }
    74  
    75      return this.get(`tracker.${metricProperty}`);
    76    }
    77  
    78    @computed('metric')
    79    get chartClass() {
    80      const metric = this.metric;
    81      const mappings = {
    82        cpu: 'is-info',
    83        memory: 'is-danger',
    84      };
    85  
    86      return mappings[metric] || 'is-primary';
    87    }
    88  
    89    @task(function*() {
    90      do {
    91        this.get('tracker.poll').perform();
    92        yield timeout(100);
    93      } while (!Ember.testing);
    94    })
    95    poller;
    96  
    97    didReceiveAttrs() {
    98      if (this.tracker) {
    99        this.poller.perform();
   100      }
   101    }
   102  
   103    willDestroy() {
   104      this.poller.cancelAll();
   105      this.get('tracker.signalPause').perform();
   106    }
   107  }