Skip to content

Commit

Permalink
Merge pull request nightscout#6 from nightscout/dev
Browse files Browse the repository at this point in the history
Dev-0.9.3
  • Loading branch information
lixgbg committed Dec 16, 2016
2 parents cb305d6 + 218b00a commit 77a2227
Show file tree
Hide file tree
Showing 40 changed files with 1,266 additions and 263 deletions.
8 changes: 8 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -153,6 +153,7 @@ You can get many more results, by using the `count`, `date`, `dateString`, and `
(replace `http://localhost:1337` with your base url, YOUR-SITE)

* 100's: `http://localhost:1337/api/v1/entries.json?find[sgv]=100`
* Count of 100's in a month: `http://localhost:1337/api/v1/count/entries/where?find[dateString][$gte]=2016-09&find[dateString][$lte]=2016-10&find[sgv]=100`
* BGs between 2 days: `http://localhost:1337/api/v1/entries/sgv.json?find[dateString][$gte]=2015-08-28&find[dateString][$lte]=2015-08-30`
* Juice Box corrections in a year: `http://localhost:1337/api/v1/treatments.json?count=1000&find[carbs]=15&find[eventType]=Carb+Correction&find[created_at][$gte]=2015`
* Boluses over 2U: `http://localhost:1337/api/v1/treatments.json?find[insulin][$gte]=2`
Expand All @@ -179,6 +180,7 @@ To learn more about the Nightscout API, visit https://YOUR-SITE.com/api-docs.htm
* `AUTH_DEFAULT_ROLES` (`readable`) - possible values `readable`, `denied`, or any valid role
name. When `readable`, anyone can view Nightscout without a token.
Setting it to `denied` will require a token from every visit, using `status-only` will enable api-secret based login.
* `IMPORT_CONFIG` - Used to import settings and extended settings from a url such as a gist. Structure of file should be something like: `{"settings": {"theme": "colors"}, "extendedSettings": {"upbat": {"enableAlerts": true}}}`
* `TREATMENTS_AUTH` (`on`) - possible values `on` or `off`. Deprecated, if set to `off` the `careportal` role will be added to `AUTH_DEFAULT_ROLES`


Expand Down Expand Up @@ -402,6 +404,12 @@ To learn more about the Nightscout API, visit https://YOUR-SITE.com/api-docs.htm
* `LOOP_URGENT` (`60`) - The number of minutes since the last loop that needs to be exceeded before an urgent alarm is triggered
* Add `loop` to `SHOW_FORECAST` to show forecasted BG.

##### `alexa` (Amazon Alexa)
Integration with Amazon Alexa, [detailed setup instructions](lib/plugins/alexa-plugin.md)

##### `cors` (CORS)
Enabled [CORS](https://en.wikipedia.org/wiki/Cross-origin_resource_sharing) so other websites can make request to your Nightscout site, uses these extended settings:
* `CORS_ALLOW_ORIGIN` (`*`) - The list of sites that are allow to make requests

#### Extended Settings
Some plugins support additional configuration using extra environment variables. These are prefixed with the name of the plugin and a `_`. For example setting `MYPLUGIN_EXAMPLE_VALUE=1234` would make `extendedSettings.exampleValue` available to the `MYPLUGIN` plugin.
Expand Down
2 changes: 2 additions & 0 deletions app.js
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ function create (env, ctx) {
// api and json object variables
///////////////////////////////////////////////////
var api = require('./lib/api/')(env, ctx);
var ddata = require('./lib/data/endpoints')(env, ctx);

app.use(compression({filter: function shouldCompress(req, res) {
//TODO: return false here if we find a condition where we don't want to compress
Expand All @@ -52,6 +53,7 @@ function create (env, ctx) {

app.use('/api/v2/properties', ctx.properties);
app.use('/api/v2/authorization', ctx.authorization.endpoints);
app.use('/api/v2/ddata', ddata);

// pebble data
app.get('/pebble', ctx.pebble);
Expand Down
5 changes: 3 additions & 2 deletions bower.json
Original file line number Diff line number Diff line change
@@ -1,14 +1,15 @@
{
"name": "nightscout",
"version": "0.9.1-dev-20161112",
"version": "0.9.3-dev-20161212b",
"dependencies": {
"colorbrewer": "~1.0.0",
"jQuery-Storage-API": "~1.7.2",
"jquery": "2.1.0",
"jquery-flot": "0.8.3",
"jquery-ui": "~1.11.3",
"swagger-ui": "~2.1.2",
"tipsy-jmalonzo": "~1.0.1"
"tipsy-jmalonzo": "~1.0.1",
"tone": "*"
},
"resolutions": {
"jquery": "2.1.0"
Expand Down
1 change: 1 addition & 0 deletions env.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ function config ( ) {
env.DISPLAY_UNITS = readENV('DISPLAY_UNITS', 'mg/dl');
env.PORT = readENV('PORT', 1337);
env.HOSTNAME = readENV('HOSTNAME', null);
env.IMPORT_CONFIG = readENV('IMPORT_CONFIG', null);
env.static_files = readENV('NIGHTSCOUT_STATIC_FILES', __dirname + '/static/');

if (env.err) {
Expand Down
32 changes: 32 additions & 0 deletions lib/aggregate.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
var find_options = require('./query');

function create (conf, api) {

var template = function ( ) {
return [
{
$group: {
_id: null
, count: { $sum: 1 }
}
}
];
};

// var collection = api( );
function aggregate (opts, done) {
var query = find_options(opts);

var pipeline = (conf.pipeline || [ ]).concat(opts.pipeline || [ ]);
var groupBy = [ {$match: query } ].concat(pipeline).concat(template( ));
console.log('$match query', query);
console.log('AGGREGATE', groupBy);
api( ).aggregate(groupBy, done);
}

return aggregate;

}

module.exports = create;

6 changes: 2 additions & 4 deletions lib/api/alexa/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,7 @@ function configure (app, wares, ctx, env) {
} else {
direction = records[0].direction;
}
var status = records[0].sgv + direction + ' as of ' + moment(records[0].date).from(moment(sbx.time)) + '.';
var status = sbx.scaleMgdl(records[0].sgv) + direction + ' as of ' + moment(records[0].date).from(moment(sbx.time)) + '.';
callback(null, {results: status, priority: -1});
});
// console.log('BG results called');
Expand All @@ -110,10 +110,8 @@ function configure (app, wares, ctx, env) {
direction = ' and rapidly dropping';
} else if (records[0].direction === 'DoubleUp') {
direction = ' and rapidly rising';
} else {
direction = records[0].direction;
}
var status = records[0].sgv + direction + ' as of ' + moment(records[0].date).from(moment(sbx.time));
var status = sbx.scaleMgdl(records[0].sgv) + direction + ' as of ' + moment(records[0].date).from(moment(sbx.time));
callback('Current blood glucose', status);
});
}, 'metric', ['bg', 'blood glucose', 'number']);
Expand Down
21 changes: 20 additions & 1 deletion lib/api/entries/index.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
'use strict';

var _ = require('lodash');
var consts = require('../../constants');
var es = require('event-stream');
var sgvdata = require('sgvdata');
Expand Down Expand Up @@ -341,6 +342,22 @@ function configure (app, wares, ctx) {
});
}

function count_records (req, res, next) {
var query = req.query;
var storage = req.storage || ctx.entries;
storage.aggregate(query, function payload (err, entries) {
// assign payload
res.entries = entries;
res.entries_err = err;
return next(err);
});
}

function format_results (req, res, next) {
res.json(res.entries);
next( );
}

/**
* @function delete_records
* Delete entries. The query logic works the same way as find/list. This
Expand Down Expand Up @@ -523,7 +540,7 @@ curl -s -g 'http://localhost:1337/api/v1/times/20{14..15}/T{13..18}:{00..15}'.js
* by default.
*/
function prep_storage (req, res, next) {
if (req.params.storage) {
if (req.params.storage && _.includes(['entries', 'treatments', 'devicestatus'], req.params.storage)) {
req.storage = ctx[req.params.storage];
} else {
req.storage = ctx.entries;
Expand Down Expand Up @@ -555,6 +572,8 @@ curl -s -g 'http://localhost:1337/api/v1/times/20{14..15}/T{13..18}:{00..15}'.js
*/
api.get('/times/:prefix?/:regex?', prep_storage, prep_pattern_field, prep_patterns, prep_patterns, query_models, format_entries);

api.get('/count/:storage/where', prep_storage, count_records, format_results);

/**
* @module get#/slice/:storage/:field/:type/:prefix/:regex
* @routed
Expand Down
1 change: 1 addition & 0 deletions lib/api/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,7 @@ function create (env, ctx) {
app.all('/echo/*', entriesRouter);
app.all('/times/*', entriesRouter);
app.all('/slice/*', entriesRouter);
app.all('/count/*', entriesRouter);

app.all('/treatments*', require('./treatments/')(app, wares, ctx));
app.all('/profile*', require('./profile/')(app, wares, ctx));
Expand Down
33 changes: 33 additions & 0 deletions lib/bootevent.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,38 @@ function boot (env, language) {
return ctx.bootErrors && ctx.bootErrors.length > 0;
}

function augmentSettings (ctx, next) {
var configURL = env.IMPORT_CONFIG || null;
var url = require('url');
var href = null;
try {
href = url.parse(configURL).href;
} catch (e) {
}
if(configURL && href) {
var request = require('request');
console.log('Getting settings from', href);
request.get({url: href, json: true}, function (err, resp, body) {
if (err) {
console.log('Attempt to fetch config', href, 'failed.');
console.error(err);
throw err;
} else {
var settings = body.settings || body;
console.log('extending settings with', settings);
_.merge(env.settings, settings);
if (body.extendedSettings) {
console.log('extending extendedSettings with', body.extendedSettings);
_.merge(env.extendedSettings, body.extendedSettings);
}
}
next( );
});
} else {
next( );
}
}

function setupStorage (ctx, next) {

if (hasBootErrors(ctx)) {
Expand Down Expand Up @@ -191,6 +223,7 @@ function boot (env, language) {

return require('bootevent')( )
.acquire(checkEnv)
.acquire(augmentSettings)
.acquire(setupStorage)
.acquire(setupAuthorization)
.acquire(setupInternals)
Expand Down
2 changes: 1 addition & 1 deletion lib/client/browser-settings.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ function init (client, serverSettings, $) {
var settings = require('../settings')();

function loadForm ( ) {
var utils = require('../utils')(settings);
var utils = client.utils;
var language = require('../language')();
language.set(settings.language);
var translate = language.translate;
Expand Down
5 changes: 4 additions & 1 deletion lib/client/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -149,7 +149,10 @@ client.load = function load(serverSettings, callback) {
, pluginBase: client.plugins.base(majorPills, minorPills, statusPills, bgStatus, client.tooltip, $.localStorage)
};

client.utils = require('../utils')(client.ctx.settings);
client.utils = require('../utils')({
settings: client.settings
, language: language
});
client.sbx = sandbox.clientInit(client.ctx, client.now);
client.renderer = require('./renderer')(client, d3, $);

Expand Down
6 changes: 3 additions & 3 deletions lib/client/renderer.js
Original file line number Diff line number Diff line change
Expand Up @@ -146,7 +146,7 @@ function init (client, d3) {
var rawbgInfo = getRawbgInfo();

client.tooltip.transition().duration(TOOLTIP_TRANS_MS).style('opacity', .9);
client.tooltip.html('<strong>' + translate('BG')+ ':</strong> ' + client.sbx.scaleEntry(d) +
client.tooltip.html('<strong>' + translate('BG')+ ':</strong> ' + client.sbx.scaleEntry( d ) +
(d.type === 'mbg' ? '<br/><strong>' + translate('Device') + ': </strong>' + d.device : '') +
(rawbgInfo.value ? '<br/><strong>' + translate('Raw BG') + ':</strong> ' + rawbgInfo.value : '') +
(rawbgInfo.noise ? '<br/><strong>' + translate('Noise') + ':</strong> ' + rawbgInfo.noise : '') +
Expand Down Expand Up @@ -174,7 +174,7 @@ function init (client, d3) {
return '<strong>'+translate('Time')+':</strong> ' + client.formatTime(new Date(d.mills)) + '<br/>' +
(d.eventType ? '<strong>'+translate('Treatment type')+':</strong> ' + translate(client.careportal.resolveEventName(d.eventType)) + '<br/>' : '') +
(d.reason ? '<strong>'+translate('Reason')+':</strong> ' + translate(d.reason) + '<br/>' : '') +
(d.glucose ? '<strong>'+translate('BG')+':</strong> ' + d.glucose + (d.glucoseType ? ' (' + translate(d.glucoseType) + ')': '') + '<br/>' : '') +
(d.glucose ? '<strong>'+translate('BG')+':</strong> ' + client.sbx.scaleMgdl(d.glucose) + (d.glucoseType ? ' (' + translate(d.glucoseType) + ')': '') + '<br/>' : '') +
(d.enteredBy ? '<strong>'+translate('Entered By')+':</strong> ' + d.enteredBy + '<br/>' : '') +
(d.targetTop ? '<strong>'+translate('Target Top')+':</strong> ' + d.targetTop + '<br/>' : '') +
(d.targetBottom ? '<strong>'+translate('Target Bottom')+':</strong> ' + d.targetBottom + '<br/>' : '') +
Expand Down Expand Up @@ -493,7 +493,7 @@ function init (client, d3) {
(treatment.carbs ? '<strong>' + translate('Carbs') + ':</strong> ' + treatment.carbs + '<br/>' : '') +
(treatment.insulin ? '<strong>' + translate('Insulin') + ':</strong> ' + treatment.insulin + '<br/>' : '') +
(treatment.enteredinsulin ? '<strong>' + translate('Combo Bolus') + ':</strong> ' + treatment.enteredinsulin + 'U, ' + treatment.splitNow + '% : ' + treatment.splitExt + '%, ' + translate('Duration') + ': ' + treatment.duration + '<br/>' : '') +
(treatment.glucose ? '<strong>' + translate('BG') + ':</strong> ' + treatment.glucose + (treatment.glucoseType ? ' (' + translate(treatment.glucoseType) + ')' : '') + '<br/>' : '') +
(treatment.glucose ? '<strong>' + translate('BG') + ':</strong> ' + client.sbx.scaleMgdl(treatment.glucose) + (treatment.glucoseType ? ' (' + translate(treatment.glucoseType) + ')' : '') + '<br/>' : '') +
(treatment.enteredBy ? '<strong>' + translate('Entered By') + ':</strong> ' + treatment.enteredBy + '<br/>' : '') +
(treatment.notes ? '<strong>' + translate('Notes') + ':</strong> ' + treatment.notes : '') +
boluscalcTooltip(treatment)
Expand Down
Loading

0 comments on commit 77a2227

Please sign in to comment.