Skip to content

Commit

Permalink
Add embedded task group to allocation to reference when allocation is…
Browse files Browse the repository at this point in the history
… historical
  • Loading branch information
DingoEatingFuzz committed May 1, 2020
1 parent 113cd4c commit 59636ad
Show file tree
Hide file tree
Showing 5 changed files with 212 additions and 1 deletion.
13 changes: 12 additions & 1 deletion ui/app/models/allocation.js
Original file line number Diff line number Diff line change
Expand Up @@ -66,11 +66,22 @@ export default Model.extend({
return classMap[this.clientStatus] || 'is-dark';
}),

taskGroup: computed('taskGroupName', 'job.taskGroups.[]', function() {
isOld: computed('jobVersion', 'job.version', function() {
return this.jobVersion !== this.get('job.version');
}),

taskGroup: computed('isOld', 'jobTaskGroup', 'allocationTaskGroup', function() {
if (!this.isOld) return this.jobTaskGroup;
return this.allocationTaskGroup;
}),

jobTaskGroup: computed('taskGroupName', 'job.taskGroups.[]', function() {
const taskGroups = this.get('job.taskGroups');
return taskGroups && taskGroups.findBy('name', this.taskGroupName);
}),

allocationTaskGroup: fragment('task-group', { defaultValue: null }),

unhealthyDrivers: computed('taskGroup.drivers.[]', 'node.unhealthyDriverNames.[]', function() {
const taskGroupUnhealthyDrivers = this.get('taskGroup.drivers');
const nodeUnhealthyDrivers = this.get('node.unhealthyDriverNames');
Expand Down
9 changes: 9 additions & 0 deletions ui/app/serializers/allocation.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,12 @@ import { inject as service } from '@ember/service';
import { get } from '@ember/object';
import ApplicationSerializer from './application';

const taskGroupFromJob = (job, taskGroupName) => {
const taskGroups = job && job.TaskGroups;
const taskGroup = taskGroups && taskGroups.find(group => group.Name === taskGroupName);
return taskGroup ? taskGroup : null;
};

export default ApplicationSerializer.extend({
system: service(),

Expand Down Expand Up @@ -54,6 +60,9 @@ export default ApplicationSerializer.extend({
// When present, the resources are nested under AllocatedResources.Shared
hash.AllocatedResources = hash.AllocatedResources && hash.AllocatedResources.Shared;

// The Job definition for an allocation is only included in findRecord responses.
hash.AllocationTaskGroup = !hash.Job ? null : taskGroupFromJob(hash.Job, hash.TaskGroup);

return this._super(typeHash, hash);
},
});
98 changes: 98 additions & 0 deletions ui/tests/unit/models/allocation-test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,98 @@
import { run } from '@ember/runloop';
import { module, test } from 'qunit';
import { setupTest } from 'ember-qunit';

module('Unit | Model | allocation', function(hooks) {
setupTest(hooks);
hooks.beforeEach(function() {
this.store = this.owner.lookup('service:store');
});

test("When the allocation's job version matches the job's version, the task group comes from the job.", function(assert) {
const job = run(() =>
this.store.createRecord('job', {
name: 'this-job',
version: 1,
taskGroups: [
{
name: 'from-job',
count: 1,
task: [],
},
],
})
);

const allocation = run(() =>
this.store.createRecord('allocation', {
job,
jobVersion: 1,
taskGroupName: 'from-job',
allocationTaskGroup: {
name: 'from-allocation',
count: 1,
task: [],
},
})
);

assert.equal(allocation.get('taskGroup.name'), 'from-job');
});

test("When the allocation's job version does not match the job's version, the task group comes from the alloc.", function(assert) {
const job = run(() =>
this.store.createRecord('job', {
name: 'this-job',
version: 1,
taskGroups: [
{
name: 'from-job',
count: 1,
task: [],
},
],
})
);

const allocation = run(() =>
this.store.createRecord('allocation', {
job,
jobVersion: 2,
taskGroupName: 'from-job',
allocationTaskGroup: {
name: 'from-allocation',
count: 1,
task: [],
},
})
);

assert.equal(allocation.get('taskGroup.name'), 'from-allocation');
});

test("When the allocation's job version does not match the job's version and the allocation has no task group, then task group is null", async function(assert) {
const job = run(() =>
this.store.createRecord('job', {
name: 'this-job',
version: 1,
taskGroups: [
{
name: 'from-job',
count: 1,
task: [],
},
],
})
);

const allocation = run(() =>
this.store.createRecord('allocation', {
job,
jobVersion: 2,
taskGroupName: 'from-job',
})
);

assert.equal(allocation.get('taskGroup.name'), null);
});
});
90 changes: 90 additions & 0 deletions ui/tests/unit/serializers/allocation-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@ module('Unit | Serializer | Allocation', function(hooks) {
},
],
wasPreempted: false,
allocationTaskGroup: null,
},
relationships: {
followUpEvaluation: {
Expand Down Expand Up @@ -116,6 +117,7 @@ module('Unit | Serializer | Allocation', function(hooks) {
},
],
wasPreempted: false,
allocationTaskGroup: null,
},
relationships: {
followUpEvaluation: {
Expand Down Expand Up @@ -180,6 +182,7 @@ module('Unit | Serializer | Allocation', function(hooks) {
},
],
wasPreempted: true,
allocationTaskGroup: null,
},
relationships: {
followUpEvaluation: {
Expand Down Expand Up @@ -213,6 +216,93 @@ module('Unit | Serializer | Allocation', function(hooks) {
},
},
},

{
name: 'Derives task group from embedded job when available',
in: {
ID: 'test-allocation',
JobID: 'test-summary',
Name: 'test-summary[1]',
Namespace: 'test-namespace',
TaskGroup: 'test-group',
CreateTime: +sampleDate * 1000000,
ModifyTime: +sampleDate * 1000000,
TaskStates: {
task: {
State: 'running',
Failed: false,
},
},
Job: {
ID: 'test-summary',
Name: 'test-summary',
TaskGroups: [
{
Name: 'fake-group',
Count: 2,
Tasks: [],
EphemeralDisk: {},
},
{
Name: 'test-group',
Count: 3,
Tasks: [],
EphemeralDisk: {},
},
],
},
},
out: {
data: {
id: 'test-allocation',
type: 'allocation',
attributes: {
taskGroupName: 'test-group',
name: 'test-summary[1]',
modifyTime: sampleDate,
createTime: sampleDate,
states: [
{
name: 'task',
state: 'running',
failed: false,
},
],
wasPreempted: false,
allocationTaskGroup: {
name: 'test-group',
count: 3,
tasks: [],
services: [],
volumes: [],
},
},
relationships: {
followUpEvaluation: {
data: null,
},
nextAllocation: {
data: null,
},
previousAllocation: {
data: null,
},
preemptedAllocations: {
data: [],
},
preemptedByAllocation: {
data: null,
},
job: {
data: {
id: '["test-summary","test-namespace"]',
type: 'job',
},
},
},
},
},
},
];

normalizationTestCases.forEach(testCase => {
Expand Down
3 changes: 3 additions & 0 deletions ui/tests/unit/serializers/volume-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -255,6 +255,7 @@ module('Unit | Serializer | Volume', function(hooks) {
taskGroupName: 'foobar',
wasPreempted: false,
states: [],
allocationTaskGroup: null,
},
relationships: {
followUpEvaluation: {
Expand Down Expand Up @@ -286,6 +287,7 @@ module('Unit | Serializer | Volume', function(hooks) {
taskGroupName: 'write-here',
wasPreempted: false,
states: [],
allocationTaskGroup: null,
},
relationships: {
followUpEvaluation: {
Expand Down Expand Up @@ -317,6 +319,7 @@ module('Unit | Serializer | Volume', function(hooks) {
taskGroupName: 'look-if-you-must',
wasPreempted: false,
states: [],
allocationTaskGroup: null,
},
relationships: {
followUpEvaluation: {
Expand Down

0 comments on commit 59636ad

Please sign in to comment.