Skip to content

Commit

Permalink
rename and move alertsDataRepo -> deviceDataForAlertsRepo
Browse files Browse the repository at this point in the history
BACK-2559
  • Loading branch information
ewollesen committed Feb 19, 2025
1 parent fc6dfff commit 23349a1
Show file tree
Hide file tree
Showing 2 changed files with 64 additions and 57 deletions.
65 changes: 64 additions & 1 deletion data/store/mongo/mongo.go
Original file line number Diff line number Diff line change
@@ -1,8 +1,19 @@
package mongo

import (
"context"
"time"

"go.mongodb.org/mongo-driver/bson"
"go.mongodb.org/mongo-driver/mongo"
"go.mongodb.org/mongo-driver/mongo/options"

"github.com/tidepool-org/platform/alerts"
"github.com/tidepool-org/platform/data/store"
"github.com/tidepool-org/platform/data/types/blood/glucose"
"github.com/tidepool-org/platform/data/types/blood/glucose/continuous"
"github.com/tidepool-org/platform/data/types/dosingdecision"
"github.com/tidepool-org/platform/errors"
storeStructuredMongo "github.com/tidepool-org/platform/store/structured/mongo"
)

Expand Down Expand Up @@ -78,6 +89,58 @@ func (s *Store) NewLastCommunicationsRepository() alerts.LastCommunicationsRepos
}

func (s *Store) NewAlertsDataRepository() alerts.DataRepository {
r := alertsDataRepo(*s.Store.GetRepository("deviceData"))
r := deviceDataForAlertsRepo(*s.Store.GetRepository("deviceData"))
return &r
}

type deviceDataForAlertsRepo storeStructuredMongo.Repository

func (r *deviceDataForAlertsRepo) GetAlertableData(ctx context.Context,
params alerts.GetAlertableDataParams) (*alerts.GetAlertableDataResponse, error) {

if params.End.IsZero() {
params.End = time.Now()
}

cursor, err := r.getAlertableData(ctx, params, dosingdecision.Type)
if err != nil {
return nil, err
}
dosingDecisions := []*dosingdecision.DosingDecision{}
if err := cursor.All(ctx, &dosingDecisions); err != nil {
return nil, errors.Wrap(err, "Unable to load alertable dosing documents")
}
cursor, err = r.getAlertableData(ctx, params, continuous.Type)
if err != nil {
return nil, err
}
glucoseData := []*glucose.Glucose{}
if err := cursor.All(ctx, &glucoseData); err != nil {
return nil, errors.Wrap(err, "Unable to load alertable glucose documents")
}
response := &alerts.GetAlertableDataResponse{
DosingDecisions: dosingDecisions,
Glucose: glucoseData,
}

return response, nil
}

func (r *deviceDataForAlertsRepo) getAlertableData(ctx context.Context,
params alerts.GetAlertableDataParams, typ string) (*mongo.Cursor, error) {

selector := bson.M{
"_active": true,
"uploadId": params.UploadID,
"type": typ,
"_userId": params.UserID,
"time": bson.M{"$gte": params.Start, "$lte": params.End},
}
findOptions := options.Find().SetSort(bson.D{{Key: "time", Value: -1}})
cursor, err := r.Find(ctx, selector, findOptions)
if err != nil {
format := "Unable to find alertable %s data in dataset %s"
return nil, errors.Wrapf(err, format, typ, params.UploadID)
}
return cursor, nil
}
56 changes: 0 additions & 56 deletions data/store/mongo/mongo_alerts.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,16 +3,12 @@ package mongo
import (
"context"
"fmt"
"time"

"go.mongodb.org/mongo-driver/bson"
"go.mongodb.org/mongo-driver/mongo"
"go.mongodb.org/mongo-driver/mongo/options"

"github.com/tidepool-org/platform/alerts"
"github.com/tidepool-org/platform/data/types/blood/glucose"
"github.com/tidepool-org/platform/data/types/blood/glucose/continuous"
"github.com/tidepool-org/platform/data/types/dosingdecision"
"github.com/tidepool-org/platform/errors"
structuredmongo "github.com/tidepool-org/platform/store/structured/mongo"
)
Expand Down Expand Up @@ -106,55 +102,3 @@ func (r *alertsRepo) filter(cfg *alerts.Config) interface{} {
{Key: "followedUserId", Value: cfg.FollowedUserID},
}
}

type alertsDataRepo structuredmongo.Repository

func (d *alertsDataRepo) GetAlertableData(ctx context.Context,
params alerts.GetAlertableDataParams) (*alerts.GetAlertableDataResponse, error) {

if params.End.IsZero() {
params.End = time.Now()
}

cursor, err := d.getAlertableData(ctx, params, dosingdecision.Type)
if err != nil {
return nil, err
}
dosingDecisions := []*dosingdecision.DosingDecision{}
if err := cursor.All(ctx, &dosingDecisions); err != nil {
return nil, errors.Wrap(err, "Unable to load alertable dosing documents")
}
cursor, err = d.getAlertableData(ctx, params, continuous.Type)
if err != nil {
return nil, err
}
glucoseData := []*glucose.Glucose{}
if err := cursor.All(ctx, &glucoseData); err != nil {
return nil, errors.Wrap(err, "Unable to load alertable glucose documents")
}
response := &alerts.GetAlertableDataResponse{
DosingDecisions: dosingDecisions,
Glucose: glucoseData,
}

return response, nil
}

func (d *alertsDataRepo) getAlertableData(ctx context.Context,
params alerts.GetAlertableDataParams, typ string) (*mongo.Cursor, error) {

selector := bson.M{
"_active": true,
"uploadId": params.UploadID,
"type": typ,
"_userId": params.UserID,
"time": bson.M{"$gte": params.Start, "$lte": params.End},
}
findOptions := options.Find().SetSort(bson.D{{Key: "time", Value: -1}})
cursor, err := d.Find(ctx, selector, findOptions)
if err != nil {
format := "Unable to find alertable %s data in dataset %s"
return nil, errors.Wrapf(err, format, typ, params.UploadID)
}
return cursor, nil
}

0 comments on commit 23349a1

Please sign in to comment.