open-nomad/ui/app/models/deployment.js

73 lines
2.5 KiB
JavaScript
Raw Normal View History

import { alias, equal } from '@ember/object/computed';
import { computed } from '@ember/object';
import { assert } from '@ember/debug';
2017-09-19 14:47:10 +00:00
import Model from 'ember-data/model';
import attr from 'ember-data/attr';
import { belongsTo, hasMany } from 'ember-data/relationships';
import { fragmentArray } from 'ember-data-model-fragments/attributes';
import shortUUIDProperty from '../utils/properties/short-uuid';
import sumAggregation from '../utils/properties/sum-aggregation';
import classic from 'ember-classic-decorator';
2017-09-19 14:47:10 +00:00
@classic
export default class Deployment extends Model {
@shortUUIDProperty('id') shortId;
2017-09-19 14:47:10 +00:00
@belongsTo('job', { inverse: 'deployments' }) job;
@belongsTo('job', { inverse: 'latestDeployment' }) jobForLatest;
@attr('number') versionNumber;
2017-09-19 14:47:10 +00:00
// If any task group is not promoted yet requires promotion and the deployment
// is still running, the deployment needs promotion.
@computed('taskGroupSummaries.@each.promoted')
get requiresPromotion() {
return (
this.status === 'running' &&
this.taskGroupSummaries
.toArray()
.some(summary => summary.get('requiresPromotion') && !summary.get('promoted'))
);
}
2017-09-19 14:47:10 +00:00
@attr('string') status;
@attr('string') statusDescription;
@equal('status', 'running') isRunning;
@fragmentArray('task-group-deployment-summary') taskGroupSummaries;
@hasMany('allocations') allocations;
2017-09-19 14:47:10 +00:00
@computed('versionNumber', 'job.versions.content.@each.number')
get version() {
2019-03-26 07:46:44 +00:00
return (this.get('job.versions') || []).findBy('number', this.versionNumber);
}
2017-09-19 14:47:10 +00:00
2017-12-12 00:19:49 +00:00
// Dependent keys can only go one level past an @each so an alias is needed
@alias('version.submitTime') versionSubmitTime;
2017-12-12 00:19:49 +00:00
@sumAggregation('taskGroupSummaries', 'placedCanaries') placedCanaries;
@sumAggregation('taskGroupSummaries', 'desiredCanaries') desiredCanaries;
@sumAggregation('taskGroupSummaries', 'desiredTotal') desiredTotal;
@sumAggregation('taskGroupSummaries', 'placedAllocs') placedAllocs;
@sumAggregation('taskGroupSummaries', 'healthyAllocs') healthyAllocs;
@sumAggregation('taskGroupSummaries', 'unhealthyAllocs') unhealthyAllocs;
2017-09-19 14:47:10 +00:00
@computed('status')
get statusClass() {
2017-09-19 14:47:10 +00:00
const classMap = {
running: 'is-running',
successful: 'is-primary',
paused: 'is-light',
failed: 'is-error',
cancelled: 'is-cancelled',
};
2019-03-26 07:46:44 +00:00
return classMap[this.status] || 'is-dark';
}
promote() {
2019-03-26 07:46:44 +00:00
assert('A deployment needs to requirePromotion to be promoted', this.requiresPromotion);
return this.store.adapterFor('deployment').promote(this);
}
}