Skip to content

Commit b9767dc

Browse files
committed
add data set id to alerts Evaluation, improve test coverage
BACK-2499
1 parent 3bfa44a commit b9767dc

File tree

3 files changed

+160
-65
lines changed

3 files changed

+160
-65
lines changed

alerts/config.go

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -57,10 +57,12 @@ func (c Config) Validate(validator structure.Validator) {
5757

5858
// Evaluate alerts in the context of the provided data.
5959
//
60-
// While this method, or the methods it calls, can fail, there's no point in returning an
61-
// error. Instead errors are logged before continuing. This is to ensure that any possible alert
62-
// that should be triggered, will be triggered.
63-
func (c Config) Evaluate(ctx context.Context, gd []*glucose.Glucose, dd []*dosingdecision.DosingDecision) *Notification {
60+
// While this method or the methods it calls can fail, there's no point in returning an
61+
// error. Instead errors are logged before continuing. This is to ensure that any possible
62+
// alert that should be triggered, will be triggered.
63+
func (c Config) Evaluate(ctx context.Context,
64+
gd []*glucose.Glucose, dd []*dosingdecision.DosingDecision) *Notification {
65+
6466
notification := c.Alerts.Evaluate(ctx, gd, dd)
6567
if notification != nil {
6668
notification.FollowedUserID = c.FollowedUserID

data/events/alerts.go

Lines changed: 16 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -76,7 +76,7 @@ func (c *Consumer) consumeAlertsConfigs(ctx context.Context,
7676
ctxLog := c.logger(ctx).WithField("followedUserID", cfg.FollowedUserID)
7777
ctx = log.NewContextWithLogger(ctx, ctxLog)
7878

79-
notes, err := c.Evaluator.Evaluate(ctx, cfg.FollowedUserID)
79+
notes, err := c.Evaluator.Evaluate(ctx, cfg.FollowedUserID, cfg.UploadID)
8080
if err != nil {
8181
format := "Unable to evalaute alerts configs triggered event for user %s"
8282
return errors.Wrapf(err, format, cfg.UserID)
@@ -103,8 +103,11 @@ func (c *Consumer) consumeDeviceData(ctx context.Context,
103103
if datum.UserID == nil {
104104
return errors.New("Unable to retrieve alerts configs: userID is nil")
105105
}
106+
if datum.UploadID == nil {
107+
return errors.New("Unable to retrieve alerts configs: uploadID is nil")
108+
}
106109
ctx = log.NewContextWithLogger(ctx, lgr.WithField("followedUserID", *datum.UserID))
107-
notes, err := c.Evaluator.Evaluate(ctx, *datum.UserID)
110+
notes, err := c.Evaluator.Evaluate(ctx, *datum.UserID, *datum.UploadID)
108111
if err != nil {
109112
format := "Unable to evalaute device data triggered event for user %s"
110113
return errors.Wrapf(err, format, *datum.UserID)
@@ -162,7 +165,7 @@ func (c *Consumer) logger(ctx context.Context) log.Logger {
162165
}
163166

164167
type AlertsEvaluator interface {
165-
Evaluate(ctx context.Context, followedUserID string) ([]*alerts.Notification, error)
168+
Evaluate(ctx context.Context, followedUserID, dataSetID string) ([]*alerts.Notification, error)
166169
}
167170

168171
func NewAlertsEvaluator(alerts AlertsClient, data store.DataRepository,
@@ -198,10 +201,10 @@ func (e *evaluator) logger(ctx context.Context) log.Logger {
198201
}
199202

200203
// Evaluate followers' alerts.Configs to generate alert notifications.
201-
func (e *evaluator) Evaluate(ctx context.Context, followedUserID string) (
204+
func (e *evaluator) Evaluate(ctx context.Context, followedUserID, dataSetID string) (
202205
[]*alerts.Notification, error) {
203206

204-
alertsConfigs, err := e.gatherAlertsConfigs(ctx, followedUserID)
207+
alertsConfigs, err := e.gatherAlertsConfigs(ctx, followedUserID, dataSetID)
205208
if err != nil {
206209
return nil, err
207210
}
@@ -231,14 +234,21 @@ func (e *evaluator) mapAlertsConfigsByUploadID(cfgs []*alerts.Config) map[string
231234
return mapped
232235
}
233236

237+
// gatherAlertsConfigs for the given followed user and data set.
238+
//
239+
// Those configs which don't match the data set or whose owners don't have permission are
240+
// removed.
234241
func (e *evaluator) gatherAlertsConfigs(ctx context.Context,
235-
followedUserID string) ([]*alerts.Config, error) {
242+
followedUserID, dataSetID string) ([]*alerts.Config, error) {
236243

237244
alertsConfigs, err := e.Alerts.List(ctx, followedUserID)
238245
if err != nil {
239246
return nil, err
240247
}
241248
alertsConfigs = slices.DeleteFunc(alertsConfigs, e.authDenied(ctx))
249+
alertsConfigs = slices.DeleteFunc(alertsConfigs, func(c *alerts.Config) bool {
250+
return c.UploadID != dataSetID
251+
})
242252
return alertsConfigs, nil
243253
}
244254

@@ -297,10 +307,6 @@ func (e *evaluator) gatherData(ctx context.Context, followedUserID, uploadID str
297307
func (e *evaluator) generateNotes(ctx context.Context,
298308
alertsConfigs []*alerts.Config, resp *store.AlertableResponse) []*alerts.Notification {
299309

300-
if len(alertsConfigs) == 0 {
301-
return nil
302-
}
303-
304310
lgr := e.logger(ctx)
305311
notifications := []*alerts.Notification{}
306312
for _, alertsConfig := range alertsConfigs {
@@ -313,7 +319,6 @@ func (e *evaluator) generateNotes(ctx context.Context,
313319
note := alertsConfig.Evaluate(c, resp.Glucose, resp.DosingDecisions)
314320
if note != nil {
315321
notifications = append(notifications, note)
316-
continue
317322
}
318323
}
319324

0 commit comments

Comments
 (0)