1+ /* eslint-disable no-unused-expressions */
12import getAGPFigures from '@tidepool/viz/dist/getAGPFigures.js' ;
23import vizDataUtil from '@tidepool/viz/dist/data.js' ;
34import { Blob } from 'buffer' ;
@@ -51,6 +52,22 @@ class Report {
5152 settings : 'settings' ,
5253 } ;
5354
55+ #commonStats = {
56+ averageGlucose : 'averageGlucose' ,
57+ averageDailyDose : 'averageDailyDose' ,
58+ bgExtents : 'bgExtents' ,
59+ carbs : 'carbs' ,
60+ coefficientOfVariation : 'coefficientOfVariation' ,
61+ glucoseManagementIndicator : 'glucoseManagementIndicator' ,
62+ readingsInRange : 'readingsInRange' ,
63+ sensorUsage : 'sensorUsage' ,
64+ standardDev : 'standardDev' ,
65+ timeInAuto : 'timeInAuto' ,
66+ timeInOverride : 'timeInOverride' ,
67+ timeInRange : 'timeInRange' ,
68+ totalInsulin : 'totalInsulin' ,
69+ } ;
70+
5471 #reports = [ this . #reportTypes. all ] ;
5572
5673 #reportDates;
@@ -156,24 +173,89 @@ class Report {
156173 } ;
157174 }
158175
159- buildReportQueries ( ) {
176+ getStatsByChartType ( chartType , data ) {
177+ const bgSource = _ . get ( data , 'metaData.bgSources.current' ) ;
178+ const cbgSelected = bgSource === 'cbg' ;
179+ const smbgSelected = bgSource === 'smbg' ;
180+ const isAutomatedBasalDevice = _ . get ( data , 'metaData.latestPumpUpload.isAutomatedBasalDevice' ) ;
181+ const isSettingsOverrideDevice = _ . get ( data , 'metaData.latestPumpUpload.isSettingsOverrideDevice' ) ;
182+
183+ const stats = [ ] ;
184+
185+ switch ( chartType ) {
186+ case 'basics' :
187+ cbgSelected && stats . push ( this . #commonStats. timeInRange ) ;
188+ smbgSelected && stats . push ( this . #commonStats. readingsInRange ) ;
189+ stats . push ( this . #commonStats. averageGlucose ) ;
190+ cbgSelected && stats . push ( this . #commonStats. sensorUsage ) ;
191+ stats . push ( this . #commonStats. totalInsulin ) ;
192+ isAutomatedBasalDevice && stats . push ( this . #commonStats. timeInAuto ) ;
193+ isSettingsOverrideDevice && stats . push ( this . #commonStats. timeInOverride ) ;
194+ stats . push ( this . #commonStats. carbs ) ;
195+ stats . push ( this . #commonStats. averageDailyDose ) ;
196+ cbgSelected && stats . push ( this . #commonStats. glucoseManagementIndicator ) ;
197+ stats . push ( this . #commonStats. standardDev ) ;
198+ stats . push ( this . #commonStats. coefficientOfVariation ) ;
199+ stats . push ( this . #commonStats. bgExtents ) ;
200+ break ;
201+
202+ case 'daily' :
203+ cbgSelected && stats . push ( this . #commonStats. timeInRange ) ;
204+ smbgSelected && stats . push ( this . #commonStats. readingsInRange ) ;
205+ stats . push ( this . #commonStats. averageGlucose ) ;
206+ stats . push ( this . #commonStats. totalInsulin ) ;
207+ isAutomatedBasalDevice && stats . push ( this . #commonStats. timeInAuto ) ;
208+ isSettingsOverrideDevice && stats . push ( this . #commonStats. timeInOverride ) ;
209+ stats . push ( this . #commonStats. carbs ) ;
210+ cbgSelected && stats . push ( this . #commonStats. standardDev ) ;
211+ cbgSelected && stats . push ( this . #commonStats. coefficientOfVariation ) ;
212+ break ;
213+
214+ case 'bgLog' :
215+ stats . push ( this . #commonStats. readingsInRange ) ;
216+ stats . push ( this . #commonStats. averageGlucose ) ;
217+ stats . push ( this . #commonStats. standardDev ) ;
218+ stats . push ( this . #commonStats. coefficientOfVariation ) ;
219+ break ;
220+
221+ case 'agp' :
222+ stats . push ( this . #commonStats. timeInRange ) ;
223+ stats . push ( this . #commonStats. averageGlucose ) ;
224+ stats . push ( this . #commonStats. sensorUsage ) ;
225+ stats . push ( this . #commonStats. glucoseManagementIndicator ) ;
226+ stats . push ( this . #commonStats. coefficientOfVariation ) ;
227+ break ;
228+
229+ case 'trends' :
230+ cbgSelected && stats . push ( this . #commonStats. timeInRange ) ;
231+ smbgSelected && stats . push ( this . #commonStats. readingsInRange ) ;
232+ stats . push ( this . #commonStats. averageGlucose ) ;
233+ cbgSelected && stats . push ( this . #commonStats. sensorUsage ) ;
234+ stats . push ( this . #commonStats. totalInsulin ) ;
235+ stats . push ( this . #commonStats. averageDailyDose ) ;
236+ isAutomatedBasalDevice && stats . push ( this . #commonStats. timeInAuto ) ;
237+ isSettingsOverrideDevice && stats . push ( this . #commonStats. timeInOverride ) ;
238+ cbgSelected && stats . push ( this . #commonStats. glucoseManagementIndicator ) ;
239+ stats . push ( this . #commonStats. standardDev ) ;
240+ stats . push ( this . #commonStats. coefficientOfVariation ) ;
241+ stats . push ( this . #commonStats. bgExtents ) ;
242+ break ;
243+
244+ default :
245+ break ;
246+ }
247+
248+ return stats ;
249+ }
250+
251+ buildReportQueries ( { data } ) {
252+ const bgSource = _ . get ( data , 'metaData.bgSources.current' ) ;
160253 const dataQueries = {
161254 basics : {
162255 endpoints : [ ] ,
163256 aggregationsByDate : 'basals, boluses, fingersticks, siteChanges' ,
164- bgSource : 'cbg' ,
165- stats : [
166- 'timeInRange' ,
167- 'averageGlucose' ,
168- 'sensorUsage' ,
169- 'totalInsulin' ,
170- 'carbs' ,
171- 'averageDailyDose' ,
172- 'glucoseManagementIndicator' ,
173- 'standardDev' ,
174- 'coefficientOfVariation' ,
175- 'bgExtents' ,
176- ] ,
257+ bgSource : bgSource || 'cbg' ,
258+ stats : this . getStatsByChartType ( 'basics' , data ) ,
177259 excludeDaysWithoutBolus : false ,
178260 bgPrefs : this . getBGPrefs ( ) ,
179261 metaData : 'latestPumpUpload, bgSources' ,
@@ -183,12 +265,7 @@ class Report {
183265 bgLog : {
184266 endpoints : [ ] ,
185267 aggregationsByDate : 'dataByDate' ,
186- stats : [
187- 'readingsInRange' ,
188- 'averageGlucose' ,
189- 'standardDev' ,
190- 'coefficientOfVariation' ,
191- ] ,
268+ stats : this . getStatsByChartType ( 'bgLog' , data ) ,
192269 types : {
193270 smbg : { } ,
194271 } ,
@@ -201,14 +278,7 @@ class Report {
201278 daily : {
202279 endpoints : [ ] ,
203280 aggregationsByDate : 'dataByDate, statsByDate' ,
204- stats : [
205- 'timeInRange' ,
206- 'averageGlucose' ,
207- 'totalInsulin' ,
208- 'carbs' ,
209- 'standardDev' ,
210- 'coefficientOfVariation' ,
211- ] ,
281+ stats : this . getStatsByChartType ( 'daily' , data ) ,
212282 types : {
213283 basal : { } ,
214284 bolus : { } ,
@@ -219,7 +289,7 @@ class Report {
219289 smbg : { } ,
220290 wizard : { } ,
221291 } ,
222- bgSource : 'cbg' ,
292+ bgSource : bgSource || 'cbg' ,
223293 bgPrefs : this . getBGPrefs ( ) ,
224294 metaData : 'latestPumpUpload, bgSources' ,
225295 timePrefs : this . getTimePrefs ( ) ,
@@ -229,13 +299,7 @@ class Report {
229299 endpoints : [ ] ,
230300 aggregationsByDate : 'dataByDate, statsByDate' ,
231301 bgSource : 'cbg' ,
232- stats : [
233- 'timeInRange' ,
234- 'averageGlucose' ,
235- 'sensorUsage' ,
236- 'glucoseManagementIndicator' ,
237- 'coefficientOfVariation' ,
238- ] ,
302+ stats : this . getStatsByChartType ( 'agp' , data ) ,
239303 types : {
240304 cbg : { } ,
241305 } ,
@@ -497,7 +561,7 @@ class Report {
497561 getReportOptions ( data ) {
498562 const datesByReport = this . getDateRangeByReport ( { data } ) ;
499563
500- const reportQueries = this . buildReportQueries ( ) ;
564+ const reportQueries = this . buildReportQueries ( { data } ) ;
501565
502566 const printOptions = {
503567 agp : {
@@ -702,6 +766,61 @@ class Report {
702766 fetchConfig ,
703767 ) ;
704768
769+ if ( this . #reportDates) {
770+ // fetch the latest pump settings record for date bound reports
771+ const pumpSettingsFetch = await fetchUserData (
772+ this . #userDetail. userId ,
773+ {
774+ headers : this . #requestData. sessionHeader ,
775+ params : {
776+ type : 'pumpSettings' ,
777+ latest : 1 ,
778+ } ,
779+ } ,
780+ ) . catch ( ( error ) => {
781+ this . #log. error ( error ) ;
782+ } ) ;
783+
784+ if ( pumpSettingsFetch ?. length > 0 ) {
785+ userData . push ( pumpSettingsFetch [ 0 ] ) ;
786+ }
787+
788+ const latestPumpSettings = find ( userData , {
789+ type : 'pumpSettings' ,
790+ } ) ;
791+
792+ const latestPumpSettingsUploadId = get (
793+ latestPumpSettings || { } ,
794+ 'uploadId' ,
795+ ) ;
796+
797+ const latestPumpSettingsUpload = find ( userData , {
798+ type : 'upload' ,
799+ uploadId : latestPumpSettingsUploadId ,
800+ } ) ;
801+
802+ if ( latestPumpSettingsUploadId && ! latestPumpSettingsUpload ) {
803+ // If we have pump settings, but we don't have the corresponing upload record used
804+ // to get the device source, we need to fetch it
805+ const pumpSettingsUploadFetch = await fetchUserData (
806+ this . #userDetail. userId ,
807+ {
808+ headers : this . #requestData. sessionHeader ,
809+ params : {
810+ type : 'upload' ,
811+ uploadId : latestPumpSettingsUploadId ,
812+ } ,
813+ } ,
814+ ) . catch ( ( error ) => {
815+ this . #log. error ( error ) ;
816+ } ) ;
817+
818+ if ( pumpSettingsUploadFetch ?. length > 0 ) {
819+ userData . push ( pumpSettingsUploadFetch [ 0 ] ) ;
820+ }
821+ }
822+ }
823+
705824 this . #log. debug ( `Downloading data for User ${ this . #userDetail. userId } ...` ) ;
706825
707826 this . #log. debug ( 'add data to dataUtil' ) ;
0 commit comments