Skip to content

perf(models): add partial index for deactivated account lookups#39937

Open
himanshu2006 wants to merge 1 commit intoRocketChat:developfrom
himanshu2006:perf/users-deactivated-index
Open

perf(models): add partial index for deactivated account lookups#39937
himanshu2006 wants to merge 1 commit intoRocketChat:developfrom
himanshu2006:perf/users-deactivated-index

Conversation

@himanshu2006
Copy link
Copy Markdown

@himanshu2006 himanshu2006 commented Mar 28, 2026

Proposed changes:
This PR implements a partial compound index on the Users collection to optimize lookups for deactivated accounts.While an existing index on { active: 1, lastLogin: 1 } exists, it utilizes a partialFilterExpression that explicitly targets only active users (active: true). This creates a performance "blind spot" for security telemetry and auditing tools—such as the one proposed in my GSoC 2026 project—that must monitor failed login attempts on deactivated accounts (active: false) .

Technical Highlights:
O(1) Efficiency: Ensures that state-checks for deactivated accounts remain a constant-time operation rather than falling back to a collection scan .
Zero-Performance-Impact: Protects the authentication "hot path" from CPU spikes during high-volume brute-force attacks targeting dormant enterprise accounts.
Enterprise Scaling: Directly supports high-performance data aggregation for the upcoming security reporting engine .
Issue(s)N/A — GSoC 2026 Preparatory Work / Proactive Performance Audit .

Relates to:
PR #39798 (Foundational Telemetry Layer) .
Steps to test or reproduce
Verify Index Creation:
After starting the server, access the MongoDB shell and run:
db.users.getIndexes()
Confirm that the new index on { active: 1, lastLogin: 1 } with partialFilterExpression: { active: false } is present.

Performance Validation:
In a workspace with a large user base (e.g., 10k+ users), perform an explain() on a query targeting a deactivated user:db.users.find({ active: false, lastLogin: { $exists: true } }).explain("executionStats")
Verify that the winning
Plan utilizes the new IXSCAN rather than a COLLSCAN.

Further comments:
This optimization follows a "Safety First" engineering rhythm by identifying and resolving a potential database bottleneck before the core GSoC coding period begins. It ensures that the security hardening features proposed do not compromise the existing performance standards of the Rocket.Chat core

Summary by CodeRabbit

  • Performance

    • Improved indexing to speed up lookups for deactivated accounts.
  • Bug Fixes

    • Refined agent routing and chat-queue calculations for more accurate agent selection and load counts.
  • Chores

    • Added release metadata entry for a patch update.

@himanshu2006 himanshu2006 requested a review from a team as a code owner March 28, 2026 21:11
@dionisio-bot
Copy link
Copy Markdown
Contributor

dionisio-bot bot commented Mar 28, 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
  • This PR has an invalid title

Please fix the issues and try again

If you have any trouble, please check the PR guidelines

@changeset-bot
Copy link
Copy Markdown

changeset-bot bot commented Mar 28, 2026

🦋 Changeset detected

Latest commit: fb3c175

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

This PR includes changesets to release 41 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/server-fetch 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

@coderabbitai
Copy link
Copy Markdown
Contributor

coderabbitai bot commented Mar 28, 2026

No actionable comments were generated in the recent review. 🎉

ℹ️ Recent review info
⚙️ Run configuration

Configuration used: Organization UI

Review profile: CHILL

Plan: Pro

Run ID: c5b5dbd6-a5e1-4f1a-ba32-0f2072a35dae

📥 Commits

Reviewing files that changed from the base of the PR and between 73f7e3b and fb3c175.

📒 Files selected for processing (1)
  • .changeset/gold-badgers-worry.md
✅ Files skipped from review due to trivial changes (1)
  • .changeset/gold-badgers-worry.md

Walkthrough

Added a changeset for a patch release of @rocket.chat/models. In Users.ts, added a partial index targeting { active: 1, lastLogin: 1 } for deactivated accounts and refactored several aggregation pipelines and some update-object formatting without changing public APIs.

Changes

Cohort / File(s) Summary
Changeset metadata
.changeset/gold-badgers-worry.md
New changeset entry declaring a patch release for @rocket.chat/models with a perf note about a partial index for deactivated account lookups.
Database indexing & aggregation refactoring
packages/models/src/models/Users.ts
Added a partial index on { active: 1, lastLogin: 1 } covering active: false. Restructured aggregation pipelines in getNextLeastBusyAgent, getLastAvailableAgentRouted, and getAgentAndAmountOngoingChats (logical equivalence maintained). Reformatted ternary-wrapped update objects in setBio and setNickname.

Estimated code review effort

🎯 3 (Moderate) | ⏱️ ~20 minutes

Suggested labels

area: authentication

🚥 Pre-merge checks | ✅ 3
✅ Passed checks (3 passed)
Check name Status Explanation
Description Check ✅ Passed Check skipped - CodeRabbit’s high-level summary is enabled.
Title check ✅ Passed The title accurately summarizes the main change: adding a partial index for deactivated account lookups, which directly matches the primary code change in Users.ts.
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.

Copy link
Copy Markdown
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 2 files

Implemented a partial compound index on { active: 1, lastLogin: 1 } for accounts where active is false. This ensures O(1) time complexity for security telemetry detection logic, preventing collection scans during brute-force attempts on deactivated accounts.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant