Skip to content

Commit

Permalink
Add runtime state tracking to Nightscout, where client now checks if …
Browse files Browse the repository at this point in the history
…the server has loaded initial data from Mongo before the user is directed to the UI
  • Loading branch information
sulkaharo committed Nov 14, 2020
1 parent ead1370 commit 376c790
Show file tree
Hide file tree
Showing 7 changed files with 21 additions and 3 deletions.
1 change: 1 addition & 0 deletions lib/api/status.js
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ function configure (app, wares, env, ctx) {
, settings: settings
, extendedSettings: extended
, authorized: ctx.authorization.authorize(authToken)
, runtimeState: ctx.runtimeState
};

var badge = 'http://img.shields.io/badge/Nightscout-OK-green';
Expand Down
6 changes: 6 additions & 0 deletions lib/client/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,12 @@ client.init = function init (callback) {
, url: src
, headers: client.headers()
}).done(function success (serverSettings) {
if (serverSettings.runtimeState !== 'loaded') {
console.log('Server is still loading data');
$('#loadingMessageText').html('Server is loading data, retrying load in 2 seconds');
window.setTimeout(window.Nightscout.client.init(), 2000);
return;
}
client.settingsFailed = false;
console.log('Application appears to be online');
$('#centerMessagePanel').hide();
Expand Down
2 changes: 0 additions & 2 deletions lib/plugins/ar2.js
Original file line number Diff line number Diff line change
Expand Up @@ -67,8 +67,6 @@ function init (ctx) {

var prop = sbx.properties.ar2;

console.log('ar2', prop);

if (prop && prop.level) {
const notify = {
level: prop.level
Expand Down
1 change: 1 addition & 0 deletions lib/plugins/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,7 @@ function init (ctx) {
, require('./timeago')(ctx)
, require('./basalprofile')(ctx)
, require('./dbsize')(ctx)
, require('./runtimestate')(ctx)
];

plugins.registerServerDefaults = function registerServerDefaults () {
Expand Down
1 change: 1 addition & 0 deletions lib/sandbox.js
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@ function init () {
reset();

sbx.runtimeEnvironment = 'server';
sbx.runtimeState = ctx.runtimeState;
sbx.time = Date.now();
sbx.settings = env.settings;
sbx.data = ctx.ddata.clone();
Expand Down
11 changes: 11 additions & 0 deletions lib/server/bootevent.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,11 @@ var UPDATE_THROTTLE = 5000;

function boot (env, language) {

function startBoot(ctx, next) {
ctx.runtimeState = 'booting';
next();
}

//////////////////////////////////////////////////
// Check Node version.
// Latest Node 8 LTS and Latest Node 10 LTS are recommended and supported.
Expand Down Expand Up @@ -255,6 +260,10 @@ function boot (env, language) {
ctx.bus.emit('data-processed');
});

ctx.bus.on('data-processed', function processed ( ) {
ctx.runtimeState = 'loaded';
});

ctx.bus.on('notification', ctx.pushnotify.emitNotification);

next( );
Expand Down Expand Up @@ -289,12 +298,14 @@ function boot (env, language) {
return next();
}

ctx.runtimeState = 'booted';
ctx.bus.uptime( );

next( );
}

return require('bootevent')( )
.acquire(startBoot)
.acquire(checkNodeVersion)
.acquire(checkEnv)
.acquire(augmentSettings)
Expand Down
2 changes: 1 addition & 1 deletion lib/settings.js
Original file line number Diff line number Diff line change
Expand Up @@ -167,7 +167,7 @@ function init () {
}

//TODO: getting sent in status.json, shouldn't be
settings.DEFAULT_FEATURES = ['bgnow', 'delta', 'direction', 'timeago', 'devicestatus', 'upbat', 'errorcodes', 'profile', 'dbsize'];
settings.DEFAULT_FEATURES = ['bgnow', 'delta', 'direction', 'timeago', 'devicestatus', 'upbat', 'errorcodes', 'profile', 'dbsize', 'runtimestate'];

var wasSet = [];

Expand Down

0 comments on commit 376c790

Please sign in to comment.