Skip to content

Commit

Permalink
brought back loadBundles script. added QICore-ModelInfo to dbsetup
Browse files Browse the repository at this point in the history
  • Loading branch information
hossenlopp authored and elsaperelli committed Oct 2, 2024
1 parent a1535a6 commit 7e1ee47
Show file tree
Hide file tree
Showing 5 changed files with 226 additions and 3 deletions.
2 changes: 1 addition & 1 deletion docker-build.sh
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
#!/bin/bash
npm#!/bin/bash

docker buildx build --platform linux/arm64,linux/amd64 -t tacoma/measure-repository-service:latest -f service.Dockerfile . --push
docker buildx build --platform linux/arm64,linux/amd64 -t tacoma/measure-repository-app:latest -f app.Dockerfile . --push
3 changes: 2 additions & 1 deletion service/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
"db:setup": "ts-node ./scripts/dbSetup.ts create",
"db:loadBundle": "ts-node ./scripts/dbSetup.ts loadBundle",
"db:postBundle": "ts-node ./scripts/dbSetup.ts postBundle",
"loadBundles": "ts-node ./scripts/loadBundles.ts",
"lint": "eslint \"./src/**/*.{js,ts}\"",
"lint:fix": "eslint \"./src/**/*.{js,ts}\" --fix",
"prettier": "prettier --check \"./src/**/*.{js,ts}\"",
Expand Down Expand Up @@ -57,4 +58,4 @@
"uuid": "^9.0.0",
"zod": "^3.20.2"
}
}
}
7 changes: 6 additions & 1 deletion service/scripts/dbSetup.ts
Original file line number Diff line number Diff line change
Expand Up @@ -183,7 +183,7 @@ async function uploadBundleResources(filePath: string) {
* Convenience modification of an array of entries to create isOwned relationships and coerce to status active.
* This lets us massage existing data that may not have the appropriate properties needed for a Publishable Measure Repository
*/
function modifyEntriesForUpload(entries: fhir4.BundleEntry<fhir4.FhirResource>[]) {
export function modifyEntriesForUpload(entries: fhir4.BundleEntry<fhir4.FhirResource>[]) {
// pre-process to find owned relationships
const ownedUrls: string[] = [];
const modifiedEntries = entries.map(ent => {
Expand Down Expand Up @@ -236,9 +236,14 @@ async function insertFHIRModelInfoLibrary() {
const fhirModelInfo = fs.readFileSync('scripts/fixtures/Library-FHIR-ModelInfo.json', 'utf8');
const fhirModelInfoLibrary: CRMIShareableLibrary = JSON.parse(fhirModelInfo);

const qicoreModelInfo = fs.readFileSync('scripts/fixtures/Library-QICore-ModelInfo.json', 'utf8');
const qicoreModelInfoLibrary: CRMIShareableLibrary = JSON.parse(qicoreModelInfo);

const collection = Connection.db.collection<FhirArtifact>('Library');
console.log(`Inserting Library/${fhirModelInfoLibrary.id} into database`);
await collection.insertOne(fhirModelInfoLibrary);
console.log(`Inserting Library/${qicoreModelInfoLibrary.id} into database`);
await collection.insertOne(qicoreModelInfoLibrary);
}

if (process.argv[2] === 'delete') {
Expand Down
63 changes: 63 additions & 0 deletions service/scripts/fixtures/Library-QICore-ModelInfo.json

Large diffs are not rendered by default.

154 changes: 154 additions & 0 deletions service/scripts/loadBundles.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,154 @@
import * as fs from 'fs';
import path from 'path';
import { modifyEntriesForUpload } from './dbSetup';

//const SERVER_URL = 'https://abacus-demo.c3ib.org/mrs/4_0_1';
const SERVER_URL = 'http://localhost:3000/4_0_1';
//const ECQM_CONTENT_PATH = '/Users/hossenlopp/Downloads/May 2024 Connectathon';
const ECQM_CONTENT_PATH = '../../ecqm-content-qicore-2024/bundles/measure';
const MEASURES_PATH = path.join(ECQM_CONTENT_PATH);

function getBundlePaths(): string[] {
const filePaths: string[] = [];

fs.readdirSync(MEASURES_PATH, { withFileTypes: true }).forEach(ent => {
// if this item is a directory, look for .json files under it
if (ent.isDirectory()) {
fs.readdirSync(path.join(MEASURES_PATH, ent.name), { withFileTypes: true }).forEach(subEnt => {
if (!subEnt.isDirectory() && subEnt.name.endsWith('.json')) {
filePaths.push(path.join(MEASURES_PATH, ent.name, subEnt.name));
} else if (subEnt.isDirectory()) {
// fs.readdirSync(path.join(MEASURES_PATH, ent.name, subEnt.name), { withFileTypes: true }).forEach(
// subSubEnt => {
// if (!subSubEnt.isDirectory() && subSubEnt.name.endsWith('.json')) {
// filePaths.push(path.join(MEASURES_PATH, ent.name, subEnt.name, subSubEnt.name));
// }
// }
// );
}
});
}
});
console.log(filePaths);
return filePaths;
}

function loadBundle(path: string): fhir4.Bundle | null {
const bundle = JSON.parse(fs.readFileSync(path, 'utf8'));
if (bundle.resourceType === 'Bundle') {
return bundle as fhir4.Bundle;
} else {
console.warn(`${path} is not a Bundle`);
return null;
}
}

async function putLibraries(bundle: fhir4.Bundle) {
const libraries: fhir4.Library[] = bundle.entry
?.filter(entry => entry.resource?.resourceType === 'Library')
.map(entry => entry.resource as fhir4.Library) as fhir4.Library[];

for (const library of libraries) {
console.log(` Library ${library.id}`);
if (library.relatedArtifact) {
for (let index = 0; index < library.relatedArtifact.length; index++) {
const ra = library.relatedArtifact[index];
if (
(ra.type === 'depends-on' || ra.type === 'composed-of') &&
ra.resource?.startsWith('http://ecqi.healthit.gov/ecqms/Library/')
) {
const newRef = ra.resource.replace(
'http://ecqi.healthit.gov/ecqms/Library/',
'https://madie.cms.gov/Library/'
);
console.log(` replacing ra ${ra.resource} with ${newRef}`);
ra.resource = newRef;
}
}
}

try {
console.log(` PUT ${SERVER_URL}/Library/${library.id}`);

const resp = await fetch(`${SERVER_URL}/Library/${library.id}`, {
method: 'PUT',
body: JSON.stringify(library),
headers: {
'Content-Type': 'application/json+fhir'
}
});
console.log(` ${resp.status}`);
} catch (e) {
console.error(e);
}
}
}

async function fixAndPUTMeasure(bundle: fhir4.Bundle) {
const measure: fhir4.Measure = bundle.entry?.find(entry => entry.resource?.resourceType === 'Measure')
?.resource as fhir4.Measure;

console.log(` Measure ${measure.id}`);
try {
console.log(` PUT ${SERVER_URL}/Measure/${measure.id}`);
measure.status = 'active';
const resp = await fetch(`${SERVER_URL}/Measure/${measure.id}`, {
method: 'PUT',
body: JSON.stringify(measure),
headers: {
'Content-Type': 'application/json+fhir'
}
});
console.log(` ${resp.status}`);
if (resp.status >= 400) {
const responseBody = await resp.json();
if (responseBody.resourceType === 'OperationOutcome') {
console.log(JSON.stringify(responseBody, null, 2));
}
}
} catch (e) {
console.error(e);
}
}

async function transactBundle(bundle: fhir4.Bundle) {
if (bundle.entry) {
for (const entry of bundle.entry) {
if (entry.request?.method === 'POST') {
entry.request.method = 'PUT';
}
}
}

try {
console.log(` POST ${SERVER_URL}`);

const resp = await fetch(`${SERVER_URL}`, {
method: 'POST',
body: JSON.stringify(bundle),
headers: {
'Content-Type': 'application/json+fhir'
}
});
console.log(` ${resp.status}`);
} catch (e) {
console.error(e);
}
}

async function loadBundles(bundlePaths: string[]) {
for (const path of bundlePaths) {
const bundle = loadBundle(path);
if (bundle?.entry) {
console.log('FILE ' + path);
bundle.entry = modifyEntriesForUpload(bundle.entry);
//await transactBundle(bundle);
await putLibraries(bundle);
await fixAndPUTMeasure(bundle);
}
}
}

const bundlePaths = getBundlePaths();

loadBundles(bundlePaths);

0 comments on commit 7e1ee47

Please sign in to comment.