open-nomad/ui/mirage/serializers/csi-volume.js
Buck Doyle 543fb24764
Fix allocation count in CSI volumes table (#9515)
This closes #9495. As detailed in there, the collection query GET
/v1/volumes?type=csi doesn’t return ReadAllocs and WriteAllocs, so the #
Allocs cell was always showing 0 upon first load because it was derived
from the lengths of those arrays. This uses the heretofore-ignored
CurrentReaders and CurrentWriters values to calculate the total instead.

The single-resource query GET /v1/volume/csi%2F:id doesn’t return
CurrentReaders and CurrentWriters that absence doesn’t override the
stored values when visiting an individual item.

Thanks to @apollo13 for reporting this and to @tgross for the API logs
and suggestion.
2020-12-07 08:51:41 -06:00

37 lines
919 B
JavaScript

import ApplicationSerializer from './application';
const groupBy = (list, attr) => {
return list.reduce((group, item) => {
group[item[attr]] = item;
return group;
}, {});
};
export default ApplicationSerializer.extend({
embed: true,
include: ['writeAllocs', 'readAllocs', 'allocations'],
serialize() {
var json = ApplicationSerializer.prototype.serialize.apply(this, arguments);
if (json instanceof Array) {
json.forEach(serializeVolumeFromArray);
} else {
serializeVolume(json);
}
return json;
},
});
function serializeVolumeFromArray(volume) {
volume.CurrentWriters = volume.WriteAllocs.length;
delete volume.WriteAllocs;
volume.CurrentReaders = volume.ReadAllocs.length;
delete volume.ReadAllocs;
}
function serializeVolume(volume) {
volume.WriteAllocs = groupBy(volume.WriteAllocs, 'ID');
volume.ReadAllocs = groupBy(volume.ReadAllocs, 'ID');
}