public async prepareDataForRendering()

in lib/report/generateReport.ts [157:359]


  public async prepareDataForRendering() {
    try {
      this.markdown = await this.readMarkdown();
      const errorDefinitions = await loadErrorDefinitions();
      let errorsForRendering: LiveValidationIssueForRendering[];
      this.sortedValidationResults.forEach((element) => {
        const payloadFile = element.payloadFilePath?.substring(
          element.payloadFilePath.lastIndexOf("/") + 1
        );
        errorsForRendering = [];
        element.errors?.forEach((error) => {
          const errorDef = errorDefinitions.get(error.code);
          errorsForRendering.push({
            friendlyName: errorDef?.friendlyName ?? error.code,
            link: errorDef?.link,
            code: error.code,
            message: error.message,
            schemaPath: error.schemaPath,
            schemaPathWithPosition: this.overrideLinkInReport
              ? `${this.specLinkPrefix}/${element.specFilePath?.substring(
                  element.specFilePath?.indexOf("specification")
                )}#L${error.source.position.line}`
              : `${element.specFilePath}#L${error.source.position.line}`,
            pathsInPayload: error.pathsInPayload,
            jsonPathsInPayload: error.jsonPathsInPayload,
            severity: error.severity,
            source: error.source,
            params: error.params,
            payloadFilePath: this.overrideLinkInReport
              ? `${this.payloadLinkPrefix}/${payloadFile}`
              : element.payloadFilePath,
            payloadFilePathWithPosition: this.overrideLinkInReport
              ? `${this.payloadLinkPrefix}/${payloadFile}#L${element.payloadFilePathPosition?.line}`
              : `${element.payloadFilePath}#L${element.payloadFilePathPosition?.line}`,
            payloadFileLinkLabel: payloadFile,
          });
        });
        this.validationResultsForRendering.push({
          payloadFilePath: this.overrideLinkInReport
            ? `${this.payloadLinkPrefix}/${payloadFile}`
            : element.payloadFilePath,
          payloadFileLinkLabel: payloadFile,
          payloadFilePathWithPosition: this.overrideLinkInReport
            ? `${this.payloadLinkPrefix}/${payloadFile}#L${element.payloadFilePathPosition?.line}`
            : `${element.payloadFilePath}#L${element.payloadFilePathPosition?.line}`,
          errors: element.errors,
          specFilePath: this.overrideLinkInReport
            ? `${this.specLinkPrefix}/${element.specFilePath?.substring(
                element.specFilePath?.indexOf("specification")
              )}`
            : element.specFilePath,
          errorsForRendering: errorsForRendering,
          errorCodeLen: errorsForRendering.length,
          operationInfo: element.operationInfo,
          runtimeExceptions: element.runtimeExceptions,
        });
      });

      const generalErrorsInnerOrigin = this.validationResultsForRendering.filter((x) => {
        return x.errors && x.errors.length > 0;
      });

      this.coverageResults.forEach((element) => {
        const specLink = this.overrideLinkInReport
          ? `${this.specLinkPrefix}/${element.spec?.substring(
              element.spec?.indexOf("specification")
            )}`
          : `${element.spec}`;

        let errorOperationIds = generalErrorsInnerOrigin.map(
          (item) => item.operationInfo?.operationId
        );
        let passOperations: ValidationPassOperationsFormatInner[] = element.coveredOperationsList
          .filter((item) => errorOperationIds.indexOf(item.operationId) === -1)
          .map((item) => {
            return {
              key: item.operationId.split("_")[0],
              operationId: item.operationId,
            };
          });

        const passOperationsInnerList: ValidationPassOperationsFormatInner[][] = Object.values(
          passOperations.reduce(
            (res: { [key: string]: ValidationPassOperationsFormatInner[] }, item) => {
              /* eslint-disable no-unused-expressions */
              res[item.key] ? res[item.key].push(item) : (res[item.key] = [item]);
              /* eslint-enable no-unused-expressions */
              return res;
            },
            {}
          )
        );

        const passOperationsListFormat: ValidationPassOperationsFormat[] = [];
        passOperationsInnerList.forEach((element) => {
          passOperationsListFormat.push({
            operationIdList: element,
          });
        });

        /**
         * Sort untested operationId by bubble sort
         * Controlling the results of localeCompare can set the sorting method
         * X.localeCompare(Y) > 0 descending sort
         * X.localeCompare(Y) < 0 ascending sort
         */
        for (let i = 0; i < passOperationsListFormat.length - 1; i++) {
          for (let j = 0; j < passOperationsListFormat.length - 1 - i; j++) {
            if (
              passOperationsListFormat[j].operationIdList[0].key.localeCompare(
                passOperationsListFormat[j + 1].operationIdList[0].key
              ) > 0
            ) {
              var temp = passOperationsListFormat[j];
              passOperationsListFormat[j] = passOperationsListFormat[j + 1];
              passOperationsListFormat[j + 1] = temp;
            }
          }
        }

        this.coverageResultsForRendering.push({
          spec: specLink,
          specLinkLabel: element.spec?.substring(element.spec?.lastIndexOf("/") + 1),
          apiVersion: element.apiVersion,
          coveredOperations: element.coveredOperations,
          coveredOperationsList: element.coveredOperationsList,
          validationPassOperations: element.coveredOperations - element.validationFailOperations,
          validationPassOperationList: passOperationsListFormat,
          validationFailOperations: element.validationFailOperations,
          unCoveredOperations: element.unCoveredOperations,
          unCoveredOperationsList: element.unCoveredOperationsList,
          unCoveredOperationsListGen: element.unCoveredOperationsListGen,
          totalOperations: element.totalOperations,
          coverageRate: element.coverageRate,
          generalErrorsInnerList: [],
        });
      });

      this.resultsForRendering = this.coverageResultsForRendering.map((item) => {
        const data = this.validationResultsForRendering.find(
          (i) =>
            i.specFilePath &&
            item.spec.split(path.win32.sep).join(path.posix.sep).includes(i.specFilePath)
        );
        return {
          ...item,
          ...data,
        } as any;
      });

      const generalErrorsInnerFormat: TrafficValidationIssueForRendering[][] = Object.values(
        generalErrorsInnerOrigin.reduce(
          (res: { [key: string]: TrafficValidationIssueForRendering[] }, item) => {
            /* eslint-disable no-unused-expressions */
            res[item!.operationInfo!.operationId + item!.specFilePath]
              ? res[item!.operationInfo!.operationId + item!.specFilePath].push(item)
              : (res[item!.operationInfo!.operationId + item!.specFilePath] = [item]);
            /* eslint-enable no-unused-expressions */
            return res;
          },
          {}
        )
      );
      const generalErrorsInnerList: TrafficValidationIssueForRenderingInner[] = [];
      generalErrorsInnerFormat.forEach((element) => {
        let errorCodeLen: number = 0;
        element.forEach((item) => {
          errorCodeLen = errorCodeLen + item.errorCodeLen;
        });
        let errorsForRendering: LiveValidationIssueForRendering[] = [];
        element.forEach((item) => {
          errorsForRendering = errorsForRendering.concat(item.errorsForRendering!);
        });
        generalErrorsInnerList.push({
          generalErrorsInner: element,
          errorCodeLen: errorCodeLen,
          errorsForRendering: errorsForRendering,
          operationInfo: element[0]!.operationInfo!,
          specFilePath: this.overrideLinkInReport
            ? `${this.specLinkPrefix}/${element[0].specFilePath?.substring(
                element[0].specFilePath?.indexOf("specification")
              )}`
            : element[0].specFilePath,
          specFilePathWithPosition: this.overrideLinkInReport
            ? `${this.specLinkPrefix}/${element[0].specFilePath?.substring(
                element[0].specFilePath?.indexOf("specification")
              )}#L${element[0]!.operationInfo!.position!.line}`
            : `${element[0].specFilePath}#L${element[0]!.operationInfo!.position!.line}`,
        });
      });

      for (const [index, e] of this.resultsForRendering.entries()) {
        e.index = index;
        for (const i of generalErrorsInnerList) {
          if (e.specFilePath === i.specFilePath && i) {
            e.generalErrorsInnerList.push(i);
          }
        }
      }
    } catch (e) {
      console.error(`Failed in prepareDataForRendering with err:${e?.stack};message:${e?.message}`);
    }
  }