github.com/manicqin/nomad@v0.9.5/ui/app/serializers/job.js (about)

     1  import { assign } from '@ember/polyfills';
     2  import ApplicationSerializer from './application';
     3  import queryString from 'query-string';
     4  
     5  export default ApplicationSerializer.extend({
     6    attrs: {
     7      parameterized: 'ParameterizedJob',
     8    },
     9  
    10    normalize(typeHash, hash) {
    11      hash.NamespaceID = hash.Namespace;
    12  
    13      // ID is a composite of both the job ID and the namespace the job is in
    14      hash.PlainId = hash.ID;
    15      hash.ID = JSON.stringify([hash.ID, hash.NamespaceID || 'default']);
    16  
    17      // ParentID comes in as "" instead of null
    18      if (!hash.ParentID) {
    19        hash.ParentID = null;
    20      } else {
    21        hash.ParentID = JSON.stringify([hash.ParentID, hash.NamespaceID || 'default']);
    22      }
    23  
    24      // Job Summary is always at /:job-id/summary, but since it can also come from
    25      // the job list, it's better for Ember Data to be linked by ID association.
    26      hash.SummaryID = hash.ID;
    27  
    28      // Periodic is a boolean on list and an object on single
    29      if (hash.Periodic instanceof Object) {
    30        hash.PeriodicDetails = hash.Periodic;
    31        hash.Periodic = true;
    32      }
    33  
    34      // Parameterized behaves like Periodic
    35      if (hash.ParameterizedJob instanceof Object) {
    36        hash.ParameterizedDetails = hash.ParameterizedJob;
    37        hash.ParameterizedJob = true;
    38      }
    39  
    40      // If the hash contains summary information, push it into the store
    41      // as a job-summary model.
    42      if (hash.JobSummary) {
    43        this.store.pushPayload('job-summary', {
    44          'job-summary': [hash.JobSummary],
    45        });
    46      }
    47  
    48      return this._super(typeHash, hash);
    49    },
    50  
    51    extractRelationships(modelClass, hash) {
    52      const namespace =
    53        !hash.NamespaceID || hash.NamespaceID === 'default' ? undefined : hash.NamespaceID;
    54      const { modelName } = modelClass;
    55  
    56      const [jobURL] = this.store
    57        .adapterFor(modelName)
    58        .buildURL(modelName, hash.ID, hash, 'findRecord')
    59        .split('?');
    60  
    61      return assign(this._super(...arguments), {
    62        allocations: {
    63          links: {
    64            related: buildURL(`${jobURL}/allocations`, { namespace }),
    65          },
    66        },
    67        versions: {
    68          links: {
    69            related: buildURL(`${jobURL}/versions`, { namespace, diffs: true }),
    70          },
    71        },
    72        deployments: {
    73          links: {
    74            related: buildURL(`${jobURL}/deployments`, { namespace }),
    75          },
    76        },
    77        latestDeployment: {
    78          links: {
    79            related: buildURL(`${jobURL}/deployment`, { namespace }),
    80          },
    81        },
    82        evaluations: {
    83          links: {
    84            related: buildURL(`${jobURL}/evaluations`, { namespace }),
    85          },
    86        },
    87      });
    88    },
    89  });
    90  
    91  function buildURL(path, queryParams) {
    92    const qpString = queryString.stringify(queryParams);
    93    if (qpString) {
    94      return `${path}?${qpString}`;
    95    }
    96    return path;
    97  }