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