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