From 376c790710999844040c69c587b795da48dedbd4 Mon Sep 17 00:00:00 2001 From: Sulka Haro Date: Sat, 14 Nov 2020 11:05:43 +0200 Subject: [PATCH] Add runtime state tracking to Nightscout, where client now checks if the server has loaded initial data from Mongo before the user is directed to the UI --- lib/api/status.js | 1 + lib/client/index.js | 6 ++++++ lib/plugins/ar2.js | 2 -- lib/plugins/index.js | 1 + lib/sandbox.js | 1 + lib/server/bootevent.js | 11 +++++++++++ lib/settings.js | 2 +- 7 files changed, 21 insertions(+), 3 deletions(-) diff --git a/lib/api/status.js b/lib/api/status.js index b630d629593..56196114a07 100644 --- a/lib/api/status.js +++ b/lib/api/status.js @@ -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'; diff --git a/lib/client/index.js b/lib/client/index.js index c329c5cec0b..aeb40642e2d 100644 --- a/lib/client/index.js +++ b/lib/client/index.js @@ -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(); diff --git a/lib/plugins/ar2.js b/lib/plugins/ar2.js index 7a5d5e5dcfa..5232feb0245 100644 --- a/lib/plugins/ar2.js +++ b/lib/plugins/ar2.js @@ -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 diff --git a/lib/plugins/index.js b/lib/plugins/index.js index 77a87bcd94d..d6bae73bdbc 100644 --- a/lib/plugins/index.js +++ b/lib/plugins/index.js @@ -75,6 +75,7 @@ function init (ctx) { , require('./timeago')(ctx) , require('./basalprofile')(ctx) , require('./dbsize')(ctx) + , require('./runtimestate')(ctx) ]; plugins.registerServerDefaults = function registerServerDefaults () { diff --git a/lib/sandbox.js b/lib/sandbox.js index 4bcee3b40e4..6805bee6982 100644 --- a/lib/sandbox.js +++ b/lib/sandbox.js @@ -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(); diff --git a/lib/server/bootevent.js b/lib/server/bootevent.js index 4ee45e054d5..2c75fad59c3 100644 --- a/lib/server/bootevent.js +++ b/lib/server/bootevent.js @@ -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. @@ -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( ); @@ -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) diff --git a/lib/settings.js b/lib/settings.js index 845cea07628..9a02d75f647 100644 --- a/lib/settings.js +++ b/lib/settings.js @@ -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 = [];