Skip to content

Commit

Permalink
additonal medtronic index
Browse files Browse the repository at this point in the history
  • Loading branch information
Roukoswarf committed Mar 19, 2024
1 parent ca6cd1f commit e9c94f3
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 16 deletions.
34 changes: 20 additions & 14 deletions store/store.go
Original file line number Diff line number Diff line change
Expand Up @@ -259,6 +259,22 @@ func (c *MongoStoreClient) EnsureIndexes() error {
},
),
},
{
Keys: bson.D{
{Key: "_userId", Value: 1},
{Key: "deviceManufacturers", Value: 1},
{Key: "type", Value: 1},
{Key: "deletedTime", Value: 1},
},
Options: options.Index().
SetName("HasMedtronicDirectData").
SetPartialFilterExpression(
bson.D{
{Key: "_active", Value: true},
{Key: "_state", Value: "closed"},
},
),
},
}

if _, err := dataSetsCollection(c).Indexes().CreateMany(context.Background(), dataSetsIndexes); err != nil {
Expand Down Expand Up @@ -390,21 +406,11 @@ func (c *MongoStoreClient) HasMedtronicDirectData(userID string) (bool, error) {
"deviceManufacturers": "Medtronic",
}

// Try reading from both collections until migration from type=upload from
// deviceData to deviceDataSets is complete.
err := dataCollection(c).FindOne(c.context, query).Err()
if err != nil && err != mongo.ErrNoDocuments {
err := dataSetsCollection(c).FindOne(c.context, query).Err()
if err != nil && !errors.Is(err, mongo.ErrNoDocuments) {
return false, err
}
if err == nil {
return true, nil
}

err = dataSetsCollection(c).FindOne(c.context, query).Err()
if err != nil && err != mongo.ErrNoDocuments {
return false, err
}
if err == mongo.ErrNoDocuments {
if errors.Is(err, mongo.ErrNoDocuments) {
return false, nil
}

Expand Down Expand Up @@ -477,7 +483,7 @@ func (c *MongoStoreClient) HasMedtronicLoopDataAfter(userID string, date string)
}

err = dataCollection(c).FindOne(c.context, query, opts).Err()
if err != nil && !errors.Is(err, mongo.ErrNoDocuments) {
if err != nil && !errors.Is(err, mongo.ErrNoDocuments) {
return false, err
}

Expand Down
12 changes: 10 additions & 2 deletions store/store_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -335,7 +335,7 @@ func TestStore_EnsureIndexes(t *testing.T) {
Name: "_id_",
},
{
Key: makeKeySlice("_userId", "origin.payload.device.manufacturer", "fakefield"),
Key: makeKeySlice("_userId", "origin.payload.device.manufacturer", "fakefield"),
PartialFilterExpression: bson.D{
{Key: "_active", Value: true},
{Key: "origin.payload.device.manufacturer", Value: "Medtronic"},
Expand Down Expand Up @@ -409,7 +409,7 @@ func TestStore_EnsureDataSetsIndexes(t *testing.T) {
Name: "_id_",
},
{
Key: makeKeySlice("_userId", "deviceModel", "fakefield"),
Key: makeKeySlice("_userId", "deviceModel", "fakefield"),
PartialFilterExpression: bson.D{
{Key: "_active", Value: true},
{Key: "type", Value: "upload"},
Expand All @@ -422,6 +422,14 @@ func TestStore_EnsureDataSetsIndexes(t *testing.T) {
},
Name: "GetLoopableMedtronicDirectUploadIdsAfter_v2_DateTime",
},
{
Key: makeKeySlice("_userId", "deviceManufacturers", "type", "deletedTime"),
PartialFilterExpression: bson.D{
{Key: "_active", Value: true},
{Key: "_state", Value: "closed"},
},
Name: "HasMedtronicDirectData",
},
}

eq := reflect.DeepEqual(indexes, expectedIndexes)
Expand Down

0 comments on commit e9c94f3

Please sign in to comment.