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

Bug fixes for missing Tandem boluses (UPLOAD-45) #905

Closed
wants to merge 6 commits into from
Closed
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
80 changes: 40 additions & 40 deletions lib/commonFunctions.js
Original file line number Diff line number Diff line change
Expand Up @@ -133,75 +133,75 @@ exports.finalScheduledBasal = function(currBasal, settings, source) {
return currBasal;
};

exports.updatePreviousDurations = async function(data, cfg, cb) {
exports.updateDuration = function(event, lastEvent) {

const withAnnotation = function(event) {
if(_.find(event.annotations || [], function(ann) {
return ann.code === 'final-basal/fabricated-from-schedule' ||
ann.code === 'basal/unknown-duration' ||
ann.code === 'status/incomplete-tuple';
ann.code === 'status/incomplete-tuple' ||
ann.code === 'bolus/extended-in-progress';
})) {
return true;
}
return false;
};

const strip = function(record) {
const stripped = _.omit(record,
'createdTime', 'id', 'modifiedTime', 'revision', 'uploadId',
'_active', '_deduplicator', '_id', '_schemaVersion', '_userId',
);
const updatedDuration = Date.parse(event.time) - Date.parse(lastEvent.time);

debug('Last' , stripped.type, 'from previous upload was:', JSON.stringify(stripped, null, 4));
return stripped;
};
if ( (updatedDuration >= 0) &&
((lastEvent.duration > updatedDuration) || (lastEvent.duration === 0))) {
// we don't update the previous duration if the new duration is the same
// or larger, because a larger updated duration could mean the pump
// wasn't in use for a while
debug('Updating duration of last event from previous upload');
if(lastEvent.duration > 0) {
lastEvent.expectedDuration = lastEvent.duration;
}
lastEvent.duration = updatedDuration;

const updateDuration = function(event, lastEvent) {
const updatedDuration = Date.parse(event.time) - Date.parse(lastEvent.time);

if ( (updatedDuration >= 0) &&
((lastEvent.duration > updatedDuration) || (lastEvent.duration === 0))) {
// we don't update the previous duration if the new duration is the same
// or larger, because a larger updated duration could mean the pump
// wasn't in use for a while
debug('Updating duration of last event from previous upload');
if(lastEvent.duration > 0) {
lastEvent.expectedDuration = lastEvent.duration;
}
lastEvent.duration = updatedDuration;
if (withAnnotation(lastEvent)) {
// if the event was previously annotated because of the missing event,
// we can now remove the annotations
lastEvent = _.omit(lastEvent, 'annotations');
}

if (withAnnotation(lastEvent)) {
// if the event was previously annotated because of the missing event,
// we can now remove the annotations
lastEvent = _.omit(lastEvent, 'annotations');
}
return lastEvent;
} else {
return null;
}
};

return lastEvent;
} else {
return null;
}
};
exports.strip = function(record) {
const stripped = _.omit(record,
'createdTime', 'id', 'modifiedTime', 'revision', 'uploadId',
'_active', '_deduplicator', '_id', '_schemaVersion', '_userId',
);

try {
debug('Last' , stripped.type, 'from previous upload was:', JSON.stringify(stripped, null, 4));
return stripped;
};

exports.updatePreviousDurations = async function(data, cfg, cb) {


try {
// update last basal
const lastBasal = strip(await api.getLatestRecord(cfg.groupId, cfg.deviceInfo.deviceId, 'basal'));
const lastBasal = this.strip(await api.getLatestRecord(cfg.groupId, cfg.deviceInfo.deviceId, 'basal'));
let datum = _.find(data.post_records, {type: 'basal'});
if (datum != null) {
data.post_records.push(updateDuration(datum, lastBasal));
data.post_records.push(this.updateDuration(datum, lastBasal));
}

// update last suspend event
if (lastBasal.deliveryType === 'suspend') {

// TODO: get latest 'status' event, as we don't want to get last alarm
const lastDeviceEvent = strip(await api.getLatestRecord(cfg.groupId, cfg.deviceInfo.deviceId, 'deviceEvent'));
const lastDeviceEvent = this.strip(await api.getLatestRecord(cfg.groupId, cfg.deviceInfo.deviceId, 'deviceEvent', 'status'));
if (lastDeviceEvent.subType === 'status') {
const index = _.findIndex(data.post_records, {type: 'deviceEvent', subType: 'status'});
datum = data.post_records[index];

if (index > -1) {
const updatedStatus = updateDuration(datum, lastDeviceEvent);
const updatedStatus = this.updateDuration(datum, lastDeviceEvent);
updatedStatus.reason.resumed = datum.reason.resumed;

if (lastDeviceEvent.payload.reason != null) {
Expand Down
3 changes: 2 additions & 1 deletion lib/core/api.js
Original file line number Diff line number Diff line change
Expand Up @@ -639,11 +639,12 @@ api.getMostRecentUploadRecord = function(userId, deviceId, cb) {
});
};

api.getLatestRecord = function(userId, deviceId, type, cb) {
api.getLatestRecord = function(userId, deviceId, type, subType) {
const options = {
type: type,
latest: 1,
deviceId: deviceId,
subType: subType,
};

api.log('GET /data/' + userId + '&options=' + JSON.stringify(options));
Expand Down
57 changes: 41 additions & 16 deletions lib/drivers/tandem/tandemDriver.js
Original file line number Diff line number Diff line change
Expand Up @@ -1360,7 +1360,7 @@ module.exports = function (config) {
data.end_seq = result.payload['end_seq'];

if ((lastEndPosition != null) &&
(lastEndPosition < data.end_seq) &&
(lastEndPosition <= data.end_seq) &&
(uploadDataPeriod.periodGlobal === uploadDataPeriod.PERIODS.DELTA)) {
cfg.isFirstUpload = false;
if (lastEndPosition < result.payload['start_seq']) {
Expand Down Expand Up @@ -1616,6 +1616,17 @@ module.exports = function (config) {
record = record.with_deviceTime(bolus.deviceTime);
if (bolus.bolex_size !== undefined) {
// extended bolus

// cancelled before any insulin was given on dual bolus
if (bolus.bolex_insulin_delivered === undefined) {
record = record.with_extended(0);
}
else {
record = record.with_extended(bolus.bolex_insulin_delivered);
}
record = record.with_expectedExtended(bolus.bolex_insulin_requested)
.with_expectedDuration(bolus.duration);

// first case: extended bolus was cancelled
if (bolus.bolex_size !== bolus.bolex_insulin_delivered) {
if(bolus.endDeviceTime !== undefined) {
Expand All @@ -1626,18 +1637,10 @@ module.exports = function (config) {
else{
// no end time, so extended bolus still in progress
debug('Extended bolus in progress: ', bolus);
continue;
record = record.with_duration(0)
.with_expectedExtended(bolus.bolex_size);
annotate.annotateEvent(record, 'bolus/extended-in-progress');
}

// cancelled before any insulin was given on dual bolus
if (bolus.bolex_insulin_delivered === undefined) {
record = record.with_extended(0);
}
else {
record = record.with_extended(bolus.bolex_insulin_delivered);
}
record = record.with_expectedExtended(bolus.bolex_insulin_requested)
.with_expectedDuration(bolus.duration);
}
// other case: extended bolus completed
else {
Expand Down Expand Up @@ -1673,7 +1676,7 @@ module.exports = function (config) {

// wizard records
if (_.includes(['standard', 'extended'], bolus.bolus_option) &&
(bolus.correction_bolus_included || (bolus.food_bolus_size > 0))) {
(bolus.correction_bolus_included || (bolus.bolus_type_str === 'carb'))) {
// a wizard bolus can be a correction bolus or food bolus (or both)

var netBolus = null;
Expand All @@ -1691,7 +1694,6 @@ module.exports = function (config) {
correction: bolus.correction_bolus_size,
net: netBolus.toFixedNumber(SIGNIFICANT_DIGITS)
})
.with_bgInput(bolus.bg)
.with_carbInput(bolus.carb_amount)
.with_insulinOnBoard(bolus.bc_iob)
.with_insulinCarbRatio(bolus.carb_ratio)
Expand All @@ -1702,12 +1704,34 @@ module.exports = function (config) {
.with_bolus(record.done())
.with_units('mg/dL')
.set('index', record.index);

if (bolus.bg > 0) {
// when BG is not recorded, Tandem stores a 0, so don't record that
wizard_record = wizard_record.with_bgInput(bolus.bg);
}

cfg.tzoUtil.fillInUTCInfo(wizard_record, bolus.jsDate);
wizard_record = wizard_record.done();
records.push(wizard_record);
} else {
if(record.subType === 'normal' && bolus.insulin_delivered === undefined) {
debug('Only part of a bolus received, dropping:', bolus);
debug('Only part of a bolus received:', bolus);

// update last extended bolus if it was running
if (data.lastBolus && (data.lastBolus.extended) === 0 && (data.lastBolus.duration) === 0) {
var updatedBolus = common.updateDuration(record, data.lastBolus);
updatedBolus.extended = bolus.bolex_insulin_delivered;
if (bolus.bolex_insulin_requested !== bolus.bolex_insulin_delivered) {
updatedBolus.expectedExtended = bolus.bolex_insulin_requested;
} else {
delete updatedBolus.expectedExtended;
}

if (updatedBolus.duration >= updatedBolus.expectedDuration) {
delete updatedBolus.expectedDuration;
}
records.push(updatedBolus);
}
}
else{
records.push(record.done());
Expand Down Expand Up @@ -2203,7 +2227,8 @@ module.exports = function (config) {
var currentTime = {};
addTimestamp(currentTime,result.payload.timestamp);

common.checkDeviceTime(currentTime.deviceTime, cfg, function(err) {
common.checkDeviceTime(currentTime.deviceTime, cfg, async function(err) {
data.lastBolus = common.strip(await api.getLatestRecord(cfg.groupId, cfg.deviceInfo.deviceId, 'bolus'));
return cb(err, data);
});
}
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,7 @@
"stack-trace": "0.0.10",
"sudo-prompt": "9.0.0",
"sundial": "1.6.0",
"tidepool-platform-client": "0.42.0-add-deviceid.0",
"tidepool-platform-client": "0.42.0-add-deviceid.1",
"usb-cdc-acm": "0.1.1",
"uuid": "3.3.3"
},
Expand Down
8 changes: 4 additions & 4 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -12362,10 +12362,10 @@ thunky@^1.0.2:
version "1.0.3"
resolved "https://registry.yarnpkg.com/thunky/-/thunky-1.0.3.tgz#f5df732453407b09191dae73e2a8cc73f381a826"

[email protected].0:
version "0.42.0-add-deviceid.0"
resolved "https://registry.yarnpkg.com/tidepool-platform-client/-/tidepool-platform-client-0.42.0-add-deviceid.0.tgz#e89eb1879cabcfc5651a08925b583fba813fa8b0"
integrity sha512-29W+rzV3iY8iqUF+4etL6IUrzj5m5z8oBC/kuFXBnWZ2ehnEc4GbTX3HY68f3RnYtMQohIflkBIXNagi9LRFsQ==
[email protected].1:
version "0.42.0-add-deviceid.1"
resolved "https://registry.yarnpkg.com/tidepool-platform-client/-/tidepool-platform-client-0.42.0-add-deviceid.1.tgz#247c6b71ac73389c0f1287bf284c3cd91d58b815"
integrity sha512-r6Zbi+Ee8Ic2/1oR6bywhEeDTw8UkBJEALX346Cxz634kO2bMjgQnEfkTpRSWOb/55/3O6t4S2Vv4FDVLrYqYw==
dependencies:
async "0.9.0"
lodash "3.3.1"
Expand Down