-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathinit.js
More file actions
170 lines (155 loc) · 5.13 KB
/
init.js
File metadata and controls
170 lines (155 loc) · 5.13 KB
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
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
// Configuration
const FHIR_SERVER = 'http://localhost:8080';
const USERNAME = 'root';
const PASSWORD = 'k5hlhmOYr4';
// Create Basic Auth header
const auth = Buffer.from(`${USERNAME}:${PASSWORD}`).toString('base64');
const headers = {
'Authorization': `Basic ${auth}`,
'Content-Type': 'application/json'
};
const NUMBER_OF_PATIENTS = 1000;
async function load(resourceType){
const startTime = Date.now();
const url = `https://storage.googleapis.com/aidbox-public/synthea/v2/${NUMBER_OF_PATIENTS}/fhir/${resourceType}.ndjson.gz`;
console.log(`Loading ${resourceType} from ${url}...`);
const response = await fetch(`${FHIR_SERVER}/fhir/$load`, {
method: 'POST',
headers: headers,
body: JSON.stringify({"source": url})
});
console.log(response.status);
const responseText = await response.text();
console.log(resourceType, responseText);
const endTime = Date.now();
console.log(`Time taken for ${resourceType}: ${(endTime - startTime) / 1000} seconds`);
}
async function sql(query){
console.log(`sql: ${query}`)
const response = await fetch(`${FHIR_SERVER}/$sql`, {
method: 'POST',
headers: headers,
body: JSON.stringify([query])
});
const responseText = await response.json();
console.log(response.status, responseText);
}
async function upsert(resource){
console.log(`create: ${resource.resourceType}`)
const response = await fetch(`${FHIR_SERVER}/fhir/${resource.resourceType}/${resource.id}`, {
method: 'PUT',
headers: headers,
body: JSON.stringify(resource)
});
const responseBody = await response.json();
console.log(response.status, responseBody.resourceType);
}
const startTime = Date.now();
await Promise.all([
load('Patient'),
load('Condition'),
load('Encounter'),
load('MedicationRequest')
]);
const endTime = Date.now();
console.log(`Time taken: ${(endTime - startTime) / 1000} seconds`);
await sql('select count(*) from Patient');
await sql('drop view if exists sof.patient cascade');
await upsert({
resourceType: 'ViewDefinition',
id: "patient",
name: "patient",
status: "active",
resource: "Patient",
select: [
{
column: [
{ name: "patient_id", path: "id" },
{ name: "gender", path: "gender" },
{ name: "dob", path: "birthDate" }
]
}
]
})
await sql(`
create or replace view sof.patient_plus AS
select *, DATE_PART('year', AGE(CURRENT_DATE, (dob)::date)) as age
from sof.patient
`)
await sql(`
select count(*) from sof.patient_plus
`)
await upsert({
resourceType: "ViewDefinition",
id: "condition",
status: "active",
resource: "Condition",
name: "condition",
select: [
{
column: [
{ name: "condition_id", path: "id" },
{ name: "onset", path: "onset.dateTime" },
{ name: "abatement", path: "abatement.dateTime" },
{ name: "status", path: "clinicalStatus.coding.code.first()" },
{ name: "code", path: "code.coding.where(system='http://snomed.info/sct').code.first()" },
{ name: "display", path: "code.text" },
{ name: "patient_id", path: "subject.id" },
{ name: "category", path: "category.first().coding.display.first()" }
]
}
]
})
await upsert({
resourceType: "ViewDefinition",
id: "encounter",
name: "encounter",
resource: "Encounter",
status: "active",
select: [{
column: [
{ name: "id", path: "id", type: "id" },
{ name: "patient_id", path: "subject.id", type: "id" },
{ name: "period_start", path: "period.start" },
{ name: "period_end", path: "period.end" },
{ name: "class", path: "class.code" },
{ name: "status", path: "status" },
{ name: "type_display", path: "type[0].coding.where(system='http://snomed.info/sct').display" },
{ name: "type_code", path: "type[0].coding.where(system='http://snomed.info/sct').code" }
]
}]
})
await upsert({
"resource": "MedicationRequest",
"id": "medreq",
"name": "medreq",
"status": "active",
"select": [
{
"column": [
{
"name": "id",
"path": "id",
"type": "id"
},
{
"name": "patient_id",
"path": "subject.id",
"type": "id"
},
{
"name": "rxnorm_display",
"path": "medication.ofType(CodeableConcept).coding.where(system='http://www.nlm.nih.gov/research/umls/rxnorm').display"
},
{
"name": "rxnorm_code",
"path": "medication.ofType(CodeableConcept).coding.where(system='http://www.nlm.nih.gov/research/umls/rxnorm').code"
},
{
"name": "rest",
"path": "$this"
}
]
}
]
})