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

115 lines
3.2 KiB
JavaScript
Raw Normal View History

import { assign } from '@ember/polyfills';
2017-09-19 14:47:10 +00:00
import ApplicationSerializer from './application';
import queryString from 'query-string';
2017-09-19 14:47:10 +00:00
export default class JobSerializer extends ApplicationSerializer {
attrs = {
2017-09-19 14:47:10 +00:00
parameterized: 'ParameterizedJob',
};
2017-09-19 14:47:10 +00:00
separateNanos = ['SubmitTime'];
2017-09-19 14:47:10 +00:00
normalize(typeHash, hash) {
hash.NamespaceID = hash.Namespace;
// ID is a composite of both the job ID and the namespace the job is in
hash.PlainId = hash.ID;
hash.ID = JSON.stringify([hash.ID, hash.NamespaceID || 'default']);
// ParentID comes in as "" instead of null
if (!hash.ParentID) {
hash.ParentID = null;
} else {
hash.ParentID = JSON.stringify([hash.ParentID, hash.NamespaceID || 'default']);
}
// Job Summary is always at /:job-id/summary, but since it can also come from
// the job list, it's better for Ember Data to be linked by ID association.
hash.SummaryID = hash.ID;
// Periodic is a boolean on list and an object on single
if (hash.Periodic instanceof Object) {
hash.PeriodicDetails = hash.Periodic;
hash.Periodic = true;
}
// Parameterized behaves like Periodic
if (hash.ParameterizedJob instanceof Object) {
hash.ParameterizedDetails = hash.ParameterizedJob;
hash.ParameterizedJob = true;
}
// If the hash contains summary information, push it into the store
// as a job-summary model.
if (hash.JobSummary) {
this.store.pushPayload('job-summary', {
'job-summary': [hash.JobSummary],
});
}
return super.normalize(typeHash, hash);
}
2017-09-19 14:47:10 +00:00
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 apiNamespace = this.store.adapterFor(modelClass.modelName).get('namespace');
2018-02-16 02:55:59 +00:00
const [jobURL] = this.store
2017-09-19 14:47:10 +00:00
.adapterFor(modelName)
2018-02-16 02:55:59 +00:00
.buildURL(modelName, hash.ID, hash, 'findRecord')
.split('?');
2017-09-19 14:47:10 +00:00
return assign(super.extractRelationships(...arguments), {
2017-09-19 14:47:10 +00:00
allocations: {
links: {
related: buildURL(`${jobURL}/allocations`, { namespace }),
2017-09-19 14:47:10 +00:00
},
},
versions: {
links: {
related: buildURL(`${jobURL}/versions`, { namespace, diffs: true }),
2017-09-19 14:47:10 +00:00
},
},
deployments: {
links: {
related: buildURL(`${jobURL}/deployments`, { namespace }),
},
},
latestDeployment: {
links: {
related: buildURL(`${jobURL}/deployment`, { namespace }),
2017-09-19 14:47:10 +00:00
},
},
evaluations: {
links: {
related: buildURL(`${jobURL}/evaluations`, { namespace }),
},
},
scaleState: {
links: {
related: buildURL(`${jobURL}/scale`, { namespace }),
},
},
recommendationSummaries: {
links: {
related: buildURL(`/${apiNamespace}/recommendations`, {
job: hash.PlainId,
namespace: hash.NamespaceID || 'default',
}),
},
},
2017-10-10 16:36:15 +00:00
});
}
}
function buildURL(path, queryParams) {
const qpString = queryString.stringify(queryParams);
if (qpString) {
return `${path}?${qpString}`;
}
return path;
}