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

fix/cleanup some things with SAGE/CAGE/IAGE and add a SAGE test #1782

Merged
merged 2 commits into from
May 29, 2016
Merged
Changes from 1 commit
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
Next Next commit
fix/cleanup some things with SAGE/CAGE/IAGE and add a SAGE test
jasoncalabrese committed May 29, 2016

Verified

This commit was created on GitHub.com and signed with GitHub’s verified signature. The key has expired.
commit 8148c5fa2cbd22abda083d0e19942dc3c41b3ea0
2 changes: 1 addition & 1 deletion lib/plugins/cannulaage.js
Original file line number Diff line number Diff line change
@@ -88,7 +88,7 @@ cage.findLatestTimeChange = function findLatestTimeChange (sbx) {
sendNotification = cannulaInfo.age === prefs.warn;
message = 'Time to change cannula';
cannulaInfo.level = levels.WARN;
} else if (cannulaInfo.age > prefs.info) {
} else if (cannulaInfo.age >= prefs.info) {
sendNotification = cannulaInfo.age === prefs.info;
message = 'Change cannula soon';
cannulaInfo.level = levels.INFO;
2 changes: 1 addition & 1 deletion lib/plugins/insulinage.js
Original file line number Diff line number Diff line change
@@ -86,7 +86,7 @@ iage.findLatestTimeChange = function findLatestTimeChange(sbx) {
sendNotification = insulinInfo.age === prefs.warn;
message = 'Time to change insulin reservoir';
insulinInfo.level = levels.WARN;
} else if (insulinInfo.age > prefs.info) {
} else if (insulinInfo.age >= prefs.info) {
sendNotification = insulinInfo.age === prefs.info;
message = 'Change insulin reservoir soon';
insulinInfo.level = levels.INFO;
35 changes: 24 additions & 11 deletions lib/plugins/sensorage.js
Original file line number Diff line number Diff line change
@@ -38,15 +38,23 @@ sage.checkNotifications = function checkNotifications(sbx) {
};

function minButValid(record) {
if (!record['Sensor Start'].found && record['Sensor Change'].found) {
return 'Sensor Change';
} else if (!record['Sensor Change'].found && record['Sensor Start'].found) {
return 'Sensor Start';
} else if (record['Sensor Change'].treatmentDate >= record['Sensor Start'].treatmentDate) {
return 'Sensor Change';
} else {
return 'Sensor Start';
var events = [ ];

var start = record['Sensor Start'];
if (start && start.found) {
events.push({eventType: 'Sensor Start', treatmentDate: start.treatmentDate});
}

var change = record['Sensor Change'];
if (change && change.found) {
events.push({eventType: 'Sensor Change', treatmentDate: change.treatmentDate});
}

var sorted = _.sortBy(events, 'treatmentDate');

var mostRecent = _.last(sorted);

return (mostRecent && mostRecent.eventType) || 'Sensor Start';
}

sage.findLatestTimeChange = function findLatestTimeChange(sbx) {
@@ -91,6 +99,11 @@ sage.findLatestTimeChange = function findLatestTimeChange(sbx) {
}
});
});

if (returnValue['Sensor Change'].found && returnValue['Sensor Start'].found &&
returnValue['Sensor Change'].treatmentDate >= returnValue['Sensor Start'].treatmentDate) {
returnValue['Sensor Start'].found = false;
}

returnValue.min = minButValid(returnValue);

@@ -103,12 +116,12 @@ sage.findLatestTimeChange = function findLatestTimeChange(sbx) {

sensorInfo.level = levels.NONE;

if (sensorInfo.age > prefs.urgent) {
if (sensorInfo.age >= prefs.urgent) {
sendNotification = sensorInfo.age === prefs.urgent;
message = 'Sensor change/restart overdue!';
sound = 'persistent';
sensorInfo.level = levels.URGENT;
} else if (sensorInfo.age > prefs.warn) {
} else if (sensorInfo.age >= prefs.warn) {
sendNotification = sensorInfo.age === prefs.warn;
message = 'Time to change/restart sensor';
sensorInfo.level = levels.WARN;
@@ -120,7 +133,7 @@ sage.findLatestTimeChange = function findLatestTimeChange(sbx) {

if (prefs.enableAlerts && sendNotification && sensorInfo.minFractions <= 30) {
sensorInfo.notification = {
title: 'Sensor age ' + sensorInfo.days + ' days' + sensorInfo.hours +' hours'
title: 'Sensor age ' + sensorInfo.days + ' days ' + sensorInfo.hours + ' hours'
, message: message
, pushoverSound: sound
, level: sensorInfo.level
162 changes: 162 additions & 0 deletions tests/sensorage.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,162 @@
'use strict';

var should = require('should');
var times = require('../lib/times');
var levels = require('../lib/levels');

describe('sage', function ( ) {
var sage = require('../lib/plugins/sensorage')();
var sandbox = require('../lib/sandbox')();
var env = require('../env')();
var ctx = {};
ctx.ddata = require('../lib/data/ddata')();
ctx.notifications = require('../lib/notifications')(env, ctx);

function prepareSandbox ( ) {
var sbx = require('../lib/sandbox')().serverInit(env, ctx);
return sbx;
}

it('set a pill to the current age since start with change', function (done) {

var data = {
sensorTreatments: [
{eventType: 'Sensor Change', notes: 'Foo', mills: Date.now() - times.days(15).msecs}
, {eventType: 'Sensor Start', notes: 'Bar', mills: Date.now() - times.days(3).msecs}
]
};

var ctx = {
settings: {}
, pluginBase: {
updatePillText: function mockedUpdatePillText(plugin, options) {
options.value.should.equal('3d0h');
options.info[0].label.should.equal('Sensor Change');
options.info[1].should.match({ label: 'Duration', value: '15 days 0 hours' });
options.info[2].should.match({ label: 'Notes:', value: 'Foo' });
options.info[3].label.should.equal('Sensor Start');
options.info[4].should.match({ label: 'Duration', value: '3 days 0 hours' });
options.info[5].should.match({ label: 'Notes:', value: 'Bar' });
done();
}
}
};

var sbx = sandbox.clientInit(ctx, Date.now(), data);
sage.updateVisualisation(sbx);

});

it('set a pill to the current age since start without change', function (done) {

var data = {
sensorTreatments: [
{eventType: 'Sensor Start', notes: 'Bar', mills: Date.now() - times.days(3).msecs}
]
};

var ctx = {
settings: {}
, pluginBase: {
updatePillText: function mockedUpdatePillText(plugin, options) {
options.value.should.equal('3d0h');
options.info[0].label.should.equal('Sensor Start');
options.info[1].should.match({ label: 'Duration', value: '3 days 0 hours' });
options.info[2].should.match({ label: 'Notes:', value: 'Bar' });
done();
}
}
};

var sbx = sandbox.clientInit(ctx, Date.now(), data);
sage.updateVisualisation(sbx);

});

it('set a pill to the current age since change without start', function (done) {

var data = {
sensorTreatments: [
{eventType: 'Sensor Change', notes: 'Foo', mills: Date.now() - times.days(3).msecs}
]
};

var ctx = {
settings: {}
, pluginBase: {
updatePillText: function mockedUpdatePillText(plugin, options) {
options.value.should.equal('3d0h');
options.info[0].label.should.equal('Sensor Change');
options.info[1].should.match({ label: 'Duration', value: '3 days 0 hours' });
options.info[2].should.match({ label: 'Notes:', value: 'Foo' });
done();
}
}
};

var sbx = sandbox.clientInit(ctx, Date.now(), data);
sage.updateVisualisation(sbx);

});

it('set a pill to the current age since change after start', function (done) {

var data = {
sensorTreatments: [
{eventType: 'Sensor Start', notes: 'Bar', mills: Date.now() - times.days(10).msecs}
, {eventType: 'Sensor Change', notes: 'Foo', mills: Date.now() - times.days(3).msecs}
]
};

var ctx = {
settings: {}
, pluginBase: {
updatePillText: function mockedUpdatePillText(plugin, options) {
options.value.should.equal('3d0h');
options.info.length.should.equal(3);
options.info[0].label.should.equal('Sensor Change');
options.info[1].should.match({ label: 'Duration', value: '3 days 0 hours' });
options.info[2].should.match({ label: 'Notes:', value: 'Foo' });
done();
}
}
};

var sbx = sandbox.clientInit(ctx, Date.now(), data);
sage.updateVisualisation(sbx);

});

it('trigger an alarm when sensor is 6 days and 22 hours old', function (done) {
ctx.notifications.initRequests();

var before = Date.now() - times.days(6).msecs - times.hours(22).msecs;

ctx.ddata.sensorTreatments = [{eventType: 'Sensor Start', mills: before}];

var sbx = prepareSandbox();
sbx.extendedSettings = { 'enableAlerts': true };
sage.checkNotifications(sbx);

var highest = ctx.notifications.findHighestAlarm('SAGE');
highest.level.should.equal(levels.URGENT);
highest.title.should.equal('Sensor age 6 days 22 hours');
done();
});

it('not trigger an alarm when sensor is 6 days and 23 hours old', function (done) {
ctx.notifications.initRequests();

var before = Date.now() - times.days(6).msecs - times.hours(23).msecs;

ctx.ddata.sensorTreatments = [{eventType: 'Sensor Start', mills: before}];

var sbx = prepareSandbox();
sbx.extendedSettings = { 'enableAlerts': true };
sage.checkNotifications(sbx);

should.not.exist(ctx.notifications.findHighestAlarm('SAGE'));
done();
});

});