Skip to content

Conversation

@ricardogarim
Copy link
Contributor

@ricardogarim ricardogarim commented Feb 9, 2026

Proposed changes (including videos or screenshots)

This PR unifies the existing statistics indexes across MongoDB collections and introduces a migration to ensure the rocketchat_statistics collection uses a consistent TTL index for automatic expiration of old documents.

This complements the PR #38568 that adds a TTL index for automatic cleanup of the rocketchat_statistics collection.

Issue(s)

Steps to test or reproduce

Further comments

Can only merge on a major version since it contains a migration.

Summary by CodeRabbit

  • Chores
    • Database maintenance: Consolidated statistics collection indexes to eliminate redundancy and improve query performance. Removed the legacy descending index configuration while retaining the optimized ascending index with TTL-based automatic data retention spanning 365 days. Database migration included to ensure compatibility, smooth upgrades, and consistent performance across all system installations.

@dionisio-bot
Copy link
Contributor

dionisio-bot bot commented Feb 9, 2026

Looks like this PR is not ready to merge, because of the following issues:

  • This PR is missing the 'stat: QA assured' label
  • This PR is missing the required milestone or project

Please fix the issues and try again

If you have any trouble, please check the PR guidelines

@coderabbitai
Copy link
Contributor

coderabbitai bot commented Feb 9, 2026

Walkthrough

This change consolidates database indexes on the Statistics collection by removing a redundant descending index on createdAt and retaining only an ascending index with a 365-day TTL. A database migration (v336) is added to drop the redundant index on existing installations.

Changes

Cohort / File(s) Summary
Changeset Documentation
.changeset/dry-gifts-report.md
New changeset documenting patch version bumps for @rocket.chat/models and @rocket.chat/meteor, tracking the migration to remove redundant statistics index.
Migration Registration
apps/meteor/server/startup/migrations/index.ts
Added import for v336 migration to register the new database migration in the startup sequence.
Database Migration
apps/meteor/server/startup/migrations/v336.ts
New migration that drops the redundant createdAt_-1 descending index from Statistics collection, with graceful error handling for fresh installations.
Model Schema Definition
packages/models/src/models/Statistics.ts
Updated modelIndexes to replace two separate indexes with a single ascending createdAt index (1) with 365-day TTL, removing the previous descending variant.

Estimated code review effort

🎯 2 (Simple) | ⏱️ ~10 minutes

Poem

🐰 A hop through the database, so neat and so clean,
We've trimmed the redundant indexes we've seen,
One TTL index to rule them all,
Pruning old stats before the servers grow tall,
A 365-day dance, then goodbye—
Old statistics fade, but the system flies high!

🚥 Pre-merge checks | ✅ 5
✅ Passed checks (5 passed)
Check name Status Explanation
Description Check ✅ Passed Check skipped - CodeRabbit’s high-level summary is enabled.
Title check ✅ Passed The title accurately describes the main change: unifying statistics indexes with TTL expiration and adding a migration to remove redundant indexes.
Linked Issues check ✅ Passed The PR implements the core requirement from CORE-1785 by introducing TTL-based automatic cleanup of statistics via a unified TTL index and migration.
Out of Scope Changes check ✅ Passed All changes are directly related to unifying statistics indexes and implementing TTL cleanup. No out-of-scope modifications detected.
Docstring Coverage ✅ Passed No functions found in the changed files to evaluate docstring coverage. Skipping docstring coverage check.

✏️ Tip: You can configure your own custom pre-merge checks in the settings.


Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

❤️ Share

Comment @coderabbitai help to get the list of available commands and usage tips.

@ricardogarim ricardogarim changed the base branch from develop to fix/statistics-ttl February 9, 2026 19:46
@changeset-bot
Copy link

changeset-bot bot commented Feb 9, 2026

🦋 Changeset detected

Latest commit: d57f8cb

The changes in this PR will be included in the next version bump.

This PR includes changesets to release 40 packages
Name Type
@rocket.chat/models Patch
@rocket.chat/meteor Patch
@rocket.chat/core-services Patch
@rocket.chat/cron Patch
@rocket.chat/instance-status Patch
@rocket.chat/omni-core Patch
@rocket.chat/ui-client Patch
@rocket.chat/account-service Patch
@rocket.chat/authorization-service Patch
@rocket.chat/ddp-streamer Patch
@rocket.chat/omnichannel-transcript Patch
@rocket.chat/presence-service Patch
@rocket.chat/queue-worker Patch
@rocket.chat/abac Patch
@rocket.chat/federation-matrix Patch
@rocket.chat/media-calls Patch
@rocket.chat/omni-core-ee Patch
@rocket.chat/omnichannel-services Patch
@rocket.chat/presence Patch
rocketchat-services Patch
@rocket.chat/network-broker Patch
@rocket.chat/core-typings Patch
@rocket.chat/rest-typings Patch
@rocket.chat/uikit-playground Patch
@rocket.chat/api-client Patch
@rocket.chat/apps Patch
@rocket.chat/ddp-client Patch
@rocket.chat/fuselage-ui-kit Patch
@rocket.chat/gazzodown Patch
@rocket.chat/http-router Patch
@rocket.chat/livechat Patch
@rocket.chat/model-typings Patch
@rocket.chat/ui-avatar Patch
@rocket.chat/ui-contexts Patch
@rocket.chat/ui-voip Patch
@rocket.chat/web-ui-registration Patch
@rocket.chat/license Patch
@rocket.chat/pdf-worker Patch
@rocket.chat/mock-providers Patch
@rocket.chat/ui-video-conf Patch

Not sure what this means? Click here to learn what changesets are.

Click here if you're a maintainer who wants to add another changeset to this PR

@ricardogarim ricardogarim marked this pull request as ready for review February 9, 2026 19:54
@ricardogarim ricardogarim requested a review from a team as a code owner February 9, 2026 19:54
Copy link
Contributor

@cubic-dev-ai cubic-dev-ai bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

No issues found across 4 files

Copy link
Contributor

@coderabbitai coderabbitai bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Actionable comments posted: 1

🤖 Fix all issues with AI agents
In `@apps/meteor/server/startup/migrations/v336.ts`:
- Around line 9-13: Replace the empty catch in the v336.ts migration around
Statistics.col.dropIndex('createdAt_-1') with error handling that only swallows
MongoDB "index not found" errors (code 27) and re-throws all other errors; i.e.,
catch the thrown error (e) and if e?.code !== 27 then throw e so
authentication/network or other failures surface, matching the pattern used in
v323.ts.
🧹 Nitpick comments (1)
packages/models/src/models/Statistics.ts (1)

12-14: TTL retention period is hardcoded to 365 days.

The linked issue (CORE-1785) mentions a configurable retention period, but this value is a compile-time constant. If making it configurable is planned for a follow-up, this is fine for now. Otherwise, consider reading from a server setting so admins can tune retention without a code change.

📜 Review details

Configuration used: Organization UI

Review profile: CHILL

Plan: Pro

📥 Commits

Reviewing files that changed from the base of the PR and between 073dfa8 and d57f8cb.

📒 Files selected for processing (4)
  • .changeset/dry-gifts-report.md
  • apps/meteor/server/startup/migrations/index.ts
  • apps/meteor/server/startup/migrations/v336.ts
  • packages/models/src/models/Statistics.ts
🧰 Additional context used
📓 Path-based instructions (1)
**/*.{ts,tsx,js}

📄 CodeRabbit inference engine (.cursor/rules/playwright.mdc)

**/*.{ts,tsx,js}: Write concise, technical TypeScript/JavaScript with accurate typing in Playwright tests
Avoid code comments in the implementation

Files:

  • apps/meteor/server/startup/migrations/v336.ts
  • apps/meteor/server/startup/migrations/index.ts
  • packages/models/src/models/Statistics.ts
🧬 Code graph analysis (1)
apps/meteor/server/startup/migrations/v336.ts (2)
apps/meteor/server/lib/migrations.ts (1)
  • addMigration (99-107)
packages/models/src/index.ts (1)
  • Statistics (197-197)
⏰ Context from checks skipped due to timeout of 90000ms. You can increase the timeout in your CodeRabbit configuration to a maximum of 15 minutes (900000ms). (3)
  • GitHub Check: 🔎 Code Check / TypeScript
  • GitHub Check: 🔨 Test Storybook / Test Storybook
  • GitHub Check: 📦 Meteor Build (coverage)
🔇 Additional comments (3)
apps/meteor/server/startup/migrations/index.ts (1)

44-44: LGTM!

Migration v336 is correctly appended to the sequence.

.changeset/dry-gifts-report.md (1)

1-6: LGTM!

Changeset accurately describes the migration and TTL index consolidation.

apps/meteor/server/startup/migrations/v336.ts (1)

5-15: No additional index drops are needed. The migration correctly removes the old descending createdAt index. The git history shows the previous modelIndexes() definition only contained a simple index on { createdAt: -1 } with no compound variants or TTL configuration, so no other conflicting indexes require removal.

✏️ Tip: You can disable this entire section by setting review_details to false in your review settings.

Comment on lines +9 to +13
try {
await Statistics.col.dropIndex('createdAt_-1');
} catch {
// Index might not exist on fresh installations
}
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

⚠️ Potential issue | 🟡 Minor

🧩 Analysis chain

🏁 Script executed:

#!/bin/bash
# Check how other migrations in this repo handle dropIndex errors
rg -n -A5 'dropIndex' --glob '**/migrations/*.ts' | head -100

Repository: RocketChat/Rocket.Chat

Length of output: 2852


🏁 Script executed:

#!/bin/bash
# Also check the specific file and its context
cat -n apps/meteor/server/startup/migrations/v336.ts

Repository: RocketChat/Rocket.Chat

Length of output: 518


🌐 Web query:

MongoDB error code 27 IndexNotFound official documentation

💡 Result:

MongoDB error code 27 = IndexNotFound in the official MongoDB Server documentation under Reference → Error Codes. (mongodb.com)

Citations:


Catch block swallows all errors, not just "index not found".

The empty catch will silently ignore any error (e.g., authentication failures, network issues), not just the expected IndexNotFound. The codebase already establishes the proper pattern in v323.ts: check for MongoDB error code 27 and re-throw unexpected errors. Match that pattern here.

Proposed fix
 		try {
 			await Statistics.col.dropIndex('createdAt_-1');
-		} catch {
-			// Index might not exist on fresh installations
+		} catch (e: any) {
+			if (e?.code !== 27 && e?.codeName !== 'IndexNotFound') {
+				throw e;
+			}
 		}
📝 Committable suggestion

‼️ IMPORTANT
Carefully review the code before committing. Ensure that it accurately replaces the highlighted code, contains no missing lines, and has no issues with indentation. Thoroughly test & benchmark the code to ensure it meets the requirements.

Suggested change
try {
await Statistics.col.dropIndex('createdAt_-1');
} catch {
// Index might not exist on fresh installations
}
try {
await Statistics.col.dropIndex('createdAt_-1');
} catch (e: any) {
if (e?.code !== 27 && e?.codeName !== 'IndexNotFound') {
throw e;
}
}
🤖 Prompt for AI Agents
In `@apps/meteor/server/startup/migrations/v336.ts` around lines 9 - 13, Replace
the empty catch in the v336.ts migration around
Statistics.col.dropIndex('createdAt_-1') with error handling that only swallows
MongoDB "index not found" errors (code 27) and re-throws all other errors; i.e.,
catch the thrown error (e) and if e?.code !== 27 then throw e so
authentication/network or other failures surface, matching the pattern used in
v323.ts.

@codecov
Copy link

codecov bot commented Feb 9, 2026

Codecov Report

❌ Patch coverage is 33.33333% with 2 lines in your changes missing coverage. Please review.
✅ Project coverage is 70.31%. Comparing base (073dfa8) to head (d57f8cb).

Additional details and impacted files

Impacted file tree graph

@@                  Coverage Diff                   @@
##           fix/statistics-ttl   #38572      +/-   ##
======================================================
- Coverage               70.44%   70.31%   -0.13%     
======================================================
  Files                    3162     3162              
  Lines                  110650   110657       +7     
  Branches                19862    19863       +1     
======================================================
- Hits                    77943    77810     -133     
- Misses                  30681    30809     +128     
- Partials                 2026     2038      +12     
Flag Coverage Δ
e2e 60.30% <ø> (-0.06%) ⬇️
e2e-api 47.79% <33.33%> (-0.97%) ⬇️
unit 71.31% <ø> (-0.15%) ⬇️

Flags with carried forward coverage won't be shown. Click here to find out more.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.
  • 📦 JS Bundle Analysis: Save yourself from yourself by tracking and limiting bundle sizes in JS merges.

@github-actions
Copy link
Contributor

github-actions bot commented Feb 9, 2026

📦 Docker Image Size Report

➡️ Changes

Service Current Baseline Change Percent
sum of all images 0B 0B 0B
account-service 0B 0B 0B
authorization-service 0B 0B 0B
ddp-streamer-service 0B 0B 0B
omnichannel-transcript-service 0B 0B 0B
presence-service 0B 0B 0B
queue-worker-service 0B 0B 0B
rocketchat 0B 0B 0B

📊 Historical Trend

---
config:
  theme: "dark"
  xyChart:
    width: 900
    height: 400
---
xychart
  title "Image Size Evolution by Service (Last 30 Days + This PR)"
  x-axis ["11/18 22:53", "11/19 23:02", "11/21 16:49", "11/24 17:34", "11/27 22:32", "11/28 19:05", "12/01 23:01", "12/02 21:57", "12/03 21:00", "12/04 18:17", "12/05 21:56", "12/08 20:15", "12/09 22:17", "12/10 23:26", "12/11 21:56", "12/12 22:45", "12/13 01:34", "12/15 22:31", "12/16 22:18", "12/17 21:04", "12/18 23:12", "12/19 23:27", "12/20 21:03", "12/22 18:54", "12/23 16:16", "12/24 19:38", "12/25 17:51", "12/26 13:18", "12/29 19:01", "12/30 20:52", "02/10 02:26 (PR)"]
  y-axis "Size (GB)" 0 --> 0.5
  line "account-service" [0.11, 0.11, 0.11, 0.11, 0.11, 0.11, 0.11, 0.11, 0.11, 0.11, 0.11, 0.11, 0.11, 0.11, 0.11, 0.11, 0.11, 0.11, 0.11, 0.11, 0.11, 0.11, 0.11, 0.11, 0.11, 0.11, 0.11, 0.11, 0.11, 0.11, 0.00]
  line "authorization-service" [0.11, 0.11, 0.11, 0.11, 0.11, 0.11, 0.11, 0.11, 0.11, 0.11, 0.11, 0.11, 0.11, 0.11, 0.11, 0.11, 0.11, 0.11, 0.11, 0.11, 0.11, 0.11, 0.11, 0.11, 0.11, 0.11, 0.11, 0.11, 0.11, 0.11, 0.00]
  line "ddp-streamer-service" [0.12, 0.12, 0.12, 0.12, 0.12, 0.12, 0.12, 0.12, 0.12, 0.12, 0.12, 0.12, 0.12, 0.12, 0.12, 0.12, 0.12, 0.12, 0.12, 0.12, 0.12, 0.12, 0.12, 0.12, 0.12, 0.12, 0.12, 0.12, 0.12, 0.12, 0.00]
  line "omnichannel-transcript-service" [0.14, 0.14, 0.13, 0.13, 0.13, 0.13, 0.13, 0.13, 0.13, 0.13, 0.13, 0.13, 0.13, 0.13, 0.13, 0.13, 0.13, 0.13, 0.13, 0.13, 0.13, 0.13, 0.13, 0.13, 0.13, 0.13, 0.13, 0.13, 0.13, 0.13, 0.00]
  line "presence-service" [0.11, 0.11, 0.11, 0.11, 0.11, 0.11, 0.11, 0.11, 0.11, 0.11, 0.11, 0.11, 0.11, 0.11, 0.11, 0.11, 0.11, 0.11, 0.11, 0.11, 0.11, 0.11, 0.11, 0.11, 0.11, 0.11, 0.11, 0.11, 0.11, 0.11, 0.00]
  line "queue-worker-service" [0.14, 0.14, 0.13, 0.13, 0.13, 0.13, 0.13, 0.13, 0.13, 0.13, 0.13, 0.13, 0.13, 0.13, 0.13, 0.13, 0.13, 0.13, 0.13, 0.13, 0.13, 0.13, 0.13, 0.13, 0.13, 0.13, 0.13, 0.13, 0.13, 0.13, 0.00]
  line "rocketchat" [0.35, 0.35, 0.34, 0.34, 0.34, 0.34, 0.34, 0.34, 0.34, 0.34, 0.34, 0.34, 0.34, 0.34, 0.34, 0.34, 0.34, 0.34, 0.34, 0.34, 0.34, 0.34, 0.34, 0.34, 0.34, 0.34, 0.34, 0.34, 0.34, 0.34, 0.00]
Loading

Statistics (last 30 days):

  • 📊 Average: 1.5GiB
  • ⬇️ Minimum: 1.4GiB
  • ⬆️ Maximum: 1.6GiB
  • 🎯 Current PR: 0B
ℹ️ About this report

This report compares Docker image sizes from this build against the develop baseline.

  • Tag: pr-38572
  • Baseline: develop
  • Timestamp: 2026-02-10 02:26:48 UTC
  • Historical data points: 30

Updated: Tue, 10 Feb 2026 02:26:49 GMT

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant