open-nomad/ui/mirage/factories/task.js
Buck Doyle b9f462fdc1
ui: Add optional memory max to task details ribbon (#10459)
This is the first step in #10268. If a maximum is not specified, the
task group sum uses the memory number instead. The maximum is only
shown when it’s higher than the memory sum.
2021-04-28 15:38:14 -05:00

68 lines
1.9 KiB
JavaScript

import { Factory } from 'ember-cli-mirage';
import faker from 'nomad-ui/mirage/faker';
import { generateResources } from '../common';
const DRIVERS = ['docker', 'java', 'rkt', 'qemu', 'exec', 'raw_exec'];
export default Factory.extend({
createRecommendations: false,
// Hidden property used to compute the Summary hash
groupNames: [],
// Set in the TaskGroup factory
volumeMounts: [],
JobID: '',
name: id => `task-${faker.hacker.noun().dasherize()}-${id}`,
driver: () => faker.helpers.randomize(DRIVERS),
originalResources: generateResources,
resources: function() {
// Generate resources the usual way, but transform to the old
// shape because that's what the job spec uses.
const resources = this.originalResources;
return {
CPU: resources.Cpu.CpuShares,
MemoryMB: resources.Memory.MemoryMB,
MemoryMaxMB: resources.Memory.MemoryMaxMB,
DiskMB: resources.Disk.DiskMB,
};
},
Lifecycle: i => {
const cycle = i % 6;
if (cycle === 0) {
return null;
} else if (cycle === 1) {
return { Hook: 'prestart', Sidecar: false };
} else if (cycle === 2) {
return { Hook: 'prestart', Sidecar: true };
} else if (cycle === 3) {
return { Hook: 'poststart', Sidecar: false };
} else if (cycle === 4) {
return { Hook: 'poststart', Sidecar: true };
} else if (cycle === 5) {
return { Hook: 'poststop' };
}
},
afterCreate(task, server) {
if (task.createRecommendations) {
const recommendations = [];
if (faker.random.number(10) >= 1) {
recommendations.push(server.create('recommendation', { task, resource: 'CPU' }));
}
if (faker.random.number(10) >= 1) {
recommendations.push(server.create('recommendation', { task, resource: 'MemoryMB' }));
}
task.save({ recommendationIds: recommendations.mapBy('id') });
}
},
});