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