From eb78671652a39c460379c9f4f95fe0dad8a7db28 Mon Sep 17 00:00:00 2001 From: Mahalingam Date: Wed, 11 Jul 2018 10:52:25 -0400 Subject: [PATCH 1/3] Add caching flag to allow disabling caching --- lib/CodeService.js | 4 ++-- lib/download-vsac.js | 37 ++++++++++++++++++++----------------- 2 files changed, 22 insertions(+), 19 deletions(-) diff --git a/lib/CodeService.js b/lib/CodeService.js index 8ddcea9..cfa7d55 100644 --- a/lib/CodeService.js +++ b/lib/CodeService.js @@ -68,7 +68,7 @@ class CodeService { * @returns {Promise.} A promise that returns nothing when * resolved and returns an error when rejected. */ - ensureValueSets(valueSetList = [], umlsUserName = env['UMLS_USER_NAME'], umlsPassword = env['UMLS_PASSWORD']) { + ensureValueSets(valueSetList = [], umlsUserName = env['UMLS_USER_NAME'], umlsPassword = env['UMLS_PASSWORD'], caching = true) { // First, filter out the value sets we already have const filteredVSList = valueSetList.filter(vs => { const result = this.findValueSet(vs.id, vs.version); @@ -80,7 +80,7 @@ class CodeService { } else if ( typeof umlsUserName === 'undefined' || umlsUserName == null ||typeof umlsPassword === 'undefined' || umlsPassword == null) { return Promise.reject('Failed to download value sets since UMLS_USER_NAME and/or UMLS_PASSWORD is not set.'); } else { - return downloadFromVSAC(umlsUserName, umlsPassword, filteredVSList, this.cache, this.valueSets); + return downloadFromVSAC(umlsUserName, umlsPassword, filteredVSList, this.cache, this.valueSets, caching); } } diff --git a/lib/download-vsac.js b/lib/download-vsac.js index d5b1cfe..6860a66 100644 --- a/lib/download-vsac.js +++ b/lib/download-vsac.js @@ -5,7 +5,7 @@ const mkdirp = require('mkdirp'); const parseVSACXML = require('./parse-vsac'); const debug = require('debug')('vsac'); // To turn on DEBUG: $ export DEBUG=vsac -function downloadFromVSAC(username, password, input, output, vsDB={}) { +function downloadFromVSAC(username, password, input, output, vsDB={}, caching=true) { var vsJSON = {}; if (typeof(input) === 'string') { path.resolve(input); @@ -21,7 +21,7 @@ function downloadFromVSAC(username, password, input, output, vsDB={}) { if (Object.keys(vsJSON).length > 0) { output = path.resolve(output); const oids = Object.keys(vsJSON).map(k => vsJSON[k]); - if (!fs.existsSync(output)){ + if (caching && !fs.existsSync(output)){ mkdirp.sync(output); } return getTicketGrantingTicket(username, password) @@ -29,7 +29,7 @@ function downloadFromVSAC(username, password, input, output, vsDB={}) { const promises = oids.map(oid => { // Catch errors and convert to resolutions returning an error. This ensures Promise.all waits for all promises. // See: http://stackoverflow.com/questions/31424561/wait-until-all-es6-promises-complete-even-rejected-promises - return downloadValueSet(ticketGrantingTicket, oid, output, vsDB) + return downloadValueSet(ticketGrantingTicket, oid, output, vsDB, caching) .catch((err) => { debug(`Error downloading valueset ${oid}`, err); return new Error(`Error downloading valueset: ${oid}`); @@ -41,7 +41,7 @@ function downloadFromVSAC(username, password, input, output, vsDB={}) { const errors = results.filter(r => r instanceof Error); if (results.length - errors.length > 0) { // There were results, so write the file first before resolving/rejecting - return writeFile(path.join(output, 'valueset-db.json'), JSON.stringify(vsDB, null, 2)) + return writeFile(path.join(output, 'valueset-db.json'), JSON.stringify(vsDB, null, 2), caching) .then( (result) => errors.length == 0 ? result : Promise.reject(errors), (err) => { errors.push(err); return Promise.reject(errors); } @@ -66,14 +66,14 @@ function getTicketGrantingTicket(username, password) { return rpn(options); } -function downloadValueSet(ticketGrantingTicket, oid, output, vsDB={}) { +function downloadValueSet(ticketGrantingTicket, oid, output, vsDB={}, caching=true) { return getServiceTicket(ticketGrantingTicket) .then((serviceTicket) => { return getValueSet(serviceTicket, oid); }) .then((data) => { parseVSACXML(data, vsDB); - return writeFile(path.join(output, `${oid}.xml`), data); + return writeFile(path.join(output, `${oid}.xml`), data, caching); }); } @@ -96,18 +96,21 @@ function getValueSet(serviceTicket, oid) { return rpn(options); } -function writeFile(file, data) { +function writeFile(file, data, caching=true) { return new Promise((resolve, reject) => { - debug('Writing:', file); - fs.writeFile(file, data, (err) => { - if (typeof err !== 'undefined' && err != null) { - debug('Error writing file', file); - reject(err); - } else { - debug('Wrote file', file); - resolve(file); - } - }); + if (caching) { + debug('Writing:', file); + fs.writeFile(file, data, (err) => { + if (typeof err !== 'undefined' && err != null) { + debug('Error writing file', file); + reject(err); + } else { + debug('Wrote file', file); + resolve(file); + } + }); + } + resolve(); }); } From 84a779f1e7669a95d3f3dbd65934870f378f269d Mon Sep 17 00:00:00 2001 From: Chris Moesel Date: Tue, 21 Apr 2020 11:54:44 -0400 Subject: [PATCH 2/3] Add caching flag to ensureValueSetsInLibrary --- lib/CodeService.js | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/lib/CodeService.js b/lib/CodeService.js index cfa7d55..b2b11b4 100644 --- a/lib/CodeService.js +++ b/lib/CodeService.js @@ -93,9 +93,9 @@ class CodeService { * @param {string} umlsPassword - the UMLS password to use when downloading value sets (defaults to env "UMLS_PASSWORD") * @returns {Promise.} A promise that returns nothing when resolved and returns an error when rejected. */ - ensureValueSetsInLibrary(library, checkIncluded = true, umlsUserName = env['UMLS_USER_NAME'], umlsPassword = env['UMLS_PASSWORD']) { + ensureValueSetsInLibrary(library, checkIncluded = true, umlsUserName = env['UMLS_USER_NAME'], umlsPassword = env['UMLS_PASSWORD'], caching = true) { const valueSets = extractSetOfValueSetsFromLibrary(library, checkIncluded); - return this.ensureValueSets(Array.from(valueSets), umlsUserName, umlsPassword); + return this.ensureValueSets(Array.from(valueSets), umlsUserName, umlsPassword, caching); } findValueSetsByOid(oid) { From 7ef8e6f80d0a228841576a380dc312c8ba5c94b6 Mon Sep 17 00:00:00 2001 From: Chris Moesel Date: Tue, 21 Apr 2020 13:46:19 -0400 Subject: [PATCH 3/3] Fix asynch bug when caching is enabled --- lib/download-vsac.js | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/lib/download-vsac.js b/lib/download-vsac.js index 6860a66..dd8aef2 100644 --- a/lib/download-vsac.js +++ b/lib/download-vsac.js @@ -109,8 +109,9 @@ function writeFile(file, data, caching=true) { resolve(file); } }); + } else { + resolve(); } - resolve(); }); }