Skip to content

Commit f91304a

Browse files
committed
rename Note => Notification
In response to request during code review.
1 parent e7df512 commit f91304a

File tree

6 files changed

+129
-115
lines changed

6 files changed

+129
-115
lines changed

alerts/config.go

Lines changed: 29 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -60,17 +60,17 @@ func (c Config) Validate(validator structure.Validator) {
6060
// While this method, or the methods it calls, can fail, there's no point in returning an
6161
// error. Instead errors are logged before continuing. This is to ensure that any possible alert
6262
// that should be triggered, will be triggered.
63-
func (c Config) Evaluate(ctx context.Context, gd []*glucose.Glucose, dd []*dosingdecision.DosingDecision) *Note {
64-
n := c.Alerts.Evaluate(ctx, gd, dd)
65-
if n != nil {
66-
n.FollowedUserID = c.FollowedUserID
67-
n.RecipientUserID = c.UserID
63+
func (c Config) Evaluate(ctx context.Context, gd []*glucose.Glucose, dd []*dosingdecision.DosingDecision) *Notification {
64+
notification := c.Alerts.Evaluate(ctx, gd, dd)
65+
if notification != nil {
66+
notification.FollowedUserID = c.FollowedUserID
67+
notification.RecipientUserID = c.UserID
6868
}
6969
if lgr := log.LoggerFromContext(ctx); lgr != nil {
70-
lgr.WithField("note", n).Info("evaluated alert")
70+
lgr.WithField("notification", notification).Info("evaluated alert")
7171
}
7272

73-
return n
73+
return notification
7474
}
7575

7676
// LongestDelay of the delays set on enabled alerts.
@@ -117,7 +117,7 @@ func (a Alerts) Validate(validator structure.Validator) {
117117
// Evaluations are performed according to priority. The process is
118118
// "short-circuited" at the first indicated notification.
119119
func (a Alerts) Evaluate(ctx context.Context,
120-
gd []*glucose.Glucose, dd []*dosingdecision.DosingDecision) *Note {
120+
gd []*glucose.Glucose, dd []*dosingdecision.DosingDecision) *Notification {
121121

122122
if a.NoCommunication != nil && a.NoCommunication.Enabled {
123123
if n := a.NoCommunication.Evaluate(ctx, gd); n != nil {
@@ -160,7 +160,7 @@ func (b Base) Validate(validator structure.Validator) {
160160
validator.Bool("enabled", &b.Enabled)
161161
}
162162

163-
func (b Base) Evaluate(ctx context.Context, data []*glucose.Glucose) *Note {
163+
func (b Base) Evaluate(ctx context.Context, data []*glucose.Glucose) *Notification {
164164
if lgr := log.LoggerFromContext(ctx); lgr != nil {
165165
lgr.Warn("alerts.Base.Evaluate called, this shouldn't happen!")
166166
}
@@ -221,7 +221,7 @@ func (a UrgentLowAlert) Validate(validator structure.Validator) {
221221
// Evaluate urgent low condition.
222222
//
223223
// Assumes data is pre-sorted in descending order by Time.
224-
func (a *UrgentLowAlert) Evaluate(ctx context.Context, data []*glucose.Glucose) (note *Note) {
224+
func (a *UrgentLowAlert) Evaluate(ctx context.Context, data []*glucose.Glucose) (notification *Notification) {
225225
lgr := log.LoggerFromContext(ctx)
226226
if len(data) == 0 {
227227
lgr.Debug("no data to evaluate for urgent low")
@@ -233,7 +233,9 @@ func (a *UrgentLowAlert) Evaluate(ctx context.Context, data []*glucose.Glucose)
233233
lgr.WithError(err).Warn("Unable to evaluate urgent low")
234234
return nil
235235
}
236-
defer func() { logGlucoseAlertEvaluation(lgr, "urgent low", note, okDatum, okThreshold) }()
236+
defer func() {
237+
logGlucoseAlertEvaluation(lgr, "urgent low", notification, okDatum, okThreshold)
238+
}()
237239
active := okDatum < okThreshold
238240
if !active {
239241
if a.IsActive() {
@@ -244,7 +246,7 @@ func (a *UrgentLowAlert) Evaluate(ctx context.Context, data []*glucose.Glucose)
244246
if !a.IsActive() {
245247
a.Triggered = time.Now()
246248
}
247-
return &Note{Message: genGlucoseThresholdMessage("below urgent low")}
249+
return &Notification{Message: genGlucoseThresholdMessage("below urgent low")}
248250
}
249251

250252
func validateGlucoseAlertDatum(datum *glucose.Glucose, t Threshold) (float64, float64, error) {
@@ -271,7 +273,7 @@ func (a NotLoopingAlert) Validate(validator structure.Validator) {
271273
}
272274

273275
// Evaluate if the device is looping.
274-
func (a NotLoopingAlert) Evaluate(ctx context.Context, decisions []*dosingdecision.DosingDecision) (note *Note) {
276+
func (a NotLoopingAlert) Evaluate(ctx context.Context, decisions []*dosingdecision.DosingDecision) (notifcation *Notification) {
275277
// TODO will be implemented in the near future.
276278
return nil
277279
}
@@ -295,7 +297,7 @@ func (a NoCommunicationAlert) Validate(validator structure.Validator) {
295297
// Evaluate if CGM data is being received by Tidepool.
296298
//
297299
// Assumes data is pre-sorted by Time in descending order.
298-
func (a NoCommunicationAlert) Evaluate(ctx context.Context, data []*glucose.Glucose) *Note {
300+
func (a NoCommunicationAlert) Evaluate(ctx context.Context, data []*glucose.Glucose) *Notification {
299301
var newest time.Time
300302
for _, d := range data {
301303
if d != nil && d.Time != nil && !(*d.Time).IsZero() {
@@ -304,7 +306,7 @@ func (a NoCommunicationAlert) Evaluate(ctx context.Context, data []*glucose.Gluc
304306
}
305307
}
306308
if time.Since(newest) > a.Delay.Duration() {
307-
return &Note{Message: NoCommunicationMessage}
309+
return &Notification{Message: NoCommunicationMessage}
308310
}
309311

310312
return nil
@@ -337,7 +339,7 @@ func (a LowAlert) Validate(validator structure.Validator) {
337339
// Evaluate the given data to determine if an alert should be sent.
338340
//
339341
// Assumes data is pre-sorted in descending order by Time.
340-
func (a *LowAlert) Evaluate(ctx context.Context, data []*glucose.Glucose) (note *Note) {
342+
func (a *LowAlert) Evaluate(ctx context.Context, data []*glucose.Glucose) (notification *Notification) {
341343
lgr := log.LoggerFromContext(ctx)
342344
if len(data) == 0 {
343345
lgr.Debug("no data to evaluate for low")
@@ -346,7 +348,7 @@ func (a *LowAlert) Evaluate(ctx context.Context, data []*glucose.Glucose) (note
346348
var eventBegan time.Time
347349
var okDatum, okThreshold float64
348350
var err error
349-
defer func() { logGlucoseAlertEvaluation(lgr, "low", note, okDatum, okThreshold) }()
351+
defer func() { logGlucoseAlertEvaluation(lgr, "low", notification, okDatum, okThreshold) }()
350352
for _, datum := range data {
351353
okDatum, okThreshold, err = validateGlucoseAlertDatum(datum, a.Threshold)
352354
if err != nil {
@@ -372,7 +374,7 @@ func (a *LowAlert) Evaluate(ctx context.Context, data []*glucose.Glucose) (note
372374
a.Triggered = time.Now()
373375
}
374376
}
375-
return &Note{Message: genGlucoseThresholdMessage("below low")}
377+
return &Notification{Message: genGlucoseThresholdMessage("below low")}
376378
}
377379

378380
func genGlucoseThresholdMessage(alertType string) string {
@@ -404,7 +406,7 @@ func (a HighAlert) Validate(validator structure.Validator) {
404406
// Evaluate the given data to determine if an alert should be sent.
405407
//
406408
// Assumes data is pre-sorted in descending order by Time.
407-
func (a *HighAlert) Evaluate(ctx context.Context, data []*glucose.Glucose) (note *Note) {
409+
func (a *HighAlert) Evaluate(ctx context.Context, data []*glucose.Glucose) (notification *Notification) {
408410
lgr := log.LoggerFromContext(ctx)
409411
if len(data) == 0 {
410412
lgr.Debug("no data to evaluate for high")
@@ -413,7 +415,7 @@ func (a *HighAlert) Evaluate(ctx context.Context, data []*glucose.Glucose) (note
413415
var eventBegan time.Time
414416
var okDatum, okThreshold float64
415417
var err error
416-
defer func() { logGlucoseAlertEvaluation(lgr, "high", note, okDatum, okThreshold) }()
418+
defer func() { logGlucoseAlertEvaluation(lgr, "high", notification, okDatum, okThreshold) }()
417419
for _, datum := range data {
418420
okDatum, okThreshold, err = validateGlucoseAlertDatum(datum, a.Threshold)
419421
if err != nil {
@@ -439,13 +441,15 @@ func (a *HighAlert) Evaluate(ctx context.Context, data []*glucose.Glucose) (note
439441
a.Triggered = time.Now()
440442
}
441443
}
442-
return &Note{Message: genGlucoseThresholdMessage("above high")}
444+
return &Notification{Message: genGlucoseThresholdMessage("above high")}
443445
}
444446

445447
// logGlucoseAlertEvaluation is called during each glucose-based evaluation for record-keeping.
446-
func logGlucoseAlertEvaluation(lgr log.Logger, alertType string, note *Note, value, threshold float64) {
448+
func logGlucoseAlertEvaluation(lgr log.Logger, alertType string, notification *Notification,
449+
value, threshold float64) {
450+
447451
fields := log.Fields{
448-
"isAlerting?": note != nil,
452+
"isAlerting?": notification != nil,
449453
"threshold": threshold,
450454
"value": value,
451455
}
@@ -522,8 +526,8 @@ type Repository interface {
522526
EnsureIndexes() error
523527
}
524528

525-
// Note gathers information necessary for sending an alert notification.
526-
type Note struct {
529+
// Notification gathers information necessary for sending an alert notification.
530+
type Notification struct {
527531
// Message communicates the alert to the recipient.
528532
Message string
529533
RecipientUserID string

0 commit comments

Comments
 (0)