github.com/anth0d/nomad@v0.0.0-20221214183521-ae3a0a2cad06/ui/app/serializers/recommendation-summary.js (about)

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