Skip to content

Commit b99ddcc

Browse files
committed
fix(api): expire MongoDB heartbeats after 60 days
Add a single-field TTL index on heartbeat timestamps while preserving the existing query index. Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com> Copilot-Session: f27d8f52-9620-4ee2-a564-ec4870a5f840
1 parent dc0359f commit b99ddcc

1 file changed

Lines changed: 6 additions & 0 deletions

File tree

‎api/pkg/repositories/mongodb.go‎

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -109,6 +109,12 @@ func createMongoIndexes(ctx context.Context, db *mongo.Database) error {
109109

110110
_, err := heartbeatsCol.Indexes().CreateMany(ctx, []mongo.IndexModel{
111111
{Keys: bson.D{{Key: "user_id", Value: 1}, {Key: "owner", Value: 1}, {Key: "timestamp", Value: -1}}},
112+
{
113+
Keys: bson.D{{Key: "timestamp", Value: 1}},
114+
Options: options.Index().
115+
SetName("heartbeats_retention").
116+
SetExpireAfterSeconds(int32((60 * 24 * time.Hour) / time.Second)),
117+
},
112118
})
113119
if err != nil {
114120
return stacktrace.Propagatef(err, "cannot create indexes on heartbeats collection")

0 commit comments

Comments
 (0)