Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[BACK-2520] include pump sleep schedule #678

Merged
merged 9 commits into from
Nov 8, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 11 additions & 0 deletions data/types/common/common_suite_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
package common_test

import (
"testing"

"github.com/tidepool-org/platform/test"
)

func TestSuite(t *testing.T) {
test.Test(t)
}
57 changes: 57 additions & 0 deletions data/types/common/day.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
package common

const (
DaySunday = "sunday"
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks like I capitalized the days of the week in the API PR and in the implementation. Not sure why we changed that, as I remember having it lowercase initially.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@gniezen So this is correct here, right? I think we want to keep lowercase to maintain compatability with similar use in Dexcom alert schedules

Copy link
Member

@gniezen gniezen Nov 8, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@darinkrauss No, Uploader uploads sleep schedules with days of the week capitalized.

Are Dexcom alert schedules currently being uploaded from anywhere? I don't think we have a ticket for Uploader yet.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, Dexcom alert schedules are currently uploaded via the Dexcom Account Connection (and have been for years).

When you say "Uploader uploads sleep schedules with days of the week capitalized", do you mean to Jellyfish?

It'd be great if we could get this consistent (specifically, all use lowercase), but if that's going to be too much trouble...

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

When you say "Uploader uploads sleep schedules with days of the week capitalized", do you mean to Jellyfish?
Yes.

I guess this could be one more thing we add to the migration? If we're changing bolus to boluses, we could also changed Monday to monday. It's an easy change in Uploader.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, I suppose we should do this. One more thing to add to the list. @jh-bate

I wouldn't be surprised if we find other issues once you (@gniezen) actually start uploading via Platform. Considering the lack of validation in Jellyfish (and the validation in Platform), there will probably be other discrepancies that we'll have to resolve.

DayMonday = "monday"
DayTuesday = "tuesday"
DayWednesday = "wednesday"
DayThursday = "thursday"
DayFriday = "friday"
DaySaturday = "saturday"
)

func DaysOfWeek() []string {
return []string{
DaySunday,
DayMonday,
DayTuesday,
DayWednesday,
DayThursday,
DayFriday,
DaySaturday,
}
}

type DaysOfWeekByDayIndex []string

func (d DaysOfWeekByDayIndex) Len() int {
return len(d)
}
func (d DaysOfWeekByDayIndex) Swap(i int, j int) {
d[i], d[j] = d[j], d[i]
}

func (d DaysOfWeekByDayIndex) Less(i int, j int) bool {
return DayIndex(d[i]) < DayIndex(d[j])
}

func DayIndex(day string) int {
switch day {
case DaySunday:
return 1
case DayMonday:
return 2
case DayTuesday:
return 3
case DayWednesday:
return 4
case DayThursday:
return 5
case DayFriday:
return 6
case DaySaturday:
return 7
default:
return 0
}
}
76 changes: 76 additions & 0 deletions data/types/common/day_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
package common_test

import (
. "github.com/onsi/ginkgo/v2"
. "github.com/onsi/gomega"

"github.com/tidepool-org/platform/data/types/common"
)

var _ = Describe("Day", func() {

It("DaySunday is expected", func() {
Expect(common.DaySunday).To(Equal("sunday"))
})

It("DayMonday is expected", func() {
Expect(common.DayMonday).To(Equal("monday"))
})

It("DayTuesday is expected", func() {
Expect(common.DayTuesday).To(Equal("tuesday"))
})

It("DayWednesday is expected", func() {
Expect(common.DayWednesday).To(Equal("wednesday"))
})

It("DayThursday is expected", func() {
Expect(common.DayThursday).To(Equal("thursday"))
})

It("DayFriday is expected", func() {
Expect(common.DayFriday).To(Equal("friday"))
})

It("DaySaturday is expected", func() {
Expect(common.DaySaturday).To(Equal("saturday"))
})

It("DaysOfWeek returns expected", func() {
Expect(common.DaysOfWeek()).To(Equal([]string{"sunday", "monday", "tuesday", "wednesday", "thursday", "friday", "saturday"}))
Expect(common.DaysOfWeek()).To(Equal([]string{
common.DaySunday,
common.DayMonday,
common.DayTuesday,
common.DayWednesday,
common.DayThursday,
common.DayFriday,
common.DaySaturday,
}))
})

Context("DayIndex", func() {
DescribeTable("return the expected index when the day",
func(day string, expectedIndex int) {
Expect(common.DayIndex(day)).To(Equal(expectedIndex))
},
Entry("is an empty string", "", 0),
Entry("is sunday", "sunday", 1),
Entry("is constant sunday", common.DaySunday, 1),
Entry("is monday", "monday", 2),
Entry("is constant monday", common.DayMonday, 2),
Entry("is tuesday", "tuesday", 3),
Entry("is constant tuesday", common.DayTuesday, 3),
Entry("is wednesday", "wednesday", 4),
Entry("isconstant wednesday", common.DayWednesday, 4),
Entry("is thursday", "thursday", 5),
Entry("is constant thursday", common.DayThursday, 5),
Entry("is friday", "friday", 6),
Entry("is constant friday", common.DayFriday, 6),
Entry("is saturday", "saturday", 7),
Entry("is constant saturday", common.DaySaturday, 7),
Entry("is an invalid string", "invalid", 0),
)
})
})
23 changes: 2 additions & 21 deletions data/types/settings/cgm/scheduled_alert.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package cgm
import (
"strconv"

"github.com/tidepool-org/platform/data/types/common"
"github.com/tidepool-org/platform/structure"
structureValidator "github.com/tidepool-org/platform/structure/validator"
)
Expand All @@ -12,33 +13,13 @@ const (

ScheduledAlertNameLengthMaximum = 100

ScheduledAlertDaysSunday = "sunday"
ScheduledAlertDaysMonday = "monday"
ScheduledAlertDaysTuesday = "tuesday"
ScheduledAlertDaysWednesday = "wednesday"
ScheduledAlertDaysThursday = "thursday"
ScheduledAlertDaysFriday = "friday"
ScheduledAlertDaysSaturday = "saturday"

ScheduledAlertStartMaximum = 86400000
ScheduledAlertStartMinimum = 0

ScheduledAlertEndMaximum = 86400000
ScheduledAlertEndMinimum = 0
)

func ScheduledAlertDays() []string {
return []string{
ScheduledAlertDaysSunday,
ScheduledAlertDaysMonday,
ScheduledAlertDaysTuesday,
ScheduledAlertDaysWednesday,
ScheduledAlertDaysThursday,
ScheduledAlertDaysFriday,
ScheduledAlertDaysSaturday,
}
}

type ScheduledAlerts []*ScheduledAlert

func ParseScheduledAlerts(parser structure.ArrayParser) *ScheduledAlerts {
Expand Down Expand Up @@ -107,7 +88,7 @@ func (s *ScheduledAlert) Parse(parser structure.ObjectParser) {

func (s *ScheduledAlert) Validate(validator structure.Validator) {
validator.String("name", s.Name).NotEmpty().LengthLessThanOrEqualTo(ScheduledAlertNameLengthMaximum)
validator.StringArray("days", s.Days).Exists().EachOneOf(ScheduledAlertDays()...).EachUnique()
validator.StringArray("days", s.Days).Exists().EachOneOf(common.DaysOfWeek()...).EachUnique()
validator.Int("start", s.Start).Exists().InRange(ScheduledAlertStartMinimum, ScheduledAlertStartMaximum)
validator.Int("end", s.End).Exists().InRange(ScheduledAlertEndMinimum, ScheduledAlertEndMaximum)
if alertsValidator := validator.WithReference("alerts"); s.Alerts != nil {
Expand Down
40 changes: 5 additions & 35 deletions data/types/settings/cgm/scheduled_alert_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@ import (
. "github.com/onsi/gomega"

dataTypesSettingsCgm "github.com/tidepool-org/platform/data/types/settings/cgm"

dataTypesCommon "github.com/tidepool-org/platform/data/types/common"
dataTypesSettingsCgmTest "github.com/tidepool-org/platform/data/types/settings/cgm/test"
errorsTest "github.com/tidepool-org/platform/errors/test"
"github.com/tidepool-org/platform/pointer"
Expand All @@ -21,34 +23,6 @@ var _ = Describe("ScheduledAlert", func() {
Expect(dataTypesSettingsCgm.ScheduledAlertNameLengthMaximum).To(Equal(100))
})

It("ScheduledAlertDaysSunday is expected", func() {
Expect(dataTypesSettingsCgm.ScheduledAlertDaysSunday).To(Equal("sunday"))
})

It("ScheduledAlertDaysMonday is expected", func() {
Expect(dataTypesSettingsCgm.ScheduledAlertDaysMonday).To(Equal("monday"))
})

It("ScheduledAlertDaysTuesday is expected", func() {
Expect(dataTypesSettingsCgm.ScheduledAlertDaysTuesday).To(Equal("tuesday"))
})

It("ScheduledAlertDaysWednesday is expected", func() {
Expect(dataTypesSettingsCgm.ScheduledAlertDaysWednesday).To(Equal("wednesday"))
})

It("ScheduledAlertDaysThursday is expected", func() {
Expect(dataTypesSettingsCgm.ScheduledAlertDaysThursday).To(Equal("thursday"))
})

It("ScheduledAlertDaysFriday is expected", func() {
Expect(dataTypesSettingsCgm.ScheduledAlertDaysFriday).To(Equal("friday"))
})

It("ScheduledAlertDaysSaturday is expected", func() {
Expect(dataTypesSettingsCgm.ScheduledAlertDaysSaturday).To(Equal("saturday"))
})

It("ScheduledAlertStartMaximum is expected", func() {
Expect(dataTypesSettingsCgm.ScheduledAlertStartMaximum).To(Equal(86400000))
})
Expand All @@ -65,10 +39,6 @@ var _ = Describe("ScheduledAlert", func() {
Expect(dataTypesSettingsCgm.ScheduledAlertEndMinimum).To(Equal(0))
})

It("ScheduledAlertDays returns expected", func() {
Expect(dataTypesSettingsCgm.ScheduledAlertDays()).To(Equal([]string{"sunday", "monday", "tuesday", "wednesday", "thursday", "friday", "saturday"}))
})

Context("ParseScheduledAlerts", func() {
// TODO
})
Expand Down Expand Up @@ -204,20 +174,20 @@ var _ = Describe("ScheduledAlert", func() {
),
Entry("days contains invalid",
func(datum *dataTypesSettingsCgm.ScheduledAlert) {
datum.Days = pointer.FromStringArray(append([]string{"invalid"}, test.RandomStringArrayFromRangeAndArrayWithoutDuplicates(0, len(dataTypesSettingsCgm.ScheduledAlertDays())-1, dataTypesSettingsCgm.ScheduledAlertDays())...))
datum.Days = pointer.FromStringArray(append([]string{"invalid"}, test.RandomStringArrayFromRangeAndArrayWithoutDuplicates(0, len(dataTypesCommon.DaysOfWeek())-1, dataTypesCommon.DaysOfWeek())...))
},
errorsTest.WithPointerSource(structureValidator.ErrorValueStringNotOneOf("invalid", []string{"sunday", "monday", "tuesday", "wednesday", "thursday", "friday", "saturday"}), "/days/0"),
),
Entry("days contains duplicate",
func(datum *dataTypesSettingsCgm.ScheduledAlert) {
duplicate := test.RandomStringFromArray(dataTypesSettingsCgm.ScheduledAlertDays())
duplicate := test.RandomStringFromArray(dataTypesCommon.DaysOfWeek())
datum.Days = pointer.FromStringArray([]string{duplicate, duplicate})
},
errorsTest.WithPointerSource(structureValidator.ErrorValueDuplicate(), "/days/1"),
),
Entry("days valid",
func(datum *dataTypesSettingsCgm.ScheduledAlert) {
datum.Days = pointer.FromStringArray(test.RandomStringArrayFromRangeAndArrayWithoutDuplicates(1, len(dataTypesSettingsCgm.ScheduledAlertDays()), dataTypesSettingsCgm.ScheduledAlertDays()))
datum.Days = pointer.FromStringArray(test.RandomStringArrayFromRangeAndArrayWithoutDuplicates(1, len(dataTypesCommon.DaysOfWeek()), dataTypesCommon.DaysOfWeek()))
},
),
Entry("start missing",
Expand Down
3 changes: 2 additions & 1 deletion data/types/settings/cgm/test/scheduled_alert.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package test

import (
"github.com/tidepool-org/platform/data/types/common"
dataTypesSettingsCgm "github.com/tidepool-org/platform/data/types/settings/cgm"
"github.com/tidepool-org/platform/pointer"
"github.com/tidepool-org/platform/test"
Expand Down Expand Up @@ -39,7 +40,7 @@ func NewArrayFromScheduledAlerts(datum *dataTypesSettingsCgm.ScheduledAlerts, ob
func RandomScheduledAlert() *dataTypesSettingsCgm.ScheduledAlert {
datum := dataTypesSettingsCgm.NewScheduledAlert()
datum.Name = pointer.FromString(test.RandomStringFromRange(1, dataTypesSettingsCgm.ScheduledAlertNameLengthMaximum))
datum.Days = pointer.FromStringArray(test.RandomStringArrayFromRangeAndArrayWithoutDuplicates(1, len(dataTypesSettingsCgm.ScheduledAlertDays()), dataTypesSettingsCgm.ScheduledAlertDays()))
datum.Days = pointer.FromStringArray(test.RandomStringArrayFromRangeAndArrayWithoutDuplicates(1, len(common.DaysOfWeek()), common.DaysOfWeek()))
datum.Start = pointer.FromInt(test.RandomIntFromRange(dataTypesSettingsCgm.ScheduledAlertStartMinimum, dataTypesSettingsCgm.ScheduledAlertStartMaximum))
datum.End = pointer.FromInt(test.RandomIntFromRange(dataTypesSettingsCgm.ScheduledAlertEndMinimum, dataTypesSettingsCgm.ScheduledAlertEndMaximum))
datum.Alerts = RandomAlerts()
Expand Down
8 changes: 8 additions & 0 deletions data/types/settings/pump/pump.go
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,7 @@ type Pump struct {
OverridePresets *OverridePresetMap `json:"overridePresets,omitempty" bson:"overridePresets,omitempty"`
ScheduleTimeZoneOffset *int `json:"scheduleTimeZoneOffset,omitempty" bson:"scheduleTimeZoneOffset,omitempty"`
SerialNumber *string `json:"serialNumber,omitempty" bson:"serialNumber,omitempty"`
SleepSchedules *SleepScheduleMap `json:"sleepSchedules,omitempty" bson:"sleepSchedules,omitempty"`
SoftwareVersion *string `json:"softwareVersion,omitempty" bson:"softwareVersion,omitempty"`
Units *Units `json:"units,omitempty" bson:"units,omitempty"` // TODO: Move into appropriate structs
}
Expand Down Expand Up @@ -99,6 +100,7 @@ func (p *Pump) Parse(parser structure.ObjectParser) {
p.Name = parser.String("name")
p.OverridePresets = ParseOverridePresetMap(parser.WithReferenceObjectParser("overridePresets"))
p.ScheduleTimeZoneOffset = parser.Int("scheduleTimeZoneOffset")
p.SleepSchedules = ParseSleepScheduleMap(parser.WithReferenceObjectParser("sleepSchedules"))
p.SerialNumber = parser.String("serialNumber")
p.SoftwareVersion = parser.String("softwareVersion")
p.Units = ParseUnits(parser.WithReferenceObjectParser("units"))
Expand Down Expand Up @@ -185,6 +187,9 @@ func (p *Pump) Validate(validator structure.Validator) {
if p.OverridePresets != nil {
p.OverridePresets.Validate(validator.WithReference("overridePresets"), unitsBloodGlucose)
}
if p.SleepSchedules != nil {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We should add SleepSchedule to Normalize, too, even if it is a NOP, for consistency.

p.SleepSchedules.Validate(validator.WithReference("sleepSchedules"))
}
validator.Int("scheduleTimeZoneOffset", p.ScheduleTimeZoneOffset).InRange(ScheduleTimeZoneOffsetMinimum, ScheduleTimeZoneOffsetMaximum)
validator.String("serialNumber", p.SerialNumber).NotEmpty().LengthLessThanOrEqualTo(SerialNumberLengthMaximum)
validator.String("softwareVersion", p.SoftwareVersion).NotEmpty().LengthLessThanOrEqualTo(SoftwareVersionLengthMaximum)
Expand Down Expand Up @@ -258,6 +263,9 @@ func (p *Pump) Normalize(normalizer data.Normalizer) {
if p.OverridePresets != nil {
p.OverridePresets.Normalize(normalizer.WithReference("overridePresets"), unitsBloodGlucose)
}
if p.SleepSchedules != nil {
p.SleepSchedules.Normalize(normalizer.WithReference("sleepSchedules"))
}
if p.Units != nil {
p.Units.Normalize(normalizer.WithReference("units"))
}
Expand Down
31 changes: 31 additions & 0 deletions data/types/settings/pump/pump_test.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package pump_test

import (
"fmt"
"sort"

. "github.com/onsi/ginkgo/v2"
Expand Down Expand Up @@ -57,6 +58,7 @@ var _ = Describe("Pump", func() {
Expect(datum.Name).To(BeNil())
Expect(datum.OverridePresets).To(BeNil())
Expect(datum.ScheduleTimeZoneOffset).To(BeNil())
Expect(datum.SleepSchedules).To(BeNil())
Expect(datum.SerialNumber).To(BeNil())
Expect(datum.SoftwareVersion).To(BeNil())
Expect(datum.Units).To(BeNil())
Expand Down Expand Up @@ -617,6 +619,35 @@ var _ = Describe("Pump", func() {
},
errorsTest.WithPointerSourceAndMeta(structureValidator.ErrorLengthNotLessThanOrEqualTo(101, 100), "/serialNumber", pumpTest.NewMeta()),
),
Entry("sleep schedules missing",
pointer.FromString("mmol/L"),
func(datum *pump.Pump, unitsBloodGlucose *string) {
datum.SleepSchedules = nil
},
),
Entry("sleep schedules empty",
pointer.FromString("mmol/L"),
func(datum *pump.Pump, unitsBloodGlucose *string) {
datum.SleepSchedules = pump.NewSleepScheduleMap()
},
),
Entry("sleep schedules valid",
pointer.FromString("mmol/L"),
func(datum *pump.Pump, unitsBloodGlucose *string) {
datum.SleepSchedules = pumpTest.RandomSleepSchedules(3)
},
),
Entry("sleep schedules invalid",
pointer.FromString("mmol/L"),
func(datum *pump.Pump, unitsBloodGlucose *string) {
datum.SleepSchedules = pumpTest.RandomSleepSchedules(2)
(*datum.SleepSchedules)[pumpTest.SleepScheduleName(0)].End = pointer.FromInt(pump.SleepSchedulesMidnightOffsetMaximum + 1)
},
errorsTest.WithPointerSourceAndMeta(structureValidator.ErrorValueNotInRange(
pump.SleepSchedulesMidnightOffsetMaximum+1, 0,
pump.SleepSchedulesMidnightOffsetMaximum),
fmt.Sprintf("/sleepSchedules/%s/end", pumpTest.SleepScheduleName(0)), pumpTest.NewMeta()),
),
Entry("software version missing",
pointer.FromString("mmol/L"),
func(datum *pump.Pump, units *string) { datum.SoftwareVersion = nil },
Expand Down
Loading