Skip to content

Commit

Permalink
#89: Added job reports
Browse files Browse the repository at this point in the history
  • Loading branch information
darnjo committed Nov 19, 2024
1 parent ddc77dd commit f3a5632
Showing 1 changed file with 65 additions and 10 deletions.
75 changes: 65 additions & 10 deletions lib/certification/data-dictionary/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,8 @@ const {
DATA_DICTIONARY_VERSIONS,
CERTIFICATION_FILES,
getErrorHandler,
getLoggers
getLoggers,
getFileSafeIso8601Timestamp
} = require('../../../common');

const { REPLICATION_STRATEGIES } = require('../../replication/utils');
Expand All @@ -40,6 +41,11 @@ const RESULTS_PATH_NAME = 'results',

const COMMANDER_PATH_UNDEFINED_MESSAGE = 'WEB_API_COMMANDER_PATH not found in .env file! See ./sample.env for examples.';

const JOB_STATUSES = Object.freeze({
PASSED: 'passed',
FAILED: 'failed'
});

const DEFAULT_LIMIT = 100000,
DEFAULT_YEARS_BACK = 3;

Expand Down Expand Up @@ -213,6 +219,15 @@ const runDDTests = async ({
} = {}) => {
const handleError = getErrorHandler(fromCli);
const { LOG_INFO, LOG_ERROR } = getLoggers(fromCli);
const endorsementName = ENDORSEMENTS.DATA_DICTIONARY;

const jobResults = {
description: 'RESO Certification Job Report',
endorsementName,
version,
startTime: new Date().toISOString(),
outcomes: []
};

const JOB_START_TIME = Object.freeze(new Date());

Expand All @@ -222,8 +237,6 @@ const runDDTests = async ({
if (!pathToConfigFile) handleError('Missing pathToConfigFile!');
if (!WEB_API_COMMANDER_PATH) handleError(COMMANDER_PATH_UNDEFINED_MESSAGE);

const endorsementName = ENDORSEMENTS.DATA_DICTIONARY;

if (!isValidEndorsement(endorsementName)) {
handleError(`Endorsement name is not valid: '${endorsementName}'`);
}
Expand All @@ -245,17 +258,24 @@ const runDDTests = async ({
if (!(providerUoi && providerUoi?.length)) handleError(`Error: 'providerUoi' is missing from config '${pathToConfigFile}'`);
if (!(configs && configs?.length)) handleError(`Error: 'configs' array is missing or empty in config '${pathToConfigFile}'`);

let configIndex = 0;
jobResults.providerUoi = providerUoi;

let configIndex = 0,
jobStatus,
jobErrorMessage,
recipientPath;

for await (const config of configs) {
let recipientPath;
const START_TIME = Object.freeze(new Date());
const { providerUsi, recipientUoi } = config;

try {
cleanUpCertificationFiles();

if (!(providerUsi && providerUsi?.length)) throw new Error(`'providerUsi' is missing at index ${configIndex} in config '${pathToConfigFile}'`);
if (!(recipientUoi && recipientUoi?.length)) throw new Error(`'recipientUoi' is missing at index ${configIndex} in config '${pathToConfigFile}'`);
if (!(providerUsi && providerUsi?.length))
throw new Error(`'providerUsi' is missing at index ${configIndex} in config '${pathToConfigFile}'`);
if (!(recipientUoi && recipientUoi?.length))
throw new Error(`'recipientUoi' is missing at index ${configIndex} in config '${pathToConfigFile}'`);

recipientPath = buildRecipientEndorsementPath({
resultsPath: RESULTS_PATH_NAME,
Expand Down Expand Up @@ -307,7 +327,12 @@ const runDDTests = async ({
limit
});
}

jobStatus = JOB_STATUSES.PASSED;
} catch (err) {
jobStatus = JOB_STATUSES.FAILED;
const message = `Data Dictionary testing errors${recipientUoi ? ` for recipientUoi: ${config?.recipientUoi}` : ''}...`;

const commanderLogPath = join(getWebApiCommanderPath(), COMMANDER_LOG_FILE_NAME);
if (fs.existsSync(commanderLogPath)) {
const { size } = fs.statSync(commanderLogPath);
Expand All @@ -317,19 +342,49 @@ const runDDTests = async ({
}
}

const message = `Data Dictionary testing errors${recipientUoi ? ` for recipientUoi: ${config?.recipientUoi}` : ''}...`;
LOG_ERROR(`\n${message}\n${err}`);
jobErrorMessage = `\n${message}\n${err}`;
LOG_ERROR(jobErrorMessage);
} finally {
const outcome = {
status: jobStatus,
startTime: START_TIME.toISOString(),
endTime: new Date().toISOString(),
recipientPath,
configIndex,
providerUsi,
recipientUoi
};

if (jobStatus === 'failed' && jobErrorMessage && jobErrorMessage?.length) {
outcome.errorMessage = jobErrorMessage;
}

jobResults.outcomes.push(outcome);

configIndex++;
jobErrorMessage = null;

if (fs.existsSync(recipientPath)) {
copyDataDictionaryTestResults(recipientPath);
}
}
}

const jobReportFilename = `reso-job-report.${getFileSafeIso8601Timestamp()}.json`;

LOG_INFO(chalk.bold(`\nTesting finished at ${new Date().toLocaleString()}`));
LOG_INFO(`Runtime: ${humanizeDuration(new Date() - JOB_START_TIME)}\n\n`);
LOG_INFO(chalk.yellowBright(`Runtime: ${humanizeDuration(new Date() - JOB_START_TIME)}`));

try {
jobResults.endTime = new Date().toISOString();
const jobReportPath = resolve(join(RESULTS_PATH_NAME, jobReportFilename));
fs.writeFileSync(jobReportPath, JSON.stringify(jobResults));
LOG_INFO(chalk.bold(`Job report: ${jobReportPath}`));
} catch (err) {
LOG_ERROR(`Could not create jobs results file: '${jobReportFilename}'!\n${err}\n`);
}

LOG_INFO('\n\n');
};

/**
Expand Down

0 comments on commit f3a5632

Please sign in to comment.