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

74 lines
2 KiB
JavaScript
Raw Normal View History

2017-09-19 14:47:10 +00:00
import Ember from 'ember';
import ApplicationSerializer from './application';
import queryString from 'npm:query-string';
2017-09-19 14:47:10 +00:00
2017-10-10 16:36:15 +00:00
const { get, assign } = Ember;
2017-09-19 14:47:10 +00:00
export default ApplicationSerializer.extend({
attrs: {
parameterized: 'ParameterizedJob',
},
normalize(typeHash, hash) {
hash.NamespaceID = hash.Namespace;
2017-09-19 14:47:10 +00:00
// Transform the map-based JobSummary object into an array-based
// JobSummary fragment list
hash.TaskGroupSummaries = Object.keys(get(hash, 'JobSummary.Summary')).map(key => {
const allocStats = get(hash, `JobSummary.Summary.${key}`);
const summary = { Name: key };
Object.keys(allocStats).forEach(
allocKey => (summary[`${allocKey}Allocs`] = allocStats[allocKey])
);
return summary;
});
// Lift the children stats out of the JobSummary object
const childrenStats = get(hash, 'JobSummary.Children');
if (childrenStats) {
Object.keys(childrenStats).forEach(
childrenKey => (hash[`${childrenKey}Children`] = childrenStats[childrenKey])
);
}
return this._super(typeHash, hash);
},
extractRelationships(modelClass, hash) {
const namespace =
!hash.NamespaceID || hash.NamespaceID === 'default' ? undefined : hash.NamespaceID;
2017-09-19 14:47:10 +00:00
const { modelName } = modelClass;
const jobURL = this.store
.adapterFor(modelName)
.buildURL(modelName, this.extractId(modelClass, hash), hash, 'findRecord');
2017-10-10 16:36:15 +00:00
return assign(this._super(...arguments), {
2017-09-19 14:47:10 +00:00
allocations: {
links: {
related: buildURL(`${jobURL}/allocations`, { namespace: namespace }),
2017-09-19 14:47:10 +00:00
},
},
versions: {
links: {
related: buildURL(`${jobURL}/versions`, { namespace: namespace, diffs: true }),
2017-09-19 14:47:10 +00:00
},
},
deployments: {
links: {
related: buildURL(`${jobURL}/deployments`, { namespace: namespace }),
2017-09-19 14:47:10 +00:00
},
},
2017-10-10 16:36:15 +00:00
});
2017-09-19 14:47:10 +00:00
},
});
function buildURL(path, queryParams) {
const qpString = queryString.stringify(queryParams);
if (qpString) {
return `${path}?${qpString}`;
}
return path;
}