github.com/anuvu/nomad@v0.8.7-atom1/ui/app/models/allocation.js (about) 1 import { inject as service } from '@ember/service'; 2 import { computed } from '@ember/object'; 3 import Model from 'ember-data/model'; 4 import attr from 'ember-data/attr'; 5 import { belongsTo } from 'ember-data/relationships'; 6 import { fragment, fragmentArray } from 'ember-data-model-fragments/attributes'; 7 import intersection from 'npm:lodash.intersection'; 8 import shortUUIDProperty from '../utils/properties/short-uuid'; 9 import AllocationStats from '../utils/classes/allocation-stats'; 10 11 const STATUS_ORDER = { 12 pending: 1, 13 running: 2, 14 complete: 3, 15 failed: 4, 16 lost: 5, 17 }; 18 19 export default Model.extend({ 20 token: service(), 21 22 shortId: shortUUIDProperty('id'), 23 job: belongsTo('job'), 24 node: belongsTo('node'), 25 name: attr('string'), 26 taskGroupName: attr('string'), 27 resources: fragment('resources'), 28 modifyIndex: attr('number'), 29 modifyTime: attr('date'), 30 jobVersion: attr('number'), 31 32 // TEMPORARY: https://github.com/emberjs/data/issues/5209 33 originalJobId: attr('string'), 34 35 clientStatus: attr('string'), 36 desiredStatus: attr('string'), 37 statusIndex: computed('clientStatus', function() { 38 return STATUS_ORDER[this.get('clientStatus')] || 100; 39 }), 40 41 // When allocations are server-side rescheduled, a paper trail 42 // is left linking all reschedule attempts. 43 previousAllocation: belongsTo('allocation', { inverse: 'nextAllocation' }), 44 nextAllocation: belongsTo('allocation', { inverse: 'previousAllocation' }), 45 46 followUpEvaluation: belongsTo('evaluation'), 47 48 statusClass: computed('clientStatus', function() { 49 const classMap = { 50 pending: 'is-pending', 51 running: 'is-primary', 52 complete: 'is-complete', 53 failed: 'is-error', 54 lost: 'is-light', 55 }; 56 57 return classMap[this.get('clientStatus')] || 'is-dark'; 58 }), 59 60 taskGroup: computed('taskGroupName', 'job.taskGroups.[]', function() { 61 const taskGroups = this.get('job.taskGroups'); 62 return taskGroups && taskGroups.findBy('name', this.get('taskGroupName')); 63 }), 64 65 unhealthyDrivers: computed('taskGroup.drivers.[]', 'node.unhealthyDriverNames.[]', function() { 66 const taskGroupUnhealthyDrivers = this.get('taskGroup.drivers'); 67 const nodeUnhealthyDrivers = this.get('node.unhealthyDriverNames'); 68 69 if (taskGroupUnhealthyDrivers && nodeUnhealthyDrivers) { 70 return intersection(taskGroupUnhealthyDrivers, nodeUnhealthyDrivers); 71 } 72 73 return []; 74 }), 75 76 fetchStats() { 77 return this.get('token') 78 .authorizedRequest(`/v1/client/allocation/${this.get('id')}/stats`) 79 .then(res => res.json()) 80 .then(json => { 81 return new AllocationStats({ 82 stats: json, 83 allocation: this, 84 }); 85 }); 86 }, 87 88 states: fragmentArray('task-state'), 89 rescheduleEvents: fragmentArray('reschedule-event'), 90 91 hasRescheduleEvents: computed('rescheduleEvents.length', 'nextAllocation', function() { 92 return this.get('rescheduleEvents.length') > 0 || this.get('nextAllocation'); 93 }), 94 95 hasStoppedRescheduling: computed( 96 'nextAllocation', 97 'clientStatus', 98 'followUpEvaluation', 99 function() { 100 return ( 101 !this.get('nextAllocation.content') && 102 !this.get('followUpEvaluation.content') && 103 this.get('clientStatus') === 'failed' 104 ); 105 } 106 ), 107 });