From 9d4a214ba0b10303f0f42e4943aa140d0bc22be0 Mon Sep 17 00:00:00 2001 From: Ian Gulliver Date: Thu, 31 Dec 2015 12:16:24 -0800 Subject: [PATCH] Show median periods. --- static/babystats.js | 18 ++++++++++++++---- 1 file changed, 14 insertions(+), 4 deletions(-) diff --git a/static/babystats.js b/static/babystats.js index 63929d0..a522cb0 100644 --- a/static/babystats.js +++ b/static/babystats.js @@ -784,19 +784,29 @@ BabyStats.prototype.updateDisplayPage_ = function() { 'never'; } - var counts = [0, 0, 0, 0, 0]; + var timestamps = [[], [], [], [], []]; tile.messages.forEach(function(message) { cutoffs.forEach(function(cutoff, i) { var timeSince = now - message.created; if (timeSince < cutoff[1]) { - counts[i]++; + // Sample belongs in this bucket + timestamps[i].push(message.created); } }.bind(this)); }.bind(this)); cutoffs.forEach(function(cutoff, i) { - this.displayEventCountCells_[tile.type][cutoff[0]].textContent = - counts[i]; + var text = timestamps[i].length.toString(); + if (timestamps[i].length >= 2) { + var deltas = []; + for (var j = 1; j < timestamps[i].length; j++) { + deltas.push(timestamps[i][j] - timestamps[i][j - 1]); + } + deltas.sort(); + var median = deltas[Math.floor(deltas.length / 2)]; + text += '\n⏱' + this.secondsToHuman_(median); + } + this.displayEventCountCells_[tile.type][cutoff[0]].textContent = text; }.bind(this)); }.bind(this)); };