Skip to content

Commit 7b894b0

Browse files
committed
implemented CSVURLModule; updated CSVextractors to support new arg
1 parent 3b18e7c commit 7b894b0

22 files changed

+300
-191
lines changed

src/client/BaseClient.js

Lines changed: 20 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -25,32 +25,37 @@ class BaseClient {
2525
}
2626

2727
// Given an extractor configuration, initialize all the necessary extractors
28-
initializeExtractors(extractorConfig, commonExtractorArgs) {
29-
let allExtractorsValid = true;
30-
28+
async initializeExtractors(extractorConfig, commonExtractorArgs) {
29+
// Loop to initialize the extractors
3130
extractorConfig.forEach((curExtractorConfig) => {
3231
const { label, type, constructorArgs } = curExtractorConfig;
3332
logger.debug(`Initializing ${label} extractor with type ${type}`);
3433
const ExtractorClass = this.extractorClasses[type];
35-
3634
try {
3735
const newExtractor = new ExtractorClass({ ...commonExtractorArgs, ...constructorArgs });
38-
39-
if (newExtractor.validate) {
40-
const isExtractorValid = newExtractor.validate();
41-
allExtractorsValid = (allExtractorsValid && isExtractorValid);
42-
if (isExtractorValid) {
43-
logger.debug(`Extractor ${label} PASSED CSV validation`);
44-
} else {
45-
logger.debug(`Extractor ${label} FAILED CSV validation`);
46-
}
47-
}
48-
4936
this.extractors.push(newExtractor);
5037
} catch (e) {
5138
throw new Error(`Unable to initialize ${label} extractor with type ${type}: ${e.message}`);
5239
}
5340
});
41+
// For validation, we are looping over extractors and performing an async operation on each.
42+
// We need to loop without forEach (since forEach is sequential).
43+
// Using Reduce to compute the validity of all extractors
44+
const allExtractorsValid = await this.extractors.reduce(async (curExtractorsValid, curExtractor) => {
45+
const { name } = curExtractor.constructor;
46+
47+
if (curExtractor.validate) {
48+
logger.debug(`Validating ${name}`);
49+
const isExtractorValid = await curExtractor.validate();
50+
if (isExtractorValid) {
51+
logger.debug(`Extractor ${name} PASSED CSV validation`);
52+
} else {
53+
logger.warn(`Extractor ${name} FAILED CSV validation`);
54+
}
55+
return (curExtractorsValid && isExtractorValid);
56+
}
57+
return curExtractorsValid;
58+
}, true);
5459

5560
if (allExtractorsValid) {
5661
logger.info('Validation succeeded');

src/extractors/BaseCSVExtractor.js

Lines changed: 14 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,25 +1,25 @@
1-
const path = require('path');
2-
const { Extractor } = require('./Extractor');
3-
const { CSVModule } = require('../modules');
4-
const { validateCSV } = require('../helpers/csvValidator');
51
const logger = require('../helpers/logger');
2+
const { Extractor } = require('./Extractor');
3+
const { CSVFileModule, CSVURLModule } = require('../modules');
64

75
class BaseCSVExtractor extends Extractor {
8-
constructor({ filePath, csvSchema, unalterableColumns }) {
6+
constructor({ filePath, url, csvSchema, unalterableColumns }) {
97
super();
108
this.unalterableColumns = unalterableColumns || [];
119
this.csvSchema = csvSchema;
12-
this.filePath = path.resolve(filePath);
13-
this.csvModule = new CSVModule(this.filePath, this.unalterableColumns);
10+
if (filePath) {
11+
this.filePath = filePath;
12+
this.csvModule = new CSVFileModule(this.filePath, this.unalterableColumns);
13+
} else if (url) {
14+
this.url = url;
15+
this.csvModule = new CSVURLModule(this.url, this.unalterableColumns);
16+
} else {
17+
throw new Error('Trying to instantiate a CSVExtractor without a filePath or url');
18+
}
1419
}
1520

16-
validate() {
17-
if (this.csvSchema) {
18-
logger.info(`Validating CSV file for ${this.filePath}`);
19-
return validateCSV(this.filePath, this.csvSchema, this.csvModule.data);
20-
}
21-
logger.warn(`No CSV schema provided for ${this.filePath}`);
22-
return true;
21+
async validate() {
22+
return this.csvModule.validate(this.csvSchema);
2323
}
2424
}
2525

src/extractors/CSVAdverseEventExtractor.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -69,8 +69,8 @@ function formatData(adverseEventData, patientId) {
6969
}
7070

7171
class CSVAdverseEventExtractor extends BaseCSVExtractor {
72-
constructor({ filePath }) {
73-
super({ filePath });
72+
constructor({ filePath, url }) {
73+
super({ filePath, url });
7474
}
7575

7676
async getAdverseEventData(mrn) {

src/extractors/CSVCancerDiseaseStatusExtractor.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,8 +8,8 @@ const logger = require('../helpers/logger');
88
const { CSVCancerDiseaseStatusSchema } = require('../helpers/schemas/csv');
99

1010
class CSVCancerDiseaseStatusExtractor extends BaseCSVExtractor {
11-
constructor({ filePath, implementation }) {
12-
super({ filePath, csvSchema: CSVCancerDiseaseStatusSchema });
11+
constructor({ filePath, url, implementation }) {
12+
super({ filePath, url, csvSchema: CSVCancerDiseaseStatusSchema });
1313
this.implementation = implementation;
1414
}
1515

src/extractors/CSVCancerRelatedMedicationExtractor.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -46,8 +46,8 @@ function formatData(medicationData, patientId) {
4646
}
4747

4848
class CSVCancerRelatedMedicationExtractor extends BaseCSVExtractor {
49-
constructor({ filePath }) {
50-
super({ filePath });
49+
constructor({ filePath, url }) {
50+
super({ filePath, url });
5151
}
5252

5353
async getMedicationData(mrn) {

src/extractors/CSVClinicalTrialInformationExtractor.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,8 +8,8 @@ const { CSVClinicalTrialInformationSchema } = require('../helpers/schemas/csv');
88

99

1010
class CSVClinicalTrialInformationExtractor extends BaseCSVExtractor {
11-
constructor({ filePath, clinicalSiteID, clinicalSiteSystem }) {
12-
super({ filePath, csvSchema: CSVClinicalTrialInformationSchema });
11+
constructor({ filePath, url, clinicalSiteID, clinicalSiteSystem }) {
12+
super({ filePath, url, csvSchema: CSVClinicalTrialInformationSchema });
1313
if (!clinicalSiteID) logger.warn(`${this.constructor.name} expects a value for clinicalSiteID but got ${clinicalSiteID}`);
1414
this.clinicalSiteID = clinicalSiteID;
1515
this.clinicalSiteSystem = clinicalSiteSystem;

src/extractors/CSVConditionExtractor.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -49,8 +49,8 @@ function formatData(conditionData, patientId) {
4949
}
5050

5151
class CSVConditionExtractor extends BaseCSVExtractor {
52-
constructor({ filePath }) {
53-
super({ filePath, csvSchema: CSVConditionSchema });
52+
constructor({ filePath, url }) {
53+
super({ filePath, url, csvSchema: CSVConditionSchema });
5454
}
5555

5656
async getConditionData(mrn) {

src/extractors/CSVObservationExtractor.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -42,8 +42,8 @@ function formatData(observationData, patientId) {
4242
}
4343

4444
class CSVObservationExtractor extends BaseCSVExtractor {
45-
constructor({ filePath }) {
46-
super({ filePath });
45+
constructor({ filePath, url }) {
46+
super({ filePath, url });
4747
}
4848

4949
async getObservationData(mrn) {

src/extractors/CSVPatientExtractor.js

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -54,10 +54,15 @@ function joinAndReformatData(patientData) {
5454
}
5555

5656
class CSVPatientExtractor extends BaseCSVExtractor {
57-
constructor({ filePath, mask = [] }) {
57+
constructor({ filePath, url, mask = [] }) {
5858
// Define CSV Columns whose values should never be altered
5959
const unalterableColumns = ['familyName', 'givenName'];
60-
super({ filePath, csvSchema: CSVPatientSchema, unalterableColumns });
60+
super({
61+
filePath,
62+
url,
63+
// csvSchema: CSVPatientSchema,
64+
unalterableColumns,
65+
});
6166
this.mask = mask;
6267
}
6368

src/extractors/CSVProcedureExtractor.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -48,8 +48,8 @@ function formatData(procedureData, patientId) {
4848
}
4949

5050
class CSVProcedureExtractor extends BaseCSVExtractor {
51-
constructor({ filePath }) {
52-
super({ filePath });
51+
constructor({ filePath, url }) {
52+
super({ filePath, url });
5353
}
5454

5555
async getProcedureData(mrn) {

0 commit comments

Comments
 (0)