-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpopulate_db.js
445 lines (365 loc) · 16.8 KB
/
populate_db.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
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
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
const mongoose = require('mongoose');
mongoose.Promise = Promise;
let names = ["Dhen", "Alex", "Joe", "Bob", "Kiran", "Randi", "Jamey", "Antione", "Tanya", "Dorathy", "Nelia", "Maribel", "Lila", "Emory", "Marjorie", "Regenia", "Meghann", "Jannie", "Pearl", "Jinny", "Un", "Elna", "Iola", "Kamilah", "Joel", "Corine", "Nelson", "Theressa", "Estrella", "Kathryn", "Elden", "Garland", "Duane", "German", "Love", "Carmella", "Myrna", "Alex", "Darcy", "Stefani", "Onita", "Starr", "Giovanni", "Magda", "Beryl", "Terresa", "Rikki", "Helaine", "Essie", "Trinidad", "Izola", "Toni", "Regine", "Ali", "Cyril"];
const user = require('./backend/db/models/user');
const data = require('./backend/db/models/data');
const goals = require('./backend/db/models/goal');
const snowmedCodes = require('./backend/db/models/FHiR/SnowmedCodes');
const Company = require('./backend/db/models/company');
const CompanyAssociation = require('./backend/db/models/companyAssociations');
const Goal = goals.Goal;
const IndividualUser = user.individual;
const CorporateUser = user.corporate;
const MedicationStatement = require('./backend/db/models/FHiR/MedicationStatement');
const Observation = require('./backend/db/models/FHiR/Observation');
const FamilyMemberHistory = require('./backend/db/models/FHiR/FamilyMemberHistory');
const Condition = require('./backend/db/models/FHiR/Condition');
mongoose.connect("mongodb://localhost:27017", {useMongoClient: true});
const db = mongoose.connection;
console.log("Done loading");
const randomDocument = (page) => (callback) => {
page.count().exec(function (err, count) {
// Get a random entry
let random = Math.floor(Math.random() * count);
// Again query all users but only fetch one offset by our random #
page.findOne().skip(random).exec(callback);
});
};
const randomUser = randomDocument(IndividualUser);
const randomCompany = randomDocument(Company);//(callback) => Company.findOne({}, callback);
function randomFrom(arr) {
return arr[Math.floor(Math.random() * arr.length)];
}
function randomDate() {
let start = new Date('1995-12-17T03:24:00');
let end = new Date();
return new Date(start.getTime() + Math.random() * (end.getTime() - start.getTime()));
}
function randomString() {
let text = "";
const possible = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789";
for (let i = 0; i < 15; i++)
text += possible.charAt(Math.floor(Math.random() * possible.length));
return text;
}
function generateDataForCorporateUser(dataType, dateRange) {
CorporateUser.findById("5a4d43866e5ba73fa060646f", (err, user) => {
console.log(JSON.stringify(user));
let individualUser = new IndividualUser();
let name = randomFrom(names);
individualUser.email = name + Math.floor(Math.random()) + "@mail.com";
individualUser.password = "password";
individualUser.name = name + " Blogs";
console.log("Created user now saving");
individualUser.save((err, savedUser) => {
if (err) console.log(JSON.stringify(err));
console.log(JSON.stringify(savedUser));
let association = new CompanyAssociation();
association.company = user.company;
association.user = savedUser._id;
let company_id = user.company;
let user_id = savedUser._id;
association.save((err, savedAssoc) => {
if (err) console.log(JSON.stringify(err));
console.log(JSON.stringify(savedAssoc));
for (let i = 0; i < 100; i++) {
let observation = new Observation();
observation.status = "registered";
observation.code = {
coding: {
snowmedCT: data.DATA_SPECIFICATION[dataType].loinc
}
};
observation.subject = user_id;
observation.issued = generateDateInDateRange(dateRange);
observation.effective = generateDateInDateRange(dateRange);
observation.value = generateRealisticDataFor(dataType);
observation.performer = company_id;
observation.device = randomFrom(["android/fs0d0sj2", "fitbit/f0sjds", "iphone/jsd0sdj3", "mac/03kdj02j3"]);
observation.save((err, savedObservation) => {
if (err) console.log(JSON.stringify(err));
console.log(JSON.stringify(savedObservation));
});
}
})
})
});
}
function generateDataForExistingCorporateUser(email, dataType, dateRange) {
CorporateUser.findOne({email: email}, (err, user) => {
console.log(JSON.stringify(user));
CompanyAssociation.find({company: user.company}, (err, associations) => {
if (err) console.log(JSON.stringify(err));
let pos = Math.floor(Math.random() * associations.length);
let association = associations[pos];
for (let i = 0; i < 100; i++) {
let observation = new Observation();
observation.status = "registered";
observation.code = {
coding: {
snowmedCT: data.DATA_SPECIFICATION[dataType].loinc
}
};
observation.subject = association.user;
observation.performer = user.company;
observation.issued = generateDateInDateRange(dateRange);
observation.effective = generateDateInDateRange(dateRange);
observation.value = generateRealisticDataFor(dataType);
observation.device = randomFrom(["android/fs0d0sj2", "fitbit/f0sjds", "iphone/jsd0sdj3", "mac/03kdj02j3"]);
observation.save((err, savedObservation) => {
if (err) console.log(JSON.stringify(err));
console.log(JSON.stringify(savedObservation));
});
}
})
});
}
function generateCondtion() {
randomUser((err, user) => {
randomCompany((err, company) => {
let condition = new Condition();
condition.status = "registered";
condition.code = {
coding: {
snowmedCT: randomFrom(snowmedCodes.snowmedCTCodes)
}
};
condition.subject = user._id;
condition.assertedDate = randomDate();
condition.asserter = company._id;
condition.save((err, savedCondition) => {
if (err)
console.log(JSON.stringify(err));
console.log(JSON.stringify(savedCondition));
})
});
});
}
function generateFamilyMemberHistory() {
randomCompany((err, company) => {
randomUser((err, user) => {
let familyMemberHistory = new FamilyMemberHistory();
familyMemberHistory.status = "registered";
familyMemberHistory.patient = user._id;
familyMemberHistory.date = randomDate();
familyMemberHistory.relationship = {
coding: {
// snowmedCT: randomFrom(snowmedCodes.snowmedRelationshipCodes)
snowmedCT: randomFrom(['63863-5', '29463-7', '63863-5', '29463-7', '63863-5', '29463-7'])
}
};
familyMemberHistory.gender = "Male";
let age = Math.abs(Math.random() * 100);
familyMemberHistory.age = age;
familyMemberHistory.estimatedAge = age + Math.random() * 10;
familyMemberHistory.deceased = "false";
familyMemberHistory.condition = {
coding: {
snowmedCT: randomFrom(snowmedCodes.snowmedConditionCodes)
},
};
familyMemberHistory.recorder = company._id;
familyMemberHistory.save((err, savedUser) => {
if (err) console.log(JSON.stringify(err));
console.log(JSON.stringify(savedUser));
});
});
});
}
function generateIndividual() {
randomCompany((err, company) => {
if (err) console.log(JSON.stringify(err));
let individualUser = new IndividualUser();
let name = randomFrom(names);
individualUser.email = name + Math.floor(Math.random()) + "@mail.com";
individualUser.password = "password";
individualUser.name = name + " Blogs";
console.log("Created user now saving");
individualUser.save((err, savedUser) => {
if (err) console.log(JSON.stringify(err));
console.log(JSON.stringify(savedUser));
})
});
}
function generateObservation() {
randomCompany((err, company) => {
randomUser((err, user) => {
let observation = new Observation();
observation.status = "registered";
observation.category = {
coding: {
snowmedCT: randomFrom(snowmedCodes.snowmedCategoryCodes)
}
};
observation.code = {
coding: {
snowmedCT: randomFrom(snowmedCodes.snowmedCTCodes)
}
};
observation.subject = user._id;
observation.effective = randomDate();
observation.issued = randomDate();
observation.performer = company._id;
observation.value = Math.random();
observation.bodySite = {
coding: {
snowmedCT: randomFrom(snowmedCodes.snowmedBodySiteCodes)
}
};
observation.method = {
coding: {
snowmedCT: randomFrom(snowmedCodes.snowmedMethodCodes)
},
};
observation.device = randomFrom(["android/fs0d0sj2", "fitbit/f0sjds", "iphone/jsd0sdj3", "mac/03kdj02j3"]);
observation.save((err, savedObservation) => {
if (err) console.log(JSON.stringify(err));
console.log(JSON.stringify(savedObservation));
});
});
});
}
function generateMedicationStatement() {
randomCompany((err, company) => {
if (err) console.log(JSON.stringify(err));
console.log(JSON.stringify(company));
randomUser((err, user) => {
if (err) console.log(JSON.stringify(err));
console.log(JSON.stringify(user));
let medicationStatement = new MedicationStatement();
medicationStatement.status = "registered";
medicationStatement.category = {snowmedCT: snowmedCodes.snowmedCategoryCodes[0]};
medicationStatement.medication = "medication/" + randomString();
medicationStatement.effective = randomDate();
medicationStatement.dateAsserted = randomDate();
medicationStatement.informationSource = company._id;
medicationStatement.subject = user._id;
medicationStatement.taken = "y";
medicationStatement.save((err, savedMedicationStatement) => {
if (err) console.log(JSON.stringify(err));
console.log(JSON.stringify(savedMedicationStatement));
});
});
});
}
function generateGenericObservation() {
let observation = new Observation();
observation.status = "registered";
observation.category = {
coding: {
// doesn't matter what this is - just to look realistic
snowmedCT: randomFrom(snowmedCodes.snowmedCategoryCodes)
}
};
observation.bodySite = {
coding: {
snowmedCT: randomFrom(snowmedCodes.snowmedBodySiteCodes)
}
};
observation.method = {
coding: {
snowmedCT: randomFrom(snowmedCodes.snowmedMethodCodes)
},
};
observation.device = randomFrom(["android/fs0d0sj2", "fitbit/f0sjds", "iphone/jsd0sdj3", "mac/03kdj02j3"]);
return observation;
}
function generateDateInDateRange(dateRange) {
let noOfDays;
switch (dateRange) {
case 'Daily':
noOfDays = 1;
break;
case 'Weekly':
noOfDays = 7;
break;
case 'Monthly':
noOfDays = 31;
break;
case 'Annual':
noOfDays = 365;
break;
}
let from_ts = new Date(Date.now() - 24 * 60 * 60 * 1000 * noOfDays).getTime();
let to_ts = new Date().getTime();
let fDate = new Date(Math.floor(Math.random() * (to_ts - from_ts)) + from_ts);
return fDate;
}
// generate an observation for a person
function generateObservationFor(email, dataType, dateRange) {
// finds an individual user by email - this is the user you are using for testing
IndividualUser.findOne({email: email}, (err, user) => {
// find the associated company - your user should have an associated company or else it will fail
CompanyAssociation.findOne({user: user._id}, (err, companyAssociations) => {
// just grab the id of your associated company
let companyId = companyAssociations.company[0];
// create an observation with like the default FHiR fields filled in
// all you need to set are the
// - the subject,
// - the performer,
// - the code,
// - the issued date,
// - the effective date,
// - the value
let observation = generateGenericObservation();
// set the subject to be your individual user (the one specified by email)
observation.subject = user._id;
// set the performer to be the associated company
observation.performer = companyId;
// sets the code for the observation so that it turns up correctly in the backend
observation.code = {coding: {snowmedCT: data.DATA_SPECIFICATION[dataType].loinc}};
// note: data specification is a mapping from dataType (i.e HeartRate) to loinc codes
// issued is the date that is used in filtering
// you should make this a date in the specified range
observation.issued = generateDateInDateRange(dateRange);
// another date value, isn't really used at all, but set it to make the data more realistic
observation.effective = generateDateInDateRange(dateRange);
// the value of the data, actually used in visualizations - you should generate this value
observation.value = generateRealisticDataFor(dataType);
// save the created observation to the database
observation.save((err, savedObservation) => {
if (err) console.log(JSON.stringify(err));
console.log(JSON.stringify(savedObservation));
});
});
});
}
function generateRealisticDataFor(dataType) {
switch (dataType) {
case 'HeartRate':
return (Math.random() * 4.27) + 127;
case 'BodyWeight':
return (Math.random() * 12.1) + 76.7;
case 'BodyHeight':
return (Math.random() * 6.8) + 197.4;
case 'BMI':
return (Math.random() * 4.9) + 21.7;
case 'BloodPressure':
return (Math.random() * 0.3) + 1.5;
}
}
// this is a loop that runs 50 times
for (let i = 0; i < 20; i++) {
let prefix = 'dhen';
// // here it generates an observation for my user, for a heartrate data point within the past week
generateObservationFor(prefix + '@mail.com', 'BodyHeight', 'Weekly');
generateObservationFor(prefix + '@mail.com', 'BodyWeight', 'Weekly');
generateObservationFor(prefix + '@mail.com', 'BMI', 'Weekly');
generateObservationFor(prefix + '@mail.com', 'HeartRate', 'Weekly');
generateObservationFor(prefix + '@mail.com', 'BloodPressure', 'Weekly');
generateObservationFor(prefix + '@mail.com', 'BodyHeight', 'Daily');
generateObservationFor(prefix + '@mail.com', 'BodyWeight', 'Daily');
generateObservationFor(prefix + '@mail.com', 'BMI', 'Daily');
generateObservationFor(prefix + '@mail.com', 'HeartRate', 'Daily');
generateObservationFor(prefix + '@mail.com', 'BloodPressure', 'Daily');
generateObservationFor(prefix + '@mail.com', 'BodyHeight', 'Annual');
generateObservationFor(prefix + '@mail.com', 'BodyWeight', 'Annual');
generateObservationFor(prefix + '@mail.com', 'BMI', 'Annual');
generateObservationFor(prefix + '@mail.com', 'HeartRate', 'Annual');
generateObservationFor(prefix + '@mail.com', 'BloodPressure', 'Annual');
generateObservationFor(prefix + '@mail.com', 'BodyHeight', 'Monthly');
generateObservationFor(prefix + '@mail.com', 'BodyWeight', 'Monthly');
generateObservationFor(prefix + '@mail.com', 'BMI', 'Monthly');
generateObservationFor(prefix + '@mail.com', 'HeartRate', 'Monthly');
generateObservationFor(prefix + '@mail.com', 'BloodPressure', 'Monthly');
}