github.com/anth0d/nomad@v0.0.0-20221214183521-ae3a0a2cad06/ui/mirage/serializers/csi-volume.js (about)

     1  import ApplicationSerializer from './application';
     2  
     3  const groupBy = (list, attr) => {
     4    return list.reduce((group, item) => {
     5      group[item[attr]] = item;
     6      return group;
     7    }, {});
     8  };
     9  
    10  export default ApplicationSerializer.extend({
    11    embed: true,
    12    include: ['writeAllocs', 'readAllocs', 'allocations'],
    13  
    14    serialize() {
    15      var json = ApplicationSerializer.prototype.serialize.apply(this, arguments);
    16      if (json instanceof Array) {
    17        json.forEach(serializeVolumeFromArray);
    18      } else {
    19        serializeVolume(json);
    20      }
    21      return json;
    22    },
    23  });
    24  
    25  function serializeVolumeFromArray(volume) {
    26    volume.CurrentWriters = volume.WriteAllocs.length;
    27    delete volume.WriteAllocs;
    28  
    29    volume.CurrentReaders = volume.ReadAllocs.length;
    30    delete volume.ReadAllocs;
    31  }
    32  
    33  function serializeVolume(volume) {
    34    volume.WriteAllocs = groupBy(volume.WriteAllocs, 'ID');
    35    volume.ReadAllocs = groupBy(volume.ReadAllocs, 'ID');
    36  }