From 15eff26a888e17d10941329d67308040488e8c1a Mon Sep 17 00:00:00 2001 From: Sulka Haro Date: Sat, 20 Apr 2019 20:12:36 +0300 Subject: [PATCH] Fix stale data alarms on latest iOS (#4542) * Suspend TimeAgo reports for 15 seconds if the app has been sleeping. Add a BACK link to reports (due to iOS now not resetting springboard web apps, so it's impossible to go back to the main view) * Move detection to another plugin call --- lib/plugins/timeago.js | 135 +++++++++++++++++++++++------------------ static/css/report.css | 4 +- views/reportindex.html | 2 +- 3 files changed, 79 insertions(+), 62 deletions(-) diff --git a/lib/plugins/timeago.js b/lib/plugins/timeago.js index 0e460eb87b07..e63d27fb331c 100644 --- a/lib/plugins/timeago.js +++ b/lib/plugins/timeago.js @@ -2,18 +2,22 @@ var levels = require('../levels'); var times = require('../times'); +var lastChecked = new Date(); +var lastSuspendTime = new Date("1900-01-01"); -function init (ctx) { +function init(ctx) { var translate = ctx.language.translate; var timeago = { - name: 'timeago' - , label: 'Timeago' - , pluginType: 'pill-status' - , pillFlip: true + name: 'timeago', + label: 'Timeago', + pluginType: 'pill-status', + pillFlip: true }; - timeago.checkNotifications = function checkNotifications (sbx) { + timeago.checkNotifications = function checkNotifications(sbx) { + + console.log('timeago.checkNotifications'); if (!sbx.extendedSettings.enableAlerts) { return; @@ -31,44 +35,59 @@ function init (ctx) { return lines.join('\n'); } - function sendAlarm (opts) { + function sendAlarm(opts) { var agoDisplay = timeago.calcDisplay(lastSGVEntry, sbx.time); sbx.notifications.requestNotify({ - level: opts.level - , title: translate('Stale data, check rig?') - , message: buildMessage(agoDisplay) - , eventName: timeago.name - , plugin: timeago - , group: 'Time Ago' - , pushoverSound: opts.pushoverSound - , debug: agoDisplay + level: opts.level, + title: translate('Stale data, check rig?'), + message: buildMessage(agoDisplay), + eventName: timeago.name, + plugin: timeago, + group: 'Time Ago', + pushoverSound: opts.pushoverSound, + debug: agoDisplay }); } var status = timeago.checkStatus(sbx); if (status === 'urgent') { sendAlarm({ - level: levels.URGENT - , pushoverSound: 'echo' + level: levels.URGENT, + pushoverSound: 'echo' }); } else if (status === 'warn') { sendAlarm({ - level: levels.WARN - , pushoverSound: 'echo' + level: levels.WARN, + pushoverSound: 'echo' }); } }; - timeago.checkStatus = function checkStatus (sbx) { + timeago.checkStatus = function checkStatus(sbx) { + + // Check if the app has been suspended; if yes, snooze data missing alarmn for 15 seconds + var now = new Date(); + var delta = now.getTime() - lastChecked.getTime(); + lastChecked = now; + + if (delta > 15 * 1000) { // Looks like we've been hibernating + lastSuspendTime = now; + } + + var timeSinceLastSuspended = now.getTime() - lastSuspendTime.getTime(); - var lastSGVEntry = sbx.lastSGVEntry() - , warn = sbx.settings.alarmTimeagoWarn - , warnMins = sbx.settings.alarmTimeagoWarnMins || 15 - , urgent = sbx.settings.alarmTimeagoUrgent - , urgentMins = sbx.settings.alarmTimeagoUrgentMins || 30 - ; + if (timeSinceLastSuspended < (15 * 10000)) { + console.log('Hibernation detected, suspending timeago alarm'); + return 'current'; + } + + var lastSGVEntry = sbx.lastSGVEntry(), + warn = sbx.settings.alarmTimeagoWarn, + warnMins = sbx.settings.alarmTimeagoWarnMins || 15, + urgent = sbx.settings.alarmTimeagoUrgent, + urgentMins = sbx.settings.alarmTimeagoUrgentMins || 30; function isStale(mins) { return sbx.time - lastSGVEntry.mills > times.mins(mins).msecs; @@ -88,63 +107,60 @@ function init (ctx) { }; - timeago.isMissing = function isMissing (opts) { + timeago.isMissing = function isMissing(opts) { if (!opts || !opts.entry || isNaN(opts.entry.mills) || isNaN(opts.time) || isNaN(opts.timeSince)) { return { - label: translate('time ago') - , shortLabel: translate('ago') + label: translate('time ago'), + shortLabel: translate('ago') }; } }; - timeago.inTheFuture = function inTheFuture (opts) { + timeago.inTheFuture = function inTheFuture(opts) { if (opts.entry.mills - times.mins(5).msecs > opts.time) { return { - label: translate('in the future') - , shortLabel: translate('future') + label: translate('in the future'), + shortLabel: translate('future') }; } }; - timeago.almostInTheFuture = function almostInTheFuture (opts) { + timeago.almostInTheFuture = function almostInTheFuture(opts) { if (opts.entry.mills > opts.time) { return { - value: 1 - , label: translate('min ago') - , shortLabel: 'm' + value: 1, + label: translate('min ago'), + shortLabel: 'm' }; } }; - timeago.isLessThan = function isLessThan (limit, divisor, label, shortLabel) { - return function checkIsLessThan (opts) { + timeago.isLessThan = function isLessThan(limit, divisor, label, shortLabel) { + return function checkIsLessThan(opts) { if (opts.timeSince < limit) { return { - value: Math.max(1, Math.round(opts.timeSince / divisor)) - , label: label - , shortLabel: shortLabel + value: Math.max(1, Math.round(opts.timeSince / divisor)), + label: label, + shortLabel: shortLabel }; } }; }; timeago.resolvers = [ - timeago.isMissing - , timeago.inTheFuture - , timeago.almostInTheFuture - , timeago.isLessThan(times.mins(2).msecs, times.min().msecs, 'min ago', 'm') - , timeago.isLessThan(times.hour().msecs, times.min().msecs, 'mins ago', 'm') - , timeago.isLessThan(times.hours(2).msecs, times.hour().msecs, 'hour ago', 'h') - , timeago.isLessThan(times.day().msecs, times.hour().msecs, 'hours ago', 'h') - , timeago.isLessThan(times.days(2).msecs, times.day().msecs, 'day ago', 'd') - , timeago.isLessThan(times.week().msecs, times.day().msecs, 'days ago', 'd') - , function ( ) { return { label: 'long ago', shortLabel: 'ago' } } + timeago.isMissing, timeago.inTheFuture, timeago.almostInTheFuture, timeago.isLessThan(times.mins(2).msecs, times.min().msecs, 'min ago', 'm'), timeago.isLessThan(times.hour().msecs, times.min().msecs, 'mins ago', 'm'), timeago.isLessThan(times.hours(2).msecs, times.hour().msecs, 'hour ago', 'h'), timeago.isLessThan(times.day().msecs, times.hour().msecs, 'hours ago', 'h'), timeago.isLessThan(times.days(2).msecs, times.day().msecs, 'day ago', 'd'), timeago.isLessThan(times.week().msecs, times.day().msecs, 'days ago', 'd'), + function () { + return { + label: 'long ago', + shortLabel: 'ago' + } + } ]; - timeago.calcDisplay = function calcDisplay (entry, time) { + timeago.calcDisplay = function calcDisplay(entry, time) { var opts = { - time: time - , entry: entry + time: time, + entry: entry }; if (time && entry && entry.mills) { @@ -159,15 +175,16 @@ function init (ctx) { } }; - timeago.updateVisualisation = function updateVisualisation (sbx) { + timeago.updateVisualisation = function updateVisualisation(sbx) { var agoDisplay = timeago.calcDisplay(sbx.lastSGVEntry(), sbx.time); var inRetroMode = sbx.data.inRetroMode; sbx.pluginBase.updatePillText(timeago, { - value: inRetroMode ? null : agoDisplay.value - , label: inRetroMode ? translate('RETRO') : translate(agoDisplay.label) - //no warning/urgent class when in retro mode - , pillClass: inRetroMode ? 'current' : timeago.checkStatus(sbx) + value: inRetroMode ? null : agoDisplay.value, + label: inRetroMode ? translate('RETRO') : translate(agoDisplay.label) + //no warning/urgent class when in retro mode + , + pillClass: inRetroMode ? 'current' : timeago.checkStatus(sbx) }); }; diff --git a/static/css/report.css b/static/css/report.css index 9ec304cb1a7e..afe046f00d95 100644 --- a/static/css/report.css +++ b/static/css/report.css @@ -25,7 +25,7 @@ body { #tabnav { text-align: left; margin: 1em 0 1em 0; - font: bold 11px verdana, arial, sans-serif; + font: bold 11pt verdana, arial, sans-serif; border-bottom: 1px solid #6c6; list-style-type: none; padding: 3px 10px 3px 10px; @@ -59,7 +59,7 @@ body { text-align: left; padding: 4px; font-size: 14px; - line-height: 15px; + line-height: 15pt; background: white; border: 2px solid black; border-radius: 8px; diff --git a/views/reportindex.html b/views/reportindex.html index a2eec3d3c22b..d2c1c8b1ebec 100644 --- a/views/reportindex.html +++ b/views/reportindex.html @@ -29,7 +29,7 @@ -

Nightscout reporting

+

Nightscout reporting Back main view