-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
93 lines (71 loc) · 2.14 KB
/
index.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
const fetch = require("node-fetch");
const fs = require("fs").promises;
const results = [];
const fhirBaseUrl = "http://local.psbrandt.io:8080/hapi-fhir-jpaserver/fhir";
const cqlServiceURl = "http://local.psbrandt.io:3333/cql/evaluate";
const phenotypePath = "./heart-failure.phenotype.cql";
let phenotype;
const buildPayload = patientId => {
return {
code: phenotype,
terminologyServiceUri: fhirBaseUrl,
dataServiceUri: fhirBaseUrl,
patientId
};
};
const findNext = links => {
const next = links.find(link => link.relation === "next");
return !next ? null : next.url;
};
const processBundle = async bundle => {
return new Promise(async (resolve, reject) => {
if (!bundle.entry) resolve(null);
for (let i = 0; i < bundle.entry.length; i++) {
const entry = bundle.entry[i];
const patientId = entry.resource.id;
const body = buildPayload(patientId);
console.log(`Posting evaluation request for Patient ${patientId}`);
await fetch(cqlServiceURl, {
method: "post",
body: JSON.stringify(body),
headers: { "Content-Type": "application/json" }
})
.then(res => {
console.log(
`Evaluation for Patient ${patientId} completed with response code ${res.status}`
);
return res.json();
})
.then(result => {
results.push({ patientId, result });
})
.catch(e => {
reject(e);
});
}
resolve(findNext(bundle.link));
});
};
const saveResults = () => {
console.log("Saving results");
fs.writeFile("results.json", JSON.stringify(results)).then(err => {
if (err) throw err;
console.log("Successfully saved results");
});
};
const loadPhenotype = async () => {
phenotype = await fs.readFile(phenotypePath, "utf8").then(data => data);
};
const run = async () => {
await loadPhenotype();
let next =
"http://local.psbrandt.io:8080/hapi-fhir-jpaserver/fhir/Patient?_count=50";
do {
next = await fetch(next)
.then(res => res.json())
.then(processBundle)
.catch(e => saveResults());
} while (next != null);
saveResults();
};
run();