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