github.com/manicqin/nomad@v0.9.5/ui/app/components/primary-metric.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 { task, timeout } from 'ember-concurrency'; 6 7 export default Component.extend({ 8 token: service(), 9 statsTrackersRegistry: service('stats-trackers-registry'), 10 11 classNames: ['primary-metric'], 12 13 // One of Node, Allocation, or TaskState 14 resource: null, 15 16 // cpu or memory 17 metric: null, 18 19 'data-test-primary-metric': true, 20 21 // An instance of a StatsTracker. An alternative interface to resource 22 tracker: computed('trackedResource', 'type', function() { 23 const resource = this.trackedResource; 24 return this.statsTrackersRegistry.getTracker(resource); 25 }), 26 27 type: computed('resource', function() { 28 const resource = this.resource; 29 return resource && resource.constructor.modelName; 30 }), 31 32 trackedResource: computed('resource', 'type', function() { 33 // TaskStates use the allocation stats tracker 34 return this.type === 'task-state' 35 ? this.get('resource.allocation') 36 : this.resource; 37 }), 38 39 metricLabel: computed('metric', function() { 40 const metric = this.metric; 41 const mappings = { 42 cpu: 'CPU', 43 memory: 'Memory', 44 }; 45 return mappings[metric] || metric; 46 }), 47 48 data: computed('resource', 'metric', 'type', function() { 49 if (!this.tracker) return []; 50 51 const metric = this.metric; 52 if (this.type === 'task-state') { 53 // handle getting the right task out of the tracker 54 const task = this.get('tracker.tasks').findBy('task', this.get('resource.name')); 55 return task && task[metric]; 56 } 57 58 return this.get(`tracker.${metric}`); 59 }), 60 61 reservedAmount: computed('resource', 'metric', 'type', function() { 62 const metricProperty = this.metric === 'cpu' ? 'reservedCPU' : 'reservedMemory'; 63 64 if (this.type === 'task-state') { 65 const task = this.get('tracker.tasks').findBy('task', this.get('resource.name')); 66 return task[metricProperty]; 67 } 68 69 return this.get(`tracker.${metricProperty}`); 70 }), 71 72 chartClass: computed('metric', function() { 73 const metric = this.metric; 74 const mappings = { 75 cpu: 'is-info', 76 memory: 'is-danger', 77 }; 78 79 return mappings[metric] || 'is-primary'; 80 }), 81 82 poller: task(function*() { 83 do { 84 this.get('tracker.poll').perform(); 85 yield timeout(100); 86 } while (!Ember.testing); 87 }), 88 89 didReceiveAttrs() { 90 if (this.tracker) { 91 this.poller.perform(); 92 } 93 }, 94 95 willDestroy() { 96 this.poller.cancelAll(); 97 this.get('tracker.signalPause').perform(); 98 }, 99 });