Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

WIP: Make glucose-get-last faster #1468

Open
wants to merge 1 commit into
base: dev
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
44 changes: 27 additions & 17 deletions lib/glucose-get-last.js
Original file line number Diff line number Diff line change
@@ -1,36 +1,41 @@

function getDateFromEntry(entry) {
return entry.date || Date.parse(entry.display_time) || Date.parse(entry.dateString);
}

var getLastGlucose = function (data) {
data = data.filter(function(obj) {
return obj.glucose || obj.sgv;
}).map(function prepGlucose (obj) {
//Support the NS sgv field to avoid having to convert in a custom way
obj.glucose = obj.glucose || obj.sgv;
if ( obj.glucose !== null ) {
return obj;
}
});

var now = data[0];
var now_date = getDateFromEntry(now);
var now = undefined;
var now_date = undefined;
var change;
var last_deltas = [];
var short_deltas = [];
var long_deltas = [];
var last_cal = 0;

//console.error(now.glucose);
for (var i=1; i < data.length; i++) {
for (var i=0; i < data.length; i++) {
// if we come across a cal record, don't process any older SGVs
if (typeof data[i] !== 'undefined' && data[i].type === "cal") {
var item = data[i];
item.glucose = item.glucose || item.sgv;
if (!item.glucose) {
continue;
}
if (typeof now === 'undefined') {
now = item;
now_date = getDateFromEntry(item);
continue;
}
if (typeof now === 'undefined') {
continue;
}
if (item.type === "cal") {
last_cal = i;
break;
}

// only use data from the same device as the most recent BG data point
if (typeof data[i] !== 'undefined' && data[i].glucose > 38 && data[i].device === now.device) {
var then = data[i];
if (item.glucose > 38 && item.device === now.device) {
var then = item;
var then_date = getDateFromEntry(then);
var avgdelta = 0;
var minutesago;
Expand All @@ -39,7 +44,10 @@ var getLastGlucose = function (data) {
// multiply by 5 to get the same units as delta, i.e. mg/dL/5m
change = now.glucose - then.glucose;
avgdelta = change/minutesago * 5;
} else { console.error("Error: date field not found: cannot calculate avgdelta"); }
} else {
console.error("Error: date field not found: cannot calculate avgdelta");
continue;
}
//if (i < 5) {
//console.error(then.glucose, minutesago, avgdelta);
//}
Expand All @@ -60,6 +68,8 @@ var getLastGlucose = function (data) {
// long_deltas are calculated from everything ~20-40 minutes ago
} else if (17.5 < minutesago && minutesago < 42.5) {
long_deltas.push(avgdelta);
} else if (minutesago > 42.5) {
break;
}
}
}
Expand Down