-
Notifications
You must be signed in to change notification settings - Fork 0
Collect data #67
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Collect data #67
Changes from all commits
Commits
Show all changes
9 commits
Select commit
Hold shift + click to select a range
04d4d69
Initial scaffolding
lmd59 2be15a2
Basic working
lmd59 9aee9e7
remove repeat patient, add evaluatedResource arr to MeasureReport
lmd59 16b4fb8
Multi-measure support
lmd59 79267a4
Cleanup and unsupported params error
lmd59 61eed2a
Various cleanup and add testing
lmd59 52175cc
Various comment cleanup
lmd59 0e6eae6
Non-side-effect map
lmd59 e0adbca
ignore 2025 folder
lmd59 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -5,4 +5,5 @@ coverage | |
| docker_ssl_setup.sh | ||
| tmp/123456/Account.ndjson | ||
| ecqm-content-r4-2021 | ||
| ecqm-content-qicore-2025 | ||
| *.ndjson | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,2 +1,3 @@ | ||
| coverage | ||
| ecqm-content-r4-2021 | ||
| ecqm-content-r4-2021 | ||
| ecqm-content-qicore-2025 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,175 @@ | ||
| const { v4: uuidv4 } = require('uuid'); | ||
| const { patientAttributePaths } = require('fhir-spec-tools/build/data/patient-attribute-paths'); | ||
| const patientResourceTypes = Object.keys(patientAttributePaths); | ||
| const { addTypeFilter, getDocuments } = require('./exportToNDJson'); | ||
| const _ = require('lodash'); | ||
|
|
||
| /** | ||
| * Creates a patient bundle resource. Creates using a patient resource and | ||
| * an array of the patient's associated resources | ||
| * @param {Object} patient FHIR Patient object | ||
| * @param {Array} resources array of resources associated with the patient | ||
| * @param {Array} measureReports array of MeasureReport resources specifying measure information for data exchange | ||
| * @returns {Object} a FHIR patient bundle resource | ||
| */ | ||
| function createPatientBundle(patient, resources, measureReports) { | ||
| const bundle = { | ||
| type: 'transaction', | ||
| resourceType: 'Bundle', | ||
| id: uuidv4(), | ||
| entry: [] | ||
| }; | ||
| resources.forEach(r => { | ||
| bundle.entry?.push({ | ||
| resource: r, | ||
| request: { | ||
| method: 'PUT', | ||
| url: `${r.resourceType}/${r.id}` | ||
| }, | ||
| fullUrl: r.fullUrl ?? `urn:uuid:${r.id}` | ||
| }); | ||
| }); | ||
|
|
||
| measureReports.forEach(measureReport => { | ||
| bundle.entry?.push({ | ||
| resource: measureReport, | ||
| request: { | ||
| method: 'PUT', | ||
| url: `MeasureReport/${measureReport.id}` | ||
| }, | ||
| fullUrl: measureReport.fullUrl ?? `urn:uuid:${measureReport.id}` | ||
| }); | ||
| }); | ||
|
|
||
| return bundle; | ||
| } | ||
|
|
||
| /** | ||
| * Creates a FHIR data exchange MeasureReport from measure and subject data | ||
| * https://hl7.org/fhir/us/davinci-deqm/STU5/StructureDefinition-datax-measurereport-deqm.html | ||
| * @param measure FHIR Measure | ||
| * @param measurementPeriod FHIR Period representing the measurement period | ||
| * @param subjectId the patient id the MeasureReport is associated with | ||
| * @param patientResources the patient resources of relevance to the passed measure | ||
| * @returns { fhir4.MeasureReport } a data exchange measure report used to send Measure-relevant data to a server | ||
| */ | ||
| function createDataExchangeMeasureReport(measure, measurementPeriod, subjectId, patientResources) { | ||
| return { | ||
| resourceType: 'MeasureReport', | ||
| id: uuidv4(), | ||
| measure: measure.url?.includes('|') ? measure.url : `${measure.url}|${measure.version}`, //canonical measure/version | ||
| period: measurementPeriod, | ||
| status: 'complete', | ||
| type: 'data-collection', | ||
| subject: { reference: `Patient/${subjectId}` }, | ||
| date: new Date().toISOString(), | ||
| reporter: { reference: 'Organization/bulk-export-server' }, //TODO: do we need to send an organization resource? | ||
| meta: { | ||
| profile: ['http://hl7.org/fhir/us/davinci-deqm/StructureDefinition/datax-measurereport-deqm'] | ||
| }, | ||
| extension: [ | ||
| { | ||
| url: 'http://hl7.org/fhir/us/davinci-deqm/StructureDefinition/extension-submitDataUpdateType', | ||
| valueCode: 'snapshot' | ||
| } | ||
| ], | ||
| evaluatedResource: patientResources?.map(r => { | ||
| return { reference: `${r.resourceType}/${r.id}` }; | ||
| }), | ||
| contained: [{ resourceType: 'Organization', id: 'bulk-export-server' }] | ||
|
elsaperelli marked this conversation as resolved.
|
||
| }; | ||
| } | ||
|
|
||
| /** | ||
| * Finds all resources related to the passed patient resource but limited by the data requirements on the passed measure | ||
| * @param {FHIR.Patient} patient fhir patient resource | ||
| * @param {FHIR.Measure} measure fhir measure resource | ||
| * @returns {Array} an array of filtered fhir resources related to the passed patient | ||
| */ | ||
| async function findPatientResources(patient, measure) { | ||
| const dataRequirements = measure.contained?.find(c => c.id === 'effective-data-requirements').dataRequirement; | ||
| const types = _.uniq(dataRequirements.map(dr => dr.type)); | ||
| const [patientTypes, nonPatientTypes] = types.reduce( | ||
| ([patient, nonPatient], type) => { | ||
| (patientResourceTypes.includes(type) ? patient : nonPatient).push(type); | ||
| return [patient, nonPatient]; | ||
| }, | ||
| [[], []] | ||
| ); | ||
| // nonPatientTypes can be in data requirements when they may be referenced from other resources that reference patients | ||
| // i.e. Patient references MedicationRequest references Medication could end up with Medication as non-patient type | ||
| // ignored for now, but should either get all (non-patient query) or be able to do more complex query creation | ||
| if (nonPatientTypes.length > 0) { | ||
| console.warn('Ignoring non-patient types found in data requirements:', nonPatientTypes); | ||
|
elsaperelli marked this conversation as resolved.
elsaperelli marked this conversation as resolved.
|
||
| } | ||
| const typeFilters = typeFiltersForMeasure(dataRequirements); | ||
| // create lookup objects for (1) _typeFilter queries that contain search parameters, and (2) _typeFilter | ||
| // queries that contain type:in/code:in/etc. queries | ||
| const searchParameterQueries = {}; | ||
| const valueSetQueries = {}; | ||
| if (typeFilters) { | ||
| addTypeFilter(typeFilters, searchParameterQueries, valueSetQueries); | ||
| } | ||
|
|
||
| // for each patient, collect resource documents from each export type collection | ||
| const typeDocs = patientTypes.map(async collectionName => { | ||
| return ( | ||
| await getDocuments(collectionName, searchParameterQueries[collectionName], valueSetQueries[collectionName], [ | ||
| patient.id | ||
| ]) | ||
| ).document; | ||
| }); | ||
|
|
||
| //flatten all type arrays into a single array | ||
| return (await Promise.all(typeDocs)).flat(); | ||
| } | ||
|
|
||
| function typeFiltersForMeasure(dataRequirements) { | ||
| // create record resourcetype => [] of valid typeFilter query strings that will be separated by "&" and logically handled as ORs | ||
| const typeFilters = {}; | ||
| dataRequirements.forEach(dr => { | ||
| //empty array is general _type query that overrides a more specific _typeFilter | ||
| if (typeFilters[dr.type]?.length === 0) return; // only handle data requirements with types for now | ||
|
|
||
| if ( | ||
| dr.codeFilter?.some(cf => { | ||
| const hasVS = cf.path && cf.valueSet; | ||
| const hasCode = cf.path && cf.code && cf.code.every(coding => !!coding.code); | ||
| const hasNeither = !hasVS && !hasCode; | ||
| return hasNeither; | ||
| }) | ||
| ) { | ||
| // if any codeFilter for a data requirement doesn't have sufficient information, default to getting all for that type (general _type query) | ||
| typeFilters[dr.type] = []; | ||
| return; | ||
| } | ||
| //all codefilters have a path and proper code and can be added to our typefilter array | ||
| const fhirQueries = dr.codeFilter?.map(cf => { | ||
| const hasVS = cf.path && cf.valueSet; | ||
| const hasCode = cf.path && cf.code && cf.code.every(coding => !!coding.code); | ||
| if (hasVS && hasCode) { | ||
| // default to valueset method for now, but this should probably be expanded to a full list of codes from the vs with additional | ||
| // codes appended (or separated into separate top-level queries where other included codeFilters are repeated | ||
| // Example: 'Procedure?code=1&category=3,4','Procedure?code=1&category:in=vs') | ||
| return `${cf.path}:in=${cf.valueSet}`; | ||
| } else if (hasVS) { | ||
| return `${cf.path}:in=${cf.valueSet}`; | ||
|
elsaperelli marked this conversation as resolved.
|
||
| } else { | ||
| // hasCode | ||
| // potential multiple codes are comma-separated to be OR'd for this path | ||
| return `${cf.path}=${cf.code?.map(coding => coding.code).join(',')}`; | ||
| } | ||
| }); | ||
| const tfStr = `${dr.type}?${fhirQueries?.join('&')}`; //Example value: 'Procedure?code=1,2&category=3,4' | ||
| if (typeFilters[dr.type]) { | ||
| typeFilters[dr.type].push(tfStr); | ||
| } else { | ||
| typeFilters[dr.type] = [tfStr]; | ||
| } | ||
| }); | ||
|
|
||
| // array of typeFilters that should be treated as OR'd (empty array will be ignored and flattened) | ||
|
elsaperelli marked this conversation as resolved.
|
||
| return Object.values(typeFilters).flat(); | ||
| } | ||
|
|
||
| module.exports = { createPatientBundle, createDataExchangeMeasureReport, findPatientResources }; | ||
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.