Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion src/application/app.js
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,8 @@ async function mcodeApp(Client, fromDate, toDate, config, pathToRunLogs, debug,
await mcodeClient.init();

// Parse CSV for list of patient mrns
const patientIds = parsePatientIds(config.patientIdCsvPath);
const dataDirectory = config.commonExtractorArgs && config.commonExtractorArgs.dataDirectory;
const patientIds = parsePatientIds(config.patientIdCsvPath, dataDirectory);

// Get RunInstanceLogger for recording new runs and inferring dates from previous runs
const runLogger = allEntries ? null : new RunInstanceLogger(pathToRunLogs);
Expand Down
33 changes: 24 additions & 9 deletions src/helpers/appUtils.js
Original file line number Diff line number Diff line change
@@ -1,25 +1,40 @@
const fs = require('fs');
const path = require('path');
const { csvParse } = require('./csvParsingUtils');
const logger = require('./logger');

/**
* Loads the patientIdCSV data from disk, with some helpful hints logged in case of failure
*
* @returns file corresponding to the patient data
*/
function getPatientIdCSVData(patientIdCsvPath, dataDirectory) {
try {
const patientIdsCsvPath = path.resolve(patientIdCsvPath);
return fs.readFileSync(patientIdsCsvPath, 'utf8');
} catch (e) {
if (dataDirectory) {
logger.error(`Could not resolve ${patientIdCsvPath}; even with a dataDirectory, the config.patientIdCsvPath variable needs to be a resolvable path to the patientID file on disk.`);
}
throw e;
}
}

/**
* Parses a provided CSV with MRN column into string array of IDs
*
* @param {string} pathToCSV filePath to the CSV content to be parsed to get IDs
* @param {string} patientIdCsvPath filePath to the CSV content to be parsed to get IDs
* @param {string} dataDirectory optional argument for if a dataDirectory was specified by the config
* @returns array of parsed IDs from the CSV
*/
function parsePatientIds(pathToCSV) {
// Parse CSV for list of patient IDs
const patientIdsCsvPath = path.resolve(pathToCSV);
const patientIds = csvParse(fs.readFileSync(patientIdsCsvPath, 'utf8')).map((row) => {
function parsePatientIds(patientIdCsvPath, dataDirectory) {
const csvData = getPatientIdCSVData(patientIdCsvPath, dataDirectory);
return csvParse(csvData).map((row) => {
if (!row.mrn) {
throw new Error(`${pathToCSV} has no "mrn" column`);
throw new Error(`${patientIdCsvPath} has no "mrn" column`);
}

return row.mrn;
});

return patientIds;
}

module.exports = {
Expand Down