Skip to content

Commit e2da7cd

Browse files
committed
merge alertsDataRepo into DataRepository
As Todd pointed out in code review, there's no need for this to be separate from the existing DataRepository, as it only queries the deviceData collection. BACK-2559
1 parent 5cc265c commit e2da7cd

File tree

6 files changed

+72
-77
lines changed

6 files changed

+72
-77
lines changed

data/service/service/standard.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -495,7 +495,7 @@ func (s *Standard) initializeAlertsEventsHandler() error {
495495
}
496496

497497
alertsRepo := s.dataStore.NewAlertsRepository()
498-
dataRepo := s.dataStore.NewAlertsDataRepository()
498+
dataRepo := s.dataStore.NewDataRepository()
499499
lastCommunicationsRepo := s.dataStore.NewLastCommunicationsRepository()
500500

501501
alertsEvaluator := alerts.NewEvaluator(alertsRepo, dataRepo, s.PermissionClient(),

data/store/mongo/mongo.go

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -76,8 +76,3 @@ func (s *Store) NewLastCommunicationsRepository() alerts.LastCommunicationsRepos
7676
r := lastCommunicationsRepo(*s.Store.GetRepository("lastCommunications"))
7777
return &r
7878
}
79-
80-
func (s *Store) NewAlertsDataRepository() alerts.DataRepository {
81-
r := alertsDataRepo(*s.Store.GetRepository("deviceData"))
82-
return &r
83-
}

data/store/mongo/mongo_alerts.go

Lines changed: 11 additions & 67 deletions
Original file line numberDiff line numberDiff line change
@@ -3,16 +3,12 @@ package mongo
33
import (
44
"context"
55
"fmt"
6-
"time"
76

87
"go.mongodb.org/mongo-driver/bson"
98
"go.mongodb.org/mongo-driver/mongo"
109
"go.mongodb.org/mongo-driver/mongo/options"
1110

1211
"github.com/tidepool-org/platform/alerts"
13-
"github.com/tidepool-org/platform/data/types/blood/glucose"
14-
"github.com/tidepool-org/platform/data/types/blood/glucose/continuous"
15-
"github.com/tidepool-org/platform/data/types/dosingdecision"
1612
"github.com/tidepool-org/platform/errors"
1713
structuredmongo "github.com/tidepool-org/platform/store/structured/mongo"
1814
)
@@ -24,7 +20,7 @@ type alertsRepo structuredmongo.Repository
2420
//
2521
// Once set, UploadID, UserID, and FollowedUserID cannot be changed. This is to prevent a
2622
// user from granting themselves access to another data set.
27-
func (r *alertsRepo) Upsert(ctx context.Context, conf *alerts.Config) error {
23+
func (a *alertsRepo) Upsert(ctx context.Context, conf *alerts.Config) error {
2824
opts := options.Update().SetUpsert(true)
2925
filter := bson.D{
3026
{Key: "userId", Value: conf.UserID},
@@ -35,28 +31,28 @@ func (r *alertsRepo) Upsert(ctx context.Context, conf *alerts.Config) error {
3531
"$set": bson.M{"alerts": conf.Alerts, "activity": conf.Activity},
3632
"$setOnInsert": filter,
3733
}
38-
_, err := r.UpdateOne(ctx, filter, doc, opts)
34+
_, err := a.UpdateOne(ctx, filter, doc, opts)
3935
if err != nil {
4036
return fmt.Errorf("upserting alerts.Config: %w", err)
4137
}
4238
return nil
4339
}
4440

4541
// Delete will delete the given Config.
46-
func (r *alertsRepo) Delete(ctx context.Context, cfg *alerts.Config) error {
47-
_, err := r.DeleteMany(ctx, r.filter(cfg), nil)
42+
func (a *alertsRepo) Delete(ctx context.Context, cfg *alerts.Config) error {
43+
_, err := a.DeleteMany(ctx, a.filter(cfg), nil)
4844
if err != nil {
4945
return fmt.Errorf("upserting alerts.Config: %w", err)
5046
}
5147
return nil
5248
}
5349

5450
// List will retrieve any Configs that are defined by followers of the given user.
55-
func (r *alertsRepo) List(ctx context.Context, followedUserID string) ([]*alerts.Config, error) {
51+
func (a *alertsRepo) List(ctx context.Context, followedUserID string) ([]*alerts.Config, error) {
5652
filter := bson.D{
5753
{Key: "followedUserId", Value: followedUserID},
5854
}
59-
cursor, err := r.Find(ctx, filter, nil)
55+
cursor, err := a.Find(ctx, filter, nil)
6056
if err != nil {
6157
return nil, errors.Wrapf(err, "Unable to list alerts.Config(s) for followed user %s", followedUserID)
6258
}
@@ -72,8 +68,8 @@ func (r *alertsRepo) List(ctx context.Context, followedUserID string) ([]*alerts
7268
}
7369

7470
// Get will retrieve the given Config.
75-
func (r *alertsRepo) Get(ctx context.Context, cfg *alerts.Config) (*alerts.Config, error) {
76-
res := r.FindOne(ctx, r.filter(cfg), nil)
71+
func (a *alertsRepo) Get(ctx context.Context, cfg *alerts.Config) (*alerts.Config, error) {
72+
res := a.FindOne(ctx, a.filter(cfg), nil)
7773
if res.Err() != nil {
7874
return nil, fmt.Errorf("getting alerts.Config: %w", res.Err())
7975
}
@@ -85,8 +81,8 @@ func (r *alertsRepo) Get(ctx context.Context, cfg *alerts.Config) (*alerts.Confi
8581
}
8682

8783
// EnsureIndexes to maintain index constraints.
88-
func (r *alertsRepo) EnsureIndexes() error {
89-
repo := structuredmongo.Repository(*r)
84+
func (a *alertsRepo) EnsureIndexes() error {
85+
repo := structuredmongo.Repository(*a)
9086
return (&repo).CreateAllIndexes(context.Background(), []mongo.IndexModel{
9187
{
9288
Keys: bson.D{
@@ -100,61 +96,9 @@ func (r *alertsRepo) EnsureIndexes() error {
10096
})
10197
}
10298

103-
func (r *alertsRepo) filter(cfg *alerts.Config) interface{} {
99+
func (a *alertsRepo) filter(cfg *alerts.Config) interface{} {
104100
return bson.D{
105101
{Key: "userId", Value: cfg.UserID},
106102
{Key: "followedUserId", Value: cfg.FollowedUserID},
107103
}
108104
}
109-
110-
type alertsDataRepo structuredmongo.Repository
111-
112-
func (d *alertsDataRepo) GetAlertableData(ctx context.Context,
113-
params alerts.GetAlertableDataParams) (*alerts.GetAlertableDataResponse, error) {
114-
115-
if params.End.IsZero() {
116-
params.End = time.Now()
117-
}
118-
119-
cursor, err := d.getAlertableData(ctx, params, dosingdecision.Type)
120-
if err != nil {
121-
return nil, err
122-
}
123-
dosingDecisions := []*dosingdecision.DosingDecision{}
124-
if err := cursor.All(ctx, &dosingDecisions); err != nil {
125-
return nil, errors.Wrap(err, "Unable to load alertable dosing documents")
126-
}
127-
cursor, err = d.getAlertableData(ctx, params, continuous.Type)
128-
if err != nil {
129-
return nil, err
130-
}
131-
glucoseData := []*glucose.Glucose{}
132-
if err := cursor.All(ctx, &glucoseData); err != nil {
133-
return nil, errors.Wrap(err, "Unable to load alertable glucose documents")
134-
}
135-
response := &alerts.GetAlertableDataResponse{
136-
DosingDecisions: dosingDecisions,
137-
Glucose: glucoseData,
138-
}
139-
140-
return response, nil
141-
}
142-
143-
func (d *alertsDataRepo) getAlertableData(ctx context.Context,
144-
params alerts.GetAlertableDataParams, typ string) (*mongo.Cursor, error) {
145-
146-
selector := bson.M{
147-
"_active": true,
148-
"uploadId": params.UploadID,
149-
"type": typ,
150-
"_userId": params.UserID,
151-
"time": bson.M{"$gte": params.Start, "$lte": params.End},
152-
}
153-
findOptions := options.Find().SetSort(bson.D{{Key: "time", Value: -1}})
154-
cursor, err := d.Find(ctx, selector, findOptions)
155-
if err != nil {
156-
format := "Unable to find alertable %s data in dataset %s"
157-
return nil, errors.Wrapf(err, format, typ, params.UploadID)
158-
}
159-
return cursor, nil
160-
}

data/store/mongo/mongo_data.go

Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,9 +8,14 @@ import (
88

99
"go.mongodb.org/mongo-driver/bson"
1010
"go.mongodb.org/mongo-driver/mongo"
11+
"go.mongodb.org/mongo-driver/mongo/options"
1112

13+
"github.com/tidepool-org/platform/alerts"
1214
"github.com/tidepool-org/platform/data"
1315
"github.com/tidepool-org/platform/data/store"
16+
"github.com/tidepool-org/platform/data/types/blood/glucose"
17+
"github.com/tidepool-org/platform/data/types/blood/glucose/continuous"
18+
"github.com/tidepool-org/platform/data/types/dosingdecision"
1419
"github.com/tidepool-org/platform/data/types/upload"
1520
"github.com/tidepool-org/platform/errors"
1621
"github.com/tidepool-org/platform/log"
@@ -198,3 +203,53 @@ func (d *DataRepository) DestroyDataForUserByID(ctx context.Context, userID stri
198203
func isTypeUpload(typ []string) bool {
199204
return slices.Contains(typ, strings.ToLower(upload.Type))
200205
}
206+
207+
func (d *DataRepository) GetAlertableData(ctx context.Context,
208+
params alerts.GetAlertableDataParams) (*alerts.GetAlertableDataResponse, error) {
209+
210+
if params.End.IsZero() {
211+
params.End = time.Now()
212+
}
213+
214+
cursor, err := d.getAlertableData(ctx, params, dosingdecision.Type)
215+
if err != nil {
216+
return nil, err
217+
}
218+
dosingDecisions := []*dosingdecision.DosingDecision{}
219+
if err := cursor.All(ctx, &dosingDecisions); err != nil {
220+
return nil, errors.Wrap(err, "Unable to load alertable dosing documents")
221+
}
222+
cursor, err = d.getAlertableData(ctx, params, continuous.Type)
223+
if err != nil {
224+
return nil, err
225+
}
226+
glucoseData := []*glucose.Glucose{}
227+
if err := cursor.All(ctx, &glucoseData); err != nil {
228+
return nil, errors.Wrap(err, "Unable to load alertable glucose documents")
229+
}
230+
response := &alerts.GetAlertableDataResponse{
231+
DosingDecisions: dosingDecisions,
232+
Glucose: glucoseData,
233+
}
234+
235+
return response, nil
236+
}
237+
238+
func (d *DataRepository) getAlertableData(ctx context.Context,
239+
params alerts.GetAlertableDataParams, typ string) (*mongo.Cursor, error) {
240+
241+
selector := bson.M{
242+
"_active": true,
243+
"uploadId": params.UploadID,
244+
"type": typ,
245+
"_userId": params.UserID,
246+
"time": bson.M{"$gte": params.Start, "$lte": params.End},
247+
}
248+
findOptions := options.Find().SetSort(bson.D{{Key: "time", Value: -1}})
249+
cursor, err := d.DatumRepository.Find(ctx, selector, findOptions)
250+
if err != nil {
251+
format := "Unable to find alertable %s data in dataset %s"
252+
return nil, errors.Wrapf(err, format, typ, params.UploadID)
253+
}
254+
return cursor, nil
255+
}

data/store/mongo/mongo_test.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -465,7 +465,7 @@ var _ = Describe("Mongo", Label("mongodb", "slow", "integration"), func() {
465465
repository = store.NewDataRepository()
466466
summaryRepository = store.NewSummaryRepository()
467467
alertsRepository = store.NewAlertsRepository()
468-
alertsDataRepository = store.NewAlertsDataRepository()
468+
alertsDataRepository = store.NewDataRepository()
469469
lastCommunicationsRepository = store.NewLastCommunicationsRepository()
470470
Expect(repository).ToNot(BeNil())
471471
Expect(summaryRepository).ToNot(BeNil())
@@ -2427,7 +2427,7 @@ var _ = Describe("Mongo", Label("mongodb", "slow", "integration"), func() {
24272427
Expect(repository.CreateDataSet(ctx, testSet)).To(Succeed())
24282428
testSetData := testDataSetData(testSet)
24292429
Expect(repository.CreateDataSetData(ctx, testSet, testSetData)).To(Succeed())
2430-
alertsDataRepository = store.NewAlertsDataRepository()
2430+
alertsDataRepository = store.NewDataRepository()
24312431
Expect(alertsDataRepository).ToNot(BeNil())
24322432

24332433
params := alerts.GetAlertableDataParams{

data/store/store.go

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -65,11 +65,12 @@ type DatumRepository interface {
6565
DistinctUserIDs(ctx context.Context, typ []string) ([]string, error)
6666
}
6767

68-
// DataRepository is the combined interface of DataSetRepository and
69-
// DatumRepository.
68+
// DataRepository is the combined interface of DataSetRepository,
69+
// DatumRepository, and [alerts.DataRepository].
7070
type DataRepository interface {
7171
DataSetRepository
7272
DatumRepository
73+
alerts.DataRepository
7374
}
7475

7576
type Filter struct {

0 commit comments

Comments
 (0)