diff --git a/lib/profilefunctions.js b/lib/profilefunctions.js index b7f85537ac0..94120d72f67 100644 --- a/lib/profilefunctions.js +++ b/lib/profilefunctions.js @@ -350,14 +350,27 @@ function init (profileData) { for (var key in profile.data[0].store) { if (profile.data[0].store.hasOwnProperty(key) && key !== current) { + if (key.indexOf('@@@@@') < 0) profiles.push(key); } } } return profiles; }; - - + + // get original store without added profiles fro profileSwitches + profile.getProfileStore = function getProfileStore () { + var newprofiledata = _.clone(profile.data[0]); + for (var key in profile.data[0].store) { + if (profile.data[0].store.hasOwnProperty(key)) { + if (key.indexOf('@@@@@') < 0) + store[key] = profile.data[0].store[key]; + } + } + return store; + }; + + if (profileData) { profile.loadData(profileData); } // init treatments array profile.updateTreatments([], []); diff --git a/lib/report_plugins/daytoday.js b/lib/report_plugins/daytoday.js index 3f66a29de44..e7bcbe79a80 100644 --- a/lib/report_plugins/daytoday.js +++ b/lib/report_plugins/daytoday.js @@ -631,8 +631,80 @@ daytoday.report = function report_daytoday(datastorage,sorteddaystoshow,options) 'translate(' + (xScale2(treatment.mills) + padding.left + 10) + ',' + (padding.top+yInsulinScale(treatment.insulin)) + ')') .text(Number(treatment.insulin).toFixed(2)+'U'); } + + // process the rest + if (treatment.insulin || treatment.carbs || treatment.eventType == 'Temp Basal' || treatment.eventType == 'Combo Bolus') { + // ignore + } else if (treatment.eventType === 'Profile Switch') { + // profile switch + appendProfileSwitch(context, treatment); + if (treatment.duration && !treatment.cuttedby) { + appendProfileSwitch(context, { + cutting: treatment.profile + , profile: client.profilefunctions.activeProfileToTime(times.mins(treatment.duration).msecs + treatment.mills + 1) + , mills: times.mins(treatment.duration).msecs + treatment.mills + , end: true + }); + } + } else if (treatment.eventType === 'Exercise' && treatment.duration) { + context.append('rect') + .attr('x', xScale2(treatment.mills) + padding.left) + .attr('y', yScale2(client.utils.scaleMgdl(396)) + padding.top) + .attr('width', xScale2(treatment.mills + times.mins(treatment.duration).msecs) - xScale2(treatment.mills)) + .attr('height', yScale2(client.utils.scaleMgdl(360)) - yScale2(client.utils.scaleMgdl(396))) + .attr('stroke-width', 1) + .attr('opacity', .2) + .attr('stroke', 'white') + .attr('fill', 'Violet'); + context.append('text') + .style('font-size', '12px') + .style('font-weight', 'bold') + .attr('fill', 'Violet') + .attr('text-anchor', 'middle') + .attr('dy', '.35em') + .attr('y', yScale2(client.utils.scaleMgdl(378)) + padding.top) + .attr('x', xScale2(treatment.mills + times.mins(treatment.duration).msecs/2) + padding.left) + .text(treatment.notes); + } else if (treatment.eventType === 'Note' && treatment.duration) { + context.append('rect') + .attr('x', xScale2(treatment.mills) + padding.left) + .attr('y', yScale2(client.utils.scaleMgdl(360)) + padding.top) + .attr('width', xScale2(treatment.mills + times.mins(treatment.duration).msecs) - xScale2(treatment.mills)) + .attr('height', yScale2(client.utils.scaleMgdl(324)) - yScale2(client.utils.scaleMgdl(360))) + .attr('stroke-width', 1) + .attr('opacity', .2) + .attr('stroke', 'white') + .attr('fill', 'Salmon'); + context.append('text') + .style('font-size', '12px') + .style('font-weight', 'bold') + .attr('fill', 'Salmon') + .attr('text-anchor', 'middle') + .attr('dy', '.35em') + .attr('y', yScale2(client.utils.scaleMgdl(342)) + padding.top) + .attr('x', xScale2(treatment.mills + times.mins(treatment.duration).msecs/2) + padding.left) + .text(treatment.notes); + } else if (treatment.eventType === 'OpenAPS Offline' && treatment.duration) { + context.append('rect') + .attr('x', xScale2(treatment.mills) + padding.left) + .attr('y', yScale2(client.utils.scaleMgdl(324)) + padding.top) + .attr('width', xScale2(treatment.mills + times.mins(treatment.duration).msecs) - xScale2(treatment.mills)) + .attr('height', yScale2(client.utils.scaleMgdl(288)) - yScale2(client.utils.scaleMgdl(324))) + .attr('stroke-width', 1) + .attr('opacity', .2) + .attr('stroke', 'white') + .attr('fill', 'Brown'); + context.append('text') + .style('font-size', '12px') + .style('font-weight', 'bold') + .attr('fill', 'Brown') + .attr('text-anchor', 'middle') + .attr('dy', '.35em') + .attr('y', yScale2(client.utils.scaleMgdl(306)) + padding.top) + .attr('x', xScale2(treatment.mills + times.mins(treatment.duration).msecs/2) + padding.left) + .text(treatment.notes); + } else if (!treatment.duration) { // other treatments without duration - if (!treatment.insulin && !treatment.carbs && !treatment.duration && treatment.eventType !== 'Temp Basal' && treatment.eventType !== 'Profile Switch') { context.append('circle') .attr('cx', xScale2(treatment.mills) + padding.left) .attr('cy', yScale2(scaledTreatmentBG(treatment,data.sgv)) + padding.top) @@ -648,42 +720,28 @@ daytoday.report = function report_daytoday(datastorage,sorteddaystoshow,options) .attr('y', yScale2(scaledTreatmentBG(treatment,data.sgv)) + padding.top -10) .attr('x', xScale2(treatment.mills) + padding.left + 10) .text(translate(client.careportal.resolveEventName(treatment.eventType))); - } + } else if (treatment.duration) { // other treatments with duration - if (!treatment.insulin && !treatment.carbs && treatment.duration && treatment.eventType !== 'Temp Basal' && treatment.eventType !== 'Profile Switch' && treatment.eventType !== 'Combo Bolus') { context.append('rect') .attr('x', xScale2(treatment.mills) + padding.left) - .attr('y', 0 + padding.top) + .attr('y', yScale2(client.utils.scaleMgdl(432)) + padding.top) .attr('width', xScale2(treatment.mills + times.mins(treatment.duration).msecs) - xScale2(treatment.mills)) - .attr('height', chartHeight) - //.attr('rx', 5) - //.attr('ry', 5) + .attr('height', yScale2(client.utils.scaleMgdl(396)) - yScale2(client.utils.scaleMgdl(432))) .attr('stroke-width', 1) .attr('opacity', .2) .attr('stroke', 'white') - .attr('fill', treatment.eventType === 'Exercise' ? 'Violet' : (treatment.eventType === 'Note' ? 'Salmon' : 'black')); + .attr('fill', 'black'); context.append('text') .style('font-size', '12px') .style('font-weight', 'bold') - .attr('fill', treatment.eventType === 'Exercise' ? 'Violet' : (treatment.eventType === 'Note' ? 'Salmon' : 'black')) + .attr('fill', 'black') .attr('text-anchor', 'middle') .attr('dy', '.35em') - .attr('y', foodtexts * 15 + 10 + padding.top) + .attr('y', yScale2(client.utils.scaleMgdl(414)) + padding.top) .attr('x', xScale2(treatment.mills + times.mins(treatment.duration).msecs/2) + padding.left) .text(treatment.notes); - foodtexts = (foodtexts + 1) % 6; - } - // profile switch - if (treatment.eventType === 'Profile Switch') { - appendProfileSwitch(context, treatment); - if (treatment.duration && !treatment.cuttedby) { - appendProfileSwitch(context, { - cutting: treatment.profile - , profile: client.profilefunctions.activeProfileToTime(times.mins(treatment.duration).msecs + treatment.mills + 1) - , mills: times.mins(treatment.duration).msecs + treatment.mills - , end: true - }); - } + } else { + console.log("missed treatment", treatment); } }); diff --git a/lib/sandbox.js b/lib/sandbox.js index 0e031bec8f5..5dd7b63e4bc 100644 --- a/lib/sandbox.js +++ b/lib/sandbox.js @@ -51,7 +51,7 @@ function init ( ) { var profile = require('./profilefunctions')(); //Plugins will expect the right profile based on time - profile.loadData(ctx.ddata.profiles); + profile.loadData(_.cloneDeep(ctx.ddata.profiles)); profile.updateTreatments(ctx.ddata.profileTreatments, ctx.ddata.tempbasalTreatments, ctx.ddata.combobolusTreatments); sbx.data.profile = profile; delete sbx.data.profiles; diff --git a/static/profile/js/profileeditor.js b/static/profile/js/profileeditor.js index 3ff7435e0e8..bcb26425414 100644 --- a/static/profile/js/profileeditor.js +++ b/static/profile/js/profileeditor.js @@ -88,7 +88,7 @@ records.push(defaultprofile); } client.profilefunctions.loadData(records); // do a conversion if needed - mongorecords = client.profilefunctions.data; + mongorecords = _.cloneDeep(client.profilefunctions.data); // create new profile to be edited from last record if (mongorecords.length) { _.each(mongorecords, function eachMongoProfile (mongoprofile) { diff --git a/static/report/js/report.js b/static/report/js/report.js index 60d70af1620..ba637365dcb 100644 --- a/static/report/js/report.js +++ b/static/report/js/report.js @@ -446,7 +446,7 @@ if (options.order === report_plugins.consts.ORDER_NEWESTONTOP) { sorteddaystoshow.reverse(); } - loadProfileSwitch(function loadProfileSwitchCallback() { + loadProfileSwitch(from, function loadProfileSwitchCallback() { loadProfiles(function loadProfilesCallback() { $('#info > b').html('' + translate('Rendering') + ' ...'); window.setTimeout(function () { @@ -629,6 +629,8 @@ } function loadTreatmentData() { + if (!datastorage.profileSwitchTreatments) + datastorage.profileSwitchTreatments = []; $('#info-' + day).html(''+translate('Loading treatments data of')+' '+day+' ...'); var tquery = '?find[created_at][$gte]='+new Date(from).toISOString()+'&find[created_at][$lt]='+new Date(to).toISOString(); return $.ajax('/api/v1/treatments.json'+tquery, { @@ -649,6 +651,11 @@ data.tempbasalTreatments = data.treatments.filter(function filterTempBasals(t) { return t.eventType === 'Temp Basal'; }); + // filter profile switch treatments + var profileSwitch = data.treatments.filter(function filterProfileSwitch(t) { + return t.eventType === 'Profile Switch'; + }); + datastorage.profileSwitchTreatments = datastorage.profileSwitchTreatments.concat(profileSwitch); } }); } @@ -682,9 +689,9 @@ }); } - function loadProfileSwitch(callback) { + function loadProfileSwitch(from, callback) { $('#info > b').html(''+translate('Loading profile switch data') + ' ...'); - var tquery = '?find[eventType]=Profile Switch'; + var tquery = '?find[eventType]=Profile Switch' + '&find[created_at][$lte]=' + new Date(from).toISOString() + '&count=1'; $.ajax('/api/v1/treatments.json'+tquery, { headers: client.headers() , success: function (xhr) { @@ -693,7 +700,9 @@ treatment.mills = timestamp.getTime(); return treatment; }); - datastorage.profileSwitchTreatments = treatmentData.slice(); + if (!datastorage.profileSwitchTreatments) + datastorage.profileSwitchTreatments = []; + datastorage.profileSwitchTreatments = datastorage.profileSwitchTreatments.concat(treatmentData); datastorage.profileSwitchTreatments.sort(function(a, b) { return a.mills - b.mills; }); } }).done(function () {