github.com/emate/nomad@v0.8.2-wo-binpacking/ui/mirage/factories/allocation.js (about)

     1  import Ember from 'ember';
     2  import { Factory, faker, trait } from 'ember-cli-mirage';
     3  import { provide, pickOne } from '../utils';
     4  
     5  const UUIDS = provide(100, faker.random.uuid.bind(faker.random));
     6  const CLIENT_STATUSES = ['pending', 'running', 'complete', 'failed', 'lost'];
     7  const DESIRED_STATUSES = ['run', 'stop', 'evict'];
     8  const REF_TIME = new Date();
     9  
    10  export default Factory.extend({
    11    id: i => (i >= 100 ? `${UUIDS[i % 100]}-${i}` : UUIDS[i]),
    12  
    13    modifyIndex: () => faker.random.number({ min: 10, max: 2000 }),
    14    jobVersion: () => faker.random.number(10),
    15  
    16    modifyTime: () => faker.date.past(2 / 365, REF_TIME) * 1000000,
    17  
    18    namespace: null,
    19  
    20    clientStatus: faker.list.random(...CLIENT_STATUSES),
    21    desiredStatus: faker.list.random(...DESIRED_STATUSES),
    22  
    23    withTaskWithPorts: trait({
    24      afterCreate(allocation, server) {
    25        const taskGroup = server.db.taskGroups.findBy({ name: allocation.taskGroup });
    26        const resources = taskGroup.taskIds.map(id =>
    27          server.create(
    28            'task-resources',
    29            {
    30              allocation,
    31              name: server.db.tasks.find(id).name,
    32            },
    33            'withReservedPorts'
    34          )
    35        );
    36  
    37        allocation.update({ taskResourcesIds: resources.mapBy('id') });
    38      },
    39    }),
    40  
    41    withoutTaskWithPorts: trait({
    42      afterCreate(allocation, server) {
    43        const taskGroup = server.db.taskGroups.findBy({ name: allocation.taskGroup });
    44        const resources = taskGroup.taskIds.map(id =>
    45          server.create(
    46            'task-resources',
    47            {
    48              allocation,
    49              name: server.db.tasks.find(id).name,
    50            },
    51            'withoutReservedPorts'
    52          )
    53        );
    54  
    55        allocation.update({ taskResourcesIds: resources.mapBy('id') });
    56      },
    57    }),
    58  
    59    afterCreate(allocation, server) {
    60      Ember.assert(
    61        '[Mirage] No jobs! make sure jobs are created before allocations',
    62        server.db.jobs.length
    63      );
    64      Ember.assert(
    65        '[Mirage] No nodes! make sure nodes are created before allocations',
    66        server.db.nodes.length
    67      );
    68  
    69      const job = allocation.jobId ? server.db.jobs.find(allocation.jobId) : pickOne(server.db.jobs);
    70      const node = allocation.nodeId
    71        ? server.db.nodes.find(allocation.nodeId)
    72        : pickOne(server.db.nodes);
    73      const taskGroup = allocation.taskGroup
    74        ? server.db.taskGroups.findBy({ name: allocation.taskGroup })
    75        : pickOne(server.db.taskGroups.where({ jobId: job.id }));
    76  
    77      const states = taskGroup.taskIds.map(id =>
    78        server.create('task-state', {
    79          allocation,
    80          name: server.db.tasks.find(id).name,
    81        })
    82      );
    83  
    84      const resources = taskGroup.taskIds.map(id =>
    85        server.create('task-resources', {
    86          allocation,
    87          name: server.db.tasks.find(id).name,
    88        })
    89      );
    90  
    91      allocation.update({
    92        jobId: job.id,
    93        nodeId: node.id,
    94        taskStateIds: states.mapBy('id'),
    95        task_state_ids: states.mapBy('id'),
    96        taskResourcesIds: resources.mapBy('id'),
    97        taskGroup: taskGroup.name,
    98        name: allocation.name || `${taskGroup.name}.[${faker.random.number(10)}]`,
    99      });
   100  
   101      // Each allocation has a corresponding allocation stats running on some client.
   102      // Create that record, even though it's not a relationship.
   103      server.create('client-allocation-stats', {
   104        id: allocation.id,
   105        _tasks: states.mapBy('name'),
   106      });
   107    },
   108  });