github.com/hernad/nomad@v1.6.112/ui/app/components/task-row.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 '@ember/component';
     8  import { inject as service } from '@ember/service';
     9  import { computed } from '@ember/object';
    10  import { alias } from '@ember/object/computed';
    11  import { task, timeout } from 'ember-concurrency';
    12  import { lazyClick } from '../helpers/lazy-click';
    13  
    14  import {
    15    classNames,
    16    tagName,
    17    attributeBindings,
    18  } from '@ember-decorators/component';
    19  import classic from 'ember-classic-decorator';
    20  
    21  @classic
    22  @tagName('tr')
    23  @classNames('task-row', 'is-interactive')
    24  @attributeBindings('data-test-task-row')
    25  export default class TaskRow extends Component {
    26    @service store;
    27    @service token;
    28    @service('stats-trackers-registry') statsTrackersRegistry;
    29  
    30    task = null;
    31  
    32    // Internal state
    33    statsError = false;
    34  
    35    @computed
    36    get enablePolling() {
    37      return !Ember.testing;
    38    }
    39  
    40    // Since all tasks for an allocation share the same tracker, use the registry
    41    @computed('task.{allocation,isRunning}')
    42    get stats() {
    43      if (!this.get('task.isRunning')) return undefined;
    44  
    45      return this.statsTrackersRegistry.getTracker(this.get('task.allocation'));
    46    }
    47  
    48    @computed('task.name', 'stats.tasks.[]')
    49    get taskStats() {
    50      if (!this.stats) return undefined;
    51  
    52      return this.get('stats.tasks').findBy('task', this.get('task.name'));
    53    }
    54  
    55    @alias('taskStats.cpu.lastObject') cpu;
    56    @alias('taskStats.memory.lastObject') memory;
    57  
    58    onClick() {}
    59  
    60    click(event) {
    61      lazyClick([this.onClick, event]);
    62    }
    63  
    64    @(task(function* () {
    65      do {
    66        if (this.stats) {
    67          try {
    68            yield this.get('stats.poll').linked().perform();
    69            this.set('statsError', false);
    70          } catch (error) {
    71            this.set('statsError', true);
    72          }
    73        }
    74  
    75        yield timeout(500);
    76      } while (this.enablePolling);
    77    }).drop())
    78    fetchStats;
    79  
    80    didReceiveAttrs() {
    81      super.didReceiveAttrs();
    82      const allocation = this.get('task.allocation');
    83  
    84      if (allocation) {
    85        this.fetchStats.perform();
    86      } else {
    87        this.fetchStats.cancelAll();
    88      }
    89    }
    90  }