Skip to content

Commit

Permalink
Cgm delta fix (nightscout#6848)
Browse files Browse the repository at this point in the history
* If CGM value has changed, push it to server

* Fix VScode overeager code add

* Merge updated CGM data into client dataset
  • Loading branch information
sulkaharo authored and lpsuerj committed Apr 17, 2021
1 parent 3870e99 commit 2581885
Show file tree
Hide file tree
Showing 3 changed files with 51 additions and 11 deletions.
41 changes: 32 additions & 9 deletions lib/client/receiveddata.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,25 +7,34 @@ var TWO_DAYS = 172800000;
function mergeDataUpdate (isDelta, cachedDataArray, receivedDataArray, maxAge) {

function nsArrayDiff (oldArray, newArray) {
var seen = [];
var knownMills = [];

var l = oldArray.length;

for (var i = 0; i < l; i++) {
/* eslint-disable security/detect-object-injection */ // verified false positive
if (oldArray[i] !== null) {
seen.push(oldArray[i].mills);
knownMills.push(oldArray[i].mills);
}
/* eslint-enable security/detect-object-injection */ // verified false positive
}

var result = [];
var result = {
updates: [],
new: []
};

l = newArray.length;
for (var j = 0; j < l; j++) {
/* eslint-disable security/detect-object-injection */ // verified false positive
if (!seen.includes(newArray[j].mills)) {
result.push(newArray[j]); //console.log('delta data found');
var item = newArray[j];
var millsSeen = knownMills.includes(item.mills);

if (!millsSeen) {
result.new.push(item);
} else {
result.updates.push(item);
}
/* eslint-enable security/detect-object-injection */ // verified false positive
}
return result;
}
Expand All @@ -44,17 +53,31 @@ function mergeDataUpdate (isDelta, cachedDataArray, receivedDataArray, maxAge) {
var mAge = (isNaN(maxAge) || maxAge == null) ? TWO_DAYS : maxAge;
var twoDaysAgo = new Date().getTime() - mAge;

for (var i = 0; i < cachedDataArray.length; i++) {
for (var i = cachedDataArray.length -1; i >= 0; i--) {
/* eslint-disable-next-line security/detect-object-injection */ // verified false positive
var element = cachedDataArray[i];
if (element !== null && element !== undefined && element.mills <= twoDaysAgo) {
cachedDataArray.splice(i, 0);
cachedDataArray.splice(i, 1);
}
}

// If this is delta, calculate the difference, merge and sort
var diff = nsArrayDiff(cachedDataArray, receivedDataArray);
return cachedDataArray.concat(diff).sort(function(a, b) {

// if there's updated elements, replace those in place
if (diff.updates.length > 0) {
for (var i = 0; i < diff.updates.length; i++) {
var element = diff.updates[i];
for (var j = 0; j < cachedDataArray.length; j++) {
if (element.mills == cachedDataArray[j].mills) {
cachedDataArray.splice(j,1,element);
}
}
}
}

// merge new items in
return cachedDataArray.concat(diff.new).sort(function(a, b) {
return a.mills - b.mills;
});
}
Expand Down
11 changes: 9 additions & 2 deletions lib/data/calcdelta.js
Original file line number Diff line number Diff line change
Expand Up @@ -66,16 +66,23 @@ module.exports = function calcDelta (oldData, newData) {
return result;
}

function genKey(o) {
let r = o.mills;
r += o.sgv ? 'sgv' + o.sgv : '';
r += o.mgdl ? 'sgv' + o.mgdl : '';
return r;
}

function nsArrayDiff(oldArray, newArray) {
var seen = {};
var l = oldArray.length;
for (var i = 0; i < l; i++) {
seen[oldArray[i].mills] = true;
seen[genKey(oldArray[i])] = true;
}
var result = [];
l = newArray.length;
for (var j = 0; j < l; j++) {
if (!Object.prototype.hasOwnProperty.call(seen, newArray[j].mills)) {
if (!Object.prototype.hasOwnProperty.call(seen, genKey(newArray[j]))) {
result.push(newArray[j]);
}
}
Expand Down
10 changes: 10 additions & 0 deletions tests/data.calcdelta.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,16 @@ describe('Data', function ( ) {
delta.sgvs.length.should.equal(1);
});

it('should update sgv if changed', function() {
var ddata = require('../lib/data/ddata')();
ddata.sgvs = [{mgdl: 100, mills: before},{mgdl: 100, mills: now}];
var newData = ddata.clone();
newData.sgvs = [{mgdl: 110, mills: before},{mgdl: 100, mills: now}];
var delta = calcDelta(ddata,newData);
delta.delta.should.equal(true);
delta.sgvs.length.should.equal(1);
});

it('adding one treatment record should return delta with one treatment', function() {
var ddata = require('../lib/data/ddata')();
ddata.treatments = [{_id: 'someid_1', mgdl: 100, mills: before},{_id: 'someid_2', mgdl: 100, mills: now}];
Expand Down

0 comments on commit 2581885

Please sign in to comment.