Skip to content

Commit

Permalink
flatten AverageGlucose
Browse files Browse the repository at this point in the history
  • Loading branch information
Roukoswarf committed Aug 14, 2023
1 parent 7685e80 commit 3b0d0c2
Show file tree
Hide file tree
Showing 5 changed files with 109 additions and 122 deletions.
21 changes: 9 additions & 12 deletions data/summary/types/bgm.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,9 +26,9 @@ type BGMBucketData struct {
}

type BGMPeriod struct {
HasAverageGlucose bool `json:"hasAverageGlucose" bson:"hasAverageGlucose"`
AverageGlucose *Glucose `json:"averageGlucose" bson:"averageGlucose"`
AverageGlucoseDelta *float64 `json:"averageGlucoseDelta" bson:"averageGlucoseDelta"`
HasAverageGlucoseMmol bool `json:"hasAverageGlucoseMmol" bson:"hasAverageGlucoseMmol"`
AverageGlucoseMmol *float64 `json:"averageGlucoseMmol" bson:"averageGlucoseMmol"`
AverageGlucoseMmolDelta *float64 `json:"averageGlucoseMmolDelta" bson:"averageGlucoseMmolDelta"`

HasTotalRecords bool `json:"hasTotalRecords" bson:"hasTotalRecords"`
TotalRecords *int `json:"totalRecords" bson:"totalRecords"`
Expand Down Expand Up @@ -228,11 +228,11 @@ func (s *BGMStats) CalculateDelta() {
// approach.

for k := range s.Periods {
if s.Periods[k].AverageGlucose != nil && s.OffsetPeriods[k].AverageGlucose != nil {
delta := s.Periods[k].AverageGlucose.Value - s.OffsetPeriods[k].AverageGlucose.Value
if s.Periods[k].AverageGlucoseMmol != nil && s.OffsetPeriods[k].AverageGlucoseMmol != nil {
delta := *s.Periods[k].AverageGlucoseMmol - *s.OffsetPeriods[k].AverageGlucoseMmol

s.Periods[k].AverageGlucoseDelta = pointer.FromAny(delta)
s.OffsetPeriods[k].AverageGlucoseDelta = pointer.FromAny(-delta)
s.Periods[k].AverageGlucoseMmolDelta = pointer.FromAny(delta)
s.OffsetPeriods[k].AverageGlucoseMmolDelta = pointer.FromAny(-delta)
}

if s.Periods[k].TotalRecords != nil && s.OffsetPeriods[k].TotalRecords != nil {
Expand Down Expand Up @@ -368,11 +368,8 @@ func (s *BGMStats) CalculatePeriod(i int, offset bool, totalStats *BGMBucketData
newPeriod.HasTimeInVeryHighPercent = true
newPeriod.TimeInVeryHighPercent = pointer.FromAny(float64(totalStats.VeryHighRecords) / float64(totalStats.TotalRecords))

newPeriod.HasAverageGlucose = true
newPeriod.AverageGlucose = &Glucose{
Value: totalStats.TotalGlucose / float64(totalStats.TotalRecords),
Units: glucose.MmolL,
}
newPeriod.HasAverageGlucoseMmol = true
newPeriod.AverageGlucoseMmol = pointer.FromAny(totalStats.TotalGlucose / float64(totalStats.TotalRecords))
}

if offset {
Expand Down
23 changes: 10 additions & 13 deletions data/summary/types/cgm.go
Original file line number Diff line number Diff line change
Expand Up @@ -49,9 +49,9 @@ type CGMPeriod struct {
TimeCGMUseRecords *int `json:"timeCGMUseRecords" bson:"timeCGMUseRecords"`
TimeCGMUseRecordsDelta *int `json:"timeCGMUseRecordsDelta" bson:"timeCGMUseRecordsDelta"`

HasAverageGlucose bool `json:"hasAverageGlucose" bson:"hasAverageGlucose"`
AverageGlucose *Glucose `json:"averageGlucose" bson:"averageGlucose"`
AverageGlucoseDelta *float64 `json:"averageGlucoseDelta" bson:"averageGlucoseDelta"`
HasAverageGlucoseMmol bool `json:"hasAverageGlucoseMmol" bson:"hasAverageGlucoseMmol"`
AverageGlucoseMmol *float64 `json:"averageGlucoseMmol" bson:"averageGlucoseMmol"`
AverageGlucoseMmolDelta *float64 `json:"averageGlucoseMmolDelta" bson:"averageGlucoseMmolDelta"`

HasGlucoseManagementIndicator bool `json:"hasGlucoseManagementIndicator" bson:"hasGlucoseManagementIndicator"`
GlucoseManagementIndicator *float64 `json:"glucoseManagementIndicator" bson:"glucoseManagementIndicator"`
Expand Down Expand Up @@ -336,11 +336,11 @@ func (s *CGMStats) CalculateDelta() {
s.OffsetPeriods[k].TimeCGMUseMinutesDelta = pointer.FromAny(-delta)
}

if s.Periods[k].AverageGlucose != nil && s.OffsetPeriods[k].AverageGlucose != nil {
delta := s.Periods[k].AverageGlucose.Value - s.OffsetPeriods[k].AverageGlucose.Value
if s.Periods[k].AverageGlucoseMmol != nil && s.OffsetPeriods[k].AverageGlucoseMmol != nil {
delta := *s.Periods[k].AverageGlucoseMmol - *s.OffsetPeriods[k].AverageGlucoseMmol

s.Periods[k].AverageGlucoseDelta = pointer.FromAny(delta)
s.OffsetPeriods[k].AverageGlucoseDelta = pointer.FromAny(-delta)
s.Periods[k].AverageGlucoseMmolDelta = pointer.FromAny(delta)
s.OffsetPeriods[k].AverageGlucoseMmolDelta = pointer.FromAny(-delta)
}

if s.Periods[k].GlucoseManagementIndicator != nil && s.OffsetPeriods[k].GlucoseManagementIndicator != nil {
Expand Down Expand Up @@ -547,16 +547,13 @@ func (s *CGMStats) CalculatePeriod(i int, offset bool, totalStats *CGMBucketData
newPeriod.TimeInVeryHighPercent = pointer.FromAny(float64(totalStats.VeryHighMinutes) / float64(totalStats.TotalMinutes))
}

newPeriod.HasAverageGlucose = true
newPeriod.AverageGlucose = &Glucose{
Value: totalStats.TotalGlucose / float64(totalStats.TotalMinutes),
Units: glucose.MmolL,
}
newPeriod.HasAverageGlucoseMmol = true
newPeriod.AverageGlucoseMmol = pointer.FromAny(totalStats.TotalGlucose / float64(totalStats.TotalMinutes))

// we only add GMI if cgm use >70%, otherwise clear it
if *newPeriod.TimeCGMUsePercent > 0.7 {
newPeriod.HasGlucoseManagementIndicator = true
newPeriod.GlucoseManagementIndicator = pointer.FromAny(CalculateGMI(newPeriod.AverageGlucose.Value))
newPeriod.GlucoseManagementIndicator = pointer.FromAny(CalculateGMI(*newPeriod.AverageGlucoseMmol))
}
}

Expand Down
76 changes: 38 additions & 38 deletions data/summary/types/summary_bgm_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -239,8 +239,8 @@ var _ = Describe("BGM Summary", func() {
Expect(userBGMSummary.Stats.Buckets[i].LastRecordTime).To(Equal(lastRecordTime))
Expect(userBGMSummary.Stats.Buckets[i].Data.TotalGlucose).To(Equal(float64((i - 143) * 6)))

averageGlucose := userBGMSummary.Stats.Buckets[i].Data.TotalGlucose / float64(userBGMSummary.Stats.Buckets[i].Data.TotalRecords)
Expect(averageGlucose).To(BeNumerically("~", i-143, 0.005))
averageGlucoseMmol := userBGMSummary.Stats.Buckets[i].Data.TotalGlucose / float64(userBGMSummary.Stats.Buckets[i].Data.TotalRecords)
Expect(averageGlucoseMmol).To(BeNumerically("~", i-143, 0.005))
}
})

Expand Down Expand Up @@ -361,7 +361,7 @@ var _ = Describe("BGM Summary", func() {
vIntTwo := vInt * 2

userBGMSummary.Stats.Periods[period] = &types.BGMPeriod{
AverageGlucose: &types.Glucose{Value: vFloat, Units: units},
AverageGlucoseMmol: pointer.FromAny(vFloat),
TotalRecords: pointer.FromAny(vInt),
AverageDailyRecords: pointer.FromAny(vFloat),
TimeInTargetPercent: pointer.FromAny(vFloat),
Expand All @@ -377,7 +377,7 @@ var _ = Describe("BGM Summary", func() {
}

userBGMSummary.Stats.OffsetPeriods[period] = &types.BGMPeriod{
AverageGlucose: &types.Glucose{Value: vFloatTwo, Units: units},
AverageGlucoseMmol: pointer.FromAny(vFloatTwo),
TotalRecords: pointer.FromAny(vIntTwo),
AverageDailyRecords: pointer.FromAny(vFloatTwo),
TimeInTargetPercent: pointer.FromAny(vFloatTwo),
Expand All @@ -399,8 +399,8 @@ var _ = Describe("BGM Summary", func() {
floatDiff := float64(i)*7.5 - float64(i)*7.5*2
intDiff := i*7 - i*7*2

Expect(*userBGMSummary.Stats.Periods[period].AverageGlucoseDelta).To(Equal(floatDiff))
Expect(*userBGMSummary.Stats.OffsetPeriods[period].AverageGlucoseDelta).To(Equal(-floatDiff))
Expect(*userBGMSummary.Stats.Periods[period].AverageGlucoseMmolDelta).To(Equal(floatDiff))
Expect(*userBGMSummary.Stats.OffsetPeriods[period].AverageGlucoseMmolDelta).To(Equal(-floatDiff))

Expect(*userBGMSummary.Stats.Periods[period].TotalRecordsDelta).To(Equal(intDiff))
Expect(*userBGMSummary.Stats.OffsetPeriods[period].TotalRecordsDelta).To(Equal(-intDiff))
Expand Down Expand Up @@ -440,7 +440,7 @@ var _ = Describe("BGM Summary", func() {

for _, period := range periodKeys {
userBGMSummary.Stats.Periods[period] = &types.BGMPeriod{
AverageGlucose: nil,
AverageGlucoseMmol: nil,
TotalRecords: nil,
AverageDailyRecords: nil,
TimeInTargetPercent: nil,
Expand All @@ -456,7 +456,7 @@ var _ = Describe("BGM Summary", func() {
}

userBGMSummary.Stats.OffsetPeriods[period] = &types.BGMPeriod{
AverageGlucose: &types.Glucose{Value: 1.0, Units: units},
AverageGlucoseMmol: pointer.FromAny(1.0),
TotalRecords: pointer.FromAny(1),
AverageDailyRecords: pointer.FromAny(1.0),
TimeInTargetPercent: pointer.FromAny(1.0),
Expand All @@ -475,8 +475,8 @@ var _ = Describe("BGM Summary", func() {
userBGMSummary.Stats.CalculateDelta()

for _, period := range periodKeys {
Expect(userBGMSummary.Stats.Periods[period].AverageGlucoseDelta).To(BeNil())
Expect(userBGMSummary.Stats.OffsetPeriods[period].AverageGlucoseDelta).To(BeNil())
Expect(userBGMSummary.Stats.Periods[period].AverageGlucoseMmolDelta).To(BeNil())
Expect(userBGMSummary.Stats.OffsetPeriods[period].AverageGlucoseMmolDelta).To(BeNil())

Expect(userBGMSummary.Stats.Periods[period].TotalRecordsDelta).To(BeNil())
Expect(userBGMSummary.Stats.OffsetPeriods[period].TotalRecordsDelta).To(BeNil())
Expand Down Expand Up @@ -516,7 +516,7 @@ var _ = Describe("BGM Summary", func() {

for _, period := range periodKeys {
userBGMSummary.Stats.Periods[period] = &types.BGMPeriod{
AverageGlucose: &types.Glucose{Value: 1.0, Units: units},
AverageGlucoseMmol: pointer.FromAny(1.0),
TotalRecords: pointer.FromAny(1),
AverageDailyRecords: pointer.FromAny(1.0),
TimeInTargetPercent: pointer.FromAny(1.0),
Expand All @@ -532,7 +532,7 @@ var _ = Describe("BGM Summary", func() {
}

userBGMSummary.Stats.OffsetPeriods[period] = &types.BGMPeriod{
AverageGlucose: nil,
AverageGlucoseMmol: nil,
TotalRecords: nil,
AverageDailyRecords: nil,
TimeInTargetPercent: nil,
Expand All @@ -551,8 +551,8 @@ var _ = Describe("BGM Summary", func() {
userBGMSummary.Stats.CalculateDelta()

for _, period := range periodKeys {
Expect(userBGMSummary.Stats.Periods[period].AverageGlucoseDelta).To(BeNil())
Expect(userBGMSummary.Stats.OffsetPeriods[period].AverageGlucoseDelta).To(BeNil())
Expect(userBGMSummary.Stats.Periods[period].AverageGlucoseMmolDelta).To(BeNil())
Expect(userBGMSummary.Stats.OffsetPeriods[period].AverageGlucoseMmolDelta).To(BeNil())

Expect(userBGMSummary.Stats.Periods[period].TotalRecordsDelta).To(BeNil())
Expect(userBGMSummary.Stats.OffsetPeriods[period].TotalRecordsDelta).To(BeNil())
Expand Down Expand Up @@ -656,8 +656,8 @@ var _ = Describe("BGM Summary", func() {
Expect(userBGMSummary.Stats.TotalHours).To(Equal(720))

for _, period := range periodKeys {
Expect(userBGMSummary.Stats.Periods[period].AverageGlucose.Value).To(Equal(inTargetBloodGlucose))
Expect(userBGMSummary.Stats.Periods[period].HasAverageGlucose).To(BeTrue())
Expect(*userBGMSummary.Stats.Periods[period].AverageGlucoseMmol).To(Equal(inTargetBloodGlucose))
Expect(userBGMSummary.Stats.Periods[period].HasAverageGlucoseMmol).To(BeTrue())
}
})

Expand All @@ -671,8 +671,8 @@ var _ = Describe("BGM Summary", func() {
Expect(userBGMSummary.Stats.TotalHours).To(Equal(720))

for _, period := range periodKeys {
Expect(userBGMSummary.Stats.Periods[period].AverageGlucose.Value).To(BeNumerically("~", inTargetBloodGlucose, 0.001))
Expect(userBGMSummary.Stats.Periods[period].HasAverageGlucose).To(BeTrue())
Expect(*userBGMSummary.Stats.Periods[period].AverageGlucoseMmol).To(BeNumerically("~", inTargetBloodGlucose, 0.001))
Expect(userBGMSummary.Stats.Periods[period].HasAverageGlucoseMmol).To(BeTrue())
}
})

Expand All @@ -687,8 +687,8 @@ var _ = Describe("BGM Summary", func() {
Expect(userBGMSummary.Stats.TotalHours).To(Equal(1))

for _, period := range periodKeys {
Expect(userBGMSummary.Stats.Periods[period].AverageGlucose.Value).To(BeNumerically("~", lowBloodGlucose, 0.001))
Expect(userBGMSummary.Stats.Periods[period].HasAverageGlucose).To(BeTrue())
Expect(*userBGMSummary.Stats.Periods[period].AverageGlucoseMmol).To(BeNumerically("~", lowBloodGlucose, 0.001))
Expect(userBGMSummary.Stats.Periods[period].HasAverageGlucoseMmol).To(BeTrue())
}

// start the actual test
Expand All @@ -702,8 +702,8 @@ var _ = Describe("BGM Summary", func() {
Expect(userBGMSummary.Stats.TotalHours).To(Equal(721))

for _, period := range periodKeys {
Expect(userBGMSummary.Stats.Periods[period].AverageGlucose.Value).To(BeNumerically("~", highBloodGlucose, 0.001))
Expect(userBGMSummary.Stats.Periods[period].HasAverageGlucose).To(BeTrue())
Expect(*userBGMSummary.Stats.Periods[period].AverageGlucoseMmol).To(BeNumerically("~", highBloodGlucose, 0.001))
Expect(userBGMSummary.Stats.Periods[period].HasAverageGlucoseMmol).To(BeTrue())
}
})

Expand All @@ -718,8 +718,8 @@ var _ = Describe("BGM Summary", func() {
Expect(userBGMSummary.Stats.TotalHours).To(Equal(720))

for _, period := range periodKeys {
Expect(userBGMSummary.Stats.Periods[period].AverageGlucose.Value).To(BeNumerically("~", lowBloodGlucose, 0.005))
Expect(userBGMSummary.Stats.Periods[period].HasAverageGlucose).To(BeTrue())
Expect(*userBGMSummary.Stats.Periods[period].AverageGlucoseMmol).To(BeNumerically("~", lowBloodGlucose, 0.005))
Expect(userBGMSummary.Stats.Periods[period].HasAverageGlucoseMmol).To(BeTrue())
}

// start the actual test
Expand All @@ -732,8 +732,8 @@ var _ = Describe("BGM Summary", func() {

for i, period := range periodKeys {
expectedAverage := ExpectedAverage(periodInts[i]*24, 23, highBloodGlucose, lowBloodGlucose)
Expect(userBGMSummary.Stats.Periods[period].AverageGlucose.Value).To(BeNumerically("~", expectedAverage, 0.005))
Expect(userBGMSummary.Stats.Periods[period].HasAverageGlucose).To(BeTrue())
Expect(*userBGMSummary.Stats.Periods[period].AverageGlucoseMmol).To(BeNumerically("~", expectedAverage, 0.005))
Expect(userBGMSummary.Stats.Periods[period].HasAverageGlucoseMmol).To(BeTrue())
}
})

Expand All @@ -748,8 +748,8 @@ var _ = Describe("BGM Summary", func() {
Expect(userBGMSummary.Stats.TotalHours).To(Equal(24))

for _, period := range periodKeys {
Expect(userBGMSummary.Stats.Periods[period].AverageGlucose.Value).To(BeNumerically("~", lowBloodGlucose, 0.001))
Expect(userBGMSummary.Stats.Periods[period].HasAverageGlucose).To(BeTrue())
Expect(*userBGMSummary.Stats.Periods[period].AverageGlucoseMmol).To(BeNumerically("~", lowBloodGlucose, 0.001))
Expect(userBGMSummary.Stats.Periods[period].HasAverageGlucoseMmol).To(BeTrue())
}

// start the actual test
Expand All @@ -761,8 +761,8 @@ var _ = Describe("BGM Summary", func() {
Expect(userBGMSummary.Stats.TotalHours).To(Equal(1440)) // 60 days

for _, period := range periodKeys {
Expect(userBGMSummary.Stats.Periods[period].AverageGlucose.Value).To(BeNumerically("~", highBloodGlucose, 0.001))
Expect(userBGMSummary.Stats.Periods[period].HasAverageGlucose).To(BeTrue())
Expect(*userBGMSummary.Stats.Periods[period].AverageGlucoseMmol).To(BeNumerically("~", highBloodGlucose, 0.001))
Expect(userBGMSummary.Stats.Periods[period].HasAverageGlucoseMmol).To(BeTrue())
}
})

Expand Down Expand Up @@ -834,28 +834,28 @@ var _ = Describe("BGM Summary", func() {
Expect(userBGMSummary.Stats.TotalHours).To(Equal(1440))

// check that the data matches the expectation described at the top of the test
Expect(userBGMSummary.Stats.Periods["1d"].AverageGlucose.Value).To(BeNumerically("~", lowBloodGlucose, 0.001))
Expect(*userBGMSummary.Stats.Periods["1d"].AverageGlucoseMmol).To(BeNumerically("~", lowBloodGlucose, 0.001))
Expect(*userBGMSummary.Stats.Periods["1d"].TotalRecords).To(Equal(144))

Expect(userBGMSummary.Stats.OffsetPeriods["1d"].AverageGlucose.Value).To(BeNumerically("~", highBloodGlucose, 0.001))
Expect(*userBGMSummary.Stats.OffsetPeriods["1d"].AverageGlucoseMmol).To(BeNumerically("~", highBloodGlucose, 0.001))
Expect(*userBGMSummary.Stats.OffsetPeriods["1d"].TotalRecords).To(Equal(144))

Expect(userBGMSummary.Stats.Periods["7d"].AverageGlucose.Value).To(BeNumerically("~", (highBloodGlucose+lowBloodGlucose)/2, 0.001))
Expect(*userBGMSummary.Stats.Periods["7d"].AverageGlucoseMmol).To(BeNumerically("~", (highBloodGlucose+lowBloodGlucose)/2, 0.001))
Expect(*userBGMSummary.Stats.Periods["7d"].TotalRecords).To(Equal(144 * 2))

Expect(userBGMSummary.Stats.OffsetPeriods["7d"].AverageGlucose.Value).To(BeNumerically("~", veryLowBloodGlucose, 0.001))
Expect(*userBGMSummary.Stats.OffsetPeriods["7d"].AverageGlucoseMmol).To(BeNumerically("~", veryLowBloodGlucose, 0.001))
Expect(*userBGMSummary.Stats.OffsetPeriods["7d"].TotalRecords).To(Equal(144))

Expect(userBGMSummary.Stats.Periods["14d"].AverageGlucose.Value).To(BeNumerically("~", (highBloodGlucose+lowBloodGlucose+veryLowBloodGlucose)/3, 0.001))
Expect(*userBGMSummary.Stats.Periods["14d"].AverageGlucoseMmol).To(BeNumerically("~", (highBloodGlucose+lowBloodGlucose+veryLowBloodGlucose)/3, 0.001))
Expect(*userBGMSummary.Stats.Periods["14d"].TotalRecords).To(Equal(144 * 3))

Expect(userBGMSummary.Stats.OffsetPeriods["14d"].AverageGlucose.Value).To(BeNumerically("~", veryHighBloodGlucose, 0.001))
Expect(*userBGMSummary.Stats.OffsetPeriods["14d"].AverageGlucoseMmol).To(BeNumerically("~", veryHighBloodGlucose, 0.001))
Expect(*userBGMSummary.Stats.OffsetPeriods["14d"].TotalRecords).To(Equal(144))

Expect(userBGMSummary.Stats.Periods["30d"].AverageGlucose.Value).To(BeNumerically("~", (veryHighBloodGlucose+highBloodGlucose+lowBloodGlucose+veryLowBloodGlucose)/4, 0.001))
Expect(*userBGMSummary.Stats.Periods["30d"].AverageGlucoseMmol).To(BeNumerically("~", (veryHighBloodGlucose+highBloodGlucose+lowBloodGlucose+veryLowBloodGlucose)/4, 0.001))
Expect(*userBGMSummary.Stats.Periods["30d"].TotalRecords).To(Equal(144 * 4))

Expect(userBGMSummary.Stats.OffsetPeriods["30d"].AverageGlucose.Value).To(BeNumerically("~", inTargetBloodGlucose, 0.001))
Expect(*userBGMSummary.Stats.OffsetPeriods["30d"].AverageGlucoseMmol).To(BeNumerically("~", inTargetBloodGlucose, 0.001))
Expect(*userBGMSummary.Stats.OffsetPeriods["30d"].TotalRecords).To(Equal(144))
})
})
Expand Down
Loading

0 comments on commit 3b0d0c2

Please sign in to comment.