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

Clamp treatments & load less data #4697

Merged
merged 2 commits into from
Jun 20, 2019
Merged
Show file tree
Hide file tree
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
21 changes: 18 additions & 3 deletions lib/data/dataloader.js
Original file line number Diff line number Diff line change
Expand Up @@ -222,8 +222,10 @@ function loadActivity(ddata, ctx, callback) {
}

function loadTreatments(ddata, ctx, callback) {

// Load 2.5 days to cover last 48 hours including overlapping temp boluses or temp targets
var dateRange = {
$gte: new Date(ddata.lastUpdated - (ONE_DAY * 8)).toISOString()
$gte: new Date(ddata.lastUpdated - (ONE_DAY * 2.5)).toISOString()
};
if (ddata.page && ddata.page.frame) {
dateRange['$lte'] = new Date(ddata.lastUpdated).toISOString();
Expand Down Expand Up @@ -256,6 +258,7 @@ function loadProfileSwitchTreatments(ddata, ctx, callback) {
dateRange['$lte'] = new Date(ddata.lastUpdated).toISOString();
}

// Load the latest profile switch treatment
var tq = {
find: {
eventType: 'Profile Switch',
Expand Down Expand Up @@ -291,6 +294,17 @@ function loadProfileSwitchTreatments(ddata, ctx, callback) {
}

function loadSensorAndInsulinTreatments(ddata, ctx, callback) {
async.parallel([
loadLatestSingle.bind(null, ddata, ctx, 'Sensor Start')
,loadLatestSingle.bind(null, ddata, ctx, 'Sensor Change')
,loadLatestSingle.bind(null, ddata, ctx, 'Site Change')
,loadLatestSingle.bind(null, ddata, ctx, 'Insulin Change')
,loadLatestSingle.bind(null, ddata, ctx, 'Pump Battery Change')
], callback);
}

function loadLatestSingle(ddata, ctx, dataType, callback) {

var dateRange = {
$gte: new Date(ddata.lastUpdated - (ONE_DAY * 32)).toISOString()
};
Expand All @@ -302,13 +316,14 @@ function loadSensorAndInsulinTreatments(ddata, ctx, callback) {
var tq = {
find: {
eventType: {
$in: ['Sensor Start', 'Sensor Change', 'Insulin Change', 'Pump Battery Change']
$eq: dataType
},
created_at: dateRange
},
sort: {
created_at: -1
}
},
count: 1
};

ctx.treatments.list(tq, function(err, results) {
Expand Down
10 changes: 7 additions & 3 deletions lib/data/treatmenttocurve.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,9 @@

var _ = require('lodash');

const MAX_BG_MMOL = 22;
const MAX_BG_MGDL = MAX_BG_MMOL * 18;

module.exports = function fitTreatmentsToBGCurve (ddata, env, ctx) {

var settings = env.settings;
Expand Down Expand Up @@ -62,15 +65,16 @@ module.exports = function fitTreatmentsToBGCurve (ddata, env, ctx) {
console.warn('found an invalid glucose value', treatment);
} else if (treatment.glucose && treatment.units) {
if (treatment.units === 'mmol') {
treatment.mmol = Number(treatment.glucose);
treatment.mmol = Math.min(Number(treatment.glucose), MAX_BG_MMOL);
} else {
treatment.mgdl = Number(treatment.glucose);
treatment.mgdl = Math.min(Number(treatment.glucose), MAX_BG_MGDL);
}
} else if (treatment.glucose) {
//no units, assume everything is the same
//console.warn('found a glucose value without any units, maybe from an old version?', _.pick(treatment, '_id', 'created_at', 'enteredBy'));
var units = settings.units === 'mmol' ? 'mmol' : 'mgdl';
treatment[units] = Number(treatment.glucose);

treatment[units] = settings.units === 'mmol' ? Math.min(Number(treatment.glucose), MAX_BG_MMOL) : Math.min(Number(treatment.glucose), MAX_BG_MGDL);
} else {
treatment.mgdl = mgdlByTime();
}
Expand Down