Created
April 25, 2016 02:05
-
-
Save mddub/9171a269b89099e44749136b36e6c242 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
diff --git a/static/report/js/report.js b/static/report/js/report.js | |
index 924bbcb..8b61b8c 100644 | |
--- a/static/report/js/report.js | |
+++ b/static/report/js/report.js | |
@@ -44,6 +44,14 @@ | |
}; | |
var ONE_MIN_IN_MS = 60000; | |
+ | |
+ function startOfDayForQuery(day) { | |
+ if (client.sbx.data.profile.getTimezone()) { | |
+ return parseInt(moment(day).tz(client.sbx.data.profile.getTimezone()).startOf('day').format('x')); | |
+ } else { | |
+ return parseInt(moment(day).startOf('day').format('x')); | |
+ } | |
+ } | |
prepareGUI(); | |
@@ -427,10 +435,12 @@ | |
if (options.order === report_plugins.consts.ORDER_NEWESTONTOP) { | |
sorteddaystoshow.reverse(); | |
} | |
- loadProfileSwitch(from, function loadProfileSwitchCallback() { | |
- $('#info > b').html('<b>'+translate('Rendering')+' ...</b>'); | |
- window.setTimeout(function () {showreports(options); }, 0); | |
+ loadExtraTreatments(from, function loadTempBasalsCallback() { | |
+ loadProfileSwitch(from, function loadProfileSwitchCallback() { | |
+ $('#info > b').html('<b>'+translate('Rendering')+' ...</b>'); | |
+ window.setTimeout(function () {showreports(options); }, 0); | |
}); | |
+ }); | |
} | |
} | |
@@ -452,15 +462,9 @@ | |
options.maxInsulinValue = maxInsulinValue; | |
options.maxCarbsValue = maxCarbsValue; | |
- datastorage.treatments = []; | |
datastorage.devicestatus = []; | |
- datastorage.combobolusTreatments = []; | |
- datastorage.tempbasalTreatments = []; | |
Object.keys(daystoshow).forEach( function eachDay(day) { | |
- datastorage.treatments = datastorage.treatments.concat(datastorage[day].treatments); | |
datastorage.devicestatus = datastorage.devicestatus.concat(datastorage[day].devicestatus); | |
- datastorage.combobolusTreatments = datastorage.combobolusTreatments.concat(datastorage[day].combobolusTreatments); | |
- datastorage.tempbasalTreatments = datastorage.tempbasalTreatments.concat(datastorage[day].tempbasalTreatments); | |
}); | |
report_plugins.eachPlugin(function (plugin) { | |
@@ -509,13 +513,7 @@ | |
, treatmentData = [] | |
, calData = [] | |
; | |
- var from; | |
- if (client.sbx.data.profile.getTimezone()) { | |
- from = moment(day).tz(client.sbx.data.profile.getTimezone()).startOf('day').format('x'); | |
- } else { | |
- from = moment(day).startOf('day').format('x'); | |
- } | |
- from = parseInt(from); | |
+ var from = startOfDayForQuery(day); | |
var to = from + 1000 * 60 * 60 * 24; | |
function loadCGMData() { | |
@@ -619,6 +617,62 @@ | |
processData(data, day, options, callback); | |
}); | |
} | |
+ | |
+ function loadExtraTreatments(from, callback) { | |
+ $('#info > b').html('<b>'+translate('Loading extra treatment data') + ' ...</b>'); | |
+ | |
+ // to accurately graph temp basals and IOB, we need to ensure that for every day graphed, | |
+ // we also fetch treatment data from the day before | |
+ var fetches = Object.keys(daystoshow).map(function dayBefore(day) { | |
+ return moment(day).subtract(1, 'day').format('YYYY-MM-DD'); | |
+ }).filter(function notAlreadyFetched(day) { | |
+ return Object.keys(daystoshow).indexOf(day) === -1; | |
+ }).map(function fetchExtraDay (extraDay) { | |
+ var from = startOfDayForQuery(extraDay); | |
+ var to = from + 1000 * 60 * 60 * 24; | |
+ 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); | |
+ }); | |
+ | |
+ $.when.apply($, fetches).done(function() { | |
+ // XXX: $.when(req1).done(fn) invokes fn(resp1, status1) | |
+ // $.when(req1, req2).done(fn) invokes fn([resp1, status1], [resp2, status2]) | |
+ // because jQuery is terrible | |
+ var responses = (fetches.length === 1) ? [arguments[0]] : Array.prototype.map.call(arguments, function(args) { return args[0]; }); | |
+ | |
+ datastorage.treatments = []; | |
+ datastorage.combobolusTreatments = []; | |
+ datastorage.tempbasalTreatments = []; | |
+ // first add the treatments already loaded | |
+ Object.keys(daystoshow).forEach(function eachDay(day) { | |
+ datastorage.treatments = datastorage.treatments.concat(datastorage[day].treatments); | |
+ datastorage.combobolusTreatments = datastorage.combobolusTreatments.concat(datastorage[day].combobolusTreatments); | |
+ datastorage.tempbasalTreatments = datastorage.tempbasalTreatments.concat(datastorage[day].tempbasalTreatments); | |
+ }); | |
+ // then add the extra treatments just fetched | |
+ responses.forEach(function eachExtraDay(response) { | |
+ var data = response.map(function (treatment) { | |
+ var timestamp = new Date(treatment.timestamp || treatment.created_at); | |
+ treatment.mills = timestamp.getTime(); | |
+ return treatment; | |
+ }); | |
+ datastorage.treatments = datastorage.treatments.concat(data); | |
+ datastorage.combobolusTreatments = datastorage.combobolusTreatments.concat(data.filter(function filterComboBoluses(treatment) { | |
+ return treatment.eventType === 'Combo Bolus'; | |
+ })); | |
+ datastorage.tempbasalTreatments = datastorage.tempbasalTreatments.concat(data.filter(function filterTempBasals(treatment) { | |
+ return treatment.eventType === 'Temp Basal'; | |
+ })); | |
+ }); | |
+ // sort | |
+ [datastorage.treatments, datastorage.combobolusTreatments, datastorage.tempbasalTreatments].forEach(function(treatments) { | |
+ treatments.sort(function(a, b) { return a.mills - b.mills; }); | |
+ }); | |
+ // trim duration of each temp basal | |
+ datastorage.tempbasalTreatments = Nightscout.client.ddata.processTempBasals(datastorage.tempbasalTreatments); | |
+ callback(); | |
+ }); | |
+ } | |
function loadProfileSwitch(from, callback) { | |
$('#info > b').html('<b>'+translate('Loading profile switch data') + ' ...</b>'); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment