open-nomad/ui/app/serializers/allocation.js

69 lines
2.5 KiB
JavaScript
Raw Normal View History

import { inject as service } from '@ember/service';
import { get } from '@ember/object';
2017-09-19 14:47:10 +00:00
import ApplicationSerializer from './application';
import classic from 'ember-classic-decorator';
2017-09-19 14:47:10 +00:00
const taskGroupFromJob = (job, taskGroupName) => {
const taskGroups = job && job.TaskGroups;
const taskGroup = taskGroups && taskGroups.find(group => group.Name === taskGroupName);
return taskGroup ? taskGroup : null;
};
@classic
export default class AllocationSerializer extends ApplicationSerializer {
@service system;
attrs = {
2017-09-19 14:47:10 +00:00
taskGroupName: 'TaskGroup',
states: 'TaskStates',
};
2017-09-19 14:47:10 +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
const states = hash.TaskStates || {};
hash.TaskStates = Object.keys(states)
.sort()
.map(key => {
const state = states[key] || {};
const summary = { Name: key };
Object.keys(state).forEach(stateKey => (summary[stateKey] = state[stateKey]));
summary.Resources = hash.TaskResources && hash.TaskResources[key];
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');
hash.PlainJobId = hash.JobID;
hash.Namespace =
hash.Namespace ||
get(hash, 'Job.Namespace') ||
this.get('system.activeNamespace.id') ||
'default';
hash.JobID = JSON.stringify([hash.JobID, hash.Namespace]);
2018-04-25 22:50:15 +00:00
hash.RescheduleEvents = (hash.RescheduleTracker || {}).Events;
hash.IsMigrating = (hash.DesiredTransition || {}).Migrate;
// 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
// 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 super.normalize(typeHash, hash);
}
}