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

     1  import Ember from 'ember';
     2  import Component from '@glimmer/component';
     3  import { tracked } from '@glimmer/tracking';
     4  import { task, timeout } from 'ember-concurrency';
     5  import { assert } from '@ember/debug';
     6  import { inject as service } from '@ember/service';
     7  import { action } from '@ember/object';
     8  
     9  export default class TaskPrimaryMetric extends Component {
    10    @service('stats-trackers-registry') statsTrackersRegistry;
    11  
    12    /** Args
    13      taskState = null;
    14      metric null; (one of 'cpu' or 'memory'
    15    */
    16  
    17    @tracked tracker = null;
    18    @tracked taskState = null;
    19  
    20    get metric() {
    21      assert('metric is a required argument', this.args.metric);
    22      return this.args.metric;
    23    }
    24  
    25    get data() {
    26      if (!this.tracker) return [];
    27      const task = this.tracker.tasks.findBy('task', this.taskState.name);
    28      return task && task[this.metric];
    29    }
    30  
    31    get reservedAmount() {
    32      if (!this.tracker) return null;
    33      const task = this.tracker.tasks.findBy('task', this.taskState.name);
    34      if (this.metric === 'cpu') return task.reservedCPU;
    35      if (this.metric === 'memory') return task.reservedMemory;
    36      return null;
    37    }
    38  
    39    get chartClass() {
    40      if (this.metric === 'cpu') return 'is-info';
    41      if (this.metric === 'memory') return 'is-danger';
    42      return 'is-primary';
    43    }
    44  
    45    @task(function* () {
    46      do {
    47        this.tracker.poll.perform();
    48        yield timeout(100);
    49      } while (!Ember.testing);
    50    })
    51    poller;
    52  
    53    @action
    54    start() {
    55      this.taskState = this.args.taskState;
    56      this.tracker = this.statsTrackersRegistry.getTracker(
    57        this.args.taskState.allocation
    58      );
    59      this.poller.perform();
    60    }
    61  
    62    willDestroy() {
    63      super.willDestroy(...arguments);
    64      this.poller.cancelAll();
    65      this.tracker.signalPause.perform();
    66    }
    67  }