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

Cgm delta fix #6848

Merged
merged 4 commits into from
Feb 8, 2021
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
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