open-nomad/ui/app/serializers/recommendation-summary.js

Ignoring revisions in .git-blame-ignore-revs. Click here to bypass and see the normal blame view.

95 lines
2.6 KiB
JavaScript
Raw Normal View History

import ApplicationSerializer from './application';
import classic from 'ember-classic-decorator';
/*
Theres no grouping of recommendations on the server, so this
processes a list of recommendations and groups them by task
group.
*/
@classic
export default class RecommendationSummarySerializer extends ApplicationSerializer {
normalizeArrayResponse(store, modelClass, payload) {
const recommendationSerializer = store.serializerFor('recommendation');
const RecommendationModel = store.modelFor('recommendation');
const slugToSummaryObject = {};
const allRecommendations = [];
2021-12-28 14:45:20 +00:00
payload.forEach((recommendationHash) => {
2021-12-28 16:08:12 +00:00
const slug = `${JSON.stringify([
recommendationHash.JobID,
recommendationHash.Namespace,
])}/${recommendationHash.Group}`;
if (!slugToSummaryObject[slug]) {
slugToSummaryObject[slug] = {
attributes: {
jobId: recommendationHash.JobID,
jobNamespace: recommendationHash.Namespace,
taskGroupName: recommendationHash.Group,
},
recommendations: [],
};
}
slugToSummaryObject[slug].recommendations.push(recommendationHash);
allRecommendations.push(recommendationHash);
});
return {
2021-12-28 14:45:20 +00:00
data: Object.values(slugToSummaryObject).map((summaryObject) => {
2021-12-28 16:08:12 +00:00
const latest = Math.max(
...summaryObject.recommendations.mapBy('SubmitTime')
);
return {
type: 'recommendation-summary',
2021-12-28 14:45:20 +00:00
id: summaryObject.recommendations.mapBy('ID').sort().join('-'),
attributes: {
...summaryObject.attributes,
submitTime: new Date(Math.floor(latest / 1000000)),
},
relationships: {
job: {
data: {
type: 'job',
id: JSON.stringify([
summaryObject.attributes.jobId,
summaryObject.attributes.jobNamespace,
]),
},
},
recommendations: {
2021-12-28 14:45:20 +00:00
data: summaryObject.recommendations.map((r) => {
return {
type: 'recommendation',
id: r.ID,
};
}),
},
},
};
}),
included: allRecommendations.map(
2021-12-28 14:45:20 +00:00
(recommendationHash) =>
2021-12-28 16:08:12 +00:00
recommendationSerializer.normalize(
RecommendationModel,
recommendationHash
).data
),
};
}
normalizeUpdateRecordResponse(store, primaryModelClass, payload, id) {
return {
data: {
id,
attributes: {
isProcessed: true,
},
},
};
}
}