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