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