Skip to content

Commit 42d0581

Browse files
authored
Merge pull request #1 from nightscout/dev
Dev
2 parents 36fabb8 + e7d4828 commit 42d0581

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

58 files changed

+2172
-920
lines changed

app.js

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -5,11 +5,6 @@ var compression = require('compression');
55
var bodyParser = require('body-parser');
66

77
function create (env, ctx) {
8-
///////////////////////////////////////////////////
9-
// api and json object variables
10-
///////////////////////////////////////////////////
11-
var api = require('./lib/api/')(env, ctx);
12-
138
var app = express();
149
var appInfo = env.name + ' ' + env.version;
1510
app.set('title', appInfo);
@@ -20,6 +15,11 @@ function create (env, ctx) {
2015
return app;
2116
}
2217

18+
///////////////////////////////////////////////////
19+
// api and json object variables
20+
///////////////////////////////////////////////////
21+
var api = require('./lib/api/')(env, ctx);
22+
2323
app.use(compression({filter: function shouldCompress(req, res) {
2424
//TODO: return false here if we find a condition where we don't want to compress
2525
// fallback to standard filter function

bower.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "nightscout",
3-
"version": "0.9.0",
3+
"version": "0.9.1-dev-20161023",
44
"dependencies": {
55
"colorbrewer": "~1.0.0",
66
"jQuery-Storage-API": "~1.7.2",

env.js

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ function config ( ) {
2727
setSSL();
2828
setAPISecret();
2929
setVersion();
30-
setMongo();
30+
setStorage();
3131
updateSettings();
3232

3333
return env;
@@ -90,12 +90,12 @@ function setVersion() {
9090
env.name = software.name;
9191
}
9292

93-
function setMongo() {
94-
env.mongo = readENV('MONGO_CONNECTION') || readENV('MONGO') || readENV('MONGOLAB_URI') || readENV('MONGODB_URI');
95-
env.mongo_collection = readENV('MONGO_COLLECTION', 'entries');
93+
function setStorage() {
94+
env.storageURI = readENV('STORAGE_URI') || readENV('MONGO_CONNECTION') || readENV('MONGO') || readENV('MONGOLAB_URI') || readENV('MONGODB_URI');
95+
env.entries_collection = readENV('ENTRIES_COLLECTION') || readENV('MONGO_COLLECTION', 'entries');
9696
env.MQTT_MONITOR = readENV('MQTT_MONITOR', null);
9797
if (env.MQTT_MONITOR) {
98-
var hostDbCollection = [env.mongo.split('mongodb://').pop().split('@').pop(), env.mongo_collection].join('/');
98+
var hostDbCollection = [env.storageURI.split('mongodb://').pop().split('@').pop(), env.entries_collection].join('/');
9999
var mongoHash = crypto.createHash('sha1');
100100
mongoHash.update(hostDbCollection);
101101
//some MQTT servers only allow the client id to be 23 chars
@@ -117,10 +117,10 @@ function setMongo() {
117117
// Some people prefer to use a json configuration file instead.
118118
// This allows a provided json config to override environment variables
119119
var DB = require('./database_configuration.json'),
120-
DB_URL = DB.url ? DB.url : env.mongo,
121-
DB_COLLECTION = DB.collection ? DB.collection : env.mongo_collection;
122-
env.mongo = DB_URL;
123-
env.mongo_collection = DB_COLLECTION;
120+
DB_URL = DB.url ? DB.url : env.storageURI,
121+
DB_COLLECTION = DB.collection ? DB.collection : env.entries_collection;
122+
env.storageURI = DB_URL;
123+
env.entries_collection = DB_COLLECTION;
124124
}
125125

126126
function updateSettings() {

lib/bootevent.js

Lines changed: 22 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -19,20 +19,33 @@ function boot (env, language) {
1919
return ctx.bootErrors && ctx.bootErrors.length > 0;
2020
}
2121

22-
function setupMongo (ctx, next) {
22+
function setupStorage (ctx, next) {
2323

2424
if (hasBootErrors(ctx)) {
2525
return next();
2626
}
2727

2828
try {
29-
require('./storage')(env, function ready ( err, store ) {
30-
// FIXME, error is always null, if there is an error, the storage.js will throw an exception
31-
console.log('Storage system ready');
32-
ctx.store = store;
33-
34-
next();
35-
});
29+
if (_.startsWith(env.storageURI, 'openaps://')) {
30+
require('./storage/openaps-storage')(env, function ready (err, store) {
31+
if (err) {
32+
throw err;
33+
}
34+
35+
ctx.store = store;
36+
console.log('OpenAPS Storage system ready');
37+
next();
38+
});
39+
} else {
40+
//TODO assume mongo for now, when there are more storage options add a lookup
41+
require('./storage/mongo-storage')(env, function ready(err, store) {
42+
// FIXME, error is always null, if there is an error, the storage.js will throw an exception
43+
console.log('Mongo Storage system ready');
44+
ctx.store = store;
45+
46+
next();
47+
});
48+
}
3649
} catch (err) {
3750
console.info('mongo err', err);
3851
ctx.bootErrors = ctx.bootErrors || [ ];
@@ -174,7 +187,7 @@ function boot (env, language) {
174187

175188
return require('bootevent')( )
176189
.acquire(checkEnv)
177-
.acquire(setupMongo)
190+
.acquire(setupStorage)
178191
.acquire(setupAuthorization)
179192
.acquire(setupInternals)
180193
.acquire(ensureIndexes)

lib/data/dataloader.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -105,7 +105,7 @@ function loadEntries (ddata, ctx, callback) {
105105

106106
function mergeToTreatments (ddata, results) {
107107
var filtered = _.filter(results, function hasId (treatment) {
108-
return _.isObject(treatment._id);
108+
return !_.isEmpty(treatment._id);
109109
});
110110

111111
var treatments = _.map(filtered, function update (treatment) {

lib/devicestatus.js

Lines changed: 23 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -25,42 +25,38 @@ function storage (collection, ctx) {
2525
});
2626
}
2727

28-
var with_collection = ctx.store.with_collection(collection);
29-
3028
function query_for (opts) {
3129
return find_options(opts, storage.queryOpts);
3230
}
3331

3432
function list(opts, fn) {
35-
with_collection(function (err, collection) {
36-
// these functions, find, sort, and limit, are used to
37-
// dynamically configure the request, based on the options we've
38-
// been given
33+
// these functions, find, sort, and limit, are used to
34+
// dynamically configure the request, based on the options we've
35+
// been given
3936

40-
// determine sort options
41-
function sort ( ) {
42-
return opts && opts.sort || {created_at: -1};
43-
}
37+
// determine sort options
38+
function sort ( ) {
39+
return opts && opts.sort || {created_at: -1};
40+
}
4441

45-
// configure the limit portion of the current query
46-
function limit ( ) {
47-
if (opts && opts.count) {
48-
return this.limit(parseInt(opts.count));
49-
}
50-
return this;
42+
// configure the limit portion of the current query
43+
function limit ( ) {
44+
if (opts && opts.count) {
45+
return this.limit(parseInt(opts.count));
5146
}
47+
return this;
48+
}
5249

53-
// handle all the results
54-
function toArray (err, entries) {
55-
fn(err, entries);
56-
}
50+
// handle all the results
51+
function toArray (err, entries) {
52+
fn(err, entries);
53+
}
5754

58-
// now just stitch them all together
59-
limit.call(collection
60-
.find(query_for(opts))
61-
.sort(sort( ))
62-
).toArray(toArray);
63-
});
55+
// now just stitch them all together
56+
limit.call(api( )
57+
.find(query_for(opts))
58+
.sort(sort( ))
59+
).toArray(toArray);
6460
}
6561

6662
function remove (_id, fn) {
@@ -74,7 +70,7 @@ function storage (collection, ctx) {
7470
}
7571

7672
function api() {
77-
return ctx.store.db.collection(collection);
73+
return ctx.store.collection(collection);
7874
}
7975

8076
api.list = list;

lib/entries.js

Lines changed: 19 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -14,11 +14,8 @@ function storage(env, ctx) {
1414

1515
// TODO: Code is a little redundant.
1616

17-
var with_collection = ctx.store.with_collection(env.mongo_collection);
18-
1917
// query for entries from storage
2018
function list (opts, fn) {
21-
with_collection(function (err, collection) {
2219
// these functions, find, sort, and limit, are used to
2320
// dynamically configure the request, based on the options we've
2421
// been given
@@ -42,17 +39,14 @@ function storage(env, ctx) {
4239
}
4340

4441
// now just stitch them all together
45-
limit.call(collection
42+
limit.call(api( )
4643
.find(query_for(opts))
4744
.sort(sort( ))
4845
).toArray(toArray);
49-
});
5046
}
5147

5248
function remove (opts, fn) {
53-
with_collection(function (err, collection) {
54-
collection.remove(query_for(opts), fn);
55-
});
49+
api( ).remove(query_for(opts), fn);
5650
}
5751

5852
// return writable stream to lint each sgv record passing through it
@@ -86,39 +80,30 @@ function storage(env, ctx) {
8680

8781
// store new documents using the storage mechanism
8882
function create (docs, fn) {
89-
with_collection(function(err, collection) {
90-
if (err) { fn(err); return; }
91-
// potentially a batch insert
92-
var firstErr = null,
93-
numDocs = docs.length,
94-
totalCreated = 0;
95-
96-
docs.forEach(function(doc) {
97-
var query = (doc.sysTime && doc.type) ? {sysTime: doc.sysTime, type: doc.type} : doc;
98-
collection.update(query, doc, {upsert: true}, function (err) {
99-
firstErr = firstErr || err;
100-
if (++totalCreated === numDocs) {
101-
//TODO: this is triggering a read from Mongo, we can do better
102-
ctx.bus.emit('data-received');
103-
fn(firstErr, docs);
104-
}
105-
});
83+
// potentially a batch insert
84+
var firstErr = null,
85+
numDocs = docs.length,
86+
totalCreated = 0;
87+
88+
docs.forEach(function(doc) {
89+
var query = (doc.sysTime && doc.type) ? {sysTime: doc.sysTime, type: doc.type} : doc;
90+
api( ).update(query, doc, {upsert: true}, function (err) {
91+
firstErr = firstErr || err;
92+
if (++totalCreated === numDocs) {
93+
//TODO: this is triggering a read from Mongo, we can do better
94+
ctx.bus.emit('data-received');
95+
fn(firstErr, docs);
96+
}
10697
});
10798
});
10899
}
109100

110101
function getEntry(id, fn) {
111-
with_collection(function(err, collection) {
102+
api( ).findOne({_id: ObjectID(id)}, function (err, entry) {
112103
if (err) {
113104
fn(err);
114105
} else {
115-
collection.findOne({_id: ObjectID(id)}, function (err, entry) {
116-
if (err) {
117-
fn(err);
118-
} else {
119-
fn(null, entry);
120-
}
121-
});
106+
fn(null, entry);
122107
}
123108
});
124109
}
@@ -131,7 +116,7 @@ function storage(env, ctx) {
131116
function api ( ) {
132117
// obtain handle usable for querying the collection associated
133118
// with these records
134-
return ctx.store.db.collection(env.mongo_collection);
119+
return ctx.store.collection(env.entries_collection);
135120
}
136121

137122
// Expose all the useful functions

lib/food.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@ function storage (env, ctx) {
3737

3838

3939
function api ( ) {
40-
return ctx.store.db.collection(env.food_collection);
40+
return ctx.store.collection(env.food_collection);
4141
}
4242

4343
api.list = list;

0 commit comments

Comments
 (0)