Skip to content

Commit

Permalink
Performance enhancement
Browse files Browse the repository at this point in the history
  • Loading branch information
ashaban committed Mar 27, 2020
1 parent 7370e96 commit b61a90d
Show file tree
Hide file tree
Showing 2 changed files with 90 additions and 83 deletions.
148 changes: 71 additions & 77 deletions server/lib/app.js
Original file line number Diff line number Diff line change
Expand Up @@ -522,7 +522,7 @@ function appRoutes() {
matchingTool.performMatch({
sourceResource: patient,
ignoreList: [patient.id],
}, (err, matches, ESMatches) => {
}, (err, matches, ESMatches, matchedGoldenRecords) => {
if(err) {
operSummary.outcome = '8';
operSummary.outcomeDesc = 'An error occured while finding matches';
Expand Down Expand Up @@ -647,89 +647,83 @@ function appRoutes() {
logger.error('Cant build query from resource id');
return callback(true);
}
fhirWrapper.getResource({
resource: 'Patient',
query,
noCaching: true
}, (goldenRecords) => {
if(!goldenRecords || !goldenRecords.entry || goldenRecords.entry.length === 0) {
operSummary.outcome = '8';
operSummary.outcomeDesc = 'Querying for CRUID details from FHIR Server returned nothing';
return callback(true);
}
if(currentLinks.length > 0) {
/**
* The purpose for this piece of code is to remove this patient from existing golden links
* if the existing golden link has just one link which is this patient, then link this golden link to new golden link of a patient
* otherwise just remove the patient from this exisitng golden link
* It also remove the exisitng golden link from the patient
*/
for (const currentLink of currentLinks) {
const exist = currentLink.resource.link && currentLink.resource.link.find((link) => {
return link.other.reference === 'Patient/' + patient.id;
if(!matchedGoldenRecords || !matchedGoldenRecords.entry || matchedGoldenRecords.entry.length === 0) {
operSummary.outcome = '8';
operSummary.outcomeDesc = 'Querying for CRUID details from FHIR Server returned nothing';
return callback(true);
}
if(currentLinks.length > 0) {
/**
* The purpose for this piece of code is to remove this patient from existing golden links
* if the existing golden link has just one link which is this patient, then link this golden link to new golden link of a patient
* otherwise just remove the patient from this exisitng golden link
* It also remove the exisitng golden link from the patient
*/
for (const currentLink of currentLinks) {
const exist = currentLink.resource.link && currentLink.resource.link.find((link) => {
return link.other.reference === 'Patient/' + patient.id;
});
let replacedByNewGolden = false;
if (currentLink.resource.link && currentLink.resource.link.length === 1 && exist) {
const inNewMatches = matchedGoldenRecords.entry.find((entry) => {
return entry.resource.id === currentLink.resource.id;
});
let replacedByNewGolden = false;
if (currentLink.resource.link && currentLink.resource.link.length === 1 && exist) {
const inNewMatches = goldenRecords.entry.find((entry) => {
return entry.resource.id === currentLink.resource.id;
});
if(!inNewMatches) {
replacedByNewGolden = true;
}
if(!inNewMatches) {
replacedByNewGolden = true;
}
for(const index in currentLink.resource.link) {
if(currentLink.resource.link[index].other.reference === 'Patient/' + patient.id) {
// remove patient from golden link
if(replacedByNewGolden) {
currentLink.resource.link[index].other.reference = 'Patient/' + goldenRecords.entry[0].resource.id;
currentLink.resource.link[index].type = 'replaced-by';
} else {
currentLink.resource.link.splice(index,1);
}
// remove golden link from patient
for(const index in patient.link) {
if(patient.link[index].other.reference === 'Patient/' + currentLink.resource.id) {
patient.link.splice(index, 1);
}
}
for(const index in currentLink.resource.link) {
if(currentLink.resource.link[index].other.reference === 'Patient/' + patient.id) {
// remove patient from golden link
if(replacedByNewGolden) {
currentLink.resource.link[index].other.reference = 'Patient/' + matchedGoldenRecords.entry[0].resource.id;
currentLink.resource.link[index].type = 'replaced-by';
} else {
currentLink.resource.link.splice(index,1);
}
// remove golden link from patient
for(const index in patient.link) {
if(patient.link[index].other.reference === 'Patient/' + currentLink.resource.id) {
patient.link.splice(index, 1);
}
bundle.entry.push({
resource: currentLink.resource,
request: {
method: 'PUT',
url: `Patient/${currentLink.resource.id}`,
},
});
}
bundle.entry.push({
resource: currentLink.resource,
request: {
method: 'PUT',
url: `Patient/${currentLink.resource.id}`,
},
});
}
}
}
// adding new links now to the patient
for (const goldenRecord of goldenRecords.entry) {
operSummary.cruid.push(goldenRecord.resource.resourceType + '/' + goldenRecord.resource.id);
responseBundle.entry.push({
response: {
location: goldenRecord.resource.resourceType + '/' + goldenRecord.resource.id
}
});
addLinks(patient, goldenRecord.resource);
bundle.entry.push({
resource: patient,
request: {
method: 'PUT',
url: `Patient/${patient.id}`,
},
}, {
resource: goldenRecord.resource,
request: {
method: 'PUT',
url: `Patient/${goldenRecord.resource.id}`,
},
});
}
operSummary.FHIRMatches = matches.entry;
operSummary.ESMatches = ESMatches;
return callback();
});
}
// adding new links now to the patient
for (const goldenRecord of matchedGoldenRecords.entry) {
operSummary.cruid.push(goldenRecord.resource.resourceType + '/' + goldenRecord.resource.id);
responseBundle.entry.push({
response: {
location: goldenRecord.resource.resourceType + '/' + goldenRecord.resource.id
}
});
addLinks(patient, goldenRecord.resource);
bundle.entry.push({
resource: patient,
request: {
method: 'PUT',
url: `Patient/${patient.id}`,
},
}, {
resource: goldenRecord.resource,
request: {
method: 'PUT',
url: `Patient/${goldenRecord.resource.id}`,
},
});
}
operSummary.FHIRMatches = matches.entry;
operSummary.ESMatches = ESMatches;
return callback();
}
} else {
operSummary.outcome = '8';
Expand Down
25 changes: 19 additions & 6 deletions server/lib/esMatching.js
Original file line number Diff line number Diff line change
Expand Up @@ -347,7 +347,6 @@ const performMatch = ({
logger.error('Matching type is not specified under decision rule, should be either deterministic or probabilistic');
return callback(true);
}
logger.error(JSON.stringify(esquery,0,2));
const url = URI(config.get('elastic:server'))
.segment(config.get('elastic:index'))
.segment('_search')
Expand Down Expand Up @@ -408,10 +407,20 @@ const performMatch = ({
error = true;
return callback(error);
}
query += '&_include=Patient:link';
fhirWrapper.getResource({
resource: 'Patient',
query
}, (results) => {
query,
noCaching: true
}, (resourceData) => {
const goldenRecords = {entry: []};
goldenRecords.entry = resourceData.entry.filter((entry) => {
return entry.search.mode === 'include';
});
const results = {entry: []};
results.entry = resourceData.entry.filter((entry) => {
return entry.search.mode === 'match';
});
// get golden id of the resource that had higher score
let goldenID;
for(const entry of results.entry) {
Expand All @@ -421,13 +430,17 @@ const performMatch = ({
}
}
}
// remove any other macthed resources that has different golden id than the one with highest score
// remove any other matched resources that has different golden id than the one with highest score
FHIRMatches.entry = results.entry.filter((entry) => {
return entry.resource.link.find((link) => {
return link.other.reference === goldenID;
});
});

goldenRecords.entry = goldenRecords.entry.filter((entry) => {
return entry.resource.resourceType + '/' + entry.resource.id === goldenID;
});

for(const index in ESMatches) {
ESMatches[index].results = ESMatches[index].results.filter((results) => {
return FHIRMatches.entry.find((entry) => {
Expand All @@ -436,11 +449,11 @@ const performMatch = ({
});
}
logger.info('Done matching');
return callback(error, FHIRMatches, ESMatches);
return callback(error, FHIRMatches, ESMatches, goldenRecords);
});
} else {
logger.info('Done matching');
return callback(error, FHIRMatches, ESMatches);
return callback(error, FHIRMatches, ESMatches, []);
}
});
});
Expand Down

0 comments on commit b61a90d

Please sign in to comment.