RealDevSquad / RealDevSquad/website-backend
Refactor: Optimize updateAllUserStatus Cron Job Query to Prevent Full Collection Scan
Nobody has claimed this yet.
- Dominant language
- JavaScript
- Stars
- 74
- Forks
- 276
- Avg merge
- 1d 26m
- Merged PRs (30d)
- 14
Description
Description
The updateAllUserStatus function runs as a cron job to transition users' statuses. Currently, the query retrieves every user status document where a future status state exists:
const userStatusDocs = await userStatusModel
.where("futureStatus.state", "in", ["ACTIVE", "IDLE", "OOO"])
.get();
This fetches all future status documents, regardless of whether their transition start date has arrived. We should add a timestamp-based filter to retrieve only the documents that actually need to transition:
const userStatusDocs = await userStatusModel
.where("futureStatus.state", "in", ["ACTIVE", "IDLE", "OOO"])
.where("futureStatus.from", "<=", Date.now())
.get();
(Note: A composite index on futureStatus.state and futureStatus.from will be required).
Why do we need to fix this?
- Cost Efficiency: Currently, we pay for Firestore reads on every single future status document, even if they aren't scheduled to change for weeks. Adding this filter reduces reads to only the documents transitioning today.
- Performance: Prevents iterating over and processing thousands of irrelevant documents in memory, reducing the memory footprint of the cron service.
- Scalability: Prevents the cron job from slowing down as the community user base and scheduled statuses grow over time.
Contributor guide
First steps
- Read the whole issue, then the project's contributing guide.
- Comment on the issue to say you are picking it up — it saves two people doing the same work.
- Fork the repository and make your change on a branch.
- Open a pull request that references the issue number.
Research direction
Locate the updateAllUserStatus function and inspect the current userStatusModel query. Add the timestamp condition described in the issue, ensure the required composite index for futureStatus.state and futureStatus.from is configured, and verify that only due status documents are processed.
Written by the indexing model from the issue text.
Assessment
- Tech stack
- javascript
- Domain
- backend, database
- Issue type
- Refactor
- Difficulty
- 2/5
- Estimated time
- 1-3 hours
- Activity status
- Quiet
- Clarity
- Mostly clear
- Newbie friendliness
- 68/100