Skip to content

Commit

Permalink
improve glucose-get-last performance
Browse files Browse the repository at this point in the history
  • Loading branch information
thomasvargiu committed May 9, 2024
1 parent 3430f6b commit c1729c0
Showing 1 changed file with 27 additions and 17 deletions.
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

0 comments on commit c1729c0

Please sign in to comment.