2017-12-15 21:39:18 +00:00
|
|
|
import { inject as service } from '@ember/service';
|
|
|
|
import { get } from '@ember/object';
|
2017-09-19 14:47:10 +00:00
|
|
|
import ApplicationSerializer from './application';
|
2020-06-22 15:48:53 +00:00
|
|
|
import classic from 'ember-classic-decorator';
|
2017-09-19 14:47:10 +00:00
|
|
|
|
2020-05-01 21:30:02 +00:00
|
|
|
const taskGroupFromJob = (job, taskGroupName) => {
|
|
|
|
const taskGroups = job && job.TaskGroups;
|
2021-12-28 14:45:20 +00:00
|
|
|
const taskGroup = taskGroups && taskGroups.find((group) => group.Name === taskGroupName);
|
2020-05-01 21:30:02 +00:00
|
|
|
return taskGroup ? taskGroup : null;
|
|
|
|
};
|
|
|
|
|
2021-12-28 14:45:20 +00:00
|
|
|
const merge = (tasks) => {
|
2020-10-12 22:26:54 +00:00
|
|
|
const mergedResources = {
|
|
|
|
Cpu: { CpuShares: 0 },
|
|
|
|
Memory: { MemoryMB: 0 },
|
|
|
|
Disk: { DiskMB: 0 },
|
|
|
|
};
|
|
|
|
|
|
|
|
return tasks.reduce((resources, task) => {
|
|
|
|
resources.Cpu.CpuShares += (task.Cpu && task.Cpu.CpuShares) || 0;
|
|
|
|
resources.Memory.MemoryMB += (task.Memory && task.Memory.MemoryMB) || 0;
|
|
|
|
resources.Disk.DiskMB += (task.Disk && task.Disk.DiskMB) || 0;
|
|
|
|
return resources;
|
|
|
|
}, mergedResources);
|
|
|
|
};
|
|
|
|
|
2020-06-22 15:48:53 +00:00
|
|
|
@classic
|
2020-06-11 21:23:00 +00:00
|
|
|
export default class AllocationSerializer extends ApplicationSerializer {
|
|
|
|
@service system;
|
2017-10-23 17:22:58 +00:00
|
|
|
|
2020-06-11 21:23:00 +00:00
|
|
|
attrs = {
|
2017-09-19 14:47:10 +00:00
|
|
|
taskGroupName: 'TaskGroup',
|
|
|
|
states: 'TaskStates',
|
2020-06-11 21:23:00 +00:00
|
|
|
};
|
2017-09-19 14:47:10 +00:00
|
|
|
|
2020-08-20 17:14:49 +00:00
|
|
|
separateNanos = ['CreateTime', 'ModifyTime'];
|
|
|
|
|
2017-09-19 14:47:10 +00:00
|
|
|
normalize(typeHash, hash) {
|
|
|
|
// Transform the map-based TaskStates object into an array-based
|
|
|
|
// TaskState fragment list
|
2018-12-13 02:21:36 +00:00
|
|
|
const states = hash.TaskStates || {};
|
2020-09-05 02:44:21 +00:00
|
|
|
hash.TaskStates = Object.keys(states)
|
|
|
|
.sort()
|
2021-12-28 14:45:20 +00:00
|
|
|
.map((key) => {
|
2020-09-05 02:44:21 +00:00
|
|
|
const state = states[key] || {};
|
|
|
|
const summary = { Name: key };
|
2021-12-28 14:45:20 +00:00
|
|
|
Object.keys(state).forEach((stateKey) => (summary[stateKey] = state[stateKey]));
|
2020-10-12 22:26:54 +00:00
|
|
|
summary.Resources = hash.AllocatedResources && hash.AllocatedResources.Tasks[key];
|
2020-09-05 02:44:21 +00:00
|
|
|
return summary;
|
|
|
|
});
|
2017-09-19 14:47:10 +00:00
|
|
|
|
2017-10-19 17:17:04 +00:00
|
|
|
hash.JobVersion = hash.JobVersion != null ? hash.JobVersion : get(hash, 'Job.Version');
|
|
|
|
|
2017-10-23 17:22:58 +00:00
|
|
|
hash.PlainJobId = hash.JobID;
|
2021-04-29 20:00:59 +00:00
|
|
|
hash.Namespace = hash.Namespace || get(hash, 'Job.Namespace') || 'default';
|
2017-10-23 17:22:58 +00:00
|
|
|
hash.JobID = JSON.stringify([hash.JobID, hash.Namespace]);
|
|
|
|
|
2018-04-25 22:50:15 +00:00
|
|
|
hash.RescheduleEvents = (hash.RescheduleTracker || {}).Events;
|
|
|
|
|
2019-10-25 00:55:13 +00:00
|
|
|
hash.IsMigrating = (hash.DesiredTransition || {}).Migrate;
|
|
|
|
|
2018-05-02 18:21:50 +00:00
|
|
|
// API returns empty strings instead of null
|
|
|
|
hash.PreviousAllocationID = hash.PreviousAllocation ? hash.PreviousAllocation : null;
|
|
|
|
hash.NextAllocationID = hash.NextAllocation ? hash.NextAllocation : null;
|
|
|
|
hash.FollowUpEvaluationID = hash.FollowupEvalID ? hash.FollowupEvalID : null;
|
|
|
|
|
2019-04-16 19:11:43 +00:00
|
|
|
hash.PreemptedAllocationIDs = hash.PreemptedAllocations || [];
|
|
|
|
hash.PreemptedByAllocationID = hash.PreemptedByAllocation || null;
|
2019-04-16 20:23:16 +00:00
|
|
|
hash.WasPreempted = !!hash.PreemptedByAllocationID;
|
2019-04-16 19:11:43 +00:00
|
|
|
|
2020-10-12 22:26:54 +00:00
|
|
|
const shared = hash.AllocatedResources && hash.AllocatedResources.Shared;
|
|
|
|
hash.AllocatedResources =
|
|
|
|
hash.AllocatedResources && merge(Object.values(hash.AllocatedResources.Tasks));
|
|
|
|
if (shared) {
|
|
|
|
hash.AllocatedResources.Ports = shared.Ports;
|
|
|
|
hash.AllocatedResources.Networks = shared.Networks;
|
|
|
|
}
|
2019-09-04 14:39:56 +00:00
|
|
|
|
2020-05-01 21:30:02 +00:00
|
|
|
// The Job definition for an allocation is only included in findRecord responses.
|
|
|
|
hash.AllocationTaskGroup = !hash.Job ? null : taskGroupFromJob(hash.Job, hash.TaskGroup);
|
|
|
|
|
2020-06-11 21:23:00 +00:00
|
|
|
return super.normalize(typeHash, hash);
|
|
|
|
}
|
|
|
|
}
|