-
Notifications
You must be signed in to change notification settings - Fork 395
/
bolus.js
181 lines (165 loc) · 5.64 KB
/
bolus.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
'use strict';
function reduce (treatments) {
var results = [ ];
var state = { };
var previous = [ ];
function in_previous (ev) {
var found = false;
previous.forEach(function (elem) {
if (elem.timestamp === ev.timestamp && ev._type === elem._type) {
found = true;
}
});
return found;
}
function within_minutes_from (origin, tail, minutes) {
var ms = minutes * 1000 * 60;
var ts = Date.parse(origin.timestamp);
return /* candidates */ tail.slice( ).filter(function (elem) {
var dt = Date.parse(elem.timestamp);
return ts - dt <= ms;
});
}
function bolus (ev, remaining) {
if (!ev) { console.error('XXX', ev, remaining); return; }
if (ev._type === 'BolusWizard') {
state.carbs = ev.carb_input.toString( );
state.ratio = ev.carb_ratio.toString( );
if (ev.bg) {
state.bg = ev.bg.toString( );
state.glucose = ev.bg.toString( );
state.glucoseType = ev._type;
}
state.wizard = ev;
state.created_at = state.timestamp = ev.timestamp;
previous.push(ev);
}
if (ev._type === 'Bolus') {
state.duration = ev.duration.toString( );
// if (state.square || state.bolus) { }
// state.insulin = (state.insulin ? state.insulin : 0) + ev.amount;
if (ev.duration && ev.duration > 0) {
state.square = ev;
} else {
if (state.bolus) {
state.bolus.amount = state.bolus.amount + ev.amount;
} else
state.bolus = ev;
}
state.created_at = state.timestamp = ev.timestamp;
previous.push(ev);
}
if (remaining && remaining.length > 0) {
if (state.bolus && state.wizard) {
// skip to end
return bolus({}, []);
}
// keep recursing
return bolus(remaining[0], remaining.slice(1));
} else {
// console.error("state", state);
// console.error("remaining", remaining);
state.eventType = '<none>';
state.insulin = (state.insulin ? state.insulin : 0) + (state.square ? state.square.amount : 0) +
(state.bolus ? state.bolus.amount : 0);
var has_insulin = state.insulin && state.insulin > 0;
var has_carbs = state.carbs && state.carbs > 0;
if (state.square && state.bolus) {
annotate("DualWave bolus for", state.square.duration, "minutes");
} else if (state.square && state.wizard) {
annotate("Square wave bolus for", state.square.duration, "minutes");
} else if (state.square) {
annotate("Solo Square wave bolus for", state.square.duration, "minutes");
annotate("No bolus wizard used.");
} else if (state.bolus && state.wizard) {
annotate("Normal bolus with wizard.");
} else if (state.bolus) {
annotate("Normal bolus (solo, no bolus wizard).");
}
if (has_insulin) {
var iobFile = "./monitor/iob.json";
var fs = require('fs');
if (fs.existsSync(iobFile)) {
var iob = JSON.parse(fs.readFileSync(iobFile));
if (iob && Array.isArray(iob) && iob.length) {
annotate("Calculated IOB:", iob[0].iob);
}
}
}
if (state.bolus) {
annotate("Programmed bolus", state.bolus.programmed);
annotate("Delivered bolus", state.bolus.amount);
annotate("Percent delivered: ", (state.bolus.amount/state.bolus.programmed * 100).toString( ) + '%');
}
if (state.square) {
annotate("Programmed square", state.square.programmed);
annotate("Delivered square", state.square.amount);
annotate("Success: ", (state.square.amount/state.square.programmed * 100).toString( ) + '%');
}
if (state.wizard) {
state.created_at = state.wizard.timestamp;
annotate("Food estimate", state.wizard.food_estimate);
annotate("Correction estimate", state.wizard.correction_estimate);
annotate("Bolus estimate", state.wizard.bolus_estimate);
annotate("Target low", state.wizard.bg_target_low);
annotate("Target high", state.wizard.bg_target_high);
var delta = state.wizard.sensitivity * state.insulin * -1;
annotate("Hypothetical glucose delta", delta);
if (state.bg && state.bg > 0) {
annotate('Glucose was:', state.bg);
// state.glucose = state.bg;
// TODO: annotate prediction
}
}
if (has_carbs && has_insulin) {
state.eventType = 'Meal Bolus';
} else {
if (has_carbs && !has_insulin) {
state.eventType = 'Carb Correction';
}
if (!has_carbs && has_insulin) {
state.eventType = 'Correction Bolus';
}
}
if (state.notes && state.notes.length > 0) {
state.notes = state.notes.join("\n");
}
if (state.insulin) {
state.insulin = state.insulin.toString( );
}
results.push(state);
state = { };
}
}
function annotate (msg) {
var args = [ ].slice.apply(arguments);
msg = args.join(' ');
if (!state.notes) {
state.notes = [ ];
}
state.notes.push(msg);
}
function step (current, index) {
if (in_previous(current)) {
return;
}
switch (current._type) {
case 'Bolus':
case 'BolusWizard':
var tail = within_minutes_from(current, treatments.slice(index+1), 2);
bolus(current, tail);
break;
case 'JournalEntryMealMarker':
current.carbs = current.carb_input;
current.eventType = 'Carb Correction';
results.push(current);
break;
default:
results.push(current);
break;
}
}
treatments.forEach(step);
return results;
}
exports = module.exports = reduce;