Skip to content

Commit

Permalink
Protein and Fat logging support (nightscout#3830)
Browse files Browse the repository at this point in the history
* Add protein and fat logging to CarePortal, simple sums to day to day reporting, grams in graphs

* Add fat and protein to swagger

* Fix aggregate report sometimes considering logged carbs, protein and fat as a string. Add editing of protein and fat to reports. Show protein and carbs on daily reports
  • Loading branch information
sulkaharo authored and tanja3981 committed May 21, 2019
1 parent 0d7c6a3 commit 5e00178
Show file tree
Hide file tree
Showing 9 changed files with 126 additions and 32 deletions.
12 changes: 11 additions & 1 deletion lib/client/careportal.js
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ function init (client, $) {
var inputMatrix = {};

_.forEach(careportal.allEventTypes, function each (event) {
inputMatrix[event.val] = _.pick(event, ['bg', 'insulin', 'carbs', 'prebolus', 'duration', 'percent', 'absolute', 'profile', 'split', 'reasons', 'targets']);
inputMatrix[event.val] = _.pick(event, ['bg', 'insulin', 'carbs', 'protein', 'fat', 'prebolus', 'duration', 'percent', 'absolute', 'profile', 'split', 'reasons', 'targets']);
});

careportal.filterInputs = function filterInputs ( event ) {
Expand All @@ -73,6 +73,8 @@ function init (client, $) {
$('#bg').css('display',displayType(inputMatrix[eventType]['bg']));
$('#insulinGivenLabel').css('display',displayType(inputMatrix[eventType]['insulin']));
$('#carbsGivenLabel').css('display',displayType(inputMatrix[eventType]['carbs']));
$('#proteinGivenLabel').css('display',displayType(inputMatrix[eventType]['protein']));
$('#fatGivenLabel').css('display',displayType(inputMatrix[eventType]['fat']));
$('#durationLabel').css('display',displayType(inputMatrix[eventType]['duration']));
$('#percentLabel').css('display',displayType(inputMatrix[eventType]['percent'] && $('#absolute').val() === ''));
$('#absoluteLabel').css('display',displayType(inputMatrix[eventType]['absolute'] && $('#percent').val() === ''));
Expand All @@ -89,6 +91,8 @@ function init (client, $) {

resetIfHidden(inputMatrix[eventType]['insulin'], '#insulinGiven');
resetIfHidden(inputMatrix[eventType]['carbs'], '#carbsGiven');
resetIfHidden(inputMatrix[eventType]['protein'], '#proteinGiven');
resetIfHidden(inputMatrix[eventType]['fat'], '#fatGiven');
resetIfHidden(inputMatrix[eventType]['duration'], '#duration');
resetIfHidden(inputMatrix[eventType]['absolute'], '#absolute');
resetIfHidden(inputMatrix[eventType]['percent'], '#percent');
Expand Down Expand Up @@ -177,6 +181,8 @@ function init (client, $) {
$('#glucoseValue').val('').attr('placeholder', translate('Value in') + ' ' + client.settings.units);
$('#meter').prop('checked', true);
$('#carbsGiven').val('');
$('#proteinGiven').val('');
$('#fatGiven').val('');
$('#insulinGiven').val('');
$('#duration').val('');
$('#percent').val('');
Expand All @@ -199,6 +205,8 @@ function init (client, $) {
, targetBottom: $('#targetBottom').val().replace(',','.')
, glucoseType: $('#treatment-form').find('input[name=glucoseType]:checked').val()
, carbs: $('#carbsGiven').val()
, protein: $('#proteinGiven').val()
, fat: $('#fatGiven').val()
, insulin: $('#insulinGiven').val()
, duration: times.msecs(parse_duration($('#duration').val())).mins < 1 ? $('#duration').val() : times.msecs(parse_duration($('#duration').val())).mins
, percent: $('#percent').val()
Expand Down Expand Up @@ -283,6 +291,8 @@ function init (client, $) {
pushIf(data.targetBottom, translate('Target Bottom') + ': ' + targetBottom);

pushIf(data.carbs, translate('Carbs Given') + ': ' + data.carbs);
pushIf(data.protein, translate('Protein Given') + ': ' + data.protein);
pushIf(data.fat, translate('Fat Given') + ': ' + data.fat);
pushIf(data.insulin, translate('Insulin Given') + ': ' + data.insulin);
pushIf(data.eventType === 'Combo Bolus', translate('Combo Bolus') + ': ' + data.splitNow + '% : ' + data.splitExt + '%');
pushIf(data.duration, translate('Duration') + ': ' + data.duration + ' ' + translate('mins'));
Expand Down
10 changes: 10 additions & 0 deletions lib/client/renderer.js
Original file line number Diff line number Diff line change
Expand Up @@ -438,6 +438,14 @@ function init (client, d3) {
arc_data[1].element = Math.round(treatment.carbs) + ' g';
}

if (treatment.protein > 0) {
arc_data[1].element = arc_data[1].element + " / " + Math.round(treatment.protein) + ' g';
}

if (treatment.fat > 0) {
arc_data[1].element = arc_data[1].element + " / " + Math.round(treatment.fat) + ' g';
}

if (treatment.foodType) {
arc_data[1].element = arc_data[1].element + " " + treatment.foodType;
}
Expand Down Expand Up @@ -513,6 +521,8 @@ function init (client, d3) {
client.tooltip.transition().duration(TOOLTIP_TRANS_MS).style('opacity', .9);
client.tooltip.html('<strong>' + translate('Time') + ':</strong> ' + client.formatTime(new Date(treatment.mills)) + '<br/>' + '<strong>' + translate('Treatment type') + ':</strong> ' + translate(client.careportal.resolveEventName(treatment.eventType)) + '<br/>' +
(treatment.carbs ? '<strong>' + translate('Carbs') + ':</strong> ' + treatment.carbs + '<br/>' : '') +
(treatment.protein ? '<strong>' + translate('Protein') + ':</strong> ' + treatment.protein + '<br/>' : '') +
(treatment.fat ? '<strong>' + translate('Fat') + ':</strong> ' + treatment.fat + '<br/>' : '') +
(treatment.absorptionTime > 0 ? '<strong>' + translate('Absorption Time') + ':</strong> ' + (Math.round( treatment.absorptionTime / 60.0 * 10) / 10) + 'h' + '<br/>' : '') +
(treatment.insulin ? '<strong>' + translate('Insulin') + ':</strong> ' + treatment.insulin + '<br/>' : '') +
(treatment.enteredinsulin ? '<strong>' + translate('Combo Bolus') + ':</strong> ' + treatment.enteredinsulin + 'U, ' + treatment.splitNow + '% : ' + treatment.splitExt + '%, ' + translate('Duration') + ': ' + treatment.duration + '<br/>' : '') +
Expand Down
40 changes: 20 additions & 20 deletions lib/plugins/careportal.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,87 +15,87 @@ function init() {
return [
{ val: '<none>'
, name: '<none>'
, bg: true, insulin: true, carbs: true, prebolus: false, duration: false, percent: false, absolute: false, profile: false, split: false
, bg: true, insulin: true, carbs: true, protein: false, fat: false, prebolus: false, duration: false, percent: false, absolute: false, profile: false, split: false
}
, { val: 'BG Check'
, name: 'BG Check'
, bg: true, insulin: false, carbs: false, prebolus: false, duration: false, percent: false, absolute: false, profile: false, split: false
, bg: true, insulin: false, carbs: false, protein: false, fat: false, prebolus: false, duration: false, percent: false, absolute: false, profile: false, split: false
}
, { val: 'Snack Bolus'
, name: 'Snack Bolus'
, bg: true, insulin: true, carbs: true, prebolus: true, duration: false, percent: false, absolute: false, profile: false, split: false
, bg: true, insulin: true, carbs: true, protein: true, fat: true, prebolus: true, duration: false, percent: false, absolute: false, profile: false, split: false
}
, { val: 'Meal Bolus'
, name: 'Meal Bolus'
, bg: true, insulin: true, carbs: true, prebolus: true, duration: false, percent: false, absolute: false, profile: false, split: false
, bg: true, insulin: true, carbs: true, protein: true, fat: true, prebolus: true, duration: false, percent: false, absolute: false, profile: false, split: false
}
, { val: 'Correction Bolus'
, name: 'Correction Bolus'
, bg: true, insulin: true, carbs: false, prebolus: false, duration: false, percent: false, absolute: false, profile: false, split: false
, bg: true, insulin: true, carbs: false, protein: false, fat: false, prebolus: false, duration: false, percent: false, absolute: false, profile: false, split: false
}
, { val: 'Carb Correction'
, name: 'Carb Correction'
, bg: true, insulin: false, carbs: true, prebolus: false, duration: false, percent: false, absolute: false, profile: false, split: false
, bg: true, insulin: false, carbs: true, protein: true, fat: true, prebolus: false, duration: false, percent: false, absolute: false, profile: false, split: false
}
, { val: 'Combo Bolus'
, name: 'Combo Bolus'
, bg: true, insulin: true, carbs: true, prebolus: true, duration: true, percent: false, absolute: false, profile: false, split: true
, bg: true, insulin: true, carbs: true, protein: true, fat: true, prebolus: true, duration: true, percent: false, absolute: false, profile: false, split: true
}
, { val: 'Announcement'
, name: 'Announcement'
, bg: true, insulin: false, carbs: false, prebolus: false, duration: false, percent: false, absolute: false, profile: false, split: false
, bg: true, insulin: false, carbs: false, protein: false, fat: false, prebolus: false, duration: false, percent: false, absolute: false, profile: false, split: false
}
, { val: 'Note'
, name: 'Note'
, bg: true, insulin: false, carbs: false, prebolus: false, duration: true, percent: false, absolute: false, profile: false, split: false
, bg: true, insulin: false, carbs: false, protein: false, fat: false, prebolus: false, duration: true, percent: false, absolute: false, profile: false, split: false
}
, { val: 'Question'
, name: 'Question'
, bg: true, insulin: false, carbs: false, prebolus: false, duration: false, percent: false, absolute: false, profile: false, split: false
, bg: true, insulin: false, carbs: false, protein: false, fat: false, prebolus: false, duration: false, percent: false, absolute: false, profile: false, split: false
}
, { val: 'Exercise'
, name: 'Exercise'
, bg: false, insulin: false, carbs: false, prebolus: false, duration: true, percent: false, absolute: false, profile: false, split: false
, bg: false, insulin: false, carbs: false, protein: false, fat: false, prebolus: false, duration: true, percent: false, absolute: false, profile: false, split: false
}
, { val: 'Site Change'
, name: 'Pump Site Change'
, bg: true, insulin: true, carbs: false, prebolus: false, duration: false, percent: false, absolute: false, profile: false, split: false
, bg: true, insulin: true, carbs: false, protein: false, fat: false, prebolus: false, duration: false, percent: false, absolute: false, profile: false, split: false
}
, { val: 'Sensor Start'
, name: 'CGM Sensor Start'
, bg: true, insulin: false, carbs: false, prebolus: false, duration: false, percent: false, absolute: false, profile: false, split: false
, bg: true, insulin: false, carbs: false, protein: false, fat: false, prebolus: false, duration: false, percent: false, absolute: false, profile: false, split: false
}
, { val: 'Sensor Change'
, name: 'CGM Sensor Insert'
, bg: true, insulin: false, carbs: false, prebolus: false, duration: false, percent: false, absolute: false, profile: false, split: false
, bg: true, insulin: false, carbs: false, protein: false, fat: false, prebolus: false, duration: false, percent: false, absolute: false, profile: false, split: false
}
, { val: 'Sensor Stop'
, name: 'CGM Sensor Stop'
, bg: true, insulin: false, carbs: false, prebolus: false, duration: false, percent: false, absolute: false, profile: false, split: false
}
, { val: 'Pump Battery Change'
, name: 'Pump Battery Change'
, bg: true, insulin: false, carbs: false, prebolus: false, duration: false, percent: false, absolute: false, profile: false, split: false
, bg: true, insulin: false, carbs: false, protein: false, fat: false, prebolus: false, duration: false, percent: false, absolute: false, profile: false, split: false
}
, { val: 'Insulin Change'
, name: 'Insulin Cartridge Change'
, bg: true, insulin: false, carbs: false, prebolus: false, duration: false, percent: false, absolute: false, profile: false, split: false
, bg: true, insulin: false, carbs: false, protein: false, fat: false, prebolus: false, duration: false, percent: false, absolute: false, profile: false, split: false
}
, { val: 'Temp Basal Start'
, name: 'Temp Basal Start'
, bg: true, insulin: false, carbs: false, prebolus: false, duration: true, percent: true, absolute: true, profile: false, split: false
, bg: true, insulin: false, carbs: false, protein: false, fat: false, prebolus: false, duration: true, percent: true, absolute: true, profile: false, split: false
}
, { val: 'Temp Basal End'
, name: 'Temp Basal End'
, bg: true, insulin: false, carbs: false, prebolus: false, duration: true, percent: false, absolute: false, profile: false, split: false
, bg: true, insulin: false, carbs: false, protein: false, fat: false, prebolus: false, duration: true, percent: false, absolute: false, profile: false, split: false
}
, { val: 'Profile Switch'
, name: 'Profile Switch'
, bg: true, insulin: false, carbs: false, prebolus: false, duration: true, percent: false, absolute: false, profile: true, split: false
, bg: true, insulin: false, carbs: false, protein: false, fat: false, prebolus: false, duration: true, percent: false, absolute: false, profile: true, split: false
}
, { val: 'D.A.D. Alert'
, name: 'D.A.D. Alert'
, bg: true, insulin: false, carbs: false, prebolus: false, duration: false, percent: false, absolute: false, profile: false, split: false
, bg: true, insulin: false, carbs: false, protein: false, fat: false, prebolus: false, duration: false, percent: false, absolute: false, profile: false, split: false
}
];

Expand Down
29 changes: 25 additions & 4 deletions lib/report_plugins/daytoday.js
Original file line number Diff line number Diff line change
Expand Up @@ -26,14 +26,14 @@ daytoday.html = function html(client) {
+ '<input type="checkbox" id="rp_optionsinsulin" checked><span style="color:blue;opacity:0.5">'+translate('Insulin')+'</span>'
+ '<input type="checkbox" id="rp_optionscarbs" checked><span style="color:red;opacity:0.5">'+translate('Carbs')+'</span>'
+ '<input type="checkbox" id="rp_optionsbasal" checked><span style="color:#0099ff;opacity:0.5">'+translate('Basal rate')+'</span>'
+ '<input type="checkbox" id="rp_optionsnotes" checked>'+translate('Notes')
+ '<input type="checkbox" id="rp_optionsnotes">'+translate('Notes')
+ '<input type="checkbox" id="rp_optionsfood" checked>'+translate('Food')
+ '<input type="checkbox" id="rp_optionsraw"><span style="color:gray;opacity:1">'+translate('Raw')+'</span>'
+ '<input type="checkbox" id="rp_optionsiob"><span style="color:blue;opacity:0.5">'+translate('IOB')+'</span>'
+ '<input type="checkbox" id="rp_optionscob"><span style="color:red;opacity:0.5">'+translate('COB')+'</span>'
+ '<input type="checkbox" id="rp_optionspredicted"><span style="color:sienna;opacity:0.5">'+translate('Predictions')+'</span>'
+ '<input type="checkbox" id="rp_optionsopenaps"><span style="color:sienna;opacity:0.5">'+translate('OpenAPS')+'</span>'
+ '<input type="checkbox" id="rp_optionsdistribution"><span style="color:blue;opacity:0.5">'+translate('Insulin distribution')+'</span>'
+ '<input type="checkbox" id="rp_optionsdistribution" checked><span style="color:blue;opacity:0.5">'+translate('Insulin distribution')+'</span>'
+ '&nbsp;'+translate('Size')
+ ' <select id="rp_size">'
+ ' <option x="800" y="250">800x250px</option>'
Expand Down Expand Up @@ -89,6 +89,8 @@ daytoday.report = function report_daytoday(datastorage,sorteddaystoshow,options)

var tddSum = 0;
var carbsSum = 0;
var proteinSum = 0;
var fatSum = 0;

daytoday.prepareHtml(sorteddaystoshow) ;
sorteddaystoshow.forEach( function eachDay(day) {
Expand All @@ -97,9 +99,16 @@ daytoday.report = function report_daytoday(datastorage,sorteddaystoshow,options)

var tddAverage = tddSum / datastorage.alldays;
var carbsAverage = carbsSum / datastorage.alldays;
var proteinAverage = proteinSum / datastorage.alldays;
var fatAverage = fatSum / datastorage.alldays;


if (options.insulindistribution)
$('#daytodaycharts').append('<br><br><b>' + translate('TDD average') + ':</b> ' + tddAverage.toFixed(1) + 'U <b>' + translate('Carbs average') + ':</b> ' + carbsAverage.toFixed(0) + 'g');
$('#daytodaycharts').append('<br><br><b>' + translate('TDD average') + ':</b> ' + tddAverage.toFixed(1) + 'U <b>'
+ translate('Carbs average') + ':</b> ' + carbsAverage.toFixed(0) + 'g'
+ translate('Protein average') + ':</b> ' + proteinAverage.toFixed(0) + 'g'
+ translate('Fat average') + ':</b> ' + fatAverage.toFixed(0) + 'g'
);

function timeTicks(n,i) {
var t12 = [
Expand Down Expand Up @@ -729,6 +738,11 @@ daytoday.report = function report_daytoday(datastorage,sorteddaystoshow,options)

if (treatment.carbs && options.carbs) {
var ic = profile.getCarbRatio(new Date(treatment.mills));
var label = ' ' + treatment.carbs +' g';
if (treatment.protein) label += ' / ' + treatment.protein +' g';
if (treatment.fat) label += ' / ' + treatment.fat +' g';
label += ' ('+(treatment.carbs/ic).toFixed(2)+'U)';

context.append('rect')
.attr('y',yCarbsScale(treatment.carbs))
.attr('height', chartHeight-yCarbsScale(treatment.carbs))
Expand All @@ -743,7 +757,7 @@ daytoday.report = function report_daytoday(datastorage,sorteddaystoshow,options)
.attr('fill', 'red')
.attr('transform', 'rotate(-45,' + (xScale2(treatment.mills) + padding.left) + ',' + (padding.top+yCarbsScale(treatment.carbs)) + ') ' +
'translate(' + (xScale2(treatment.mills) + padding.left +10) + ',' + (padding.top+yCarbsScale(treatment.carbs)) + ')')
.text(treatment.carbs+' g ('+(treatment.carbs/ic).toFixed(2)+'U)');
.text(''+label);
}

if (treatment.insulin && options.insulin) {
Expand Down Expand Up @@ -898,6 +912,11 @@ daytoday.report = function report_daytoday(datastorage,sorteddaystoshow,options)
$('<tr><td><b>' + translate('Total basal insulin:') + '</b></td><td align="right">' + totalBasalInsulin.toFixed(1) + 'U</td></tr>').appendTo(table);
var totalDailyInsulin = bolusInsulin + baseBasalInsulin + positiveTemps + negativeTemps;
$('<tr><td><b>' + translate('Total daily insulin:') + '</b></td><td align="right">' + totalDailyInsulin.toFixed(1) + 'U</td></tr>').appendTo(table);

$('<tr><td>' + translate('Total carbs') + ':</td><td align="right">' + data.dailyCarbs + ' g</td></tr>').appendTo(table);
$('<tr><td>' + translate('Total protein') + ':</td><td align="right">' + data.dailyProtein + ' g</td></tr>').appendTo(table);
$('<tr><td>' + translate('Total fat') + ':</td><td align="right">' + data.dailyFat + ' g</td></tr>').appendTo(table);

$('<tr><td colspan="2"><span id="daytodaystatinsulinpiechart-' + day + '"></span><span id="daytodaystatcarbspiechart-' + day + '"></span></td></tr>').appendTo(table);
$('#daytodaystatchart-' + day).append(table);

Expand Down Expand Up @@ -1014,6 +1033,8 @@ daytoday.report = function report_daytoday(datastorage,sorteddaystoshow,options)

tddSum += totalDailyInsulin;
carbsSum += data.dailyCarbs;
proteinSum += data.dailyProtein;
fatSum += data.dailyFat;

appendProfileSwitch(context, {
//eventType: 'Profile Switch'
Expand Down
Loading

0 comments on commit 5e00178

Please sign in to comment.