Optimize updateAllUserStatus Query & Filter Scheduled Statuses on Database Level#2598
Optimize updateAllUserStatus Query & Filter Scheduled Statuses on Database Level#2598Soumava-221B wants to merge 1 commit into
Conversation
Optimizes the database query in updateAllUserStatus by performing range filtering on the database level instead of in-memory application logic. Key Changes: - Added filter on futureStatus.from directly to the query. - Replaced the internal snapshot property _size with public size property. - Instantiated today once at the top of the updateAllUserStatus model.
Summary by CodeRabbit
Walkthrough
ChangesFirestore Query Optimization in updateAllUserStatus
Estimated code review effort🎯 2 (Simple) | ⏱️ ~5 minutes Possibly related issues
Poem
🚥 Pre-merge checks | ✅ 5✅ Passed checks (5 passed)
✏️ Tip: You can configure your own custom pre-merge checks in the settings. ✨ Finishing Touches🧪 Generate unit tests (beta)
Warning There were issues while running some tools. Please review the errors and either fix the tool's configuration or disable the tool if it's a critical failure. 🔧 ESLint
ESLint install failed. For unrecoverable errors, disable the tool in CodeRabbit configuration. 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. Comment |
There was a problem hiding this comment.
Actionable comments posted: 1
🤖 Prompt for all review comments with AI agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.
Inline comments:
In `@models/userStatus.js`:
- Around line 301-304: The query in userStatusModel combining an `in` filter on
`futureStatus.state` with a range filter on `futureStatus.from` requires a
composite Firestore index that is not currently defined in the repository.
Create a composite index for the `futureStatus.state` field (in ascending order)
and `futureStatus.from` field (in ascending order) either through the Firebase
Console or by adding it to the firestore.indexes.json configuration file and
deploying it. This index must be deployed to the production Firestore instance
before merging to avoid FAILED_PRECONDITION errors at runtime when the cron
executes.
🪄 Autofix (Beta)
Fix all unresolved CodeRabbit comments on this PR:
- Push a commit to this branch (recommended)
- Create a new PR with the fixes
ℹ️ Review info
⚙️ Run configuration
Configuration used: Organization UI
Review profile: ASSERTIVE
Plan: Pro
Run ID: 862df3df-1b4e-4a15-8e7f-af534058bd47
📒 Files selected for processing (1)
models/userStatus.js
| const userStatusDocs = await userStatusModel | ||
| .where("futureStatus.state", "in", ["ACTIVE", "IDLE", "OOO"]) | ||
| .where("futureStatus.from", "<=", today) | ||
| .get(); |
There was a problem hiding this comment.
🧩 Analysis chain
🏁 Script executed:
#!/bin/bash
# Verify index definitions exist for usersStatus + futureStatus.state + futureStatus.from.
# Read-only reconnaissance only.
set -euo pipefail
echo "== Candidate Firebase/Firestore config files =="
fd -i 'firebase\.json|firestore.*index.*\.json|index.*\.json'
echo
echo "== Search for usersStatus index definitions =="
rg -n -C3 '"usersStatus"|futureStatus\.state|futureStatus\.from|__name__'
echo
echo "Expected: one composite index entry for collectionGroup/collection 'usersStatus'"
echo "including fields:"
echo " - futureStatus.state (ASCENDING)"
echo " - futureStatus.from (ASCENDING)"
echo " - __name__ (ASCENDING)"Repository: RealDevSquad/website-backend
Length of output: 42423
🏁 Script executed:
cat firebase.jsonRepository: RealDevSquad/website-backend
Length of output: 252
🏁 Script executed:
fd -i 'firestore' -type f | head -20Repository: RealDevSquad/website-backend
Length of output: 243
🏁 Script executed:
fd -i 'firestore' | head -20Repository: RealDevSquad/website-backend
Length of output: 91
🏁 Script executed:
fd 'index' -type f | grep -i firestoreRepository: RealDevSquad/website-backend
Length of output: 243
🏁 Script executed:
find . -name '*index*' -o -name '*firestore*config*' | grep -v node_modulesRepository: RealDevSquad/website-backend
Length of output: 187
🏁 Script executed:
rg -l 'composite.*index|index.*field' --type jsonRepository: RealDevSquad/website-backend
Length of output: 54
🏁 Script executed:
ls -la | grep -i fire
cat firebase.json
find . -maxdepth 2 -name '*.json' | xargs grep -l 'usersStatus' 2>/dev/null | head -5Repository: RealDevSquad/website-backend
Length of output: 318
🏁 Script executed:
find . -maxdepth 3 -name '*.json' -exec grep -l 'futureStatus\|composite' {} \; 2>/dev/null
ls -la .firebaserc .fire* 2>/dev/null || echo "No .firebaserc found"Repository: RealDevSquad/website-backend
Length of output: 109
🏁 Script executed:
find . -name 'firestore.indexes.json' -o -name '*indexes.json'
git ls-files | grep -i 'index.*json\|firestore.*json'Repository: RealDevSquad/website-backend
Length of output: 54
🏁 Script executed:
cat models/userStatus.js | sed -n '299,310p'
ls -la | head -20
find . -maxdepth 2 -type d | head -20Repository: RealDevSquad/website-backend
Length of output: 1942
🏁 Script executed:
ls -la .github/workflows/
find . -maxdepth 2 -name 'README*' -o -name 'DEPLOY*' -o -name 'SETUP*'Repository: RealDevSquad/website-backend
Length of output: 313
🏁 Script executed:
cat README.md | head -100
rg -i 'composite.*index|firestore.*index' --no-headingRepository: RealDevSquad/website-backend
Length of output: 3640
🏁 Script executed:
cat CONTRIBUTING.md | grep -A 20 -B 5 -i 'index\|firestore'Repository: RealDevSquad/website-backend
Length of output: 3944
Ensure the composite Firestore index for this query is created and deployed before merging to production.
The query on lines 301-304 combines an in filter on futureStatus.state with a range filter on futureStatus.from, which requires a composite index in Firestore. This index is not defined in the repository. Create it via the Firebase Console or deploy it using firestore.indexes.json to prevent runtime failures (FAILED_PRECONDITION errors) when the cron executes against production Firestore.
🤖 Prompt for AI Agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.
In `@models/userStatus.js` around lines 301 - 304, The query in userStatusModel
combining an `in` filter on `futureStatus.state` with a range filter on
`futureStatus.from` requires a composite Firestore index that is not currently
defined in the repository. Create a composite index for the `futureStatus.state`
field (in ascending order) and `futureStatus.from` field (in ascending order)
either through the Firebase Console or by adding it to the
firestore.indexes.json configuration file and deploying it. This index must be
deployed to the production Firestore instance before merging to avoid
FAILED_PRECONDITION errors at runtime when the cron executes.
Date: 16/06/2026
Developer Name: Soumava Das
Issue Ticket Number
#2597
Description
This PR optimizes the database query inside the
updateAllUserStatuscron job model.Previously, the query loaded all documents matching the
futureStatus.statecriteria from Firestore and performed the date filtering in application memory. This caused full collection scans/large subset scans, leading to high Firestore read costs and memory consumption.With this change, date filtering is delegated directly to Firestore using a range query, fetching only the documents that actually need to be updated.
Key Changes
.where("futureStatus.from", "<=", today)filter directly to the Firestore query to fetch only the statuses that are due/past._sizeproperty of the FirestoreQuerySnapshotobject with the standard publicsizeproperty.todaytimestamp instantiation to the top ofupdateAllUserStatusto be used for both the database query and the document update timestamps.Firestore Index Requirement
Important
Because this change introduces a multi-field compound query (equality/in filter combined with an inequality/range filter), it requires a custom composite index.
Index Details:
usersStatusfutureStatus.state(Ascending)futureStatus.from(Ascending)__name__(Ascending)Development Tested?
Screenshots
Screenshot 1
Test Coverage
Screenshot 1
Additional Notes