github.com/Ilhicas/nomad@v1.0.4-0.20210304152020-e86851182bc3/ui/app/components/allocation-row.js (about)

     1  import Ember from 'ember';
     2  import { inject as service } from '@ember/service';
     3  import Component from '@ember/component';
     4  import { computed } from '@ember/object';
     5  import { computed as overridable } from 'ember-overridable-computed';
     6  import { alias } from '@ember/object/computed';
     7  import { run } from '@ember/runloop';
     8  import { task, timeout } from 'ember-concurrency';
     9  import { lazyClick } from '../helpers/lazy-click';
    10  import AllocationStatsTracker from 'nomad-ui/utils/classes/allocation-stats-tracker';
    11  import classic from 'ember-classic-decorator';
    12  import { classNames, tagName } from '@ember-decorators/component';
    13  
    14  @classic
    15  @tagName('tr')
    16  @classNames('allocation-row', 'is-interactive')
    17  export default class AllocationRow extends Component {
    18    @service store;
    19    @service token;
    20  
    21    allocation = null;
    22  
    23    // Used to determine whether the row should mention the node or the job
    24    context = null;
    25  
    26    // Internal state
    27    statsError = false;
    28  
    29    @overridable(() => !Ember.testing) enablePolling;
    30  
    31    @computed('allocation', 'allocation.isRunning')
    32    get stats() {
    33      if (!this.get('allocation.isRunning')) return undefined;
    34  
    35      return AllocationStatsTracker.create({
    36        fetch: url => this.token.authorizedRequest(url),
    37        allocation: this.allocation,
    38      });
    39    }
    40  
    41    @alias('stats.cpu.lastObject') cpu;
    42    @alias('stats.memory.lastObject') memory;
    43  
    44    onClick() {}
    45  
    46    click(event) {
    47      lazyClick([this.onClick, event]);
    48    }
    49  
    50    didReceiveAttrs() {
    51      this.updateStatsTracker();
    52    }
    53  
    54    updateStatsTracker() {
    55      const allocation = this.allocation;
    56  
    57      if (allocation) {
    58        run.scheduleOnce('afterRender', this, qualifyAllocation);
    59      } else {
    60        this.fetchStats.cancelAll();
    61      }
    62    }
    63  
    64    @(task(function*() {
    65      do {
    66        if (this.stats) {
    67          try {
    68            yield this.get('stats.poll').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  
    81  async function qualifyAllocation() {
    82    const allocation = this.allocation;
    83  
    84    // Make sure the allocation is a complete record and not a partial so we
    85    // can show information such as preemptions and rescheduled allocation.
    86    if (allocation.isPartial) {
    87      await allocation.reload();
    88    }
    89  
    90    if (allocation.get('job.isPending')) {
    91      // Make sure the job is loaded before starting the stats tracker
    92      await allocation.get('job');
    93    } else if (!allocation.get('taskGroup')) {
    94      // Make sure that the job record in the store for this allocation
    95      // is complete and not a partial from the list endpoint
    96      const job = allocation.get('job.content');
    97      if (job) await job.reload();
    98    }
    99  
   100    this.fetchStats.perform();
   101  }