Skip to content

Commit 12cee7d

Browse files
committed
lift Repeat out of the base alert config
Through discussions it was confirmed that Repeat is not universal to all alerts. So it's lifted out of the Base alert and re-inserted into those alerts where it should be present (namely Low and High alerts only). BACK-2554
1 parent 8549c33 commit 12cee7d

File tree

2 files changed

+72
-62
lines changed

2 files changed

+72
-62
lines changed

alerts/config.go

Lines changed: 17 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -72,16 +72,10 @@ func (a Alerts) Validate(validator structure.Validator) {
7272
type Base struct {
7373
// Enabled controls whether notifications should be sent for this alert.
7474
Enabled bool `json:"enabled" bson:"enabled"`
75-
// Repeat is measured in minutes.
76-
//
77-
// A value of 0 (the default) disables repeat notifications.
78-
Repeat DurationMinutes `json:"repeat,omitempty" bson:"repeat"`
7975
}
8076

8177
func (b Base) Validate(validator structure.Validator) {
8278
validator.Bool("enabled", &b.Enabled)
83-
dur := b.Repeat.Duration()
84-
validator.Duration("repeat", &dur).Using(validateRepeat)
8579
}
8680

8781
const (
@@ -110,7 +104,7 @@ type UrgentLowAlert struct {
110104
Base `bson:",inline"`
111105
// Threshold is compared the current value to determine if an alert should
112106
// be triggered.
113-
Threshold `json:"threshold"`
107+
Threshold `json:"threshold" bson:"threshold"`
114108
}
115109

116110
func (a UrgentLowAlert) Validate(validator structure.Validator) {
@@ -149,13 +143,19 @@ type LowAlert struct {
149143
// be triggered.
150144
Threshold `json:"threshold"`
151145
Delay DurationMinutes `json:"delay,omitempty"`
146+
// Repeat is measured in minutes.
147+
//
148+
// A value of 0 (the default) disables repeat notifications.
149+
Repeat DurationMinutes `json:"repeat,omitempty" bson:"repeat"`
152150
}
153151

154152
func (a LowAlert) Validate(validator structure.Validator) {
155153
a.Base.Validate(validator)
156-
dur := a.Delay.Duration()
157-
validator.Duration("delay", &dur).InRange(0, 2*time.Hour)
154+
delayDur := a.Delay.Duration()
155+
validator.Duration("delay", &delayDur).InRange(0, 2*time.Hour)
158156
a.Threshold.Validate(validator)
157+
repeatDur := a.Repeat.Duration()
158+
validator.Duration("repeat", &repeatDur).Using(validateRepeat)
159159
}
160160

161161
// HighAlert extends Base with a threshold and a delay.
@@ -165,13 +165,19 @@ type HighAlert struct {
165165
// be triggered.
166166
Threshold `json:"threshold"`
167167
Delay DurationMinutes `json:"delay,omitempty"`
168+
// Repeat is measured in minutes.
169+
//
170+
// A value of 0 (the default) disables repeat notifications.
171+
Repeat DurationMinutes `json:"repeat,omitempty" bson:"repeat"`
168172
}
169173

170174
func (a HighAlert) Validate(validator structure.Validator) {
171175
a.Base.Validate(validator)
172176
a.Threshold.Validate(validator)
173-
dur := a.Delay.Duration()
174-
validator.Duration("delay", &dur).InRange(0, 6*time.Hour)
177+
delayDur := a.Delay.Duration()
178+
validator.Duration("delay", &delayDur).InRange(0, 6*time.Hour)
179+
repeatDur := a.Repeat.Duration()
180+
validator.Duration("repeat", &repeatDur).Using(validateRepeat)
175181
}
176182

177183
// DurationMinutes reads a JSON integer and converts it to a time.Duration.

alerts/config_test.go

Lines changed: 55 additions & 51 deletions
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,6 @@ var _ = Describe("Config", func() {
4343
},
4444
"urgentLow": {
4545
"enabled": false,
46-
"repeat": 30,
4746
"threshold": {
4847
"units": "mg/dL",
4948
"value": 47.5
@@ -60,12 +59,10 @@ var _ = Describe("Config", func() {
6059
},
6160
"notLooping": {
6261
"enabled": true,
63-
"repeat": 32,
6462
"delay": 4
6563
},
6664
"noCommunication": {
6765
"enabled": true,
68-
"repeat": 33,
6966
"delay": 6
7067
}
7168
}`, mockUserID1, mockUserID2, mockUploadID)
@@ -86,14 +83,11 @@ var _ = Describe("Config", func() {
8683
Expect(conf.Low.Threshold.Value).To(Equal(80.0))
8784
Expect(conf.Low.Threshold.Units).To(Equal(glucose.MgdL))
8885
Expect(conf.UrgentLow.Enabled).To(Equal(false))
89-
Expect(conf.UrgentLow.Repeat).To(Equal(DurationMinutes(30 * time.Minute)))
9086
Expect(conf.UrgentLow.Threshold.Value).To(Equal(47.5))
9187
Expect(conf.UrgentLow.Threshold.Units).To(Equal(glucose.MgdL))
9288
Expect(conf.NotLooping.Enabled).To(Equal(true))
93-
Expect(conf.NotLooping.Repeat).To(Equal(DurationMinutes(32 * time.Minute)))
9489
Expect(conf.NotLooping.Delay).To(Equal(DurationMinutes(4 * time.Minute)))
9590
Expect(conf.NoCommunication.Enabled).To(Equal(true))
96-
Expect(conf.NoCommunication.Repeat).To(Equal(DurationMinutes(33 * time.Minute)))
9791
Expect(conf.NoCommunication.Delay).To(Equal(DurationMinutes(6 * time.Minute)))
9892
})
9993

@@ -322,32 +316,41 @@ var _ = Describe("Config", func() {
322316
})
323317

324318
Context("repeat", func() {
319+
var defaultAlert = LowAlert{
320+
Threshold: Threshold{Value: 11, Units: glucose.MmolL},
321+
}
322+
325323
It("accepts values of 0 (indicating disabled)", func() {
326324
val := validator.New()
327-
b := Base{Repeat: 0}
328-
b.Validate(val)
325+
l := defaultAlert
326+
l.Repeat = 0
327+
l.Validate(val)
329328
Expect(val.Error()).To(Succeed())
330329
})
331330

332331
It("accepts values of 15 minutes to 4 hours (inclusive)", func() {
333332
val := validator.New()
334-
b := Base{Repeat: DurationMinutes(15 * time.Minute)}
335-
b.Validate(val)
333+
l := defaultAlert
334+
l.Repeat = DurationMinutes(15 * time.Minute)
335+
l.Validate(val)
336336
Expect(val.Error()).To(Succeed())
337337

338338
val = validator.New()
339-
b = Base{Repeat: DurationMinutes(4 * time.Hour)}
340-
b.Validate(val)
339+
l = defaultAlert
340+
l.Repeat = DurationMinutes(4 * time.Hour)
341+
l.Validate(val)
341342
Expect(val.Error()).To(Succeed())
342343

343344
val = validator.New()
344-
b = Base{Repeat: DurationMinutes(4*time.Hour + 1)}
345-
b.Validate(val)
345+
l = defaultAlert
346+
l.Repeat = DurationMinutes(4*time.Hour + 1)
347+
l.Validate(val)
346348
Expect(val.Error()).NotTo(Succeed())
347349

348350
val = validator.New()
349-
b = Base{Repeat: DurationMinutes(15*time.Minute - 1)}
350-
b.Validate(val)
351+
l = defaultAlert
352+
l.Repeat = DurationMinutes(15*time.Minute - 1)
353+
l.Validate(val)
351354
Expect(val.Error()).NotTo(Succeed())
352355
})
353356
})
@@ -359,67 +362,68 @@ var _ = Describe("Config", func() {
359362
err := request.DecodeObject(nil, buf, threshold)
360363
Expect(err).To(MatchError("json is malformed"))
361364
})
362-
It("validates repeat minutes (negative)", func() {
365+
})
366+
367+
Context("low", func() {
368+
It("accepts a blank repeat", func() {
363369
buf := buff(`{
364370
"userId": "%s",
365371
"followedUserId": "%s",
366372
"uploadId": "%s",
367-
"urgentLow": {
368-
"enabled": false,
369-
"repeat": -11,
373+
"low": {
374+
"enabled": true,
375+
"delay": 10,
370376
"threshold": {
371-
"units": "%s",
372-
"value": 47.5
377+
"units": "mg/dL",
378+
"value": 80
373379
}
374380
}
375-
}`, mockUserID1, mockUserID2, mockUploadID, glucose.MgdL)
376-
cfg := &Config{}
377-
err := request.DecodeObject(nil, buf, cfg)
378-
Expect(err).To(MatchError("value -11m0s is not greater than or equal to 15m0s"))
381+
}`, mockUserID1, mockUserID2, mockUploadID)
382+
conf := &Config{}
383+
err := request.DecodeObject(nil, buf, conf)
384+
Expect(err).To(Succeed())
385+
Expect(conf.Low.Repeat).To(Equal(DurationMinutes(0)))
379386
})
380-
It("validates repeat minutes (string)", func() {
381-
buf := buff(`{
387+
})
388+
It("validates repeat minutes (negative)", func() {
389+
buf := buff(`{
382390
"userId": "%s",
383391
"followedUserId": "%s",
384-
"urgentLow": {
392+
"uploadId": "%s",
393+
"low": {
385394
"enabled": false,
386-
"repeat": "a",
395+
"repeat": -11,
387396
"threshold": {
388397
"units": "%s",
389-
"value": 1
398+
"value": 47.5
390399
}
391400
}
392-
}`, mockUserID1, mockUserID2, glucose.MgdL)
393-
cfg := &Config{}
394-
err := request.DecodeObject(nil, buf, cfg)
395-
Expect(err).To(MatchError("json is malformed"))
396-
})
401+
}`, mockUserID1, mockUserID2, mockUploadID, glucose.MgdL)
402+
cfg := &Config{}
403+
err := request.DecodeObject(nil, buf, cfg)
404+
Expect(err).To(MatchError("value -11m0s is not greater than or equal to 15m0s"))
397405
})
398-
399-
Context("low", func() {
400-
It("accepts a blank repeat", func() {
401-
buf := buff(`{
406+
It("validates repeat minutes (string)", func() {
407+
buf := buff(`{
402408
"userId": "%s",
403409
"followedUserId": "%s",
404410
"uploadId": "%s",
405411
"low": {
406-
"enabled": true,
407-
"delay": 10,
412+
"enabled": false,
413+
"repeat": "a",
408414
"threshold": {
409-
"units": "mg/dL",
410-
"value": 80
415+
"units": "%s",
416+
"value": 1
411417
}
412418
}
413-
}`, mockUserID1, mockUserID2, mockUploadID)
414-
conf := &Config{}
415-
err := request.DecodeObject(nil, buf, conf)
416-
Expect(err).To(Succeed())
417-
Expect(conf.Low.Repeat).To(Equal(DurationMinutes(0)))
418-
})
419+
}`, mockUserID1, mockUserID2, mockUploadID, glucose.MgdL)
420+
cfg := &Config{}
421+
err := request.DecodeObject(nil, buf, cfg)
422+
Expect(err).To(MatchError("json is malformed"))
419423
})
420424
})
421425

422-
var _ = Describe("Duration", func() {
426+
var _ = Describe("DurationMinutes", func() {
423427
It("parses 42", func() {
424428
d := DurationMinutes(0)
425429
err := d.UnmarshalJSON([]byte(`42`))

0 commit comments

Comments
 (0)