Skip to content

Commit

Permalink
Merge pull request #2736 from sulkaharo/wip/variability
Browse files Browse the repository at this point in the history
Variability reporting, Webpack bundling
  • Loading branch information
sulkaharo authored Aug 23, 2017
2 parents 6ef8d8c + 530df90 commit 2afafab
Show file tree
Hide file tree
Showing 82 changed files with 9,258 additions and 1,141 deletions.
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -21,3 +21,5 @@ coverage/

npm-debug.log
*.heapsnapshot

/tmp
228 changes: 148 additions & 80 deletions app.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,95 +4,163 @@ var _ = require('lodash');
var express = require('express');
var compression = require('compression');
var bodyParser = require('body-parser');
var prettyjson = require('prettyjson');


function create(env, ctx) {
var app = express();
var appInfo = env.name + ' ' + env.version;
app.set('title', appInfo);
app.enable('trust proxy'); // Allows req.secure test on heroku https connections.

if (ctx.bootErrors && ctx.bootErrors.length > 0) {
app.get('*', require('./lib/booterror')(ctx));
return app;
}

if (env.settings.isEnabled('cors')) {
var allowOrigin = _.get(env, 'extendedSettings.cors.allowOrigin') || '*';
console.info('Enabled CORS, allow-origin:', allowOrigin);
app.use(function allowCrossDomain(req, res, next) {
res.header('Access-Control-Allow-Origin', allowOrigin);
res.header('Access-Control-Allow-Methods', 'GET,PUT,POST,DELETE,OPTIONS');
res.header('Access-Control-Allow-Headers', 'Content-Type, Authorization, Content-Length, X-Requested-With');

// intercept OPTIONS method
if ('OPTIONS' === req.method) {
res.send(200);
} else {
next();
}
});
}

///////////////////////////////////////////////////
// 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
// fallback to standard filter function
return compression.filter(req, res);
}
}));
// app.use(bodyParser({limit: 1048576 * 50, extended: true }));

//if (env.api_secret) {
// console.log("API_SECRET", env.api_secret);
//}
app.use('/api/v1', bodyParser({
limit: 1048576 * 50
}), api);

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);

// expose swagger.yaml
app.get('/swagger.yaml', function(req, res) {
res.sendFile(__dirname + '/swagger.yaml');
});

function create (env, ctx) {
var app = express();
var appInfo = env.name + ' ' + env.version;
app.set('title', appInfo);
app.enable('trust proxy'); // Allows req.secure test on heroku https connections.
if (env.settings.isEnabled('dumps')) {
var heapdump = require('heapdump');
app.get('/api/v2/dumps/start', function(req, res) {
var path = new Date().toISOString() + '.heapsnapshot';
path = path.replace(/:/g, '-');
console.info('writing dump to', path);
heapdump.writeSnapshot(path);
res.send('wrote dump to ' + path);
});
}

if (ctx.bootErrors && ctx.bootErrors.length > 0) {
app.get('*', require('./lib/booterror')(ctx));
return app;
}

if (env.settings.isEnabled('cors')) {
var allowOrigin = _.get(env, 'extendedSettings.cors.allowOrigin') || '*';
console.info('Enabled CORS, allow-origin:', allowOrigin);
app.use(function allowCrossDomain (req, res, next) {
res.header('Access-Control-Allow-Origin', allowOrigin);
res.header('Access-Control-Allow-Methods', 'GET,PUT,POST,DELETE,OPTIONS');
res.header('Access-Control-Allow-Headers', 'Content-Type, Authorization, Content-Length, X-Requested-With');

// intercept OPTIONS method
if ('OPTIONS' === req.method) {
res.send(200);
} else {
next();
}

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

// Allow static resources to be cached for week
var maxAge = 7 * 24 * 60 * 60 * 1000;

if (process.env.NODE_ENV === 'development') {
maxAge = 10;
console.log('Development environment detected, setting static file cache age to 10 seconds');

app.get('/nightscout.appcache', function(req, res) {
res.sendStatus(404);
});
}

//TODO: JC - changed cache to 1 hour from 30d ays to bypass cache hell until we have a real solution
var staticFiles = express.static(env.static_files, {
maxAge: maxAge
});
}

///////////////////////////////////////////////////
// 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
// fallback to standard filter function
return compression.filter(req, res);
}}));
// app.use(bodyParser({limit: 1048576 * 50, extended: true }));

//if (env.api_secret) {
// console.log("API_SECRET", env.api_secret);
//}
app.use('/api/v1', bodyParser({limit: 1048576 * 50 }), api);

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);

// expose swagger.yaml
app.get('/swagger.yaml', function (req, res) {
res.sendFile(__dirname + '/swagger.yaml');
});

if (env.settings.isEnabled('dumps')) {
var heapdump = require('heapdump');
app.get('/api/v2/dumps/start', function (req, res) {
var path = new Date().toISOString() + '.heapsnapshot';
path = path.replace(/:/g, '-');
console.info('writing dump to', path);
heapdump.writeSnapshot(path);
res.send('wrote dump to ' + path);

// serve the static content
app.use(staticFiles);

var tmpFiles = express.static('tmp', {
maxAge: maxAge
});
}

// serve the static content
app.use(tmpFiles);

if (process.env.NODE_ENV !== 'development') {

//app.get('/package.json', software);
console.log('Production environment detected, enabling Minify');

// define static server
//TODO: JC - changed cache to 1 hour from 30d ays to bypass cache hell until we have a real solution
var staticFiles = express.static(env.static_files, {maxAge: 60 * 60 * 1000});
var minify = require('express-minify');
var myUglifyJS = require('uglify-js');
var myCssmin = require('cssmin');

// serve the static content
app.use(staticFiles);
app.use(minify({
js_match: /\.js/,
css_match: /\.css/,
sass_match: /scss/,
less_match: /less/,
stylus_match: /stylus/,
coffee_match: /coffeescript/,
json_match: /json/,
uglifyJS: myUglifyJS,
cssmin: myCssmin,
cache: __dirname + '/cache',
onerror: undefined,
}));

var bundle = require('./bundle')(env);
app.use(bundle);
}

// Handle errors with express's errorhandler, to display more readable error messages.
var errorhandler = require('errorhandler');
//if (process.env.NODE_ENV === 'development') {
// if this is dev environment, package scripts on the fly
// if production, rely on postinstall script to run packaging for us

if (process.env.NODE_ENV === 'development') {

var webpack = require("webpack");
var webpack_conf = require('./webpack.config');

webpack(webpack_conf, function(err, stats) {

var json = stats.toJson() // => webpack --json

var options = {
noColor: true
};

console.log(prettyjson.render(json.errors, options));
console.log(prettyjson.render(json.assets, options));

});
}

// Handle errors with express's errorhandler, to display more readable error messages.
var errorhandler = require('errorhandler');
//if (process.env.NODE_ENV === 'development') {
app.use(errorhandler());
//}
return app;
//}
return app;
}
module.exports = create;

module.exports = create;
45 changes: 29 additions & 16 deletions bundle/bundle.source.js
Original file line number Diff line number Diff line change
@@ -1,21 +1,34 @@
(function () {
import '../static/css/drawer.css';
import '../static/css/dropdown.css';
import '../static/css/sgv.css';
import '../node_modules/jquery.tipsy/src/jquery.tipsy.css';

window._ = require('lodash');
window.d3 = require('d3');
window.$ = window.jQuery = require('jquery');
window.moment = require('moment-timezone');
window.sugar = require('sugar');
window.crossfilter = require('crossfilter');
window.Nightscout = window.Nightscout || {};

window.Nightscout = {
client: require('../lib/client')
, units: require('../lib/units')()
, report_plugins: require('../lib/report_plugins/')()
, admin_plugins: require('../lib/admin_plugins/')()
};
$ = require("jquery");

console.info('Nightscout bundle ready');
require('jquery-ui-bundle');

})();
window._ = require('lodash');
window.d3 = require('d3');

require('jquery.tipsy');

window.Storage = require('js-storage');

require('flot');
require('../node_modules/flot/jquery.flot.time');
require('../node_modules/flot/jquery.flot.pie');
require('../node_modules/flot/jquery.flot.fillbetween');

window.moment = require('moment-timezone');

window.Nightscout = window.Nightscout || {};

window.Nightscout = {
client: require('../lib/client'),
units: require('../lib/units')(),
report_plugins: require('../lib/report_plugins/')(),
admin_plugins: require('../lib/admin_plugins/')()
};

console.info('Nightscout bundle ready');
5 changes: 3 additions & 2 deletions lib/client/boluscalc.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,13 @@
var _ = require('lodash');
var moment = require('moment-timezone');
var times = require('../times');
var Storages = require('js-storage');

function init(client, $) {
var boluscalc = { };

var translate = client.translate;
var storage = $.localStorage;
var storage = Storages.localStorage;
var iob = client.plugins('iob');
var cob = client.plugins('cob');

Expand Down Expand Up @@ -176,7 +177,7 @@ function init(client, $) {
$('#bc_quickpick').val(-1);
$('#bc_preBolus').val(0);
$('#bc_notes').val('');
$('#bc_enteredBy').val($.localStorage.get('enteredBy') || '');
$('#bc_enteredBy').val(Storages.localStorage.get('enteredBy') || '');
$('#bc_nowtime').prop('checked', true);
$('#bc_othercorrection').val(0);
$('#bc_profile').val(client.profilefunctions.activeProfileToTime());
Expand Down
3 changes: 2 additions & 1 deletion lib/client/browser-settings.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,13 @@ var _ = require('lodash');

// VERSION 1 - 0.9.0 - 2015-Nov-07 - initial version
var STORAGE_VERSION = 1;
var Storages = require('js-storage');

function init (client, serverSettings, $) {

serverSettings = serverSettings || {settings: {}};

var storage = $.localStorage;
var storage = Storages.localStorage;
var settings = require('../settings')();

function loadForm ( ) {
Expand Down
3 changes: 2 additions & 1 deletion lib/client/careportal.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,13 @@ var moment = require('moment-timezone');
var _ = require('lodash');
var parse_duration = require('parse-duration'); // https://www.npmjs.com/package/parse-duration
var times = require('../times');
var Storages = require('js-storage');

function init (client, $) {
var careportal = { };

var translate = client.translate;
var storage = $.localStorage;
var storage = Storages.localStorage;

careportal.allEventTypes = client.plugins.getAllEventTypes(client.sbx);

Expand Down
Loading

0 comments on commit 2afafab

Please sign in to comment.