Skip to content

Commit

Permalink
Don't store a Connect SGV more than once
Browse files Browse the repository at this point in the history
  • Loading branch information
mddub committed Oct 22, 2015
1 parent ab6887f commit b657c8b
Show file tree
Hide file tree
Showing 2 changed files with 68 additions and 5 deletions.
35 changes: 30 additions & 5 deletions lib/plugins/mmconnect.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ function makeRunner_ (env, entries) {
var client = connect.carelink.Client(options);
connect.logger.setVerbose(options.verbose);

var handleData = makeHandler_(entries, options.storeRawData);
var handleData = makeHandler_(entries, makeRecentSgvFilter_(), options.storeRawData);

return function run () {
setInterval(function() {
Expand All @@ -40,7 +40,7 @@ function getOptions_ (env) {
};
}

function makeHandler_ (entries, storeRawData) {
function makeHandler_ (entries, filter, storeRawData) {
return function handleCarelinkData (err, data) {
if (err) {
console.error('MiniMed Connect error: ' + err);
Expand All @@ -51,7 +51,12 @@ function makeHandler_ (entries, storeRawData) {
transformed.push(rawDataEntry_(data));
}

entries.create(transformed, function afterCreate (err) {
// If we blindly upsert the SGV entries, we will lose trend data for
// entries we've already stored, since all SGVs from CareLink except
// the most recent are missing trend data.
var filtered = filter(transformed);

entries.create(filtered, function afterCreate (err) {
if (err) {
console.error('MiniMed Connect storage error: ' + err);
}
Expand All @@ -60,6 +65,27 @@ function makeHandler_ (entries, storeRawData) {
};
}

function makeRecentSgvFilter_ () {
var lastSgvDate = 0;

return function filter (entries) {
var out = [];

entries.forEach(function(entry) {
if (entry['type'] !== 'sgv' || entry['date'] > lastSgvDate) {
out.push(entry);
}
});

out.filter(function(e) { return e['type'] === 'sgv'; })
.forEach(function(e) {
lastSgvDate = Math.max(lastSgvDate, e['date']);
});

return out;
};
}

function rawDataEntry_ (data) {
var cleansed = _.cloneDeep(data);

Expand All @@ -83,8 +109,7 @@ function rawDataEntry_ (data) {
module.exports = {
init: init
// exposed for testing
, makeRunner_: makeRunner_
, getOptions_: getOptions_
, makeHandler_: makeHandler_
, makeRecentSgvFilter_: makeRecentSgvFilter_
, rawDataEntry_: rawDataEntry_
};
38 changes: 38 additions & 0 deletions tests/mmconnect.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,44 @@ describe('mmconnect', function () {

});

describe('makeRecentSgvFilter_()', function () {
function sgv(date) {
return {type: 'sgv', date: date};
}
function pumpStatus(date) {
return {type: 'pump_status', date: date};
}

it('should return a stateful filter which discards sgvs older than the most recent one seen', function() {
var filter = mmconnect.makeRecentSgvFilter_();

filter([2, 3, 4].map(sgv)).length.should.equal(3);

filter([2, 3, 4].map(sgv)).length.should.equal(0);

var filtered = filter([1, 2, 3, 4, 5, 6].map(sgv));
filtered.length.should.equal(2);
_.pluck(filtered, 'date').should.containEql(5);
_.pluck(filtered, 'date').should.containEql(6);
});

it('should return a stateful filter which allows non-sgv entries to be old', function() {
var filter = mmconnect.makeRecentSgvFilter_();

filter([2, 3, 4].map(sgv)).length.should.equal(3);

filter([1, 2, 3, 4, 5].map(pumpStatus)).length.should.equal(5);

var filtered = filter(
[1, 2, 3, 4, 5].map(pumpStatus).concat(
[3, 4, 5, 6, 7].map(sgv)
)
);
_.filter(filtered, {type: 'pump_status'}).length.should.equal(5);
_.filter(filtered, {type: 'sgv'}).length.should.equal(3);
});
});

describe('rawDataEntry_()', function () {
it('should generate a "carelink_raw" entry with sgs truncated and PII redacted', function () {
var data = {
Expand Down

0 comments on commit b657c8b

Please sign in to comment.