Skip to content

Commit

Permalink
Actions aggregated at job model level (#18733)
Browse files Browse the repository at this point in the history
  • Loading branch information
philrenaud committed Oct 17, 2023
1 parent ae4e343 commit d7c5e08
Show file tree
Hide file tree
Showing 4 changed files with 144 additions and 0 deletions.
15 changes: 15 additions & 0 deletions ui/app/models/action.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
/**
* Copyright (c) HashiCorp, Inc.
* SPDX-License-Identifier: BUSL-1.1
*/

import { attr } from '@ember-data/model';
import { fragmentOwner } from 'ember-data-model-fragments/attributes';
import Fragment from 'ember-data-model-fragments/fragment';

export default class ActionModel extends Fragment {
@attr('string') name;
@attr('string') command;
@attr() args;
@fragmentOwner() task;
}
12 changes: 12 additions & 0 deletions ui/app/models/job.js
Original file line number Diff line number Diff line change
Expand Up @@ -155,6 +155,18 @@ export default class Job extends Model {

@hasMany('recommendation-summary') recommendationSummaries;

get actions() {
return this.taskGroups.reduce((acc, taskGroup) => {
return acc.concat(
taskGroup.tasks
.map((task) => {
return task.get('actions').toArray();
})
.reduce((taskAcc, taskActions) => taskAcc.concat(taskActions), [])
);
}, []);
}

@computed('taskGroups.@each.drivers')
get drivers() {
return this.taskGroups
Expand Down
2 changes: 2 additions & 0 deletions ui/app/models/task.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,8 @@ export default class Task extends Fragment {
@attr('string') driver;
@attr('string') kind;

@fragmentArray('action') actions;

@attr() meta;

@computed('taskGroup.mergedMeta', 'meta')
Expand Down
115 changes: 115 additions & 0 deletions ui/tests/unit/models/job-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -139,6 +139,121 @@ module('Unit | Model | job', function (hooks) {
);
});

test('actions are aggregated from taskgroups tasks', function (assert) {
const job = run(() =>
this.owner.lookup('service:store').createRecord('job', {
name: 'example',
taskGroups: [
{
name: 'one',
count: 0,
tasks: [
{
name: '1.1',
actions: [
{
name: 'one',
command: 'date',
args: ['+%s'],
},
{
name: 'two',
command: 'sh',
args: ['-c "echo hello"'],
},
],
},
],
},
{
name: 'two',
count: 0,
tasks: [
{
name: '2.1',
},
],
},
{
name: 'three',
count: 0,
tasks: [
{
name: '3.1',
actions: [
{
name: 'one',
command: 'date',
args: ['+%s'],
},
],
},
{
name: '3.2',
actions: [
{
name: 'one',
command: 'date',
args: ['+%s'],
},
],
},
],
},
],
})
);

assert.equal(
job.get('actions.length'),
4,
'Job draws actions from its task groups tasks'
);

// Three actions named one, one named two
assert.equal(
job.get('actions').filterBy('name', 'one').length,
3,
'Job has three actions named one'
);
assert.equal(
job.get('actions').filterBy('name', 'two').length,
1,
'Job has one action named two'
);

// Job's actions mapped by task.name return 1.1, 1.1, 3.1, 3.2
assert.equal(
job.get('actions').mapBy('task.name').length,
4,
'Job action fragments surface their task properties'
);
assert.equal(
job
.get('actions')
.mapBy('task.name')
.filter((name) => name === '1.1').length,
2,
'Two of the job actions are from task 1.1'
);
assert.equal(
job
.get('actions')
.mapBy('task.name')
.filter((name) => name === '3.1').length,
1,
'One of the job actions is from task 3.1'
);
assert.equal(
job
.get('actions')
.mapBy('task.name')
.filter((name) => name === '3.2').length,
1,
'One of the job actions is from task 3.2'
);
});

module('#parse', function () {
test('it parses JSON', async function (assert) {
const store = this.owner.lookup('service:store');
Expand Down

0 comments on commit d7c5e08

Please sign in to comment.