github.com/blixtra/nomad@v0.7.2-0.20171221000451-da9a1d7bb050/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    clientStatus: faker.list.random(...CLIENT_STATUSES),
    19    desiredStatus: faker.list.random(...DESIRED_STATUSES),
    20  
    21    // Meta property for hinting at task events
    22    useMessagePassthru: false,
    23  
    24    withTaskWithPorts: trait({
    25      afterCreate(allocation, server) {
    26        const taskGroup = server.db.taskGroups.findBy({ name: allocation.taskGroup });
    27        const resources = taskGroup.taskIds.map(id =>
    28          server.create(
    29            'task-resources',
    30            {
    31              allocation,
    32              name: server.db.tasks.find(id).name,
    33            },
    34            'withReservedPorts'
    35          )
    36        );
    37  
    38        allocation.update({ taskResourcesIds: resources.mapBy('id') });
    39      },
    40    }),
    41  
    42    withoutTaskWithPorts: trait({
    43      afterCreate(allocation, server) {
    44        const taskGroup = server.db.taskGroups.findBy({ name: allocation.taskGroup });
    45        const resources = taskGroup.taskIds.map(id =>
    46          server.create(
    47            'task-resources',
    48            {
    49              allocation,
    50              name: server.db.tasks.find(id).name,
    51            },
    52            'withoutReservedPorts'
    53          )
    54        );
    55  
    56        allocation.update({ taskResourcesIds: resources.mapBy('id') });
    57      },
    58    }),
    59  
    60    afterCreate(allocation, server) {
    61      Ember.assert(
    62        '[Mirage] No jobs! make sure jobs are created before allocations',
    63        server.db.jobs.length
    64      );
    65      Ember.assert(
    66        '[Mirage] No nodes! make sure nodes are created before allocations',
    67        server.db.nodes.length
    68      );
    69  
    70      const job = allocation.jobId ? server.db.jobs.find(allocation.jobId) : pickOne(server.db.jobs);
    71      const node = allocation.nodeId
    72        ? server.db.nodes.find(allocation.nodeId)
    73        : pickOne(server.db.nodes);
    74      const taskGroup = allocation.taskGroup
    75        ? server.db.taskGroups.findBy({ name: allocation.taskGroup })
    76        : pickOne(server.db.taskGroups.where({ jobId: job.id }));
    77  
    78      const states = taskGroup.taskIds.map(id =>
    79        server.create('task-state', {
    80          allocation,
    81          name: server.db.tasks.find(id).name,
    82          useMessagePassthru: allocation.useMessagePassthru,
    83        })
    84      );
    85  
    86      const resources = taskGroup.taskIds.map(id =>
    87        server.create('task-resources', {
    88          allocation,
    89          name: server.db.tasks.find(id).name,
    90        })
    91      );
    92  
    93      allocation.update({
    94        jobId: job.id,
    95        nodeId: node.id,
    96        taskStateIds: states.mapBy('id'),
    97        task_state_ids: states.mapBy('id'),
    98        taskResourcesIds: resources.mapBy('id'),
    99        taskGroup: taskGroup.name,
   100        name: allocation.name || `${taskGroup.name}.[${faker.random.number(10)}]`,
   101      });
   102  
   103      // Each allocation has a corresponding allocation stats running on some client.
   104      // Create that record, even though it's not a relationship.
   105      server.create('client-allocation-stats', {
   106        id: allocation.id,
   107        _tasks: states.mapBy('name'),
   108      });
   109    },
   110  });