Skip to content

Commit e926564

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 747d67b commit e926564

File tree

2 files changed

+70
-61
lines changed

2 files changed

+70
-61
lines changed

alerts/config.go

Lines changed: 17 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -67,16 +67,10 @@ func (a Alerts) Validate(validator structure.Validator) {
6767
type Base struct {
6868
// Enabled controls whether notifications should be sent for this alert.
6969
Enabled bool `json:"enabled" bson:"enabled"`
70-
// Repeat is measured in minutes.
71-
//
72-
// A value of 0 (the default) disables repeat notifications.
73-
Repeat DurationMinutes `json:"repeat,omitempty" bson:"repeat"`
7470
}
7571

7672
func (b Base) Validate(validator structure.Validator) {
7773
validator.Bool("enabled", &b.Enabled)
78-
dur := b.Repeat.Duration()
79-
validator.Duration("repeat", &dur).Using(validateRepeat)
8074
}
8175

8276
const (
@@ -105,7 +99,7 @@ type UrgentLowAlert struct {
10599
Base `bson:",inline"`
106100
// Threshold is compared the current value to determine if an alert should
107101
// be triggered.
108-
Threshold `json:"threshold"`
102+
Threshold `json:"threshold" bson:"threshold"`
109103
}
110104

111105
func (a UrgentLowAlert) Validate(validator structure.Validator) {
@@ -144,13 +138,19 @@ type LowAlert struct {
144138
// be triggered.
145139
Threshold `json:"threshold"`
146140
Delay DurationMinutes `json:"delay,omitempty"`
141+
// Repeat is measured in minutes.
142+
//
143+
// A value of 0 (the default) disables repeat notifications.
144+
Repeat DurationMinutes `json:"repeat,omitempty" bson:"repeat"`
147145
}
148146

149147
func (a LowAlert) Validate(validator structure.Validator) {
150148
a.Base.Validate(validator)
151-
dur := a.Delay.Duration()
152-
validator.Duration("delay", &dur).InRange(0, 2*time.Hour)
149+
delayDur := a.Delay.Duration()
150+
validator.Duration("delay", &delayDur).InRange(0, 2*time.Hour)
153151
a.Threshold.Validate(validator)
152+
repeatDur := a.Repeat.Duration()
153+
validator.Duration("repeat", &repeatDur).Using(validateRepeat)
154154
}
155155

156156
// HighAlert extends Base with a threshold and a delay.
@@ -160,13 +160,19 @@ type HighAlert struct {
160160
// be triggered.
161161
Threshold `json:"threshold"`
162162
Delay DurationMinutes `json:"delay,omitempty"`
163+
// Repeat is measured in minutes.
164+
//
165+
// A value of 0 (the default) disables repeat notifications.
166+
Repeat DurationMinutes `json:"repeat,omitempty" bson:"repeat"`
163167
}
164168

165169
func (a HighAlert) Validate(validator structure.Validator) {
166170
a.Base.Validate(validator)
167171
a.Threshold.Validate(validator)
168-
dur := a.Delay.Duration()
169-
validator.Duration("delay", &dur).InRange(0, 6*time.Hour)
172+
delayDur := a.Delay.Duration()
173+
validator.Duration("delay", &delayDur).InRange(0, 6*time.Hour)
174+
repeatDur := a.Repeat.Duration()
175+
validator.Duration("repeat", &repeatDur).Using(validateRepeat)
170176
}
171177

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

alerts/config_test.go

Lines changed: 53 additions & 50 deletions
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,6 @@ var _ = Describe("Config", func() {
4141
},
4242
"urgentLow": {
4343
"enabled": false,
44-
"repeat": 30,
4544
"threshold": {
4645
"units": "mg/dL",
4746
"value": 47.5
@@ -58,12 +57,10 @@ var _ = Describe("Config", func() {
5857
},
5958
"notLooping": {
6059
"enabled": true,
61-
"repeat": 32,
6260
"delay": 4
6361
},
6462
"noCommunication": {
6563
"enabled": true,
66-
"repeat": 33,
6764
"delay": 6
6865
}
6966
}`, mockUserID1, mockUserID2)
@@ -83,14 +80,11 @@ var _ = Describe("Config", func() {
8380
Expect(conf.Low.Threshold.Value).To(Equal(80.0))
8481
Expect(conf.Low.Threshold.Units).To(Equal(glucose.MgdL))
8582
Expect(conf.UrgentLow.Enabled).To(Equal(false))
86-
Expect(conf.UrgentLow.Repeat).To(Equal(DurationMinutes(30 * time.Minute)))
8783
Expect(conf.UrgentLow.Threshold.Value).To(Equal(47.5))
8884
Expect(conf.UrgentLow.Threshold.Units).To(Equal(glucose.MgdL))
8985
Expect(conf.NotLooping.Enabled).To(Equal(true))
90-
Expect(conf.NotLooping.Repeat).To(Equal(DurationMinutes(32 * time.Minute)))
9186
Expect(conf.NotLooping.Delay).To(Equal(DurationMinutes(4 * time.Minute)))
9287
Expect(conf.NoCommunication.Enabled).To(Equal(true))
93-
Expect(conf.NoCommunication.Repeat).To(Equal(DurationMinutes(33 * time.Minute)))
9488
Expect(conf.NoCommunication.Delay).To(Equal(DurationMinutes(6 * time.Minute)))
9589
})
9690

@@ -285,32 +279,41 @@ var _ = Describe("Config", func() {
285279
})
286280

287281
Context("repeat", func() {
282+
var defaultAlert = LowAlert{
283+
Threshold: Threshold{Value: 11, Units: glucose.MmolL},
284+
}
285+
288286
It("accepts values of 0 (indicating disabled)", func() {
289287
val := validator.New()
290-
b := Base{Repeat: 0}
291-
b.Validate(val)
288+
l := defaultAlert
289+
l.Repeat = 0
290+
l.Validate(val)
292291
Expect(val.Error()).To(Succeed())
293292
})
294293

295294
It("accepts values of 15 minutes to 4 hours (inclusive)", func() {
296295
val := validator.New()
297-
b := Base{Repeat: DurationMinutes(15 * time.Minute)}
298-
b.Validate(val)
296+
l := defaultAlert
297+
l.Repeat = DurationMinutes(15 * time.Minute)
298+
l.Validate(val)
299299
Expect(val.Error()).To(Succeed())
300300

301301
val = validator.New()
302-
b = Base{Repeat: DurationMinutes(4 * time.Hour)}
303-
b.Validate(val)
302+
l = defaultAlert
303+
l.Repeat = DurationMinutes(4 * time.Hour)
304+
l.Validate(val)
304305
Expect(val.Error()).To(Succeed())
305306

306307
val = validator.New()
307-
b = Base{Repeat: DurationMinutes(4*time.Hour + 1)}
308-
b.Validate(val)
308+
l = defaultAlert
309+
l.Repeat = DurationMinutes(4*time.Hour + 1)
310+
l.Validate(val)
309311
Expect(val.Error()).NotTo(Succeed())
310312

311313
val = validator.New()
312-
b = Base{Repeat: DurationMinutes(15*time.Minute - 1)}
313-
b.Validate(val)
314+
l = defaultAlert
315+
l.Repeat = DurationMinutes(15*time.Minute - 1)
316+
l.Validate(val)
314317
Expect(val.Error()).NotTo(Succeed())
315318
})
316319
})
@@ -322,65 +325,65 @@ var _ = Describe("Config", func() {
322325
err := request.DecodeObject(nil, buf, threshold)
323326
Expect(err).To(MatchError("json is malformed"))
324327
})
325-
It("validates repeat minutes (negative)", func() {
328+
})
329+
330+
Context("low", func() {
331+
It("accepts a blank repeat", func() {
326332
buf := buff(`{
327333
"userId": "%s",
328334
"followedUserId": "%s",
329-
"urgentLow": {
330-
"enabled": false,
331-
"repeat": -11,
335+
"low": {
336+
"enabled": true,
337+
"delay": 10,
332338
"threshold": {
333-
"units": "%s",
334-
"value": 47.5
339+
"units": "mg/dL",
340+
"value": 80
335341
}
336342
}
337-
}`, mockUserID1, mockUserID2, glucose.MgdL)
338-
cfg := &Config{}
339-
err := request.DecodeObject(nil, buf, cfg)
340-
Expect(err).To(MatchError("value -11m0s is not greater than or equal to 15m0s"))
343+
}`, mockUserID1, mockUserID2)
344+
conf := &Config{}
345+
err := request.DecodeObject(nil, buf, conf)
346+
Expect(err).To(Succeed())
347+
Expect(conf.Low.Repeat).To(Equal(DurationMinutes(0)))
341348
})
342-
It("validates repeat minutes (string)", func() {
343-
buf := buff(`{
349+
})
350+
It("validates repeat minutes (negative)", func() {
351+
buf := buff(`{
344352
"userId": "%s",
345353
"followedUserId": "%s",
346-
"urgentLow": {
354+
"low": {
347355
"enabled": false,
348-
"repeat": "a",
356+
"repeat": -11,
349357
"threshold": {
350358
"units": "%s",
351-
"value": 1
359+
"value": 47.5
352360
}
353361
}
354362
}`, mockUserID1, mockUserID2, glucose.MgdL)
355-
cfg := &Config{}
356-
err := request.DecodeObject(nil, buf, cfg)
357-
Expect(err).To(MatchError("json is malformed"))
358-
})
363+
cfg := &Config{}
364+
err := request.DecodeObject(nil, buf, cfg)
365+
Expect(err).To(MatchError("value -11m0s is not greater than or equal to 15m0s"))
359366
})
360-
361-
Context("low", func() {
362-
It("accepts a blank repeat", func() {
363-
buf := buff(`{
367+
It("validates repeat minutes (string)", func() {
368+
buf := buff(`{
364369
"userId": "%s",
365370
"followedUserId": "%s",
366371
"low": {
367-
"enabled": true,
368-
"delay": 10,
372+
"enabled": false,
373+
"repeat": "a",
369374
"threshold": {
370-
"units": "mg/dL",
371-
"value": 80
375+
"units": "%s",
376+
"value": 1
372377
}
373378
}
374-
}`, mockUserID1, mockUserID2)
375-
conf := &Config{}
376-
err := request.DecodeObject(nil, buf, conf)
377-
Expect(err).To(Succeed())
378-
Expect(conf.Low.Repeat).To(Equal(DurationMinutes(0)))
379-
})
379+
}`, mockUserID1, mockUserID2, glucose.MgdL)
380+
cfg := &Config{}
381+
err := request.DecodeObject(nil, buf, cfg)
382+
Expect(err).To(MatchError("json is malformed"))
380383
})
381384
})
382385

383-
var _ = Describe("Duration", func() {
386+
var _ = Describe("DurationMinutes", func() {
384387
It("parses 42", func() {
385388
d := DurationMinutes(0)
386389
err := d.UnmarshalJSON([]byte(`42`))

0 commit comments

Comments
 (0)