Skip to content

[Backend] Replace getDashboardStats with MongoDB aggregation to prevent OOM#438

Merged
Nitya-003 merged 1 commit into
Nitya-003:mainfrom
Siddh2024:fix/dashboard-oom
Jun 18, 2026
Merged

[Backend] Replace getDashboardStats with MongoDB aggregation to prevent OOM#438
Nitya-003 merged 1 commit into
Nitya-003:mainfrom
Siddh2024:fix/dashboard-oom

Conversation

@Siddh2024

Copy link
Copy Markdown
Contributor

Description

getDashboardStats() in batchService.js used Batch.find() with no filter, limit, or projection, loading the entire Batch collection into memory. On a production dataset with millions of supply chain records, this will crash the Node.js server with an out-of-memory error. This method is called by the AI service on every relevant user query.

Fixes issue #434

Changes Made

Replaced the in-memory approach with MongoDB aggregation pipeline:

  • Uses $group, $sum, $addToSet, and $cond for efficient server-side computation
  • Adds $ifNull guard for null quantity values
  • Eliminates the calculateStats helper that required loading all documents

Testing

  • Returns the same stats shape as before (totalBatches, totalFarmers, totalQuantity, recentBatches)
  • Handles empty collection gracefully with default values

…t OOM

getDashboardStats() used Batch.find() with no filter, loading the entire
collection into memory. For large datasets this causes out-of-memory crashes.

Replaced with MongoDB aggregation pipeline using , , ,
and  for efficient server-side computation. Also added  guard
for null quantity values.
@vercel

vercel Bot commented Jun 16, 2026

Copy link
Copy Markdown

@Siddh2024 is attempting to deploy a commit to the Nitya Gosain's projects Team on Vercel.

A member of the Team first needs to authorize it.

@Nitya-003 Nitya-003 added gssoc:approved gssoc'26 approved quality:clean *1.2 for contributor +5 pts for mentor level:intermediate 35 pts for contributor 20 pts for mentor labels Jun 18, 2026
@Nitya-003 Nitya-003 merged commit 1445c25 into Nitya-003:main Jun 18, 2026
3 of 7 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

gssoc:approved gssoc'26 approved level:intermediate 35 pts for contributor 20 pts for mentor quality:clean *1.2 for contributor +5 pts for mentor

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants