2017-12-15 21:39:18 +00:00
|
|
|
import { assign } from '@ember/polyfills';
|
2017-09-19 14:47:10 +00:00
|
|
|
import ApplicationSerializer from './application';
|
2018-07-06 20:38:57 +00:00
|
|
|
import queryString from 'query-string';
|
2022-03-08 17:28:36 +00:00
|
|
|
import classic from 'ember-classic-decorator';
|
2017-09-19 14:47:10 +00:00
|
|
|
|
2022-03-08 17:28:36 +00:00
|
|
|
@classic
|
2020-06-11 21:23:00 +00:00
|
|
|
export default class JobSerializer extends ApplicationSerializer {
|
|
|
|
attrs = {
|
2017-09-19 14:47:10 +00:00
|
|
|
parameterized: 'ParameterizedJob',
|
2020-06-11 21:23:00 +00:00
|
|
|
};
|
2017-09-19 14:47:10 +00:00
|
|
|
|
2020-09-19 00:33:43 +00:00
|
|
|
separateNanos = ['SubmitTime'];
|
|
|
|
|
2017-09-19 14:47:10 +00:00
|
|
|
normalize(typeHash, hash) {
|
2017-10-18 17:39:36 +00:00
|
|
|
hash.NamespaceID = hash.Namespace;
|
|
|
|
|
2017-10-23 17:22:58 +00:00
|
|
|
// 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']);
|
|
|
|
|
2018-01-23 21:23:16 +00:00
|
|
|
// ParentID comes in as "" instead of null
|
|
|
|
if (!hash.ParentID) {
|
|
|
|
hash.ParentID = null;
|
|
|
|
} else {
|
2021-12-28 16:08:12 +00:00
|
|
|
hash.ParentID = JSON.stringify([
|
|
|
|
hash.ParentID,
|
|
|
|
hash.NamespaceID || 'default',
|
|
|
|
]);
|
2018-01-23 21:23:16 +00:00
|
|
|
}
|
|
|
|
|
2018-03-20 19:35:32 +00:00
|
|
|
// 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;
|
|
|
|
|
2018-01-23 21:23:16 +00:00
|
|
|
// Periodic is a boolean on list and an object on single
|
|
|
|
if (hash.Periodic instanceof Object) {
|
|
|
|
hash.PeriodicDetails = hash.Periodic;
|
|
|
|
hash.Periodic = true;
|
|
|
|
}
|
|
|
|
|
2018-01-25 17:30:46 +00:00
|
|
|
// Parameterized behaves like Periodic
|
|
|
|
if (hash.ParameterizedJob instanceof Object) {
|
|
|
|
hash.ParameterizedDetails = hash.ParameterizedJob;
|
|
|
|
hash.ParameterizedJob = true;
|
|
|
|
}
|
|
|
|
|
2018-03-15 17:27:01 +00:00
|
|
|
// 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],
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2020-06-11 21:23:00 +00:00
|
|
|
return super.normalize(typeHash, hash);
|
|
|
|
}
|
2017-09-19 14:47:10 +00:00
|
|
|
|
|
|
|
extractRelationships(modelClass, hash) {
|
2017-10-18 17:39:36 +00:00
|
|
|
const namespace =
|
2021-12-28 16:08:12 +00:00
|
|
|
!hash.NamespaceID || hash.NamespaceID === 'default'
|
|
|
|
? undefined
|
|
|
|
: hash.NamespaceID;
|
2017-09-19 14:47:10 +00:00
|
|
|
const { modelName } = modelClass;
|
2017-10-23 17:22:58 +00:00
|
|
|
|
2021-12-28 16:08:12 +00:00
|
|
|
const apiNamespace = this.store
|
|
|
|
.adapterFor(modelClass.modelName)
|
|
|
|
.get('namespace');
|
2020-10-29 12:46:42 +00:00
|
|
|
|
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
|
|
|
|
2022-07-13 15:40:13 +00:00
|
|
|
const variableLookup = hash.ParentID
|
|
|
|
? JSON.parse(hash.ParentID)[0]
|
|
|
|
: hash.PlainId;
|
|
|
|
|
2020-06-11 21:23:00 +00:00
|
|
|
return assign(super.extractRelationships(...arguments), {
|
2017-09-19 14:47:10 +00:00
|
|
|
allocations: {
|
|
|
|
links: {
|
2018-07-28 00:40:09 +00:00
|
|
|
related: buildURL(`${jobURL}/allocations`, { namespace }),
|
2017-09-19 14:47:10 +00:00
|
|
|
},
|
|
|
|
},
|
|
|
|
versions: {
|
|
|
|
links: {
|
2018-07-28 00:40:09 +00:00
|
|
|
related: buildURL(`${jobURL}/versions`, { namespace, diffs: true }),
|
2017-09-19 14:47:10 +00:00
|
|
|
},
|
|
|
|
},
|
|
|
|
deployments: {
|
|
|
|
links: {
|
2018-07-28 00:40:09 +00:00
|
|
|
related: buildURL(`${jobURL}/deployments`, { namespace }),
|
|
|
|
},
|
|
|
|
},
|
|
|
|
latestDeployment: {
|
|
|
|
links: {
|
|
|
|
related: buildURL(`${jobURL}/deployment`, { namespace }),
|
2017-09-19 14:47:10 +00:00
|
|
|
},
|
|
|
|
},
|
2017-11-29 01:23:30 +00:00
|
|
|
evaluations: {
|
|
|
|
links: {
|
2018-07-28 00:40:09 +00:00
|
|
|
related: buildURL(`${jobURL}/evaluations`, { namespace }),
|
2017-11-29 01:23:30 +00:00
|
|
|
},
|
|
|
|
},
|
2022-08-26 03:22:55 +00:00
|
|
|
services: {
|
|
|
|
links: {
|
|
|
|
related: buildURL(`${jobURL}/services`, { namespace }),
|
|
|
|
},
|
|
|
|
},
|
2022-07-13 15:40:13 +00:00
|
|
|
variables: {
|
|
|
|
links: {
|
|
|
|
related: buildURL(`/${apiNamespace}/vars`, {
|
2022-08-09 18:08:28 +00:00
|
|
|
prefix: `nomad/jobs/${variableLookup}`,
|
|
|
|
namespace,
|
2022-07-13 15:40:13 +00:00
|
|
|
}),
|
|
|
|
},
|
|
|
|
},
|
2020-07-28 04:26:17 +00:00
|
|
|
scaleState: {
|
2020-07-24 04:39:50 +00:00
|
|
|
links: {
|
|
|
|
related: buildURL(`${jobURL}/scale`, { namespace }),
|
|
|
|
},
|
|
|
|
},
|
2020-10-29 12:46:42 +00:00
|
|
|
recommendationSummaries: {
|
|
|
|
links: {
|
|
|
|
related: buildURL(`/${apiNamespace}/recommendations`, {
|
|
|
|
job: hash.PlainId,
|
|
|
|
namespace: hash.NamespaceID || 'default',
|
|
|
|
}),
|
|
|
|
},
|
|
|
|
},
|
2017-10-10 16:36:15 +00:00
|
|
|
});
|
2020-06-11 21:23:00 +00:00
|
|
|
}
|
|
|
|
}
|
2017-10-18 17:39:36 +00:00
|
|
|
|
|
|
|
function buildURL(path, queryParams) {
|
|
|
|
const qpString = queryString.stringify(queryParams);
|
|
|
|
if (qpString) {
|
|
|
|
return `${path}?${qpString}`;
|
|
|
|
}
|
|
|
|
return path;
|
|
|
|
}
|