github.com/hernad/nomad@v1.6.112/ui/app/serializers/recommendation-summary.js (about)

     1  /**
     2   * Copyright (c) HashiCorp, Inc.
     3   * SPDX-License-Identifier: MPL-2.0
     4   */
     5  
     6  import ApplicationSerializer from './application';
     7  import classic from 'ember-classic-decorator';
     8  
     9  /*
    10    There’s no grouping of recommendations on the server, so this
    11    processes a list of recommendations and groups them by task
    12    group.
    13  */
    14  
    15  @classic
    16  export default class RecommendationSummarySerializer extends ApplicationSerializer {
    17    normalizeArrayResponse(store, modelClass, payload) {
    18      const recommendationSerializer = store.serializerFor('recommendation');
    19      const RecommendationModel = store.modelFor('recommendation');
    20  
    21      const slugToSummaryObject = {};
    22      const allRecommendations = [];
    23  
    24      payload.forEach((recommendationHash) => {
    25        const slug = `${JSON.stringify([
    26          recommendationHash.JobID,
    27          recommendationHash.Namespace,
    28        ])}/${recommendationHash.Group}`;
    29  
    30        if (!slugToSummaryObject[slug]) {
    31          slugToSummaryObject[slug] = {
    32            attributes: {
    33              jobId: recommendationHash.JobID,
    34              jobNamespace: recommendationHash.Namespace,
    35              taskGroupName: recommendationHash.Group,
    36            },
    37            recommendations: [],
    38          };
    39        }
    40  
    41        slugToSummaryObject[slug].recommendations.push(recommendationHash);
    42        allRecommendations.push(recommendationHash);
    43      });
    44  
    45      return {
    46        data: Object.values(slugToSummaryObject).map((summaryObject) => {
    47          const latest = Math.max(
    48            ...summaryObject.recommendations.mapBy('SubmitTime')
    49          );
    50  
    51          return {
    52            type: 'recommendation-summary',
    53            id: summaryObject.recommendations.mapBy('ID').sort().join('-'),
    54            attributes: {
    55              ...summaryObject.attributes,
    56              submitTime: new Date(Math.floor(latest / 1000000)),
    57            },
    58            relationships: {
    59              job: {
    60                data: {
    61                  type: 'job',
    62                  id: JSON.stringify([
    63                    summaryObject.attributes.jobId,
    64                    summaryObject.attributes.jobNamespace,
    65                  ]),
    66                },
    67              },
    68              recommendations: {
    69                data: summaryObject.recommendations.map((r) => {
    70                  return {
    71                    type: 'recommendation',
    72                    id: r.ID,
    73                  };
    74                }),
    75              },
    76            },
    77          };
    78        }),
    79        included: allRecommendations.map(
    80          (recommendationHash) =>
    81            recommendationSerializer.normalize(
    82              RecommendationModel,
    83              recommendationHash
    84            ).data
    85        ),
    86      };
    87    }
    88  
    89    normalizeUpdateRecordResponse(store, primaryModelClass, payload, id) {
    90      return {
    91        data: {
    92          id,
    93          attributes: {
    94            isProcessed: true,
    95          },
    96        },
    97      };
    98    }
    99  }