github.com/anth0d/nomad@v0.0.0-20221214183521-ae3a0a2cad06/ui/app/components/primary-metric/node.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 } from '@ember/object';
     7  import {
     8    formatScheduledBytes,
     9    formatScheduledHertz,
    10  } from 'nomad-ui/utils/units';
    11  
    12  export default class NodePrimaryMetric extends Component {
    13    @service('stats-trackers-registry') statsTrackersRegistry;
    14  
    15    /** Args
    16      node = null;
    17      metric null; (one of 'cpu' or 'memory')
    18    */
    19  
    20    get metric() {
    21      assert('metric is a required argument', this.args.metric);
    22      return this.args.metric;
    23    }
    24  
    25    get tracker() {
    26      return this.statsTrackersRegistry.getTracker(this.args.node);
    27    }
    28  
    29    get data() {
    30      if (!this.tracker) return [];
    31      return get(this, `tracker.${this.metric}`);
    32    }
    33  
    34    get reservedAmount() {
    35      if (this.metric === 'cpu') return this.tracker.reservedCPU;
    36      if (this.metric === 'memory') return this.tracker.reservedMemory;
    37      return null;
    38    }
    39  
    40    get chartClass() {
    41      if (this.metric === 'cpu') return 'is-info';
    42      if (this.metric === 'memory') return 'is-danger';
    43      return 'is-primary';
    44    }
    45  
    46    get reservedAnnotations() {
    47      if (this.metric === 'cpu' && get(this.args.node, 'reserved.cpu')) {
    48        const cpu = this.args.node.reserved.cpu;
    49        return [
    50          {
    51            label: `${formatScheduledHertz(cpu, 'MHz')} reserved`,
    52            percent: cpu / this.reservedAmount,
    53          },
    54        ];
    55      }
    56  
    57      if (this.metric === 'memory' && get(this.args.node, 'reserved.memory')) {
    58        const memory = this.args.node.reserved.memory;
    59        return [
    60          {
    61            label: `${formatScheduledBytes(memory, 'MiB')} reserved`,
    62            percent: memory / this.reservedAmount,
    63          },
    64        ];
    65      }
    66  
    67      return [];
    68    }
    69  
    70    @task(function* () {
    71      do {
    72        this.tracker.poll.perform();
    73        yield timeout(100);
    74      } while (!Ember.testing);
    75    })
    76    poller;
    77  
    78    @action
    79    start() {
    80      if (this.tracker) this.poller.perform();
    81    }
    82  
    83    willDestroy() {
    84      super.willDestroy(...arguments);
    85      this.poller.cancelAll();
    86      this.tracker.signalPause.perform();
    87    }
    88  }