github.com/blixtra/nomad@v0.7.2-0.20171221000451-da9a1d7bb050/ui/app/serializers/job.js (about)

     1  import Ember from 'ember';
     2  import ApplicationSerializer from './application';
     3  import queryString from 'npm:query-string';
     4  
     5  const { get, assign } = Ember;
     6  
     7  export default ApplicationSerializer.extend({
     8    attrs: {
     9      parameterized: 'ParameterizedJob',
    10    },
    11  
    12    normalize(typeHash, hash) {
    13      hash.NamespaceID = hash.Namespace;
    14  
    15      // ID is a composite of both the job ID and the namespace the job is in
    16      hash.PlainId = hash.ID;
    17      hash.ID = JSON.stringify([hash.ID, hash.NamespaceID || 'default']);
    18  
    19      // Transform the map-based JobSummary object into an array-based
    20      // JobSummary fragment list
    21      hash.TaskGroupSummaries = Object.keys(get(hash, 'JobSummary.Summary') || {}).map(key => {
    22        const allocStats = get(hash, `JobSummary.Summary.${key}`) || {};
    23        const summary = { Name: key };
    24  
    25        Object.keys(allocStats).forEach(
    26          allocKey => (summary[`${allocKey}Allocs`] = allocStats[allocKey])
    27        );
    28  
    29        return summary;
    30      });
    31  
    32      // Lift the children stats out of the JobSummary object
    33      const childrenStats = get(hash, 'JobSummary.Children');
    34      if (childrenStats) {
    35        Object.keys(childrenStats).forEach(
    36          childrenKey => (hash[`${childrenKey}Children`] = childrenStats[childrenKey])
    37        );
    38      }
    39  
    40      return this._super(typeHash, hash);
    41    },
    42  
    43    extractRelationships(modelClass, hash) {
    44      const namespace =
    45        !hash.NamespaceID || hash.NamespaceID === 'default' ? undefined : hash.NamespaceID;
    46      const { modelName } = modelClass;
    47  
    48      const jobURL = this.store
    49        .adapterFor(modelName)
    50        .buildURL(modelName, hash.PlainId, hash, 'findRecord');
    51  
    52      return assign(this._super(...arguments), {
    53        allocations: {
    54          links: {
    55            related: buildURL(`${jobURL}/allocations`, { namespace: namespace }),
    56          },
    57        },
    58        versions: {
    59          links: {
    60            related: buildURL(`${jobURL}/versions`, { namespace: namespace, diffs: true }),
    61          },
    62        },
    63        deployments: {
    64          links: {
    65            related: buildURL(`${jobURL}/deployments`, { namespace: namespace }),
    66          },
    67        },
    68        evaluations: {
    69          links: {
    70            related: buildURL(`${jobURL}/evaluations`, { namespace: namespace }),
    71          },
    72        },
    73      });
    74    },
    75  });
    76  
    77  function buildURL(path, queryParams) {
    78    const qpString = queryString.stringify(queryParams);
    79    if (qpString) {
    80      return `${path}?${qpString}`;
    81    }
    82    return path;
    83  }