github.com/hernad/nomad@v1.6.112/ui/app/components/primary-metric/allocation.js (about)

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