Skip to content

Commit

Permalink
Revert "Revert to c1b2988 (Merge branch 'hotfix/0.3.5')"
Browse files Browse the repository at this point in the history
This reverts commit 1cc9c61.
  • Loading branch information
Jason Calabrese committed Aug 31, 2014
1 parent 1cc9c61 commit 0404905
Show file tree
Hide file tree
Showing 35 changed files with 882 additions and 180 deletions.
5 changes: 4 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -8,5 +8,8 @@ my.env
*.env
static/bower_components/
.*.sw?
.DS_Store

.vagrant
.vagrant
/iisnode
/web.config
1 change: 1 addition & 0 deletions Procfile
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
web: node server.js
2 changes: 2 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@ cgm-remote-monitor (a.k.a. NightScout)
[![Dependency Status](https://david-dm.org/nightscout/cgm-remote-monitor.png)](https://david-dm.org/nightscout/cgm-remote-monitor)
[![Gitter chat](https://badges.gitter.im/nightscout.png)](https://gitter.im/nightscout/public)

[![Deploy to Heroku](https://www.herokucdn.com/deploy/button.png)](https://heroku.com/deploy)

This acts as a web-based CGM (Continuous Glucose Montinor) to allow
multiple caregivers to remotely view a patients glucose data in
realtime. The server reads a MongoDB which is intended to be data
Expand Down
13 changes: 13 additions & 0 deletions app.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
{
"name": "CGM Remote Monitor",
"repository": "https://github.com/nightscout/cgm-remote-monitor",
"env": {
"MONGO_COLLECTION": {
"description": "The mongo collection to connect to.",
"value": "nightscout"
}
},
"addons": [
"mongolab"
]
}
11 changes: 11 additions & 0 deletions bin/post-treatment.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
#!/bin/sh

curl -H "Content-Type: application/json" -XPOST 'http://localhost:1337/api/v1/treatments/' -d '{
"enteredBy": "Dad",
"eventType":"Site Change",
"glucoseValue": 322,
"glucoseType": "sensor",
"carbsGiven": 0,
"insulinGiven": 1.25,
"notes": "Argh..."
}'
2 changes: 2 additions & 0 deletions bower.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@
"name": "nightscout",
"version": "0.3.5",
"dependencies": {
"angularjs": "1.3.0-beta.19",
"bootstrap": "~3.2.0",
"d3": "3.4.3",
"jquery": "2.1.0",
"jQuery-Storage-API": "~1.7.2",
Expand Down
5 changes: 3 additions & 2 deletions env.js
Original file line number Diff line number Diff line change
Expand Up @@ -23,9 +23,10 @@ function config ( ) {
env.name = software.name;
env.DISPLAY_UNITS = process.env.DISPLAY_UNITS || 'mg/dl';
env.PORT = process.env.PORT || 1337;
env.mongo = process.env.MONGO_CONNECTION || process.env.CUSTOMCONNSTR_mongo;
env.mongo_collection = process.env.CUSTOMCONNSTR_mongo_collection || 'entries';
env.mongo = process.env.MONGO_CONNECTION || process.env.CUSTOMCONNSTR_mongo || process.env.MONGOLAB_URI;
env.mongo_collection = process.env.CUSTOMCONNSTR_mongo_collection || process.env.MONGO_COLLECTION || 'entries';
env.settings_collection = process.env.CUSTOMCONNSTR_mongo_settings_collection || 'settings';
env.treatments_collection = process.env.CUSTOMCONNSTR_mongo_treatments_collection || 'treatments';
var shasum = crypto.createHash('sha1');
var useSecret = (process.env.API_SECRET && process.env.API_SECRET.length > 0);
env.api_secret = null;
Expand Down
3 changes: 2 additions & 1 deletion lib/api/index.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
'use strict';

function create (env, entries, settings) {
function create (env, entries, settings, treatments) {
var express = require('express'),
app = express( )
;
Expand Down Expand Up @@ -29,6 +29,7 @@ function create (env, entries, settings) {
// Entries and settings
app.use('/', require('./entries/')(app, wares, entries));
app.use('/', require('./settings/')(app, wares, settings));
app.use('/', require('./treatments/')(app, wares, treatments));

// Status
app.use('/', require('./status')(app, wares));
Expand Down
47 changes: 47 additions & 0 deletions lib/api/treatments/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
'use strict';

var consts = require('../../constants');

function configure (app, wares, treatments) {
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('/treatments/', function(req, res) {
treatments.list(function (err, profiles) {
return res.json(profiles);
});
});

function config_authed (app, api, wares, treatments) {

api.post('/treatments/', /*TODO: auth disabled for quick UI testing... wares.verifyAuthorization, */ function(req, res) {
var treatment = req.body;
treatments.create(treatment, function (err, created) {
if (err)
res.sendJSONStatus(res, consts.HTTP_INTERNAL_ERROR, 'Mongo Error', err);
else
res.json(created);
});
});

}

if (app.enabled('api') || true /*TODO: auth disabled for quick UI testing...*/) {
config_authed(app, api, wares, treatments);
}

return api;
}

module.exports = configure;

12 changes: 9 additions & 3 deletions lib/entries.js
Original file line number Diff line number Diff line change
Expand Up @@ -92,10 +92,16 @@ function entries (name, storage) {
with_collection(function(err, collection) {
if (err) { fn(err); return; }
// potentially a batch insert
collection.insert(docs, function (err, created) {
// execute the callback
fn(err, created, docs);
var firstErr = null,
totalCreated = 0;

docs.forEach(function(doc) {
collection.update(doc, doc, {upsert: true}, function (err, created) {
firstErr = firstErr || err;
totalCreated += created;
});
});
fn(firstErr, totalCreated, docs);
});
}

Expand Down
24 changes: 24 additions & 0 deletions lib/treatments.js
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;
50 changes: 38 additions & 12 deletions lib/websocket.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@

function websocket (env, server, entries) {
function websocket (env, server, entries, treatments) {
"use strict";
// CONSTANTS
var ONE_HOUR = 3600000,
Expand All @@ -24,8 +24,10 @@ var dir2Char = {
var io;
var watchers = 0;
var now = new Date().getTime();
var cgmData = [];
var patientData = [];

var cgmData = [],
treatmentData = [],
patientData = [];

function start ( ) {
io = require('socket.io').listen(server);
Expand Down Expand Up @@ -101,6 +103,7 @@ function update() {
now = Date.now();

cgmData = [];
treatmentData = [];
var earliest_data = now - TWO_DAYS;
var q = { find: {"date": {"$gte": earliest_data}} };
entries.list(q, function (err, results) {
Expand All @@ -114,8 +117,15 @@ function update() {
cgmData.push(obj);
}
});
// all done, do loadData
loadData( );
treatments.list(function (err, results) {
treatmentData = results.map(function(treatment) {
var timestamp = new Date(treatment.timestamp || treatment.created_at);
treatment.x = timestamp.getTime();
return treatment;
});
// all done, do loadData
loadData( );
});
});

return update;
Expand All @@ -133,23 +143,37 @@ function emitAlarm(alarmType) {
function loadData() {

console.log('running loadData');
var treatment = [];
var mbg = [];

var actual = [];
var actual = [],
actualCurrent,
treatment = [],
errorCode;

if (cgmData) {
actual = cgmData.slice();
actual.sort(function(a, b) {
return a.x - b.x;
});

actualCurrent = actual.length > 0 ? actual[actual.length - 1].y : null;

// sgv less than or equal to 10 means error code
// or warm up period code, so ignore
actual = actual.filter(function (a) {
return a.y > 10;
})
}

if (treatmentData) {
treatment = treatmentData.slice();
treatment.sort(function(a, b) {
return a.x - b.x;
});
}

if (actualCurrent && actualCurrent < 39) errorCode = actualCurrent;

var actualLength = actual.length - 1;

if (actualLength > 1) {
Expand Down Expand Up @@ -181,8 +205,8 @@ function loadData() {
//TODO: need to consider when data being sent has less than the 2 day minimum

// consolidate and send the data to the client
var shouldEmit = is_different(actual, predicted, mbg, treatment);
patientData = [actual, predicted, mbg, treatment];
var shouldEmit = is_different(actual, predicted, mbg, treatment, errorCode);
patientData = [actual, predicted, mbg, treatment, errorCode];
console.log('patientData', patientData.length);
if (shouldEmit) {
emitData( );
Expand All @@ -195,16 +219,16 @@ function loadData() {
avgLoss += 1 / size * Math.pow(log10(predicted[j].y / 120), 2);
}

//console.log(alarms['urgent_alarm'].threshold);
//console.log(alarms['alarm'].threshold);
if (avgLoss > alarms['urgent_alarm'].threshold) {
emitAlarm('urgent_alarm');
} else if (avgLoss > alarms['alarm'].threshold) {
emitAlarm('alarm');
} else if (errorCode) {
emitAlarm('urgent_alarm');
}
}
}
function is_different (actual, predicted, mbg, treatment) {
function is_different (actual, predicted, mbg, treatment, errorCode) {
if (patientData && patientData.length < 3) {
return true;
}
Expand All @@ -213,12 +237,14 @@ function loadData() {
, predicted: patientData[1].slice(-1).pop( )
, mbg: patientData[2].slice(-1).pop( )
, treatment: patientData[3].slice(-1).pop( )
, errorCode: patientData.length >= 5 ? patientData[4] : 0
};
var last = {
actual: actual.slice(-1).pop( )
, predicted: predicted.slice(-1).pop( )
, mbg: mbg.slice(-1).pop( )
, treatment: treatment.slice(-1).pop( )
, errorCode: errorCode
};

// textual diff of objects
Expand Down
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@
"express": "^4.6.1",
"express-extension-to-accept": "0.0.2",
"mongodb": "^1.4.7",
"moment": "2.8.1",
"sgvdata": "0.0.2",
"socket.io": "^0.9.17"
},
Expand Down
9 changes: 5 additions & 4 deletions server.js
Original file line number Diff line number Diff line change
Expand Up @@ -31,15 +31,15 @@ var express = require('express');
///////////////////////////////////////////////////
var entries = require('./lib/entries')(env.mongo_collection, store);
var settings = require('./lib/settings')(env.settings_collection, store);
var api = require('./lib/api/')(env, entries, settings);
var treatments = require('./lib/treatments')(env.treatments_collection, store);
var api = require('./lib/api/')(env, entries, settings, treatments);
var pebble = require('./lib/pebble');
///////////////////////////////////////////////////

///////////////////////////////////////////////////
// setup http server
///////////////////////////////////////////////////
var PORT = env.PORT;
var THIRTY_DAYS = 2592000;

var app = express();
var appInfo = software.name + ' ' + software.version;
Expand All @@ -57,7 +57,8 @@ app.get('/pebble', pebble(entries));
//app.get('/package.json', software);

// define static server
var staticFiles = express.static(env.static_files, {maxAge: THIRTY_DAYS * 1000});
//TODO: JC - changed cache to 1 hour from 30d ays to bypass cache hell until we have a real solution
var staticFiles = express.static(env.static_files, {maxAge: 60 * 60 * 1000});

// serve the static content
app.use(staticFiles);
Expand All @@ -76,7 +77,7 @@ store(function ready ( ) {
// setup socket io for data and message transmission
///////////////////////////////////////////////////
var websocket = require('./lib/websocket');
var io = websocket(env, server, entries);
var io = websocket(env, server, entries, treatments);
});

///////////////////////////////////////////////////
Loading

0 comments on commit 0404905

Please sign in to comment.