Skip to content

Commit

Permalink
lot of refactoring to have common settings for client and server
Browse files Browse the repository at this point in the history
  • Loading branch information
jasoncalabrese committed Jul 30, 2015
1 parent ef86c7c commit 31ce8cb
Show file tree
Hide file tree
Showing 35 changed files with 712 additions and 593 deletions.
142 changes: 31 additions & 111 deletions env.js
Original file line number Diff line number Diff line change
@@ -1,10 +1,13 @@
'use strict';

var env = { };
var _ = require('lodash');
var fs = require('fs');
var crypto = require('crypto');
var consts = require('./lib/constants');
var fs = require('fs');

var env = {
settings: require('./lib/settings')()
};

// Module to constrain all config and environment parsing to one spot.
// See the
Expand All @@ -21,26 +24,13 @@ function config ( ) {
setAPISecret();
setVersion();
setMongo();
setAlarmType();
setEnableAndExtendedSettnigs();
setDefaults();
setThresholds();
updateSettings();

env.isEnabled = isEnabled;
env.anyEnabled = anyEnabled;
env.hasExtendedSetting = hasExtendedSetting;

return env;
}

function isEnabled (feature) {
return env.enable.indexOf(feature) > -1;
}

function anyEnabled (features) {
return _.findIndex(features, isEnabled) > -1;
}

function setSSL() {
env.SSL_KEY = readENV('SSL_KEY');
env.SSL_CERT = readENV('SSL_CERT');
Expand Down Expand Up @@ -121,121 +111,51 @@ function setMongo() {
env.mongo_collection = DB_COLLECTION;
}

//if any of the BG_* thresholds are set, default to `simple` and `predict` otherwise default to only `predict`
function setAlarmType() {
var thresholdsSet = readIntENV('BG_HIGH') || readIntENV('BG_TARGET_TOP') || readIntENV('BG_TARGET_BOTTOM') || readIntENV('BG_LOW');
env.alarm_types = readENV('ALARM_TYPES') || (thresholdsSet ? 'simple' : 'predict');
}
function updateSettings() {

function setEnableAndExtendedSettnigs() {
env.enable = readENV('ENABLE', '');
//TODO: maybe get rid of ALARM_TYPES and only use enable?
if (env.alarm_types.indexOf('simple') > -1) {
env.enable = 'simplealarms ' + env.enable;
}
if (env.alarm_types.indexOf('predict') > -1) {
env.enable = 'ar2 ' + env.enable;
var enable = readENV('ENABLE', '');

function anyEnabled (features) {
return _.findIndex(features, function (feature) {
return enable.indexOf(feature) > -1;
}) > -1;
}

//don't require pushover to be enabled to preserve backwards compatibility if there are extendedSettings for it
if (hasExtendedSetting('PUSHOVER', process.env)) {
env.enable += ' pushover';
enable += ' pushover';
}

if (anyEnabled(['careportal', 'pushover', 'maker'])) {
env.enable += ' treatmentnotify';
enable += ' treatmentnotify';
}

//TODO: figure out something for default plugins, how can they be disabled?
env.enable += ' delta direction upbat errorcodes';
enable += ' delta direction upbat errorcodes';

env.extendedSettings = findExtendedSettings(env.enable, process.env);
}
env.extendedSettings = findExtendedSettings(enable, process.env);

function setDefaults() {

// currently supported keys must defined be here
env.defaults = {
'units': 'mg/dL'
, 'timeFormat': '12'
, 'nightMode': false
, 'showRawbg': 'never'
, 'customTitle': 'Nightscout'
, 'theme': 'default'
, 'alarmUrgentHigh': true
, 'alarmHigh': true
, 'alarmLow': true
, 'alarmUrgentLow': true
, 'alarmTimeAgoWarn': true
, 'alarmTimeAgoWarnMins': 15
, 'alarmTimeAgoUrgent': true
, 'alarmTimeAgoUrgentMins': 30
, 'language': 'en'
};

// add units from separate variable
//TODO: figure out where this is used, should only be in 1 spot
env.defaults.units = env.DISPLAY_UNITS;

// Highest priority per line defaults
env.defaults.timeFormat = readENV('TIME_FORMAT', env.defaults.timeFormat);
env.defaults.nightMode = readENV('NIGHT_MODE', env.defaults.nightMode);
env.defaults.showRawbg = readENV('SHOW_RAWBG', env.defaults.showRawbg);
env.defaults.customTitle = readENV('CUSTOM_TITLE', env.defaults.customTitle);
env.defaults.theme = readENV('THEME', env.defaults.theme);
env.defaults.alarmUrgentHigh = readENV('ALARM_URGENT_HIGH', env.defaults.alarmUrgentHigh);
env.defaults.alarmHigh = readENV('ALARM_HIGH', env.defaults.alarmHigh);
env.defaults.alarmLow = readENV('ALARM_LOW', env.defaults.alarmLow);
env.defaults.alarmUrgentLow = readENV('ALARM_URGENT_LOW', env.defaults.alarmUrgentLow);
env.defaults.alarmTimeAgoWarn = readENV('ALARM_TIMEAGO_WARN', env.defaults.alarmTimeAgoWarn);
env.defaults.alarmTimeAgoWarnMins = readENV('ALARM_TIMEAGO_WARN_MINS', env.defaults.alarmTimeAgoWarnMins);
env.defaults.alarmTimeAgoUrgent = readENV('ALARM_TIMEAGO_URGENT', env.defaults.alarmTimeAgoUrgent);
env.defaults.alarmTimeAgoUrgentMins = readENV('ALARM_TIMEAGO_URGENT_MINS', env.defaults.alarmTimeAgoUrgentMins);
env.defaults.showPlugins = readENV('SHOW_PLUGINS', '');
env.defaults.language = readENV('LANGUAGE', env.defaults.language);

//TODO: figure out something for some plugins to have them shown by default
if (env.defaults.showPlugins !== '') {
env.defaults.showPlugins += ' delta direction upbat';
if (env.defaults.showRawbg === 'always' || env.defaults.showRawbg === 'noise') {
env.defaults.showPlugins += 'rawbg';
}
}
}

function setThresholds() {
env.thresholds = {
bg_high: readIntENV('BG_HIGH', 260)
, bg_target_top: readIntENV('BG_TARGET_TOP', 180)
, bg_target_bottom: readIntENV('BG_TARGET_BOTTOM', 80)
, bg_low: readIntENV('BG_LOW', 55)
var envNameOverrides = {
UNITS: 'DISPLAY_UNITS'
};

//NOTE: using +/- 1 here to make the thresholds look visibly wrong in the UI
// if all thresholds were set to the same value you should see 4 lines stacked right on top of each other
if (env.thresholds.bg_target_bottom >= env.thresholds.bg_target_top) {
console.warn('BG_TARGET_BOTTOM(' + env.thresholds.bg_target_bottom + ') was >= BG_TARGET_TOP(' + env.thresholds.bg_target_top + ')');
env.thresholds.bg_target_bottom = env.thresholds.bg_target_top - 1;
console.warn('BG_TARGET_BOTTOM is now ' + env.thresholds.bg_target_bottom);
}
env.settings.eachSettingAsEnv(function settingFromEnv (name) {
var envName = envNameOverrides[name] || name;
return readENV(envName);
});

if (env.thresholds.bg_target_top <= env.thresholds.bg_target_bottom) {
console.warn('BG_TARGET_TOP(' + env.thresholds.bg_target_top + ') was <= BG_TARGET_BOTTOM(' + env.thresholds.bg_target_bottom + ')');
env.thresholds.bg_target_top = env.thresholds.bg_target_bottom + 1;
console.warn('BG_TARGET_TOP is now ' + env.thresholds.bg_target_top);
//TODO: maybe get rid of ALARM_TYPES and only use enable?
if (env.settings.alarmTypes.indexOf('simple') > -1) {
enable = 'simplealarms ' + enable;
}

if (env.thresholds.bg_low >= env.thresholds.bg_target_bottom) {
console.warn('BG_LOW(' + env.thresholds.bg_low + ') was >= BG_TARGET_BOTTOM(' + env.thresholds.bg_target_bottom + ')');
env.thresholds.bg_low = env.thresholds.bg_target_bottom - 1;
console.warn('BG_LOW is now ' + env.thresholds.bg_low);
if (env.settings.alarmTypes.indexOf('predict') > -1) {
enable = 'ar2 ' + enable;
}

if (env.thresholds.bg_high <= env.thresholds.bg_target_top) {
console.warn('BG_HIGH(' + env.thresholds.bg_high + ') was <= BG_TARGET_TOP(' + env.thresholds.bg_target_top + ')');
env.thresholds.bg_high = env.thresholds.bg_target_top + 1;
console.warn('BG_HIGH is now ' + env.thresholds.bg_high);
}
env.settings.enable = enable;

env.settings.processRawSettings();
}

function readIntENV(varName, defaultValue) {
Expand Down
12 changes: 3 additions & 9 deletions lib/api/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -23,23 +23,17 @@ function create (env, ctx) {
app.enable('api');
}

if (env.enable) {
app.enabledOptions = env.enable || '';
if (env.settings.enable) {
app.extendedClientSettings = ctx.plugins && ctx.plugins.extendedClientSettings ? ctx.plugins.extendedClientSettings(env.extendedSettings) : {};
env.enable.toLowerCase().split(' ').forEach(function (value) {
env.settings.enable.toLowerCase().split(' ').forEach(function (value) {
var enable = value.trim();
console.info('enabling feature:', enable);
app.enable(enable);
});
}

app.defaults = env.defaults || '';

app.set('title', [app.get('name'), 'API', app.get('version')].join(' '));

app.thresholds = env.thresholds;
app.alarm_types = env.alarm_types;

// Start setting up routes
if (app.enabled('api')) {
// experiments
Expand All @@ -54,7 +48,7 @@ function create (env, ctx) {
app.use('/', require('./notifications-api')(app, wares, ctx));

// Status
app.use('/', require('./status')(app, wares));
app.use('/', require('./status')(app, wares, env));
return app;
}

Expand Down
75 changes: 36 additions & 39 deletions lib/api/status.js
Original file line number Diff line number Diff line change
@@ -1,51 +1,48 @@
'use strict';

function configure (app, wares) {
function configure (app, wares, env) {
var express = require('express'),
api = express.Router( )
;
api = express.Router( )
;

api.use(wares.extensions([
'json', 'svg', 'csv', 'txt', 'png', 'html', 'js'
]));
// Status badge/text/json
api.get('/status', function (req, res) {
var info = { status: 'ok'
, apiEnabled: app.enabled('api')
, careportalEnabled: app.enabled('api') && app.enabled('careportal')
, enabledOptions: app.enabledOptions
, extendedSettings: app.extendedClientSettings
, defaults: app.defaults
, units: app.get('units')
, head: wares.get_head( )
, version: app.get('version')
, thresholds: app.thresholds
, alarm_types: app.alarm_types
, name: app.get('name')};
var badge = 'http://img.shields.io/badge/Nightscout-OK-green';
return res.format({
html: function ( ) {
res.send('<h1>STATUS OK</h1>');
},
png: function ( ) {
res.redirect(302, badge + '.png');
},
svg: function ( ) {
res.redirect(302, badge + '.svg');
},
js: function ( ) {
var head = 'this.serverSettings =';
var body = JSON.stringify(info);
var tail = ';';
res.send([head, body, tail].join(' '));
},
text: function ( ) {
res.send('STATUS OK');
},
json: function ( ) {
res.json(info);
}
});
var info = { status: 'ok'
, name: app.get('name')
, version: app.get('version')
, apiEnabled: app.enabled('api')
, careportalEnabled: app.enabled('api') && env.settings.enable.indexOf('careportal') > -1
, head: wares.get_head( )
, settings: env.settings
, extendedSettings: app.extendedClientSettings
};
var badge = 'http://img.shields.io/badge/Nightscout-OK-green';
return res.format({
html: function ( ) {
res.send('<h1>STATUS OK</h1>');
},
png: function ( ) {
res.redirect(302, badge + '.png');
},
svg: function ( ) {
res.redirect(302, badge + '.svg');
},
js: function ( ) {
var head = 'this.serverSettings =';
var body = JSON.stringify(info);
var tail = ';';
res.send([head, body, tail].join(' '));
},
text: function ( ) {
res.send('STATUS OK');
},
json: function ( ) {
res.json(info);
}
});
});

return api;
Expand Down
Loading

0 comments on commit 31ce8cb

Please sign in to comment.