open-nomad/ui/app/components/job-version.js

102 lines
2.6 KiB
JavaScript
Raw Normal View History

import Component from '@ember/component';
import { action, computed } from '@ember/object';
import { classNames } from '@ember-decorators/component';
import { inject as service } from '@ember/service';
import { task } from 'ember-concurrency';
import messageForError from 'nomad-ui/utils/message-from-adapter-error';
import classic from 'ember-classic-decorator';
2017-09-19 14:47:10 +00:00
const changeTypes = ['Added', 'Deleted', 'Edited'];
@classic
@classNames('job-version', 'boxed-section')
export default class JobVersion extends Component {
version = null;
isOpen = false;
2017-09-19 14:47:10 +00:00
// Passes through to the job-diff component
verbose = true;
2017-09-19 14:47:10 +00:00
@service router;
@computed('version.diff')
get changeCount() {
2017-09-19 14:47:10 +00:00
const diff = this.get('version.diff');
const taskGroups = diff.TaskGroups || [];
if (!diff) {
return 0;
}
return (
fieldChanges(diff) +
taskGroups.reduce(arrayOfFieldChanges, 0) +
2021-12-28 16:08:12 +00:00
(taskGroups.mapBy('Tasks') || [])
.reduce(flatten, [])
.reduce(arrayOfFieldChanges, 0)
2017-09-19 14:47:10 +00:00
);
}
@computed('version.{number,job.version}')
get isCurrent() {
return this.get('version.number') === this.get('version.job.version');
}
@action
toggleDiff() {
this.toggleProperty('isOpen');
}
2021-12-28 14:45:20 +00:00
@task(function* () {
try {
const versionBeforeReversion = this.get('version.job.version');
yield this.version.revertTo();
yield this.version.job.reload();
const versionAfterReversion = this.get('version.job.version');
if (versionBeforeReversion === versionAfterReversion) {
this.handleError({
level: 'warn',
title: 'Reversion Had No Effect',
2021-12-28 16:08:12 +00:00
description:
'Reverting to an identical older version doesnt produce a new version',
});
} else {
const job = this.get('version.job');
this.router.transitionTo('jobs.job', job.get('plainId'), {
queryParams: { namespace: job.get('namespace.name') },
});
}
} catch (e) {
this.handleError({
level: 'danger',
title: 'Could Not Revert',
description: messageForError(e, 'revert'),
});
}
})
revertTo;
}
2017-09-19 14:47:10 +00:00
const flatten = (accumulator, array) => accumulator.concat(array);
2021-12-28 16:08:12 +00:00
const countChanges = (total, field) =>
changeTypes.includes(field.Type) ? total + 1 : total;
2017-09-19 14:47:10 +00:00
function fieldChanges(diff) {
return (
(diff.Fields || []).reduce(countChanges, 0) +
(diff.Objects || []).reduce(arrayOfFieldChanges, 0)
);
}
function arrayOfFieldChanges(count, diff) {
if (!diff) {
return count;
}
return count + fieldChanges(diff);
}