Skip to content

fix(analytics): Optimize pricing query handling - #1254

Merged
vprashrex merged 2 commits into
mainfrom
fix/analytics-n-plus-one-pricing
Oct 14, 2026
Merged

vprashrex merged 2 commits into
mainfrom
fix/analytics-n-plus-one-pricing

Conversation

@Ayush8923

@Ayush8923 Ayush8923 commented Oct 14, 2026 •

Copy link
Copy Markdown
Collaborator

Issue

Closes #1255

Summary

Bug: analytics API (GET /analytics/monthly/chart and /analytics/monthly) ran a separate database query to fetch model pricing for every row in the result. One request was making ~98 extra queries (N+1), which Sentry flagged.

Fix: all the needed model pricing is now fetched in a single query, and the cost for each row is calculated in memory. The number of queries no longer grows with the number of rows.

Checklist

Before submitting a pull request, please ensure that you mark these task.

  • Ran fastapi run --reload app/main.py or docker compose up in the repository root and test.
  • If you've fixed a bug or added code that is tested and has test cases.

Note:

  • Sentry Error: here

@coderabbitai

coderabbitai Bot commented Oct 14, 2026 •

Copy link
Copy Markdown
Contributor

Warning

Review limit reached

You've used all free OSS reviews for now. Wait for the free limit to reset to keep reviewing this public repository.

Next included review available in 15 minutes.

Check out review usage here.

View limit details

Limit details: You’ve used the included review currently available.

Learn how review limits work.

Review configuration:

⚙️ Run configuration
  • Configuration used: Repository: ProjectTech4DevAI/kaapi-backend/.coderabbit.yaml
  • Review profile: CHILL
  • Plan: Advanced
  • Run ID: 8ce7542f-2ab9-42ea-8d3b-d7ac4e0b0a6f
📥 Commits

Reviewing files that changed from the base of the PR and between eb0d2b5 and 29e28ed.

📒 Files selected for processing: 5
  • backend/app/api/docs/analytics/monthly.md
  • backend/app/crud/model_config.py
  • backend/app/services/analytics/aggregation.py
  • backend/app/tests/api/routes/test_analytics.py
  • docs/wiki/modules/platform.md
  • Autofix · Keep fixing CodeRabbit findings and required CI, and resolving merge conflicts

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.

@github-actions github-actions Bot changed the title fix(analytics): Batch model pricing lookup to remove N+1 query fix(analytics): Optimize pricing query handling Oct 14, 2026
@github-actions

github-actions Bot commented Oct 14, 2026 •

Copy link
Copy Markdown

OpenAPI changes   ⚪ No API surface changes

Note

This PR does not modify the API contract.

main ↔ 2b1a6643 · generated by oasdiff

@Ayush8923 Ayush8923 self-assigned this Oct 14, 2026
@vprashrex
vprashrex merged commit a7a69b2 into main Oct 14, 2026
5 of 6 checks passed
@vprashrex
vprashrex deleted the fix/analytics-n-plus-one-pricing branch October 14, 2026 12:31
@codecov

codecov Bot commented Oct 14, 2026

Copy link
Copy Markdown

Codecov Report

❌ Patch coverage is 97.72727% with 1 line in your changes missing coverage. Please review.

Files with missing lines Patch % Lines
backend/app/services/analytics/aggregation.py 87.50% 1 Missing ⚠️

📢 Thoughts on this report? Let us know!

@github-actions

Copy link
Copy Markdown

🎉 This PR is included in version 1.8.0-main.5 🎉

The release is available on GitHub release

Your semantic-release bot 📦🚀

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.

Analytics: Optimize N+1 query issue

2 participants