-
Notifications
You must be signed in to change notification settings - Fork 71.7k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #452 from nightscout/wip/enable-iob
Add an option to enable IOB in the mainline
- Loading branch information
Showing
17 changed files
with
431 additions
and
42 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,6 +1,9 @@ | ||
bower_components/ | ||
node_modules/ | ||
|
||
|
||
bundle/bundle.out.js | ||
|
||
.idea/ | ||
*.iml | ||
my.env | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
(function () { | ||
|
||
window.Nightscout = window.Nightscout || {}; | ||
|
||
window.Nightscout = { | ||
iob: require('../lib/iob')() | ||
}; | ||
|
||
console.info("Nightscout bundle ready", window.Nightscout); | ||
|
||
})(); | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
'use strict'; | ||
|
||
var browserify_express = require('browserify-express'); | ||
|
||
function bundle() { | ||
return browserify_express({ | ||
entry: __dirname + '/bundle.source.js', | ||
watch: [__dirname + '../lib/', __dirname + '/bundle.source.js'], | ||
mount: '/public/js/bundle.js', | ||
verbose: true, | ||
//minify: true, | ||
bundle_opts: { debug: true }, // enable inline sourcemap on js files | ||
write_file: __dirname + '/bundle.out.js' | ||
}); | ||
} | ||
|
||
module.exports = bundle; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
'use strict'; | ||
|
||
var consts = require('../../constants'); | ||
|
||
function configure (app, wares, profile) { | ||
var express = require('express'), | ||
api = express.Router( ); | ||
|
||
// invoke common middleware | ||
api.use(wares.sendJSONStatus); | ||
// text body types get handled as raw buffer stream | ||
api.use(wares.bodyParser.raw( )); | ||
// json body types get handled as parsed json | ||
api.use(wares.bodyParser.json( )); | ||
// also support url-encoded content-type | ||
api.use(wares.bodyParser.urlencoded({ extended: true })); | ||
|
||
// List settings available | ||
api.get('/profile/', function(req, res) { | ||
profile.list(function (err, attribute) { | ||
return res.json(attribute); | ||
}); | ||
}); | ||
|
||
return api; | ||
} | ||
|
||
module.exports = configure; | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,80 @@ | ||
'use strict'; | ||
|
||
function calcTotal(treatments, profile, time) { | ||
|
||
console.info("trying to calc"); | ||
|
||
var iob = 0 | ||
, activity = 0; | ||
|
||
if (!treatments) return {}; | ||
|
||
if (profile === undefined) { | ||
//if there is no profile default to 3 hour dia | ||
profile = {dia: 3, sens: 0}; | ||
} | ||
|
||
if (time === undefined) { | ||
time = new Date(); | ||
} | ||
|
||
treatments.forEach(function (treatment) { | ||
if (new Date(treatment.created_at) < time) { | ||
var tIOB = calcTreatment(treatment, profile, time); | ||
if (tIOB && tIOB.iobContrib) iob += tIOB.iobContrib; | ||
if (tIOB && tIOB.activityContrib) activity += tIOB.activityContrib; | ||
} | ||
}); | ||
|
||
return { | ||
iob: iob, | ||
display: iob.toFixed(2), | ||
activity: activity | ||
}; | ||
} | ||
|
||
function calcTreatment(treatment, profile, time) { | ||
|
||
var dia = profile.dia | ||
, scaleFactor = 3.0 / dia | ||
, peak = 75 | ||
, sens = profile.sens | ||
, iobContrib = 0 | ||
, activityContrib = 0; | ||
|
||
if (treatment.insulin) { | ||
var bolusTime = new Date(treatment.created_at); | ||
var minAgo = scaleFactor * (time - bolusTime) / 1000 / 60; | ||
|
||
if (minAgo < peak) { | ||
var x = minAgo / 5 + 1; | ||
iobContrib = treatment.insulin * (1 - 0.001852 * x * x + 0.001852 * x); | ||
activityContrib = sens * treatment.insulin * (2 / dia / 60 / peak) * minAgo; | ||
|
||
} else if (minAgo < 180) { | ||
var x = (minAgo - 75) / 5; | ||
iobContrib = treatment.insulin * (0.001323 * x * x - .054233 * x + .55556); | ||
activityContrib = sens * treatment.insulin * (2 / dia / 60 - (minAgo - peak) * 2 / dia / 60 / (60 * dia - peak)); | ||
} else { | ||
iobContrib = 0; | ||
activityContrib = 0; | ||
} | ||
|
||
} | ||
|
||
return { | ||
iobContrib: iobContrib, | ||
activityContrib: activityContrib | ||
}; | ||
|
||
} | ||
|
||
function IOB(opts) { | ||
|
||
return { | ||
calcTotal: calcTotal | ||
}; | ||
|
||
} | ||
|
||
module.exports = IOB; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
'use strict'; | ||
|
||
function configure (collection, storage) { | ||
|
||
function create (obj, fn) { | ||
obj.created_at = (new Date( )).toISOString( ); | ||
api( ).insert(obj, function (err, doc) { | ||
fn(null, doc); | ||
}); | ||
} | ||
|
||
function list (fn) { | ||
return api( ).find({ }).sort({created_at: -1}).toArray(fn); | ||
} | ||
|
||
function api ( ) { | ||
return storage.pool.db.collection(collection); | ||
} | ||
|
||
api.list = list; | ||
api.create = create; | ||
return api; | ||
} | ||
module.exports = configure; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.