From f231180fdd70d268007a4dff832fa512a0e4e870 Mon Sep 17 00:00:00 2001 From: Jakob Sandberg Date: Thu, 5 Dec 2019 15:30:03 -0800 Subject: [PATCH] Handle mmol to mgdl conversions with a constant reference --- lib/client/careportal.js | 21 +++++++++++---------- lib/client/renderer.js | 7 ++++--- lib/constants.json | 3 ++- lib/data/ddata.js | 9 +++++---- lib/data/treatmenttocurve.js | 3 ++- lib/plugins/basalprofile.js | 3 ++- lib/plugins/openaps.js | 3 ++- lib/report_plugins/glucosedistribution.js | 12 +++++++----- lib/report_plugins/utils.js | 4 +++- lib/units.js | 6 ++++-- 10 files changed, 42 insertions(+), 29 deletions(-) diff --git a/lib/client/careportal.js b/lib/client/careportal.js index 0d1220d55a7..a5c86232d8e 100644 --- a/lib/client/careportal.js +++ b/lib/client/careportal.js @@ -4,6 +4,7 @@ var moment = require('moment-timezone'); var _ = require('lodash'); var parse_duration = require('parse-duration'); // https://www.npmjs.com/package/parse-duration var times = require('../times'); +var consts = require('../constants'); var Storages = require('js-storage'); function init (client, $) { @@ -240,8 +241,8 @@ function init (client, $) { } if (units == "mmol") { - data.targetTop = data.targetTop * 18; - data.targetBottom = data.targetBottom * 18; + data.targetTop = data.targetTop * consts.MMOL_TO_MGDL; + data.targetBottom = data.targetBottom * consts.MMOL_TO_MGDL; } //special handling for absolute to support temp to 0 @@ -298,14 +299,14 @@ function init (client, $) { let targetTop = parseInt(data.targetTop); let targetBottom = parseInt(data.targetBottom); - let minTarget = 4 * 18; - let maxTarget = 18 * 18; + let minTarget = 4 * consts.MMOL_TO_MGDL; + let maxTarget = 18 * consts.MMOL_TO_MGDL; if (units == "mmol") { - targetTop = Math.round(targetTop / 18.0 * 10) / 10; - targetBottom = Math.round(targetBottom / 18.0 * 10) / 10; - minTarget = Math.round(minTarget / 18.0 * 10) / 10; - maxTarget = Math.round(maxTarget / 18.0 * 10) / 10; + targetTop = Math.round(targetTop / consts.MMOL_TO_MGDL * 10) / 10; + targetBottom = Math.round(targetBottom / consts.MMOL_TO_MGDL * 10) / 10; + minTarget = Math.round(minTarget / consts.MMOL_TO_MGDL * 10) / 10; + maxTarget = Math.round(maxTarget / consts.MMOL_TO_MGDL * 10) / 10; } if (targetTop > maxTarget) { @@ -358,8 +359,8 @@ function init (client, $) { var targetBottom = data.targetBottom; if (units == "mmol") { - targetTop = Math.round(data.targetTop / 18.0 * 10) / 10; - targetBottom = Math.round(data.targetBottom / 18.0 * 10) / 10; + targetTop = Math.round(data.targetTop / consts.MMOL_TO_MGDL * 10) / 10; + targetBottom = Math.round(data.targetBottom / consts.MMOL_TO_MGDL * 10) / 10; } pushIf(data.targetTop, translate('Target Top') + ': ' + targetTop); diff --git a/lib/client/renderer.js b/lib/client/renderer.js index 688b046b7b2..5a0354afc51 100644 --- a/lib/client/renderer.js +++ b/lib/client/renderer.js @@ -2,6 +2,7 @@ var _ = require('lodash'); var times = require('../times'); +var consts = require('../constants'); var DEFAULT_FOCUS = times.hours(3).msecs , WIDTH_SMALL_DOTS = 420 @@ -210,8 +211,8 @@ function init (client, d3) { var targetTop = d.targetTop; if (client.settings.units === 'mmol') { - targetBottom = Math.round(targetBottom / 18.0 * 10) / 10; - targetTop = Math.round(targetTop / 18.0 * 10) / 10; + targetBottom = Math.round(targetBottom / consts.MMOL_TO_MGDL * 10) / 10; + targetTop = Math.round(targetTop / consts.MMOL_TO_MGDL * 10) / 10; } var correctionRangeText; @@ -632,7 +633,7 @@ function init (client, d3) { function treatmentTooltip () { var glucose = treatment.glucose; if (client.settings.units != client.ddata.profile.getUnits()) { - glucose *= (client.settings.units === 'mmol' ? 0.055 : 18); + glucose *= (client.settings.units === 'mmol' ? (1 / consts.MMOL_TO_MGDL) : consts.MMOL_TO_MGDL); var decimals = (client.settings.units === 'mmol' ? 10 : 1); glucose = Math.round(glucose * decimals) / decimals; diff --git a/lib/constants.json b/lib/constants.json index 31a4524b4d3..c2736f7e6e9 100644 --- a/lib/constants.json +++ b/lib/constants.json @@ -4,5 +4,6 @@ "HTTP_UNAUTHORIZED" : 401, "HTTP_VALIDATION_ERROR" : 422, "HTTP_INTERNAL_ERROR" : 500, - "ENTRIES_DEFAULT_COUNT" : 10 + "ENTRIES_DEFAULT_COUNT" : 10, + "MMOL_TO_MGDL": 18 } diff --git a/lib/data/ddata.js b/lib/data/ddata.js index 0251ec17d99..9fa470e3f16 100644 --- a/lib/data/ddata.js +++ b/lib/data/ddata.js @@ -2,6 +2,7 @@ var _ = require('lodash'); var times = require('../times'); +var consts = require('../constants'); var DEVICE_TYPE_FIELDS = ['uploader', 'pump', 'openaps', 'loop', 'xdripjs']; @@ -213,18 +214,18 @@ function init () { if (t.units) { if (t.units == 'mmol') { //convert to mgdl - t.targetTop = t.targetTop * 18; - t.targetBottom = t.targetBottom * 18; + t.targetTop = t.targetTop * consts.MMOL_TO_MGDL; + t.targetBottom = t.targetBottom * consts.MMOL_TO_MGDL; t.units = 'mg/dl'; } } //if we have a temp target thats below 20, assume its mmol and convert to mgdl for safety. if (t.targetTop < 20) { - t.targetTop = t.targetTop * 18; + t.targetTop = t.targetTop * consts.MMOL_TO_MGDL; t.units = 'mg/dl'; } if (t.targetBottom < 20) { - t.targetBottom = t.targetBottom * 18; + t.targetBottom = t.targetBottom * consts.MMOL_TO_MGDL; t.units = 'mg/dl'; } return t.eventType && t.eventType.indexOf('Temporary Target') > -1; diff --git a/lib/data/treatmenttocurve.js b/lib/data/treatmenttocurve.js index 8bca564d091..afa17b397ec 100644 --- a/lib/data/treatmenttocurve.js +++ b/lib/data/treatmenttocurve.js @@ -1,9 +1,10 @@ 'use strict'; var _ = require('lodash'); +var consts = require('../constants'); const MAX_BG_MMOL = 22; -const MAX_BG_MGDL = MAX_BG_MMOL * 18; +const MAX_BG_MGDL = MAX_BG_MMOL * consts.MMOL_TO_MGDL; module.exports = function fitTreatmentsToBGCurve (ddata, env, ctx) { diff --git a/lib/plugins/basalprofile.js b/lib/plugins/basalprofile.js index 73c9492ea44..a766f9ab0a8 100644 --- a/lib/plugins/basalprofile.js +++ b/lib/plugins/basalprofile.js @@ -1,6 +1,7 @@ 'use strict'; var times = require('../times'); var moment = require('moment'); +var consts = require('../constants'); function init (ctx) { @@ -65,7 +66,7 @@ function init (ctx) { var units = profile.getUnits(); if (sbx.settings.units != units) { - sensitivity *= (sbx.settings.units === 'mmol' ? 0.055 : 18); + sensitivity *= (sbx.settings.units === 'mmol' ? (1 / consts.MMOL_TO_MGDL) : consts.MMOL_TO_MGDL); var decimals = (sbx.settings.units === 'mmol' ? 10 : 1); sensitivity = Math.round(sensitivity * decimals) / decimals; diff --git a/lib/plugins/openaps.js b/lib/plugins/openaps.js index 77f27288b14..4d2535a3b76 100644 --- a/lib/plugins/openaps.js +++ b/lib/plugins/openaps.js @@ -4,6 +4,7 @@ var _ = require('lodash'); var moment = require('moment'); var times = require('../times'); var levels = require('../levels'); +var consts = require('../constants'); // var ALL_STATUS_FIELDS = ['status-symbol', 'status-label', 'iob', 'meal-assist', 'freq', 'rssi']; Unused variable @@ -352,7 +353,7 @@ function init (ctx) { var units = sbx.data.profile.getUnits(); if (units === 'mmol') { - bg = Math.round(bg / 18 * 10) / 10; + bg = Math.round(bg / consts.MMOL_TO_MGDL * 10) / 10; } var valueParts = [ diff --git a/lib/report_plugins/glucosedistribution.js b/lib/report_plugins/glucosedistribution.js index 3d8d109610b..2d034d2bba8 100644 --- a/lib/report_plugins/glucosedistribution.js +++ b/lib/report_plugins/glucosedistribution.js @@ -1,5 +1,7 @@ 'use strict'; +var consts = require('../constants'); + var glucosedistribution = { name: 'glucosedistribution' , label: 'Distribution' @@ -174,7 +176,7 @@ glucosedistribution.report = function report_glucosedistribution (datastorage, s var bg = Math.floor(entry.bgValue + bgDelta * j); var t = new Date(entry.displayTime.getTime() + j * timePatch); var newEntry = { - sgv: displayUnits === 'mmol' ? bg / 18 : bg + sgv: displayUnits === 'mmol' ? bg / consts.MMOL_TO_MGDL : bg , bgValue: bg , displayTime: t }; @@ -217,7 +219,7 @@ glucosedistribution.report = function report_glucosedistribution (datastorage, s const interpolatedValue = prevEntry.bgValue + d; let newEntry = { - sgv: displayUnits === 'mmol' ? interpolatedValue / 18 : interpolatedValue + sgv: displayUnits === 'mmol' ? interpolatedValue / consts.MMOL_TO_MGDL : interpolatedValue , bgValue: interpolatedValue , displayTime: entry.displayTime }; @@ -433,11 +435,11 @@ glucosedistribution.report = function report_glucosedistribution (datastorage, s var unitString = ' mg/dl'; if (displayUnits == 'mmol') { - TDC = TDC / 18.0; - TDCHourly = TDCHourly / 18.0; + TDC = TDC / consts.MMOL_TO_MGDL; + TDCHourly = TDCHourly / consts.MMOL_TO_MGDL; unitString = ' mmol/L'; - RMS = Math.sqrt(RMSTotal / events) / 18; + RMS = Math.sqrt(RMSTotal / events) / consts.MMOL_TO_MGDL; } TDC = Math.round(TDC * 100) / 100; diff --git a/lib/report_plugins/utils.js b/lib/report_plugins/utils.js index 10daec32304..be6ba940003 100644 --- a/lib/report_plugins/utils.js +++ b/lib/report_plugins/utils.js @@ -1,5 +1,7 @@ 'use strict'; +var consts = require('../constants'); + var moment = window.moment; var utils = { }; @@ -69,7 +71,7 @@ utils.scaledTreatmentBG = function scaledTreatmentBG(treatment,data) { console.info('found mismatched glucose units, converting ' + treatment.units + ' into ' + client.settings.units, treatment); if (treatment.units === 'mmol') { //BG is in mmol and display in mg/dl - treatmentGlucose = Math.round(treatment.glucose * 18); + treatmentGlucose = Math.round(treatment.glucose * consts.MMOL_TO_MGDL); } else { //BG is in mg/dl and display in mmol treatmentGlucose = client.utils.scaleMgdl(treatment.glucose); diff --git a/lib/units.js b/lib/units.js index f548d55744e..5eb36c10950 100644 --- a/lib/units.js +++ b/lib/units.js @@ -1,11 +1,13 @@ 'use strict'; +var consts = require('./constants'); + function mgdlToMMOL(mgdl) { - return (Math.round((mgdl / 18) * 10) / 10).toFixed(1); + return (Math.round((mgdl / consts.MMOL_TO_MGDL) * 10) / 10).toFixed(1); } function mmolToMgdl(mgdl) { - return Math.round(mgdl * 18); + return Math.round(mgdl * consts.MMOL_TO_MGDL); } function configure() {