Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Handle mmol to mgdl conversions with a constant reference #5274

Merged
merged 1 commit into from
Dec 6, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 11 additions & 10 deletions lib/client/careportal.js
Original file line number Diff line number Diff line change
Expand Up @@ -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, $) {
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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) {
Expand Down Expand Up @@ -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);
Expand Down
7 changes: 4 additions & 3 deletions lib/client/renderer.js
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -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;
Expand Down
3 changes: 2 additions & 1 deletion lib/constants.json
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
9 changes: 5 additions & 4 deletions lib/data/ddata.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

var _ = require('lodash');
var times = require('../times');
var consts = require('../constants');

var DEVICE_TYPE_FIELDS = ['uploader', 'pump', 'openaps', 'loop', 'xdripjs'];

Expand Down Expand Up @@ -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;
Expand Down
3 changes: 2 additions & 1 deletion lib/data/treatmenttocurve.js
Original file line number Diff line number Diff line change
@@ -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) {

Expand Down
3 changes: 2 additions & 1 deletion lib/plugins/basalprofile.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
'use strict';
var times = require('../times');
var moment = require('moment');
var consts = require('../constants');

function init (ctx) {

Expand Down Expand Up @@ -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;
Expand Down
3 changes: 2 additions & 1 deletion lib/plugins/openaps.js
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down Expand Up @@ -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 = [
Expand Down
12 changes: 7 additions & 5 deletions lib/report_plugins/glucosedistribution.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
'use strict';

var consts = require('../constants');

var glucosedistribution = {
name: 'glucosedistribution'
, label: 'Distribution'
Expand Down Expand Up @@ -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
};
Expand Down Expand Up @@ -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
};
Expand Down Expand Up @@ -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;
Expand Down
4 changes: 3 additions & 1 deletion lib/report_plugins/utils.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
'use strict';

var consts = require('../constants');

var moment = window.moment;
var utils = { };

Expand Down Expand Up @@ -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);
Expand Down
6 changes: 4 additions & 2 deletions lib/units.js
Original file line number Diff line number Diff line change
@@ -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() {
Expand Down