Skip to content

Home Information - Your home, your data, organized.

Python

Improved javascript handling of polling errors.

Anthony Cassandra1ce92376f426
diff --git a/src/hi/apps/api/views.py b/src/hi/apps/api/views.py
index 57f4f5ba..80d0885f 100644
--- a/src/hi/apps/api/views.py
+++ b/src/hi/apps/api/views.py
@@ -31,9 +31,6 @@ class StatusView( View ):
except (TypeError, ValueError):
logger.warning( f'Bad client timestamp param "{last_server_timestamp}"' )
- # Make sure to set this before querying monitors so that the client
- # does not get a time that is after some newly arriving data.
- #
server_start_datetime = SettingsManager().get_server_start_datetime()
server_datetime = datetimeproxy.now()
diff --git a/src/hi/settings/base.py b/src/hi/settings/base.py
index eaa0c7b7..755a45ed 100644
--- a/src/hi/settings/base.py
+++ b/src/hi/settings/base.py
@@ -285,6 +285,7 @@ PIPELINE = {
'js_after_content_custom': {
'source_filenames': (
'js/main.js',
+ 'js/sound.js',
'js/svg-utils.js',
'js/watchdog.js',
'js/polling.js',
diff --git a/src/hi/static/css/main.css b/src/hi/static/css/main.css
index 8df0cd88..44d1a57a 100644
--- a/src/hi/static/css/main.css
+++ b/src/hi/static/css/main.css
@@ -180,6 +180,37 @@ a.underline:visited {
border: 2px dashed #888888;
}
+#hi-server-error-msg {
+ position: fixed;
+ display: none;
+ top: 50%;
+ left: 50%;
+ transform: translate(-50%, -50%);
+ padding: 1rem;
+ z-index: 200;
+ text-align: center;
+ font-size: 2rem;
+ font-weight: bold;
+ color: #fff12e;
+ background-color: #282425;
+ border-radius: 0.8rem;
+ border: 0.1rem solid #282425;
+ box-shadow: 0 0 1rem rgba(0, 0, 0, 0.5);
+}
+
+#hi-server-error-msg > div {
+ margin-top: 1rem;
+ padding: 3rem;
+ width: 36rem;
+ height: 14rem;
+ color: #282425;
+ font-size: 4rem;
+ background-color: #fff12e;
+ border-bottom-left-radius: 0.8rem;
+ border-bottom-right-radius: 0.8rem;
+ text-align: center;
+}
+
svg[action-state="move"] {
cursor: move;
}
diff --git a/src/hi/static/js/polling.js b/src/hi/static/js/polling.js
index 56f1b113..c041bb3d 100644
--- a/src/hi/static/js/polling.js
+++ b/src/hi/static/js/polling.js
@@ -21,6 +21,8 @@
const ServerPollingWatchdogType = 'serverPolling';
const ServerPollingIntervalMs = 5 * 1000;
+ const PollingErrorNotifyTimeMs = 15 * 1000;
+ const ServerErrorMessageSelector = '#hi-server-error-msg';
const ServerPollingUrl = '/api/status';
const ServerStartTimestampAttr = 'startTimestamp';
const ServerTimestampAttr = 'timestamp';
@@ -29,6 +31,8 @@
let gPollingTimer = null;
let gCssClassElementCache = {};
+ let gLastServerPollSuccessTime = (new Date()).getTime();
+ let gIsServerErrorShowing = false;
let gLastStartServerDate = null;
let gLastServerDate = null;
@@ -58,39 +62,44 @@
$.ajaxSuppressLoader = false;
},
success: function( data, status, xhr ) {
- handleServerResponse( data, status, xhr );
- return false;
+ try {
+ Hi.watchdog.ok( ServerPollingWatchdogType );
+ gLastServerPollSuccessTime = (new Date()).getTime();
+ handleServerResponse( data, status, xhr );
+ return false;
+
+ } catch (e) {
+ console.error( `Exception parsing server response: ${e} (line=${e.lineNumber})` );
+ } finally {
+ gPollingTimer = setTimeout( fetchServerResponse, ServerPollingIntervalMs );
+ }
},
error: function (xhr, ajaxOptions, thrownError) {
- console.error( `Server polling error [${xhr.status}] : ${thrownError}` );
- return false;
+ try {
+ Hi.watchdog.ok( ServerPollingWatchdogType );
+ console.error( `Server polling error [${xhr.status}] : ${thrownError}` );
+ handlePollingError();
+ return false;
+ } catch (e) {
+ console.error( `Exception handling polling error: ${e} (line=${e.lineNumber})` );
+ } finally {
+ gPollingTimer = setTimeout( fetchServerResponse, ServerPollingIntervalMs );
+ }
}
});
}
function handleServerResponse( respObj, textStatus, jqXHR ) {
- try {
- if ( Hi.DEBUG ) { console.log( "Server response: "+JSON.stringify( respObj)); }
- Hi.watchdog.ok( ServerPollingWatchdogType );
- clearPollingTimer();
-
- doServerStartTimeCheck( respObj );
-
- if ( ServerTimestampAttr in respObj ) {
- gLastServerDate = new Date( respObj[ServerTimestampAttr] );
- }
-
- if ( CssClassUpdateMap in respObj ) {
- handleCssClassUpdates( respObj[CssClassUpdateMap] );
- }
-
- } catch (e) {
- console.error( "Exception parsing server response: " + e
- + " (line="+e.lineNumber+")" );
-
- } finally {
- gPollingTimer = setTimeout( fetchServerResponse,
- ServerPollingIntervalMs );
+ if ( Hi.DEBUG ) { console.log( "Server response: "+JSON.stringify( respObj)); }
+
+ doServerStartTimeCheck( respObj );
+
+ if ( ServerTimestampAttr in respObj ) {
+ gLastServerDate = new Date( respObj[ServerTimestampAttr] );
+ }
+
+ if ( CssClassUpdateMap in respObj ) {
+ handleCssClassUpdates( respObj[CssClassUpdateMap] );
}
}
@@ -178,5 +187,32 @@
}
}
+ function handlePollingError() {
+ var nowTime = (new Date()).getTime();
+ var elapsedMs = nowTime - gLastServerPollSuccessTime;
+ if ( Hi.DEBUG ) { console.log( `Polling error elapsedMs=${elapsedMs}` ); }
+ if ( elapsedMs >= PollingErrorNotifyTimeMs ) {
+ notifyServerError();
+ }
+ }
+
+ function notifyServerError() {
+ if ( gIsServerErrorShowing ) {
+ return;
+ }
+ $(ServerErrorMessageSelector).show();
+ gIsServerErrorShowing = true;
+ Hi.sound.startAudibleSignal( "warning" );
+ }
+
+ function clearServerError() {
+ if ( ! gIsServerErrorShowing ) {
+ return;
+ }
+ $(ServerErrorMessageSelector).hide();
+ gIsServerErrorShowing = false;
+ Hi.sound.startAudibleSignal( "info" );
+ }
+
})();
diff --git a/src/hi/static/js/sound.js b/src/hi/static/js/sound.js
new file mode 100644
index 00000000..45922c8b
--- /dev/null
+++ b/src/hi/static/js/sound.js
@@ -0,0 +1,20 @@
+(function() {
+ window.Hi = window.Hi || {};
+
+ const HiSound = {
+ startAudibleSignal: function( signalName ) {
+ _startAudibleSignal( signalName );
+ },
+ };
+
+ window.Hi.sound = HiSound;
+
+ function _startAudibleSignal( signalName ) {
+ if ( Hi.DEBUG ) { console.log( `Starting audible signal: ${signalName}` ); }
+
+
+ // TBD
+
+ }
+
+})();
diff --git a/src/hi/templates/pages/base.html b/src/hi/templates/pages/base.html
index 61ff5318..b1a5f0be 100644
--- a/src/hi/templates/pages/base.html
+++ b/src/hi/templates/pages/base.html
@@ -25,7 +25,14 @@
{% javascript 'js_before_content' %}
{% block content %}{% endblock %}
-
+
+ <div id="hi-server-error-msg">
+ WARNING
+ <div>
+ Server Is Down !
+ </div>
+ </div>
+
{% block footer_pre_javascript %}{% endblock %}
{% javascript 'js_after_content' %}
{% javascript 'js_after_content_custom' %}