github.com/anth0d/nomad@v0.0.0-20221214183521-ae3a0a2cad06/ui/app/models/deployment.js (about) 1 import { alias, equal } from '@ember/object/computed'; 2 import { computed } from '@ember/object'; 3 import { assert } from '@ember/debug'; 4 import Model from '@ember-data/model'; 5 import { attr, belongsTo, hasMany } from '@ember-data/model'; 6 import { fragmentArray } from 'ember-data-model-fragments/attributes'; 7 import shortUUIDProperty from '../utils/properties/short-uuid'; 8 import sumAggregation from '../utils/properties/sum-aggregation'; 9 import classic from 'ember-classic-decorator'; 10 11 @classic 12 export default class Deployment extends Model { 13 @shortUUIDProperty('id') shortId; 14 15 @belongsTo('job', { inverse: 'deployments' }) job; 16 @belongsTo('job', { inverse: 'latestDeployment' }) jobForLatest; 17 @attr('number') versionNumber; 18 19 // If any task group is not promoted yet requires promotion and the deployment 20 // is still running, the deployment needs promotion. 21 @computed('status', 'taskGroupSummaries.@each.promoted') 22 get requiresPromotion() { 23 return ( 24 this.status === 'running' && 25 this.taskGroupSummaries 26 .toArray() 27 .some( 28 (summary) => 29 summary.get('requiresPromotion') && !summary.get('promoted') 30 ) 31 ); 32 } 33 34 @attr('string') status; 35 @attr('string') statusDescription; 36 37 @equal('status', 'running') isRunning; 38 39 @fragmentArray('task-group-deployment-summary') taskGroupSummaries; 40 @hasMany('allocations') allocations; 41 42 @computed('versionNumber', 'job.versions.content.@each.number') 43 get version() { 44 return (this.get('job.versions') || []).findBy( 45 'number', 46 this.versionNumber 47 ); 48 } 49 50 // Dependent keys can only go one level past an @each so an alias is needed 51 @alias('version.submitTime') versionSubmitTime; 52 53 @sumAggregation('taskGroupSummaries', 'placedCanaries') placedCanaries; 54 @sumAggregation('taskGroupSummaries', 'desiredCanaries') desiredCanaries; 55 @sumAggregation('taskGroupSummaries', 'desiredTotal') desiredTotal; 56 @sumAggregation('taskGroupSummaries', 'placedAllocs') placedAllocs; 57 @sumAggregation('taskGroupSummaries', 'healthyAllocs') healthyAllocs; 58 @sumAggregation('taskGroupSummaries', 'unhealthyAllocs') unhealthyAllocs; 59 60 @computed('status') 61 get statusClass() { 62 const classMap = { 63 running: 'is-running', 64 successful: 'is-primary', 65 paused: 'is-light', 66 failed: 'is-error', 67 cancelled: 'is-cancelled', 68 }; 69 70 return classMap[this.status] || 'is-dark'; 71 } 72 73 promote() { 74 assert( 75 'A deployment needs to requirePromotion to be promoted', 76 this.requiresPromotion 77 ); 78 return this.store.adapterFor('deployment').promote(this); 79 } 80 81 fail() { 82 assert('A deployment must be running to be failed', this.isRunning); 83 return this.store.adapterFor('deployment').fail(this); 84 } 85 }