From 0f8131b7797ff6e6e8bfde033f568c228f8976db Mon Sep 17 00:00:00 2001 From: Sulka Haro Date: Tue, 3 Jul 2018 10:18:01 +0300 Subject: [PATCH] Fixes issue https://github.com/nightscout/cgm-remote-monitor/issues/3660 (#3661) --- lib/report_plugins/glucosedistribution.js | 32 +++++++++++++++++------ 1 file changed, 24 insertions(+), 8 deletions(-) diff --git a/lib/report_plugins/glucosedistribution.js b/lib/report_plugins/glucosedistribution.js index 637ed08050e..ad08c3084ff 100644 --- a/lib/report_plugins/glucosedistribution.js +++ b/lib/report_plugins/glucosedistribution.js @@ -88,6 +88,7 @@ glucosedistribution.report = function report_glucosedistribution(datastorage, so var Nightscout = window.Nightscout; var client = Nightscout.client; var translate = client.translate; + var displayUnits = Nightscout.client.settings.units; var ss = require('simple-statistics'); @@ -187,6 +188,8 @@ glucosedistribution.report = function report_glucosedistribution(datastorage, so var prevEntry = glucose_data[0]; + const maxGap = (5 * 60 * 1000) + 10000; + for (var i = 1; i < glucose_data.length-2; i++) { // var prevEntry = glucose_data[i-1]; @@ -196,8 +199,6 @@ glucosedistribution.report = function report_glucosedistribution(datastorage, so var timeDelta = nextEntry.displayTime.getTime() - entry.displayTime.getTime(); var timeDelta2 = entry.displayTime.getTime() - prevEntry.displayTime.getTime(); - var maxGap = (5 * 60 * 1000) + 10000; - if (timeDelta > maxGap || timeDelta2 > maxGap ) { glucose_data2.push(entry); prevEntry = entry; @@ -233,6 +234,23 @@ glucosedistribution.report = function report_glucosedistribution(datastorage, so glucose_data = data = glucose_data2.filter(function(r) { return enabledHours[new Date(r.displayTime).getHours()] }); + + glucose_data.sort(function(a,b){ + return a.displayTime.getTime()-b.displayTime.getTime(); + }); + + var timeTotal = 0; + + for (var i = 1; i < glucose_data.length-2; i++) { + var entry = glucose_data[i]; + var nextEntry = glucose_data[i+1]; + var timeDelta = nextEntry.displayTime.getTime() - entry.displayTime.getTime(); + if (timeDelta < maxGap) { + timeTotal += timeDelta; + } + } + + var daysTotal = timeTotal / (1000*60*60*24); ['Low', 'Normal', 'High'].forEach(function(range) { result[range] = {}; @@ -403,9 +421,8 @@ glucosedistribution.report = function report_glucosedistribution(datastorage, so console.log('glucoseMean', glucoseMean,'tirMultiplier',tirMultiplier, 'PGS',PGS); - var days = (glucose_data[glucose_data.length-1].displayTime.getTime() - glucose_data[0].displayTime.getTime()) / (24*60*60*1000.0); - - var TDC = deltaTotal / days; + var TDC = deltaTotal / daysTotal; + var TDCHourly = TDC / 24.0; var RMS = Math.sqrt(RMSTotal / events); @@ -417,13 +434,12 @@ glucosedistribution.report = function report_glucosedistribution(datastorage, so var unitString = ' mg/dl'; - if (client.settings.units == 'mmol') { + if (displayUnits == 'mmol') { TDC = TDC / 18.0; TDCHourly = TDCHourly / 18.0; unitString = ' mmol/L'; RMS = Math.sqrt(RMSTotal / events) / 18; - } TDC = Math.round(TDC * 100) / 100; @@ -434,7 +450,7 @@ glucosedistribution.report = function report_glucosedistribution(datastorage, so var t1exp = '>5 mg/dl/5m'; var t2exp = '>10 mg/dl/5m'; - if (client.settings.units == 'mmol') { + if (displayUnits == 'mmol') { t1exp = '>0.27 mmol/l/5m'; t2exp = '>0.55 mmol/l/5m'; }