Skip to content

Commit

Permalink
don't let future data block the current
Browse files Browse the repository at this point in the history
  • Loading branch information
jasoncalabrese committed Jul 6, 2015
1 parent 77c6f80 commit 556091b
Show file tree
Hide file tree
Showing 4 changed files with 29 additions and 15 deletions.
10 changes: 7 additions & 3 deletions lib/plugins/treatmentnotify.js
Expand Up @@ -17,15 +17,19 @@ function init() {

var now = Date.now();

var lastMBG = _.last(sbx.data.mbgs);
var lastTreatment = _.last(sbx.data.treatments);
function notInTheFuture (entry) {
return entry.x <= now;
}

var lastMBG = _.findLast(sbx.data.mbgs, notInTheFuture);
var lastTreatment = _.findLast(sbx.data.treatments, notInTheFuture);

//TODO: figure out why date is x here #CleanUpDataModel
var lastMBGTime = lastMBG ? lastMBG.x : 0;
var mbgAgo = (lastMBGTime && lastMBGTime <= now) ? now - lastMBGTime : -1;
var mbgCurrent = mbgAgo !== -1 && mbgAgo < TIME_10_MINS_MS;

var lastTreatmentTime = lastTreatment ? new Date(lastTreatment.created_at).getTime() : 0;
var lastTreatmentTime = lastTreatment ? lastTreatment.x : 0;
var treatmentAgo = (lastTreatmentTime && lastTreatmentTime <= now) ? now - lastTreatmentTime : -1;
var treatmentCurrent = treatmentAgo !== -1 && treatmentAgo < TIME_10_MINS_MS;

Expand Down
10 changes: 8 additions & 2 deletions lib/sandbox.js
Expand Up @@ -113,13 +113,19 @@ function init ( ) {
}
};

sbx.lastSGVEntry = function lastSGVEntry ( ) {
return _.findLast(sbx.data.sgvs, function notInTheFuture (entry) {
return entry.x <= sbx.time;
});
};

sbx.lastSGV = function lastSGV ( ) {
var last = _.last(sbx.data.sgvs);
var last = sbx.lastSGVEntry();
return last && last.y;
};

sbx.lastSGVMills = function lastSGVMills ( ) {
var last = _.last(sbx.data.sgvs);
var last = sbx.lastSGVEntry();
return (last && last.x) || (last && last.date && (new Date(last.date)).getTime()) || 0;
};

Expand Down
10 changes: 6 additions & 4 deletions tests/sandbox.test.js
Expand Up @@ -3,6 +3,8 @@ var should = require('should');
describe('sandbox', function ( ) {
var sandbox = require('../lib/sandbox')();

var now = Date.now();

it('init on client', function (done) {
var app = {
thresholds:{
Expand All @@ -18,7 +20,7 @@ describe('sandbox', function ( ) {
};

var pluginBase = {};
var data = {sgvs: [{y: 100}]};
var data = {sgvs: [{y: 100, x: now}]};

var sbx = sandbox.clientInit(app, clientSettings, Date.now(), pluginBase, data);

Expand All @@ -40,7 +42,7 @@ describe('sandbox', function ( ) {

it('init on server', function (done) {
var sbx = createServerSandbox();
sbx.data.sgvs = [{y: 100}];
sbx.data.sgvs = [{y: 100, x: now}];

should.exist(sbx.notifications.requestNotify);
should.not.exist(sbx.notifications.process);
Expand All @@ -61,7 +63,7 @@ describe('sandbox', function ( ) {

it('build BG Now line using properties', function ( ) {
var sbx = createServerSandbox();
sbx.data.sgvs = [{y: 99}];
sbx.data.sgvs = [{y: 99, x: now}];
sbx.properties = { delta: {display: '+5' }, direction: {value: 'FortyFiveUp', label: '↗', entity: '&#8599;'} };

sbx.buildBGNowLine().should.equal('BG Now: 99 +5 ↗ mg/dl');
Expand All @@ -70,7 +72,7 @@ describe('sandbox', function ( ) {

it('build default message using properties', function ( ) {
var sbx = createServerSandbox();
sbx.data.sgvs = [{y: 99}];
sbx.data.sgvs = [{y: 99, x: now}];
sbx.properties = {
delta: {display: '+5' }
, direction: {value: 'FortyFiveUp', label: '↗', entity: '&#8599;'}
Expand Down
14 changes: 8 additions & 6 deletions tests/treatmentnotify.test.js
Expand Up @@ -10,10 +10,12 @@ describe('treatmentnotify', function ( ) {
ctx.data = require('../lib/data')(env, ctx);
ctx.notifications = require('../lib/notifications')(env, ctx);

var now = Date.now();

it('Request a snooze for a recent treatment and request an info notify', function (done) {
ctx.notifications.initRequests();
ctx.data.sgvs = [{sgv: 100}];
ctx.data.treatments = [{eventType: 'BG Check', glucose: '100', created_at: (new Date()).toISOString()}];
ctx.data.sgvs = [{x: now, y: 100}];
ctx.data.treatments = [{eventType: 'BG Check', glucose: '100', x: now}];

var sbx = require('../lib/sandbox')().serverInit(env, ctx);
treatmentnotify.checkNotifications(sbx);
Expand All @@ -27,8 +29,8 @@ describe('treatmentnotify', function ( ) {

it('Not Request a snooze for an older treatment and not request an info notification', function (done) {
ctx.notifications.initRequests();
ctx.data.sgvs = [{sgv: 100}];
ctx.data.treatments = [{created_at: (new Date(Date.now() - (15 * 60 * 1000))).toISOString()}];
ctx.data.sgvs = [{x: now, y: 100}];
ctx.data.treatments = [{x: Date.now() - (15 * 60 * 1000)}];

var sbx = require('../lib/sandbox')().serverInit(env, ctx);
treatmentnotify.checkNotifications(sbx);
Expand All @@ -42,7 +44,7 @@ describe('treatmentnotify', function ( ) {

it('Request a snooze for a recent calibration and request an info notify', function (done) {
ctx.notifications.initRequests();
ctx.data.sgvs = [{sgv: 100}];
ctx.data.sgvs = [{x: now, y: 100}];
ctx.data.mbgs = [{y: '100', x: Date.now()}];

var sbx = require('../lib/sandbox')().serverInit(env, ctx);
Expand All @@ -57,7 +59,7 @@ describe('treatmentnotify', function ( ) {

it('Not Request a snooze for an older calibration treatment and not request an info notification', function (done) {
ctx.notifications.initRequests();
ctx.data.sgvs = [{sgv: 100}];
ctx.data.sgvs = [{x: now, y: 100}];
ctx.data.mbgs = [{y: '100', x: Date.now() - (15 * 60 * 1000)}];

var sbx = require('../lib/sandbox')().serverInit(env, ctx);
Expand Down

0 comments on commit 556091b

Please sign in to comment.