github.com/ferranbt/nomad@v0.9.3-0.20190607002617-85c449b7667c/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 { alias } from '@ember/object/computed'; 6 import { run } from '@ember/runloop'; 7 import { task, timeout } from 'ember-concurrency'; 8 import { lazyClick } from '../helpers/lazy-click'; 9 import AllocationStatsTracker from 'nomad-ui/utils/classes/allocation-stats-tracker'; 10 11 export default Component.extend({ 12 store: service(), 13 token: service(), 14 15 tagName: 'tr', 16 17 classNames: ['allocation-row', 'is-interactive'], 18 19 allocation: null, 20 21 // Used to determine whether the row should mention the node or the job 22 context: null, 23 24 // Internal state 25 statsError: false, 26 27 enablePolling: computed(() => !Ember.testing), 28 29 stats: computed('allocation', 'allocation.isRunning', function() { 30 if (!this.get('allocation.isRunning')) return; 31 32 return AllocationStatsTracker.create({ 33 fetch: url => this.token.authorizedRequest(url), 34 allocation: this.allocation, 35 }); 36 }), 37 38 cpu: alias('stats.cpu.lastObject'), 39 memory: alias('stats.memory.lastObject'), 40 41 onClick() {}, 42 43 click(event) { 44 lazyClick([this.onClick, event]); 45 }, 46 47 didReceiveAttrs() { 48 const allocation = this.allocation; 49 50 if (allocation) { 51 run.scheduleOnce('afterRender', this, qualifyAllocation); 52 } else { 53 this.fetchStats.cancelAll(); 54 } 55 }, 56 57 fetchStats: task(function*() { 58 do { 59 if (this.stats) { 60 try { 61 yield this.get('stats.poll').perform(); 62 this.set('statsError', false); 63 } catch (error) { 64 this.set('statsError', true); 65 } 66 } 67 68 yield timeout(500); 69 } while (this.enablePolling); 70 }).drop(), 71 }); 72 73 function qualifyAllocation() { 74 const allocation = this.allocation; 75 return allocation.reload().then(() => { 76 this.fetchStats.perform(); 77 78 // Make sure that the job record in the store for this allocation 79 // is complete and not a partial from the list endpoint 80 if ( 81 allocation && 82 allocation.get('job') && 83 !allocation.get('job.isPending') && 84 !allocation.get('taskGroup') 85 ) { 86 const job = allocation.get('job.content'); 87 job && job.reload(); 88 } 89 }); 90 }