Skip to content

Commit

Permalink
Merge pull request #5394 from nightscout/fix_timeago
Browse files Browse the repository at this point in the history
Fix timeago alarms (again)
  • Loading branch information
sulkaharo authored Jan 1, 2020
2 parents 78be0f6 + 9817b03 commit df03577
Show file tree
Hide file tree
Showing 3 changed files with 10 additions and 40 deletions.
3 changes: 2 additions & 1 deletion lib/client/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -774,7 +774,8 @@ client.load = function load (serverSettings, callback) {

function updateClock () {
updateClockDisplay();
var interval = (60 - (new Date()).getSeconds()) * 1000 + 5;
// Update at least every 15 seconds
var interval = Math.min(15 * 1000, (60 - (new Date()).getSeconds()) * 1000 + 5);
setTimeout(updateClock, interval);

updateTimeAgo();
Expand Down
20 changes: 8 additions & 12 deletions lib/plugins/timeago.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,10 @@
var levels = require('../levels');
var times = require('../times');
var lastChecked = new Date();
var lastSuspendTime = new Date("1900-01-01");
var lastRecoveryTimeFromSuspend = new Date("1900-01-01");

function init(ctx) {
var translate = ctx.language.translate;
var heartbeatMs = ctx.settings.heartbeat * 1000;

var timeago = {
name: 'timeago',
Expand Down Expand Up @@ -72,19 +71,16 @@ function init(ctx) {

function isHibernationDetected() {
if (sbx.runtimeEnvironment === 'client') {
if (delta > 15 * 1000) { // Looks like we've been hibernating
lastSuspendTime = now;
if (delta > 20 * 1000) { // Looks like we've been hibernating
lastRecoveryTimeFromSuspend = now;
}
var timeSinceLastRecovered = now.getTime() - lastRecoveryTimeFromSuspend.getTime();
return timeSinceLastRecovered < (10 * 1000);
}

var timeSinceLastSuspended = now.getTime() - lastSuspendTime.getTime();
// Assume server never hibernates, or if it does, it's alarm-worthy
return false;

return timeSinceLastSuspended < (10 * 1000);
} else if (sbx.runtimeEnvironment === 'server') {
return delta > 2 * heartbeatMs;
} else {
console.error('Cannot detect hibernation, because runtimeEnvironment is not detected from sbx.runtimeEnvironment:', sbx.runtimeEnvironment);
return false;
}
}

if (isHibernationDetected()) {
Expand Down
27 changes: 0 additions & 27 deletions tests/timeago.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -43,33 +43,6 @@ describe('timeago', function() {
done();
});

it('should suspend alarms due to hibernation when 2 heartbeats are skipped on server', function() {
ctx.ddata.sgvs = [{ mills: Date.now() - times.mins(16).msecs, mgdl: 100, type: 'sgv' }];

var sbx = freshSBX()
var status = timeago.checkStatus(sbx);
// By default (no hibernation detected) a warning should be given
// we force no hibernation by checking status twice
status = timeago.checkStatus(sbx);
should.equal(status, 'warn');

// 10ms more than suspend-threshold to prevent flapping tests
var timeoutMs = 2 * ctx.settings.heartbeat * 1000 + 100;
return new Promise(function(resolve, reject) {
setTimeout(function() {
status = timeago.checkStatus(sbx);
// Because hibernation should now be detected, no warning should be given
should.equal(status, 'current');

// We immediately ask status again, so hibernation should not be detected anymore,
// and we should receive a warning again
status = timeago.checkStatus(sbx);
should.equal(status, 'warn');

resolve()
}, timeoutMs)
})
});

it('should trigger a warning when data older than 15m', function(done) {
ctx.notifications.initRequests();
Expand Down

0 comments on commit df03577

Please sign in to comment.