mirror of
https://github.com/bckelley/cgm-remote-monitor.git
synced 2026-08-24 03:14:12 -05:00
removed settings; fixed profiles/profile mixups; fix current/id queries
This commit is contained in:
@@ -5,8 +5,7 @@ MONGO_CONNECTION?=mongodb://localhost/test_db
|
||||
CUSTOMCONNSTR_mongo_settings_collection?=test_settings
|
||||
CUSTOMCONNSTR_mongo_collection?=test_sgvs
|
||||
MONGO_SETTINGS=MONGO_CONNECTION=${MONGO_CONNECTION} \
|
||||
CUSTOMCONNSTR_mongo_collection=${CUSTOMCONNSTR_mongo_collection} \
|
||||
CUSTOMCONNSTR_mongo_settings_collection=${CUSTOMCONNSTR_mongo_settings_collection}
|
||||
CUSTOMCONNSTR_mongo_collection=${CUSTOMCONNSTR_mongo_collection}
|
||||
|
||||
# XXX.bewest: Mocha is an odd process, and since things are being
|
||||
# wrapped and transformed, this odd path needs to be used, not the
|
||||
|
||||
@@ -12,7 +12,6 @@ function config ( ) {
|
||||
* * PORT - serve http on this port
|
||||
* * MONGO_CONNECTION, CUSTOMCONNSTR_mongo - mongodb://... uri
|
||||
* * CUSTOMCONNSTR_mongo_collection - name of mongo collection with "sgv" documents
|
||||
* * CUSTOMCONNSTR_mongo_settings_collection - name of mongo collection to store configurable settings
|
||||
* * API_SECRET - if defined, this passphrase is fed to a sha1 hash digest, the hex output is used to create a single-use token for API authorization
|
||||
* * NIGHTSCOUT_STATIC_FILES - the "base directory" to use for serving
|
||||
* static files over http. Default value is the included `static`
|
||||
@@ -50,7 +49,6 @@ function config ( ) {
|
||||
console.info('MQTT configured to use a custom client id, it will override the default: ', env.mqtt_client_id);
|
||||
}
|
||||
}
|
||||
env.settings_collection = readENV('MONGO_SETTINGS_COLLECTION', 'settings');
|
||||
env.treatments_collection = readENV('MONGO_TREATMENTS_COLLECTION', 'treatments');
|
||||
env.profile_collection = readENV('MONGO_PROFILE_COLLECTION', 'profile');
|
||||
env.devicestatus_collection = readENV('MONGO_DEVICESTATUS_COLLECTION', 'devicestatus');
|
||||
@@ -176,11 +174,9 @@ function config ( ) {
|
||||
// This allows a provided json config to override environment variables
|
||||
var DB = require('./database_configuration.json'),
|
||||
DB_URL = DB.url ? DB.url : env.mongo,
|
||||
DB_COLLECTION = DB.collection ? DB.collection : env.mongo_collection,
|
||||
DB_SETTINGS_COLLECTION = DB.settings_collection ? DB.settings_collection : env.settings_collection;
|
||||
DB_COLLECTION = DB.collection ? DB.collection : env.mongo_collection
|
||||
env.mongo = DB_URL;
|
||||
env.mongo_collection = DB_COLLECTION;
|
||||
env.settings_collection = DB_SETTINGS_COLLECTION;
|
||||
env.static_files = readENV('NIGHTSCOUT_STATIC_FILES', __dirname + '/static/');
|
||||
|
||||
return env;
|
||||
|
||||
@@ -21,8 +21,8 @@ function configure (app, wares, devicestatus) {
|
||||
if (!q.count) {
|
||||
q.count = 10;
|
||||
}
|
||||
devicestatus.list(q, function (err, profiles) {
|
||||
return res.json(profiles);
|
||||
devicestatus.list(q, function (err, results) {
|
||||
return res.json(results);
|
||||
});
|
||||
});
|
||||
|
||||
|
||||
+31
-15
@@ -123,15 +123,14 @@ function configure (app, wares, core) {
|
||||
es.pipeline(inputs( ), persist(done));
|
||||
}
|
||||
|
||||
api.param('model', function (req, res, next, model) {
|
||||
function prepReqModel(req, model) {
|
||||
var find = { };
|
||||
switch (model) {
|
||||
case 'treatments':
|
||||
case 'devicestatus':
|
||||
case 'settings':
|
||||
case 'profile':
|
||||
//TODO: profiles not working now, maybe profiles are special
|
||||
//case 'profiles':
|
||||
req.model = core[model];
|
||||
// find.type = model;
|
||||
break;
|
||||
|
||||
case 'meter':
|
||||
@@ -155,14 +154,22 @@ function configure (app, wares, core) {
|
||||
req.model = core.entries;
|
||||
break;
|
||||
}
|
||||
|
||||
if (!req.query.find) {
|
||||
req.query.find = find;
|
||||
} else {
|
||||
req.query.find.type = find.type;
|
||||
}
|
||||
}
|
||||
|
||||
api.param('model', function (req, res, next, model) {
|
||||
prepReqModel(req, model);
|
||||
next( );
|
||||
});
|
||||
|
||||
api.get('/entries/current', function(req, res, next) {
|
||||
//assume sgv
|
||||
req.params.model = 'sgv';
|
||||
entries.list({count: 1}, function(err, records) {
|
||||
res.entries = records;
|
||||
res.entries_err = err;
|
||||
@@ -170,6 +177,25 @@ function configure (app, wares, core) {
|
||||
});
|
||||
}, format_entries);
|
||||
|
||||
// Fetch one entry by id
|
||||
api.get('/entries/:id', function(req, res, next) {
|
||||
var ID_PATTERN = /^[a-f\d]{24}$/;
|
||||
if (ID_PATTERN.test(req.params.id)) {
|
||||
//assume sgv
|
||||
req.params.model = 'sgv';
|
||||
entries.getEntry(req.params.id, function(err, entry) {
|
||||
res.entries = [entry];
|
||||
res.entries_err = err;
|
||||
next();
|
||||
});
|
||||
} else {
|
||||
//TODO: /entries/:id is blocking /entries/:model
|
||||
req.params.model = req.params.id;
|
||||
prepReqModel(req, req.params.model);
|
||||
query_models(req, res, next);
|
||||
}
|
||||
}, format_entries);
|
||||
|
||||
api.get('/entries/:model', query_models, format_entries);
|
||||
api.get('/entries', query_models, format_entries);
|
||||
|
||||
@@ -180,7 +206,7 @@ function configure (app, wares, core) {
|
||||
req.model = core.entries;
|
||||
}
|
||||
var query = req.query;
|
||||
if (!query.count) { query.count = 10 };
|
||||
if (!query.count) { query.count = 10 }
|
||||
req.model.list(query, function(err, entries) {
|
||||
res.entries = entries;
|
||||
res.entries_err = err;
|
||||
@@ -204,16 +230,6 @@ function configure (app, wares, core) {
|
||||
}, insert_entries, format_entries);
|
||||
}
|
||||
|
||||
// Fetch one entry by id
|
||||
api.get('/entries/:id', function(req, res, next) {
|
||||
entries.getEntry(req.params.id, function(err, entry) {
|
||||
res.entries = [entry];
|
||||
res.entries_err = err;
|
||||
next()
|
||||
});
|
||||
}, format_entries);
|
||||
|
||||
|
||||
return api;
|
||||
}
|
||||
module.exports = configure;
|
||||
|
||||
+1
-2
@@ -47,9 +47,8 @@ function create (env, core) {
|
||||
|
||||
// Entries and settings
|
||||
app.use('/', require('./entries/')(app, wares, core));
|
||||
app.use('/', require('./settings/')(app, wares, core.settings));
|
||||
app.use('/', require('./treatments/')(app, wares, core.treatments));
|
||||
app.use('/', require('./profile/')(app, wares, core.profile));
|
||||
app.use('/', require('./profile/')(app, wares, core.profiles));
|
||||
app.use('/', require('./devicestatus/')(app, wares, core.devicestatus));
|
||||
|
||||
// Status
|
||||
|
||||
@@ -1,73 +0,0 @@
|
||||
'use strict';
|
||||
|
||||
var consts = require('../../constants');
|
||||
|
||||
function configure (app, wares, settings) {
|
||||
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 }));
|
||||
|
||||
|
||||
/**********\
|
||||
* Settings
|
||||
\**********/
|
||||
// Handler for grabbing alias/profile
|
||||
api.param('alias', function (req, res, next, alias) {
|
||||
settings.alias(alias, function (err, profile) {
|
||||
req.alias = profile;
|
||||
next(err);
|
||||
});
|
||||
});
|
||||
|
||||
// List settings available
|
||||
api.get('/settings/', function(req, res) {
|
||||
settings.list(function (err, profiles) {
|
||||
return res.json(profiles);
|
||||
});
|
||||
});
|
||||
|
||||
// Fetch settings
|
||||
api.get('/settings/:alias', function(req, res) {
|
||||
res.json(req.alias);
|
||||
});
|
||||
|
||||
function config_authed (app, api, wares, settings) {
|
||||
|
||||
// Delete settings
|
||||
api.delete('/settings/:alias', wares.verifyAuthorization, function(req, res) {
|
||||
settings.remove(req.alias.alias, function ( ) {
|
||||
res.json({ });
|
||||
});
|
||||
});
|
||||
|
||||
function save_settings (req, res) {
|
||||
var b = req.body;
|
||||
b.alias = req.params.alias
|
||||
settings.update(b, function (err, profile) {
|
||||
res.json(profile);
|
||||
});
|
||||
}
|
||||
|
||||
// Update settings
|
||||
api.put('/settings/:alias', wares.verifyAuthorization, save_settings);
|
||||
api.post('/settings/:alias', wares.verifyAuthorization, save_settings);
|
||||
|
||||
}
|
||||
|
||||
if (app.enabled('api')) {
|
||||
config_authed(app, api, wares, settings);
|
||||
}
|
||||
|
||||
return api;
|
||||
}
|
||||
|
||||
module.exports = configure;
|
||||
|
||||
@@ -15,10 +15,10 @@ function configure (app, wares, treatments) {
|
||||
// also support url-encoded content-type
|
||||
api.use(wares.bodyParser.urlencoded({ extended: true }));
|
||||
|
||||
// List settings available
|
||||
// List treatments available
|
||||
api.get('/treatments/', function(req, res) {
|
||||
treatments.list({find: req.params}, function (err, profiles) {
|
||||
return res.json(profiles);
|
||||
treatments.list({find: req.params}, function (err, results) {
|
||||
return res.json(results);
|
||||
});
|
||||
});
|
||||
|
||||
|
||||
@@ -18,7 +18,6 @@ function boot (env) {
|
||||
///////////////////////////////////////////////////
|
||||
ctx.pushover = require('./pushover')(env);
|
||||
ctx.entries = require('./entries')(env.mongo_collection, ctx.store, ctx.pushover);
|
||||
ctx.settings = require('./settings')(env.settings_collection, ctx.store);
|
||||
ctx.treatments = require('./treatments')(env.treatments_collection, ctx.store, ctx.pushover);
|
||||
ctx.devicestatus = require('./devicestatus')(env.devicestatus_collection, ctx.store);
|
||||
ctx.profiles = require('./profile')(env.profile_collection, ctx.store);
|
||||
|
||||
@@ -4,6 +4,8 @@
|
||||
function storage (collection, storage) {
|
||||
var ObjectID = require('mongodb').ObjectID;
|
||||
|
||||
console.info('>>>>profile storage init', collection, storage);
|
||||
|
||||
function create (obj, fn) {
|
||||
obj.created_at = (new Date( )).toISOString( );
|
||||
api().insert(obj, function (err, doc) {
|
||||
|
||||
-164
@@ -1,164 +0,0 @@
|
||||
'use strict';
|
||||
|
||||
|
||||
var ObjectID = require('mongodb').ObjectID;
|
||||
function defaults ( ) {
|
||||
var DEFAULT_SETTINGS_JSON = {
|
||||
"units": "mg/dl"
|
||||
}; // possible future settings: "theme": "subdued", "websockets": false, alertLow: 80, alertHigh: 180
|
||||
return DEFAULT_SETTINGS_JSON;
|
||||
}
|
||||
|
||||
function configure (collection, storage) {
|
||||
var DEFAULT_SETTINGS_JSON = {
|
||||
"units": "mg/dl"
|
||||
};
|
||||
|
||||
function pop (fn) {
|
||||
return function (err, results) {
|
||||
if (err) fn(err);
|
||||
fn(err, results.pop( ));
|
||||
}
|
||||
}
|
||||
|
||||
function alias (alias, fn) {
|
||||
return api( ).find({ alias: alias }).toArray(pop(fn));
|
||||
}
|
||||
|
||||
function create (obj, fn) {
|
||||
var result = merge(DEFAULT_SETTINGS_JSON, obj);
|
||||
result.alias = obj.alias;
|
||||
result.created_at = (new Date( )).toISOString( );
|
||||
api( ).insert(result, function (err, doc) {
|
||||
fn(null, doc);
|
||||
});
|
||||
}
|
||||
|
||||
function lint (obj) {
|
||||
var result = merge(DEFAULT_SETTINGS_JSON, json);
|
||||
if (result.alias) return result;
|
||||
}
|
||||
|
||||
function list (fn) {
|
||||
return api( ).find({ }, { alias: 1, nick: 1 }).toArray(pop(fn));
|
||||
}
|
||||
|
||||
function remove (alias, fn) {
|
||||
return api( ).remove({ alias: alias }, fn);
|
||||
}
|
||||
|
||||
var with_collection = storage.with_collection(collection);
|
||||
function getSettings (fn) {
|
||||
with_collection(function(err, collection) {
|
||||
if (err)
|
||||
fn(err);
|
||||
else
|
||||
// Retrieve the existing settings record.
|
||||
collection.find().toArray(function(err, settings) {
|
||||
if (err)
|
||||
fn(err);
|
||||
else
|
||||
// Strip the record of the enclosing square brackets.
|
||||
settings = settings.pop( );
|
||||
if (!settings) {
|
||||
settings = DEFAULT_SETTINGS_JSON;
|
||||
}
|
||||
fn(null, settings);
|
||||
});
|
||||
});
|
||||
}
|
||||
var whitelist = [ 'units', 'data' ];
|
||||
|
||||
function merge (older, newer) {
|
||||
var update = { };
|
||||
whitelist.forEach(function (prop) {
|
||||
if (prop in newer) {
|
||||
update[prop] = newer[prop];
|
||||
}
|
||||
});
|
||||
return update;
|
||||
}
|
||||
|
||||
function update (json, fn) {
|
||||
var updated = (new Date( )).toISOString( );
|
||||
alias(json.alias, function last (err, older) {
|
||||
if (err) { return fn(err); }
|
||||
var result;
|
||||
if (older && older._id) {
|
||||
// result = merge(older, json);
|
||||
result = json;
|
||||
result.updated_at = updated;
|
||||
var deltas = Object.keys(result);
|
||||
if (deltas.length < 1) {
|
||||
fn("Bad Keys");
|
||||
return;
|
||||
}
|
||||
api( ).update(
|
||||
{ '_id' : new ObjectID(older._id) },
|
||||
{ $set: result },
|
||||
function (err, res) {
|
||||
// Return to the calling function to display our success.
|
||||
fn(err, res);
|
||||
}
|
||||
);
|
||||
} else {
|
||||
create(json, fn);
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
function updateSettings (json, fn) {
|
||||
getSettings(function last (err, older) {
|
||||
var result = merge(older, json);
|
||||
var deltas = Object.keys(result);
|
||||
if (deltas.length < 1) {
|
||||
fn("Bad Keys");
|
||||
return;
|
||||
}
|
||||
result.updated_at = (new Date( )).toISOString( );
|
||||
with_collection(function(err, collection) {
|
||||
if (err) {
|
||||
console.log('error', result);
|
||||
return fn(err);
|
||||
} else if (older && older._id && Object.keys(deltas).length > 0) {
|
||||
collection.update(
|
||||
{ '_id' : new ObjectID(older._id) },
|
||||
{ $set: result },
|
||||
function (err, stats) {
|
||||
// Return to the calling function to display our success.
|
||||
fn(err, result);
|
||||
}
|
||||
);
|
||||
} else {
|
||||
collection.insert(result, function (err, doc) {
|
||||
fn(null, result);
|
||||
|
||||
});
|
||||
|
||||
}
|
||||
});
|
||||
});
|
||||
}
|
||||
|
||||
function clear (fn) {
|
||||
with_collection(function (err, collection) {
|
||||
collection.remove({ }, fn);
|
||||
});
|
||||
}
|
||||
|
||||
function api ( ) {
|
||||
return storage.pool.db.collection(collection);
|
||||
}
|
||||
|
||||
api.getSettings = getSettings;
|
||||
api.updateSettings = updateSettings;
|
||||
api.remove = remove;
|
||||
api.clear = clear;
|
||||
api.list = list;
|
||||
api.create = create;
|
||||
api.lint = lint;
|
||||
api.alias = alias;
|
||||
api.update = update;
|
||||
return api;
|
||||
}
|
||||
module.exports = configure;
|
||||
@@ -69,7 +69,7 @@ describe('Entries REST api', function ( ) {
|
||||
});
|
||||
});
|
||||
|
||||
it('/entries/ID', function (done) {
|
||||
it('/entries/sgv/ID', function (done) {
|
||||
var app = this.app;
|
||||
this.archive.list({count: 1}, function(err, records) {
|
||||
var currentId = records.pop()._id.toString();
|
||||
|
||||
@@ -14,10 +14,9 @@ describe('API_SECRET', function ( ) {
|
||||
ctx.wares = require('../lib/middleware/')(env);
|
||||
ctx.store = require('../lib/storage')(env);
|
||||
ctx.archive = require('../lib/entries').storage(env.mongo_collection, ctx.store);
|
||||
ctx.settings = require('../lib/settings')(env.settings_collection, ctx.store);
|
||||
|
||||
ctx.store(function ( ) {
|
||||
ctx.app = api(env, ctx.wares, ctx.archive, ctx.settings);
|
||||
ctx.app = api(env, ctx.wares, ctx.archive);
|
||||
scope.app = ctx.app;
|
||||
ctx.archive.create(load('json'), fn);
|
||||
scope.archive = ctx.archive;
|
||||
|
||||
Reference in New Issue
Block a user