Skip to content

Commit d65f69a

Browse files
authored
Merge pull request #172 from mcode/data-directory-round-2
Improving dataDirectory approach
2 parents 469efde + 57aaed8 commit d65f69a

File tree

2 files changed

+26
-10
lines changed

2 files changed

+26
-10
lines changed

src/application/app.js

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,8 @@ async function mcodeApp(Client, fromDate, toDate, config, pathToRunLogs, debug,
3030
await mcodeClient.init();
3131

3232
// Parse CSV for list of patient mrns
33-
const patientIds = parsePatientIds(config.patientIdCsvPath);
33+
const dataDirectory = config.commonExtractorArgs && config.commonExtractorArgs.dataDirectory;
34+
const patientIds = parsePatientIds(config.patientIdCsvPath, dataDirectory);
3435

3536
// Get RunInstanceLogger for recording new runs and inferring dates from previous runs
3637
const runLogger = allEntries ? null : new RunInstanceLogger(pathToRunLogs);

src/helpers/appUtils.js

Lines changed: 24 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,25 +1,40 @@
11
const fs = require('fs');
22
const path = require('path');
33
const { csvParse } = require('./csvParsingUtils');
4+
const logger = require('./logger');
5+
6+
/**
7+
* Loads the patientIdCSV data from disk, with some helpful hints logged in case of failure
8+
*
9+
* @returns file corresponding to the patient data
10+
*/
11+
function getPatientIdCSVData(patientIdCsvPath, dataDirectory) {
12+
try {
13+
const patientIdsCsvPath = path.resolve(patientIdCsvPath);
14+
return fs.readFileSync(patientIdsCsvPath, 'utf8');
15+
} catch (e) {
16+
if (dataDirectory) {
17+
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.`);
18+
}
19+
throw e;
20+
}
21+
}
422

523
/**
624
* Parses a provided CSV with MRN column into string array of IDs
725
*
8-
* @param {string} pathToCSV filePath to the CSV content to be parsed to get IDs
26+
* @param {string} patientIdCsvPath filePath to the CSV content to be parsed to get IDs
27+
* @param {string} dataDirectory optional argument for if a dataDirectory was specified by the config
928
* @returns array of parsed IDs from the CSV
1029
*/
11-
function parsePatientIds(pathToCSV) {
12-
// Parse CSV for list of patient IDs
13-
const patientIdsCsvPath = path.resolve(pathToCSV);
14-
const patientIds = csvParse(fs.readFileSync(patientIdsCsvPath, 'utf8')).map((row) => {
30+
function parsePatientIds(patientIdCsvPath, dataDirectory) {
31+
const csvData = getPatientIdCSVData(patientIdCsvPath, dataDirectory);
32+
return csvParse(csvData).map((row) => {
1533
if (!row.mrn) {
16-
throw new Error(`${pathToCSV} has no "mrn" column`);
34+
throw new Error(`${patientIdCsvPath} has no "mrn" column`);
1735
}
18-
1936
return row.mrn;
2037
});
21-
22-
return patientIds;
2338
}
2439

2540
module.exports = {

0 commit comments

Comments
 (0)