Skip to content

Commit

Permalink
Merge pull request #207 from nightscout/release/0.5.0
Browse files Browse the repository at this point in the history
Cookie Monster (0.5.0)
  • Loading branch information
bewest committed Nov 6, 2014
2 parents adc24df + 6e8cacb commit 6893781
Show file tree
Hide file tree
Showing 22 changed files with 336 additions and 67 deletions.
14 changes: 14 additions & 0 deletions .gitattributes
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
# Auto detect text files and perform LF normalization
* text=auto

# Standard to msysgit
*.doc diff=astextplain
*.DOC diff=astextplain
*.docx diff=astextplain
*.DOCX diff=astextplain
*.dot diff=astextplain
*.DOT diff=astextplain
*.pdf diff=astextplain
*.PDF diff=astextplain
*.rtf diff=astextplain
*.RTF diff=astextplain
3 changes: 1 addition & 2 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -11,5 +11,4 @@ static/bower_components/
.DS_Store

.vagrant
/iisnode
/web.config
/iisnode
8 changes: 8 additions & 0 deletions bin/post-mbg.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
#!/bin/sh
# "date": "1413782506964"

curl -H "Content-Type: application/json" -H "api-secret: $API_SECRET" -XPOST 'http://localhost:1337/api/v1/entries/' -d '{
"mbg": 100,
"type": "mbg",
"date": "1413779270000"
}'
2 changes: 1 addition & 1 deletion bower.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "nightscout",
"version": "0.4.3",
"version": "0.5.0",
"dependencies": {
"angularjs": "1.3.0-beta.19",
"bootstrap": "~3.2.0",
Expand Down
12 changes: 8 additions & 4 deletions env.js
Original file line number Diff line number Diff line change
Expand Up @@ -23,10 +23,14 @@ function config ( ) {
if (readENV('SCM_GIT_EMAIL') == 'windowsazure' && readENV('ScmType') == 'GitHub') {
git.cwd('/home/site/repository');
}
git.short(function record_git_head (head) {
console.log("GIT HEAD", head);
env.head = head;
});
if (readENV('SCM_COMMIT_ID')) {
env.head = readENV('SCM_COMMIT_ID');
} else {
git.short(function record_git_head (head) {
console.log("GIT HEAD", head);
env.head = head;
});
}
env.version = software.version;
env.name = software.name;

Expand Down
2 changes: 1 addition & 1 deletion lib/api/entries/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ function configure (app, wares, entries) {
var incoming = [ ];
// Potentially a single json encoded body.
// This can happen from either an url-encoded or json content-type.
if ('sgv' in req.body) {
if ('date' in req.body) {
// add it to the incoming list
incoming.push(req.body);
}
Expand Down
18 changes: 16 additions & 2 deletions lib/devicestatus.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
'use strict';

function configure (collection, storage) {
function storage (collection, storage) {

function create (obj, fn) {
obj.created_at = (new Date( )).toISOString( );
Expand Down Expand Up @@ -31,4 +31,18 @@ function configure (collection, storage) {
api.last = last;
return api;
}
module.exports = configure;

function ensureIndexes(name, storage) {
storage.with_collection(name)(function (err, collection) {
if (err) {
console.error("ensureIndexes, unable to get collection for: " + name + " - " + err);
} else {
storage.ensureIndexes(collection, ['created_at']);
}
});
}

module.exports = {
storage: storage,
ensureIndexes: ensureIndexes
};
56 changes: 52 additions & 4 deletions lib/entries.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,14 @@
var es = require('event-stream');
var sgvdata = require('sgvdata');

var TEN_MINS = 10 * 60 * 1000;

/**********\
* Entries
* Encapsulate persistent storage of sgv entries.
\**********/

function entries (name, storage) {
function storage(name, storage, pushover) {

// TODO: Code is a little redundant.

Expand Down Expand Up @@ -61,7 +63,13 @@ function entries (name, storage) {

// return writable stream to lint each sgv record passing through it
function map ( ) {
return sgvdata.lint( );
function iter (item, next) {
if (item && item.type) {
return next(null, item);
}
return next(null, sgvdata.sync.json.echo(item));
}
return es.map(iter);
}

// writable stream that persists all records
Expand All @@ -73,7 +81,6 @@ function entries (name, storage) {
if (err) return fn(err, result);
// batch insert a list of records
create(result, fn);
return;
}
// lint and store the entire list
return es.pipeline(map( ), es.writeArray(done));
Expand All @@ -100,11 +107,39 @@ function entries (name, storage) {
firstErr = firstErr || err;
totalCreated += created;
});
sendPushover(doc);
});
fn(firstErr, totalCreated, docs);
});
}

//currently the Android upload will send the last MBG over and over, make sure we get a single notification
var lastMBG = 0;

function sendPushover(doc) {
if (doc.type && doc.mbg && doc.type == 'mbg' && doc.date && doc.date != lastMBG && pushover) {
var offset = new Date().getTime() - doc.date;
if (offset > TEN_MINS) {
console.info('No MBG Pushover, offset: ' + offset + ' too big, doc.date: ' + doc.date + ', now: ' + new Date().getTime());
} else {
var msg = {
expire: 14400, // 4 hours
message: '\nMeter BG: ' + doc.mbg,
title: 'Calibration',
sound: 'magic',
timestamp: new Date(doc.date),
priority: 0,
retry: 30
};

pushover.send(msg, function (err, result) {
console.log(result);
});
}
lastMBG = doc.date;
}
}

function getEntry(fn, id) {
console.info("trying to find entry for id: " + id);
with_collection(function(err, collection) {
Expand Down Expand Up @@ -152,6 +187,19 @@ function entries (name, storage) {
return api;
}

function ensureIndexes(name, storage) {
storage.with_collection(name)(function(err, collection) {
if (err) {
console.error("ensureIndexes, unable to get collection for: " + name + " - " + err);
} else {
storage.ensureIndexes(collection, ['date', 'type', 'sgv']);
}
});
}

// expose module
module.exports = entries;
module.exports = {
storage: storage,
ensureIndexes: ensureIndexes
};

2 changes: 1 addition & 1 deletion lib/pebble.js
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,7 @@ function pebble (req, res) {
console.error("req.devicestatus.tail", err);
}

req.entries.list({count: 2}, get_latest);
req.entries.list({count: 2, find: { "sgv": { $exists: true }}}, get_latest);
});
}
function configure (entries, devicestatus) {
Expand Down
25 changes: 20 additions & 5 deletions lib/storage.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
'use strict';
var mongodb = require('mongodb');

function init (env) {
function init (env, cb) {
var MongoClient = mongodb.MongoClient;

var my = { };
Expand All @@ -13,8 +13,12 @@ function init (env) {
}
console.log("Connecting to mongo");
MongoClient.connect(env.mongo, function connected (err, db) {
console.log("Connected to mongo, ERROR: %j", err);
if (err) { throw err; }
if (err) {
console.log("Error connecting to mongo, ERROR: %j", err);
throw err;
} else {
console.log("Connected to mongo");
}
my.db = db;
mongo.pool.db = my.db = db;

Expand All @@ -30,7 +34,7 @@ function init (env) {

mongo.pool = function ( ) {
return my;
}
};

mongo.collection = function get_collection (name) {
return mongo.pool( ).db.collection(name);
Expand All @@ -42,7 +46,18 @@ function init (env) {
};
};

return mongo( );
mongo.ensureIndexes = function(collection, fields) {
fields.forEach(function (field) {
console.info("ensuring index for: " + field);
collection.ensureIndex(field, function (err) {
if (err) {
console.error("unable to ensureIndex for: " + field + " - " + err);
}
});
});
};

return mongo(cb);
}

module.exports = init;
18 changes: 16 additions & 2 deletions lib/treatments.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
'use strict';

function configure (collection, storage, pushover) {
function storage (collection, storage, pushover) {

function create (obj, fn) {
obj.created_at = (new Date( )).toISOString( );
Expand Down Expand Up @@ -45,4 +45,18 @@ function configure (collection, storage, pushover) {
api.create = create;
return api;
}
module.exports = configure;

function ensureIndexes(name, storage) {
storage.with_collection(name)(function (err, collection) {
if (err) {
console.error("ensureIndexes, unable to get collection for: " + name + " - " + err);
} else {
storage.ensureIndexes(collection, ['created_at', 'eventType']);
}
});
}

module.exports = {
storage: storage,
ensureIndexes: ensureIndexes
};
4 changes: 2 additions & 2 deletions lib/websocket.js
Original file line number Diff line number Diff line change
Expand Up @@ -54,9 +54,9 @@ var dir2Char = {
// reduce logging
io.set('log level', 0);

//Windows Azure Web Sites does not currently support WebSockets, so use long-polling
//Try for Websockets (Azure Supports if it's on) then fallback
io.configure(function () {
io.set('transports', ['xhr-polling']);
io.set('transports', ['websocket','xhr-polling']);
});
}

Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "Nightscout",
"version": "0.4.3",
"version": "0.5.0",
"description": "Nightscout acts as a web-based CGM (Continuous Glucose Montinor) to allow multiple caregivers to remotely view a patients glucose data in realtime.",
"license": "AGPL3",
"author": "Nightscout Team",
Expand Down
26 changes: 18 additions & 8 deletions server.js
Original file line number Diff line number Diff line change
Expand Up @@ -28,20 +28,30 @@

var software = require('./package.json');
var env = require('./env')( );
var store = require('./lib/storage')(env);
var pushover = require('./lib/pushover')(env);

var entries = require('./lib/entries');
var treatments = require('./lib/treatments');
var devicestatus = require('./lib/devicestatus');

var store = require('./lib/storage')(env, function() {
console.info("Mongo ready");
entries.ensureIndexes(env.mongo_collection, store);
treatments.ensureIndexes(env.treatments_collection, store);
devicestatus.ensureIndexes(env.devicestatus_collection, store);
});


var express = require('express');

var pushover = require('./lib/pushover')(env);
///////////////////////////////////////////////////
// api and json object variables
///////////////////////////////////////////////////
var entries = require('./lib/entries')(env.mongo_collection, store);
var entriesStorage = entries.storage(env.mongo_collection, store, pushover);
var settings = require('./lib/settings')(env.settings_collection, store);
var treatments = require('./lib/treatments')(env.treatments_collection, store, pushover);
var devicestatus = require('./lib/devicestatus')(env.devicestatus_collection, store);
var api = require('./lib/api/')(env, entries, settings, treatments, devicestatus);
var treatmentsStorage = treatments.storage(env.treatments_collection, store, pushover);
var devicestatusStorage = devicestatus.storage(env.devicestatus_collection, store);
var api = require('./lib/api/')(env, entriesStorage, settings, treatmentsStorage, devicestatusStorage);
var pebble = require('./lib/pebble');
///////////////////////////////////////////////////

Expand All @@ -61,7 +71,7 @@ app.enable('trust proxy'); // Allows req.secure test on heroku https connections
app.use('/api/v1', api);

// pebble data
app.get('/pebble', pebble(entries, devicestatus));
app.get('/pebble', pebble(entriesStorage, devicestatusStorage));

//app.get('/package.json', software);

Expand All @@ -86,7 +96,7 @@ store(function ready ( ) {
// setup socket io for data and message transmission
///////////////////////////////////////////////////
var websocket = require('./lib/websocket');
var io = websocket(env, server, entries, treatments);
var io = websocket(env, server, entriesStorage, treatmentsStorage);
});

///////////////////////////////////////////////////
4 changes: 4 additions & 0 deletions static/css/main.css
Original file line number Diff line number Diff line change
Expand Up @@ -206,6 +206,10 @@ div.tooltip {
font-size: 20%;
}

#currentTime{
padding: 0 0 10px 0;
}

/* Large desktop */
@media (min-width: 980px) {
.content {
Expand Down

0 comments on commit 6893781

Please sign in to comment.