forked from nightscout/cgm-remote-monitor
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathprofilefunctions.js
367 lines (303 loc) · 12.5 KB
/
profilefunctions.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
'use strict';
var _ = require('lodash');
var moment = require('moment-timezone');
var c = require('memory-cache');
var times = require('./times');
var crypto = require('crypto');
var cacheTTL = 600;
var prevBasalTreatment = null;
function init (profileData) {
var profile = {};
var cache = new c.Cache();
profile.loadData = function loadData (profileData) {
if (profileData && profileData.length) {
profile.data = profile.convertToProfileStore(profileData);
_.each(profile.data, function eachProfileRecord (record) {
_.each(record.store, profile.preprocessProfileOnLoad);
record.mills = new Date(record.startDate).getTime();
});
}
};
profile.convertToProfileStore = function convertToProfileStore (dataArray) {
var convertedProfiles = [];
_.each(dataArray, function(profile) {
if (!profile.defaultProfile) {
var newObject = {};
newObject.defaultProfile = 'Default';
newObject.store = {};
newObject.startDate = profile.startDate;
newObject._id = profile._id;
newObject.convertedOnTheFly = true;
delete profile.startDate;
delete profile._id;
delete profile.created_at;
newObject.store['Default'] = profile;
convertedProfiles.push(newObject);
console.log('Profile not updated yet. Converted profile:', newObject);
} else {
delete profile.convertedOnTheFly;
convertedProfiles.push(profile);
}
});
return convertedProfiles;
};
profile.timeStringToSeconds = function timeStringToSeconds (time) {
var split = time.split(':');
return parseInt(split[0]) * 3600 + parseInt(split[1]) * 60;
};
// preprocess the timestamps to seconds for a couple orders of magnitude faster operation
profile.preprocessProfileOnLoad = function preprocessProfileOnLoad (container) {
_.each(container, function eachValue (value) {
if (Object.prototype.toString.call(value) === '[object Array]') {
profile.preprocessProfileOnLoad(value);
}
if (value.time) {
var sec = profile.timeStringToSeconds(value.time);
if (!isNaN(sec)) { value.timeAsSeconds = sec; }
}
});
};
profile.getValueByTime = function getValueByTime (time, valueType, spec_profile) {
if (!time) { time = Date.now(); }
// CircadianPercentageProfile support
var timeshift = 0;
var percentage = 100;
var activeTreatment = profile.activeProfileTreatmentToTime(time);
var isCcpProfile = !spec_profile && activeTreatment && activeTreatment.CircadianPercentageProfile;
if (isCcpProfile) {
percentage = activeTreatment.percentage;
timeshift = activeTreatment.timeshift; // in hours
}
var offset = timeshift % 24;
time = time + offset * times.hours(offset).msecs;
//round to the minute for better caching
var minuteTime = Math.round(time / 60000) * 60000;
var cacheKey = (minuteTime + valueType + spec_profile + profile.profiletreatments_hash);
var returnValue = cache.get(cacheKey);
if (returnValue) {
return returnValue;
}
var valueContainer = profile.getCurrentProfile(time, spec_profile)[valueType];
// Assumes the timestamps are in UTC
// Use local time zone if profile doesn't contain a time zone
// This WILL break on the server; added warnings elsewhere that this is missing
// TODO: Better warnings to user for missing configuration
var t = profile.getTimezone(spec_profile) ? moment(minuteTime).tz(profile.getTimezone(spec_profile)) : moment(minuteTime);
// Convert to seconds from midnight
var mmtMidnight = t.clone().startOf('day');
var timeAsSecondsFromMidnight = t.clone().diff(mmtMidnight, 'seconds');
// If the container is an Array, assume it's a valid timestamped value container
returnValue = valueContainer;
if (Object.prototype.toString.call(valueContainer) === '[object Array]') {
_.each(valueContainer, function eachValue (value) {
if (timeAsSecondsFromMidnight >= value.timeAsSeconds) {
returnValue = value.value;
}
});
}
if (returnValue) {
returnValue = parseFloat(returnValue);
if (isCcpProfile) {
switch (valueType) {
case "sens":
case "carbratio":
returnValue = returnValue * 100 / percentage;
break;
case "basal":
returnValue = returnValue * percentage / 100;
break;
}
}
}
cache.put(cacheKey, returnValue, cacheTTL);
return returnValue;
};
profile.getCurrentProfile = function getCurrentProfile (time, spec_profile) {
time = time || new Date().getTime();
var data = profile.hasData() ? profile.data[0] : null;
var timeprofile = spec_profile || profile.activeProfileToTime(time);
return data && data.store[timeprofile] ? data.store[timeprofile] : {};
};
profile.getUnits = function getUnits (spec_profile) {
return profile.getCurrentProfile(null, spec_profile)['units'];
};
profile.getTimezone = function getTimezone (spec_profile) {
return profile.getCurrentProfile(null, spec_profile)['timezone'];
};
profile.hasData = function hasData () {
return profile.data ? true : false;
};
profile.getDIA = function getDIA (time, spec_profile) {
return profile.getValueByTime(Number(time), 'dia', spec_profile);
};
profile.getSensitivity = function getSensitivity (time, spec_profile) {
return profile.getValueByTime(Number(time), 'sens', spec_profile);
};
profile.getCarbRatio = function getCarbRatio (time, spec_profile) {
return profile.getValueByTime(Number(time), 'carbratio', spec_profile);
};
profile.getCarbAbsorptionRate = function getCarbAbsorptionRate (time, spec_profile) {
return profile.getValueByTime(Number(time), 'carbs_hr', spec_profile);
};
profile.getLowBGTarget = function getLowBGTarget (time, spec_profile) {
return profile.getValueByTime(Number(time), 'target_low', spec_profile);
};
profile.getHighBGTarget = function getHighBGTarget (time, spec_profile) {
return profile.getValueByTime(Number(time), 'target_high', spec_profile);
};
profile.getBasal = function getBasal (time, spec_profile) {
return profile.getValueByTime(Number(time), 'basal', spec_profile);
};
profile.updateTreatments = function updateTreatments (profiletreatments, tempbasaltreatments, combobolustreatments) {
profile.profiletreatments = profiletreatments || [];
profile.tempbasaltreatments = tempbasaltreatments || [];
// dedupe temp basal events
profile.tempbasaltreatments = _.uniqBy(profile.tempbasaltreatments, 'mills');
_.each(profile.tempbasaltreatments, function addDuration (t) {
t.endmills = t.mills + times.mins(t.duration || 0).msecs;
});
profile.tempbasaltreatments.sort(function compareTreatmentMills (a, b) {
return a.mills - b.mills;
});
profile.combobolustreatments = combobolustreatments || [];
profile.profiletreatments_hash = crypto.createHash('sha1').update(JSON.stringify(profile.profiletreatments)).digest('hex');
profile.tempbasaltreatments_hash = crypto.createHash('sha1').update(JSON.stringify(profile.tempbasaltreatments)).digest('hex');
profile.combobolustreatments_hash = crypto.createHash('sha1').update(JSON.stringify(profile.combobolustreatments)).digest('hex');
};
profile.activeProfileToTime = function activeProfileToTime (time) {
if (profile.hasData()) {
var timeprofile = profile.data[0].defaultProfile;
time = Number(time) || new Date().getTime();
var treatment = profile.activeProfileTreatmentToTime(time);
if (treatment && profile.data[0].store && profile.data[0].store[treatment.profile]) {
timeprofile = treatment.profile;
}
return timeprofile;
}
return null;
};
profile.activeProfileTreatmentToTime = function activeProfileTreatmentToTime (time) {
var cacheKey = 'profile' + time + profile.profiletreatments_hash;
//var returnValue = profile.timeValueCache[cacheKey];
var returnValue;
if (returnValue) {
return returnValue;
}
var treatment = null;
if (profile.hasData()) {
profile.profiletreatments.forEach(function eachTreatment (t) {
if (time >= t.mills && t.mills >= profile.data[0].mills) {
var duration = times.mins(t.duration || 0).msecs;
if (duration != 0 && time < t.mills + duration) {
treatment = t;
// if profile switch contains json of profile inject it in to store to be findable by profile name
if (treatment.profileJson && !profile.data[0].store[treatment.profile]) {
if (treatment.profile.indexOf("@@@@@") < 0)
treatment.profile += "@@@@@" + treatment.mills;
let json = JSON.parse(treatment.profileJson);
profile.data[0].store[treatment.profile] = json;
}
}
if (duration == 0) {
treatment = t;
// if profile switch contains json of profile inject it in to store to be findable by profile name
if (treatment.profileJson && !profile.data[0].store[treatment.profile]) {
if (treatment.profile.indexOf("@@@@@") < 0)
treatment.profile += "@@@@@" + treatment.mills;
let json = JSON.parse(treatment.profileJson);
profile.data[0].store[treatment.profile] = json;
}
}
}
});
}
returnValue = treatment;
cache.put(cacheKey, returnValue, cacheTTL);
return returnValue;
};
profile.profileSwitchName = function profileSwitchName (name) {
var index = name.indexOf("@@@@@");
if (index < 0) return name;
else return name.substring(0, index);
}
profile.tempBasalTreatment = function tempBasalTreatment (time) {
// Most queries for the data in reporting will match the latest found value, caching that hugely improves performance
if (prevBasalTreatment && time >= prevBasalTreatment.mills && time <= prevBasalTreatment.endmills) {
return prevBasalTreatment;
}
// Binary search for events for O(log n) performance
var first = 0
, last = profile.tempbasaltreatments.length - 1;
while (first <= last) {
var i = first + Math.floor((last - first) / 2);
var t = profile.tempbasaltreatments[i];
if (time >= t.mills && time <= t.endmills) {
prevBasalTreatment = t;
return t;
}
if (time < t.mills) {
last = i - 1;
} else {
first = i + 1;
}
}
return null;
};
profile.comboBolusTreatment = function comboBolusTreatment (time) {
var treatment = null;
profile.combobolustreatments.forEach(function eachTreatment (t) {
var duration = times.mins(t.duration || 0).msecs;
if (time < t.mills + duration && time > t.mills) {
treatment = t;
}
});
return treatment;
};
profile.getTempBasal = function getTempBasal (time, spec_profile) {
var cacheKey = 'basal' + time + profile.tempbasaltreatments_hash + profile.combobolustreatments_hash + profile.profiletreatments_hash + spec_profile;
var returnValue = cache.get(cacheKey);
if (returnValue) {
return returnValue;
}
var basal = profile.getBasal(time, spec_profile);
var tempbasal = basal;
var combobolusbasal = 0;
var treatment = profile.tempBasalTreatment(time);
var combobolustreatment = profile.comboBolusTreatment(time);
//special handling for absolute to support temp to 0
if (treatment && !isNaN(treatment.absolute) && treatment.duration > 0) {
tempbasal = Number(treatment.absolute);
} else if (treatment && treatment.percent) {
tempbasal = basal * (100 + treatment.percent) / 100;
}
if (combobolustreatment && combobolustreatment.relative) {
combobolusbasal = combobolustreatment.relative;
}
returnValue = {
basal: basal
, treatment: treatment
, combobolustreatment: combobolustreatment
, tempbasal: tempbasal
, combobolusbasal: combobolusbasal
, totalbasal: tempbasal + combobolusbasal
};
cache.put(cacheKey, returnValue, cacheTTL);
return returnValue;
};
profile.listBasalProfiles = function listBasalProfiles () {
var profiles = [];
if (profile.hasData()) {
var current = profile.activeProfileToTime();
profiles.push(current);
Object.keys(profile.data[0].store).forEach(key => {
if (key !== current && key.indexOf('@@@@@') < 0) profiles.push(key);
})
}
return profiles;
};
if (profileData) { profile.loadData(profileData); }
// init treatments array
profile.updateTreatments([], []);
return profile;
}
module.exports = init;