github.com/anth0d/nomad@v0.0.0-20221214183521-ae3a0a2cad06/ui/app/components/primary-metric/allocation.js (about)

     1  import Ember from 'ember';
     2  import Component from '@glimmer/component';
     3  import { task, timeout } from 'ember-concurrency';
     4  import { assert } from '@ember/debug';
     5  import { inject as service } from '@ember/service';
     6  import { action, get, computed } from '@ember/object';
     7  import { dependentKeyCompat } from '@ember/object/compat';
     8  
     9  export default class AllocationPrimaryMetric extends Component {
    10    @service('stats-trackers-registry') statsTrackersRegistry;
    11  
    12    /** Args
    13      allocation = null;
    14      metric null; (one of 'cpu' or 'memory'
    15    */
    16  
    17    get metric() {
    18      assert('metric is a required argument', this.args.metric);
    19      return this.args.metric;
    20    }
    21  
    22    @dependentKeyCompat
    23    get allocation() {
    24      return this.args.allocation;
    25    }
    26  
    27    @computed('allocation')
    28    get tracker() {
    29      return this.statsTrackersRegistry.getTracker(this.allocation);
    30    }
    31  
    32    get data() {
    33      if (!this.tracker) return [];
    34      return get(this, `tracker.${this.metric}`);
    35    }
    36  
    37    @computed('tracker.tasks.[]', 'metric')
    38    get series() {
    39      const ret = this.tracker.tasks
    40        .map((task) => ({
    41          name: task.task,
    42          data: task[this.metric],
    43        }))
    44        .reverse();
    45  
    46      return ret;
    47    }
    48  
    49    get reservedAmount() {
    50      if (this.metric === 'cpu') return this.tracker.reservedCPU;
    51      if (this.metric === 'memory') return this.tracker.reservedMemory;
    52      return null;
    53    }
    54  
    55    get chartClass() {
    56      if (this.metric === 'cpu') return 'is-info';
    57      if (this.metric === 'memory') return 'is-danger';
    58      return 'is-primary';
    59    }
    60  
    61    get colorScale() {
    62      if (this.metric === 'cpu') return 'blues';
    63      if (this.metric === 'memory') return 'reds';
    64      return 'ordinal';
    65    }
    66  
    67    @task(function* () {
    68      do {
    69        this.tracker.poll.perform();
    70        yield timeout(100);
    71      } while (!Ember.testing);
    72    })
    73    poller;
    74  
    75    @action
    76    start() {
    77      if (this.tracker) this.poller.perform();
    78    }
    79  
    80    willDestroy() {
    81      super.willDestroy(...arguments);
    82      this.poller.cancelAll();
    83      this.tracker.signalPause.perform();
    84    }
    85  }