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

     1  import Ember from 'ember';
     2  import Component from '@glimmer/component';
     3  import { inject as service } from '@ember/service';
     4  import { action } from '@ember/object';
     5  import { computed } from '@ember/object';
     6  import { alias } from '@ember/object/computed';
     7  import { task, timeout } from 'ember-concurrency';
     8  import { tracked } from '@glimmer/tracking';
     9  
    10  export default class TaskSubRowComponent extends Component {
    11    @service store;
    12    @service router;
    13    @service('stats-trackers-registry') statsTrackersRegistry;
    14  
    15    constructor() {
    16      super(...arguments);
    17      // Kick off stats polling
    18      const allocation = this.task.allocation;
    19      if (allocation) {
    20        this.fetchStats.perform();
    21      } else {
    22        this.fetchStats.cancelAll();
    23      }
    24    }
    25  
    26    @alias('args.taskState') task;
    27  
    28    @action
    29    gotoTask(allocation, task) {
    30      this.router.transitionTo('allocations.allocation.task', allocation, task);
    31    }
    32  
    33    // Since all tasks for an allocation share the same tracker, use the registry
    34    @computed('task.{allocation,isRunning}')
    35    get stats() {
    36      if (!this.task.isRunning) return undefined;
    37  
    38      return this.statsTrackersRegistry.getTracker(this.task.allocation);
    39    }
    40  
    41    // Internal state
    42    @tracked statsError = false;
    43  
    44    @computed
    45    get enablePolling() {
    46      return !Ember.testing;
    47    }
    48  
    49    @computed('task.name', 'stats.tasks.[]')
    50    get taskStats() {
    51      if (!this.stats) return undefined;
    52  
    53      return this.stats.tasks.findBy('task', this.task.name);
    54    }
    55  
    56    @alias('taskStats.cpu.lastObject') cpu;
    57    @alias('taskStats.memory.lastObject') memory;
    58  
    59    @(task(function* () {
    60      do {
    61        if (this.stats) {
    62          try {
    63            yield this.stats.poll.linked().perform();
    64            this.statsError = false;
    65          } catch (error) {
    66            this.statsError = true;
    67          }
    68        }
    69  
    70        yield timeout(500);
    71      } while (this.enablePolling);
    72    }).drop())
    73    fetchStats;
    74  
    75    //#region Logs Sidebar
    76  
    77    @alias('args.active') shouldShowLogs;
    78  
    79    @action handleTaskLogsClick(task) {
    80      if (this.args.onSetActiveTask) {
    81        this.args.onSetActiveTask(task);
    82      }
    83    }
    84  
    85    @action closeSidebar() {
    86      if (this.args.onSetActiveTask) {
    87        this.args.onSetActiveTask(null);
    88      }
    89    }
    90  
    91    //#endregion Logs Sidebar
    92  }