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