@@ -19,7 +19,21 @@ const { DataUtil } = vizDataUtil;
1919const { createPrintPDFPackage, utils : PrintPDFUtils } = vizPrintUtil ;
2020const { generateAGPFigureDefinitions } = getAGPFigures ;
2121const {
22- reject, includes, max, map, find, get, pick, flatten, valuesIn, some, keys, isArray, fromPairs,
22+ reject,
23+ includes,
24+ max,
25+ map,
26+ find,
27+ get,
28+ pick,
29+ flatten,
30+ valuesIn,
31+ some,
32+ keys,
33+ isArray,
34+ fromPairs,
35+ reduce,
36+ each,
2337} = _ ;
2438PrintPDFUtils . PDFDocument = PDFKit ;
2539PrintPDFUtils . blobStream = blobStream . default ;
@@ -47,7 +61,8 @@ class Report {
4761 all : 'all' ,
4862 basics : 'basics' ,
4963 bgLog : 'bgLog' ,
50- agp : 'agp' ,
64+ agpBGM : 'agpBGM' ,
65+ agpCGM : 'agpCGM' ,
5166 daily : 'daily' ,
5267 settings : 'settings' ,
5368 } ;
@@ -174,11 +189,11 @@ class Report {
174189 }
175190
176191 getStatsByChartType ( chartType , data ) {
177- const bgSource = _ . get ( data , 'metaData.bgSources.current' ) ;
192+ const bgSource = get ( data , 'metaData.bgSources.current' ) ;
178193 const cbgSelected = bgSource === 'cbg' ;
179194 const smbgSelected = bgSource === 'smbg' ;
180- const isAutomatedBasalDevice = _ . get ( data , 'metaData.latestPumpUpload.isAutomatedBasalDevice' ) ;
181- const isSettingsOverrideDevice = _ . get ( data , 'metaData.latestPumpUpload.isSettingsOverrideDevice' ) ;
195+ const isAutomatedBasalDevice = get ( data , 'metaData.latestPumpUpload.isAutomatedBasalDevice' ) ;
196+ const isSettingsOverrideDevice = get ( data , 'metaData.latestPumpUpload.isSettingsOverrideDevice' ) ;
182197
183198 const stats = [ ] ;
184199
@@ -218,12 +233,21 @@ class Report {
218233 stats . push ( this . #commonStats. coefficientOfVariation ) ;
219234 break ;
220235
221- case 'agp' :
222- stats . push ( this . #commonStats. timeInRange ) ;
236+ case 'agpBGM' :
223237 stats . push ( this . #commonStats. averageGlucose ) ;
224- stats . push ( this . #commonStats. sensorUsage ) ;
238+ stats . push ( this . #commonStats. bgExtents ) ;
239+ stats . push ( this . #commonStats. coefficientOfVariation ) ;
225240 stats . push ( this . #commonStats. glucoseManagementIndicator ) ;
241+ stats . push ( this . #commonStats. readingsInRange ) ;
242+ break ;
243+
244+ case 'agpCGM' :
245+ stats . push ( this . #commonStats. averageGlucose ) ;
246+ stats . push ( this . #commonStats. bgExtents ) ;
226247 stats . push ( this . #commonStats. coefficientOfVariation ) ;
248+ stats . push ( this . #commonStats. glucoseManagementIndicator ) ;
249+ stats . push ( this . #commonStats. sensorUsage ) ;
250+ stats . push ( this . #commonStats. timeInRange ) ;
227251 break ;
228252
229253 case 'trends' :
@@ -249,7 +273,7 @@ class Report {
249273 }
250274
251275 buildReportQueries ( { data } ) {
252- const bgSource = _ . get ( data , 'metaData.bgSources.current' ) ;
276+ const bgSource = get ( data , 'metaData.bgSources.current' ) ;
253277 const dataQueries = {
254278 basics : {
255279 endpoints : [ ] ,
@@ -295,11 +319,24 @@ class Report {
295319 timePrefs : this . getTimePrefs ( ) ,
296320 excludedDevices : [ ] ,
297321 } ,
298- agp : {
322+ agpBGM : {
323+ endpoints : [ ] ,
324+ aggregationsByDate : 'dataByDate, statsByDate' ,
325+ bgSource : 'smbg' ,
326+ stats : this . getStatsByChartType ( 'agpBGM' , data ) ,
327+ types : {
328+ smbg : { } ,
329+ } ,
330+ bgPrefs : this . getBGPrefs ( ) ,
331+ metaData : 'latestPumpUpload, bgSources' ,
332+ timePrefs : this . getTimePrefs ( ) ,
333+ excludedDevices : [ ] ,
334+ } ,
335+ agpCGM : {
299336 endpoints : [ ] ,
300337 aggregationsByDate : 'dataByDate, statsByDate' ,
301338 bgSource : 'cbg' ,
302- stats : this . getStatsByChartType ( 'agp ' , data ) ,
339+ stats : this . getStatsByChartType ( 'agpCGM ' , data ) ,
303340 types : {
304341 cbg : { } ,
305342 } ,
@@ -324,8 +361,11 @@ class Report {
324361 if ( ! this . #reports. includes ( this . #reportTypes. bgLog ) ) {
325362 delete dataQueries . bgLog ;
326363 }
327- if ( ! this . #reports. includes ( this . #reportTypes. agp ) ) {
328- delete dataQueries . agp ;
364+ if ( ! this . #reports. includes ( this . #reportTypes. agpBGM ) ) {
365+ delete dataQueries . agpBGM ;
366+ }
367+ if ( ! this . #reports. includes ( this . #reportTypes. agpCGM ) ) {
368+ delete dataQueries . agpCGM ;
329369 }
330370 if ( ! this . #reports. includes ( this . #reportTypes. settings ) ) {
331371 delete dataQueries . settings ;
@@ -436,7 +476,8 @@ class Report {
436476 const {
437477 data,
438478 days = {
439- agp : 14 ,
479+ agpBGM : 30 ,
480+ agpCGM : 14 ,
440481 basics : 14 ,
441482 daily : 14 ,
442483 bgLog : 30 ,
@@ -463,7 +504,11 @@ class Report {
463504 }
464505
465506 return {
466- agp : {
507+ agpBGM : {
508+ startDate : bgLogStartDate ,
509+ endDate,
510+ } ,
511+ agpCGM : {
467512 startDate,
468513 endDate,
469514 } ,
@@ -484,7 +529,8 @@ class Report {
484529 }
485530
486531 const dates = {
487- agp : { } ,
532+ agpBGM : { } ,
533+ agpCGM : { } ,
488534 daily : { } ,
489535 basics : { } ,
490536 bgLog : { } ,
@@ -521,13 +567,19 @@ class Report {
521567 ] ) ,
522568 ) ;
523569
524- dates . agp . endDate = lastAGPDate
570+ dates . agpCGM . endDate = lastAGPDate
525571 ? moment . utc ( lastAGPDate ) . tz ( this . #timezoneName) . add ( 1 , 'day' ) . startOf ( 'day' )
526572 : endOfToday ( ) ;
573+ dates . agpCGM . startDate = moment . utc ( dates . agp . endDate )
574+ . tz ( this . #timezoneName)
575+ . subtract ( days . agpCGM - 1 , 'days' ) ;
527576
528- dates . agp . startDate = moment . utc ( dates . agp . endDate )
577+ dates . agpBGM . endDate = lastAGPDate
578+ ? moment . utc ( lastAGPDate ) . tz ( this . #timezoneName) . add ( 1 , 'day' ) . startOf ( 'day' )
579+ : endOfToday ( ) ;
580+ dates . agpBGM . startDate = moment . utc ( dates . agpBGM . endDate )
529581 . tz ( this . #timezoneName)
530- . subtract ( days . agp - 1 , 'days' ) ;
582+ . subtract ( days . agpBGM - 1 , 'days' ) ;
531583
532584 dates . daily . endDate = lastDailyDate
533585 ? moment . utc ( lastDailyDate ) . tz ( this . #timezoneName) . add ( 1 , 'day' ) . startOf ( 'day' )
@@ -564,10 +616,17 @@ class Report {
564616 const reportQueries = this . buildReportQueries ( { data } ) ;
565617
566618 const printOptions = {
567- agp : {
619+ agpBGM : {
568620 endpoints : [
569- datesByReport . agp . startDate . toDate ( ) ,
570- datesByReport . agp . endDate . toDate ( ) ,
621+ datesByReport . agpBGM . startDate . toDate ( ) ,
622+ datesByReport . agpBGM . endDate . toDate ( ) ,
623+ ] ,
624+ disabled : false ,
625+ } ,
626+ agpCGM : {
627+ endpoints : [
628+ datesByReport . agpCGM . startDate . toDate ( ) ,
629+ datesByReport . agpCGM . endDate . toDate ( ) ,
571630 ] ,
572631 disabled : false ,
573632 } ,
@@ -619,8 +678,11 @@ class Report {
619678 ) {
620679 reportQueries . basics . endpoints = printOptions . basics . endpoints ;
621680 }
622- if ( this . #reports. includes ( this . #reportTypes. all , this . #reportTypes. agp ) ) {
623- reportQueries . agp . endpoints = printOptions . agp . endpoints ;
681+ if ( this . #reports. includes ( this . #reportTypes. all , this . #reportTypes. agpBGM ) ) {
682+ reportQueries . agpBGM . endpoints = printOptions . agpBGM . endpoints ;
683+ }
684+ if ( this . #reports. includes ( this . #reportTypes. all , this . #reportTypes. agpCGM ) ) {
685+ reportQueries . agpCGM . endpoints = printOptions . agpCGM . endpoints ;
624686 }
625687 if (
626688 this . #reports. includes ( this . #reportTypes. all , this . #reportTypes. bgLog )
@@ -660,11 +722,18 @@ class Report {
660722 const pdfData = { } ;
661723 const options = opts ;
662724
663- if ( queries . agp ) {
664- pdfData . agp = this . #dataUtil. query ( queries . agp ) ;
665- options . agp . disabled = ! containsDataForReport (
725+ if ( queries . agpBGM ) {
726+ pdfData . agpBGM = this . #dataUtil. query ( queries . agpBGM ) ;
727+ options . agpBGM . disabled = ! containsDataForReport (
728+ pdfData ,
729+ 'agpBGM.data.current.data' ,
730+ ) ;
731+ }
732+ if ( queries . agpCGM ) {
733+ pdfData . agpCGM = this . #dataUtil. query ( queries . agpCGM ) ;
734+ options . agpCGM . disabled = ! containsDataForReport (
666735 pdfData ,
667- 'agp .data.current.data' ,
736+ 'agpCGM .data.current.data' ,
668737 ) ;
669738 }
670739 if ( queries . daily ) {
@@ -707,22 +776,35 @@ class Report {
707776 return this . resp . data ;
708777 }
709778
710- async processAGPSVGs ( agpPDFData ) {
711- this . agpSVGFigures = await generateAGPFigureDefinitions ( {
712- ...agpPDFData ,
713- } ) ;
714- this . agpFigurePromises = map ( this . agpSVGFigures , async ( image , key ) => {
715- if ( isArray ( image ) ) {
716- const processedArray = await Promise . all (
717- map ( image , async ( img ) => this . graphRendererOrca ( img ) ) ,
718- ) ;
719- return [ key , processedArray ] ;
720- }
721- const processedValue = await this . graphRendererOrca ( image ) ;
722- return [ key , processedValue ] ;
779+ async processAGPSVGs ( agpPDFData , reportTypes ) {
780+ const promises = [ ] ;
781+
782+ await each ( reportTypes , async ( reportType ) => {
783+ const images = await generateAGPFigureDefinitions ( {
784+ ...agpPDFData ?. [ reportType ] ,
785+ } ) ;
786+
787+ promises . push ( ...map ( images , async ( image , key ) => {
788+ if ( isArray ( image ) ) {
789+ const processedArray = await Promise . all (
790+ map ( image , async ( img ) => this . graphRendererOrca ( img ) ) ,
791+ ) ;
792+ return [ reportType , [ key , processedArray ] ] ;
793+ }
794+ const processedValue = await this . graphRendererOrca ( image ) ;
795+ return [ reportType , [ key , processedValue ] ] ;
796+ } ) ) ;
723797 } ) ;
724- const processedEntries = await Promise . all ( this . agpFigurePromises ) ;
725- return fromPairs ( processedEntries ) ;
798+
799+ const results = await Promise . all ( promises ) ;
800+
801+ const processedImages = reduce ( results , ( res , entry ) => {
802+ const processedImage = fromPairs ( entry . slice ( 1 ) ) ;
803+ res [ entry [ 0 ] ] = { ...res [ entry [ 0 ] ] , ...processedImage } ;
804+ return res ;
805+ } , { } ) ;
806+
807+ return processedImages ;
726808 }
727809
728810 async generate ( ) {
@@ -836,9 +918,17 @@ class Report {
836918 this . #log. debug ( 'getting pdf report data' ) ;
837919
838920 const reportData = this . runReportQueries ( queries , printOptions ) ;
839- if ( ! reportData . options . agp . disabled ) {
921+ const reportTypes = [ ] ;
922+ if ( ! reportData . options . agpBGM . disabled ) {
923+ reportTypes . push ( 'agpBGM' ) ;
924+ }
925+ if ( ! reportData . options . agpCGM . disabled ) {
926+ reportTypes . push ( 'agpCGM' ) ;
927+ }
928+ if ( reportTypes . length > 0 ) {
840929 reportData . options . svgDataURLS = await this . processAGPSVGs (
841- reportData . pdfData . agp ,
930+ reportData . pdfData ,
931+ reportTypes ,
842932 ) ;
843933 }
844934
0 commit comments