github.com/anth0d/nomad@v0.0.0-20221214183521-ae3a0a2cad06/ui/app/serializers/allocation.js (about)

     1  import { inject as service } from '@ember/service';
     2  import { get } from '@ember/object';
     3  import ApplicationSerializer from './application';
     4  import classic from 'ember-classic-decorator';
     5  
     6  const taskGroupFromJob = (job, taskGroupName) => {
     7    const taskGroups = job && job.TaskGroups;
     8    const taskGroup =
     9      taskGroups && taskGroups.find((group) => group.Name === taskGroupName);
    10    return taskGroup ? taskGroup : null;
    11  };
    12  
    13  const merge = (tasks) => {
    14    const mergedResources = {
    15      Cpu: { CpuShares: 0 },
    16      Memory: { MemoryMB: 0 },
    17      Disk: { DiskMB: 0 },
    18    };
    19  
    20    return tasks.reduce((resources, task) => {
    21      resources.Cpu.CpuShares += (task.Cpu && task.Cpu.CpuShares) || 0;
    22      resources.Memory.MemoryMB += (task.Memory && task.Memory.MemoryMB) || 0;
    23      resources.Disk.DiskMB += (task.Disk && task.Disk.DiskMB) || 0;
    24      return resources;
    25    }, mergedResources);
    26  };
    27  
    28  @classic
    29  export default class AllocationSerializer extends ApplicationSerializer {
    30    @service system;
    31  
    32    attrs = {
    33      taskGroupName: 'TaskGroup',
    34      states: 'TaskStates',
    35    };
    36  
    37    separateNanos = ['CreateTime', 'ModifyTime'];
    38  
    39    normalize(typeHash, hash) {
    40      // Transform the map-based TaskStates object into an array-based
    41      // TaskState fragment list
    42      const states = hash.TaskStates || {};
    43      hash.TaskStates = Object.keys(states)
    44        .sort()
    45        .map((key) => {
    46          const state = states[key] || {};
    47          const summary = { Name: key };
    48          Object.keys(state).forEach(
    49            (stateKey) => (summary[stateKey] = state[stateKey])
    50          );
    51          summary.Resources =
    52            hash.AllocatedResources && hash.AllocatedResources.Tasks[key];
    53          return summary;
    54        });
    55  
    56      hash.JobVersion =
    57        hash.JobVersion != null ? hash.JobVersion : get(hash, 'Job.Version');
    58  
    59      hash.PlainJobId = hash.JobID;
    60      hash.Namespace = hash.Namespace || get(hash, 'Job.Namespace') || 'default';
    61      hash.JobID = JSON.stringify([hash.JobID, hash.Namespace]);
    62  
    63      hash.RescheduleEvents = (hash.RescheduleTracker || {}).Events;
    64  
    65      hash.IsMigrating = (hash.DesiredTransition || {}).Migrate;
    66  
    67      // API returns empty strings instead of null
    68      hash.PreviousAllocationID = hash.PreviousAllocation
    69        ? hash.PreviousAllocation
    70        : null;
    71      hash.NextAllocationID = hash.NextAllocation ? hash.NextAllocation : null;
    72      hash.FollowUpEvaluationID = hash.FollowupEvalID
    73        ? hash.FollowupEvalID
    74        : null;
    75  
    76      hash.PreemptedAllocationIDs = hash.PreemptedAllocations || [];
    77      hash.PreemptedByAllocationID = hash.PreemptedByAllocation || null;
    78      hash.WasPreempted = !!hash.PreemptedByAllocationID;
    79  
    80      const shared = hash.AllocatedResources && hash.AllocatedResources.Shared;
    81      hash.AllocatedResources =
    82        hash.AllocatedResources &&
    83        merge(Object.values(hash.AllocatedResources.Tasks));
    84      if (shared) {
    85        hash.AllocatedResources.Ports = shared.Ports;
    86        hash.AllocatedResources.Networks = shared.Networks;
    87      }
    88  
    89      // The Job definition for an allocation is only included in findRecord responses.
    90      hash.AllocationTaskGroup = !hash.Job
    91        ? null
    92        : taskGroupFromJob(hash.Job, hash.TaskGroup);
    93  
    94      return super.normalize(typeHash, hash);
    95    }
    96  }