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