From 8148c5fa2cbd22abda083d0e19942dc3c41b3ea0 Mon Sep 17 00:00:00 2001 From: Jason Calabrese Date: Sun, 29 May 2016 13:00:00 -0700 Subject: [PATCH 1/2] fix/cleanup some things with SAGE/CAGE/IAGE and add a SAGE test --- lib/plugins/cannulaage.js | 2 +- lib/plugins/insulinage.js | 2 +- lib/plugins/sensorage.js | 35 +++++--- tests/sensorage.test.js | 162 ++++++++++++++++++++++++++++++++++++++ 4 files changed, 188 insertions(+), 13 deletions(-) create mode 100644 tests/sensorage.test.js diff --git a/lib/plugins/cannulaage.js b/lib/plugins/cannulaage.js index 9d2d664cffc..111bc3ff707 100644 --- a/lib/plugins/cannulaage.js +++ b/lib/plugins/cannulaage.js @@ -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; diff --git a/lib/plugins/insulinage.js b/lib/plugins/insulinage.js index dc1c60dc9f0..74e6a343c69 100644 --- a/lib/plugins/insulinage.js +++ b/lib/plugins/insulinage.js @@ -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; diff --git a/lib/plugins/sensorage.js b/lib/plugins/sensorage.js index 2630536207c..77d6b786d9b 100644 --- a/lib/plugins/sensorage.js +++ b/lib/plugins/sensorage.js @@ -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 diff --git a/tests/sensorage.test.js b/tests/sensorage.test.js new file mode 100644 index 00000000000..97b8e2d321c --- /dev/null +++ b/tests/sensorage.test.js @@ -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(); + }); + +}); From 299a958cc3c10bf6e3d7271bbb0a42f4dfe4262b Mon Sep 17 00:00:00 2001 From: Jason Calabrese Date: Sun, 29 May 2016 16:32:58 -0700 Subject: [PATCH 2/2] renamed Sendor Change to Sensor Insert --- lib/language.js | 2 +- lib/plugins/careportal.js | 2 +- lib/plugins/sensorage.js | 3 ++- tests/sensorage.test.js | 6 +++--- 4 files changed, 7 insertions(+), 6 deletions(-) diff --git a/lib/language.js b/lib/language.js index 6eacd778815..cadc41fd799 100644 --- a/lib/language.js +++ b/lib/language.js @@ -4087,7 +4087,7 @@ function init() { ,sk: 'Spustenie senzoru' ,nl: 'CGM Sensor start' } - ,'CGM Sensor Change' : { + ,'CGM Sensor Insert' : { cs: 'Výměna sensoru' ,de: 'CGM Sensor Wechsel' ,es: 'Cambio de sensor' diff --git a/lib/plugins/careportal.js b/lib/plugins/careportal.js index ab979102f84..15d47ce598f 100644 --- a/lib/plugins/careportal.js +++ b/lib/plugins/careportal.js @@ -66,7 +66,7 @@ function init() { , bg: true, insulin: false, carbs: false, prebolus: false, duration: false, percent: false, absolute: false, profile: false, split: false } , { val: 'Sensor Change' - , name: 'CGM Sensor Change' + , name: 'CGM Sensor Insert' , bg: true, insulin: false, carbs: false, prebolus: false, duration: false, percent: false, absolute: false, profile: false, split: false } , { val: 'Insulin Change' diff --git a/lib/plugins/sensorage.js b/lib/plugins/sensorage.js index 77d6b786d9b..c1ab19207dc 100644 --- a/lib/plugins/sensorage.js +++ b/lib/plugins/sensorage.js @@ -157,7 +157,8 @@ sage.updateVisualisation = function updateVisualisation (sbx) { ['Sensor Change', 'Sensor Start'].forEach( function eachEvent(event) { if (latest[event].found) { - info.push( { label: event, value: new Date(latest[event].treatmentDate).toLocaleString() } ); + var label = event === 'Sensor Change' ? 'Sensor Insert' : event; + info.push( { label: label, value: new Date(latest[event].treatmentDate).toLocaleString() } ); shownAge = ''; if (latest[event].age > 24) { shownAge += latest[event].days + ' days '; diff --git a/tests/sensorage.test.js b/tests/sensorage.test.js index 97b8e2d321c..d320d33ed68 100644 --- a/tests/sensorage.test.js +++ b/tests/sensorage.test.js @@ -31,7 +31,7 @@ describe('sage', function ( ) { , pluginBase: { updatePillText: function mockedUpdatePillText(plugin, options) { options.value.should.equal('3d0h'); - options.info[0].label.should.equal('Sensor Change'); + options.info[0].label.should.equal('Sensor Insert'); 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'); @@ -86,7 +86,7 @@ describe('sage', function ( ) { , pluginBase: { updatePillText: function mockedUpdatePillText(plugin, options) { options.value.should.equal('3d0h'); - options.info[0].label.should.equal('Sensor Change'); + options.info[0].label.should.equal('Sensor Insert'); options.info[1].should.match({ label: 'Duration', value: '3 days 0 hours' }); options.info[2].should.match({ label: 'Notes:', value: 'Foo' }); done(); @@ -114,7 +114,7 @@ describe('sage', function ( ) { 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[0].label.should.equal('Sensor Insert'); options.info[1].should.match({ label: 'Duration', value: '3 days 0 hours' }); options.info[2].should.match({ label: 'Notes:', value: 'Foo' }); done();