-
Notifications
You must be signed in to change notification settings - Fork 36
⚡ Bolt: Optimize get_system_stats query #502
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,5 +1,6 @@ | ||
| from fastapi import APIRouter, Depends, HTTPException, status | ||
| from sqlalchemy.orm import Session | ||
| from sqlalchemy import func, case | ||
| from typing import List | ||
|
|
||
| from backend.database import get_db | ||
|
|
@@ -20,11 +21,18 @@ def get_users(skip: int = 0, limit: int = 100, db: Session = Depends(get_db)): | |
|
|
||
| @router.get("/stats") | ||
| def get_system_stats(db: Session = Depends(get_db)): | ||
| total_users = db.query(User).count() | ||
| admin_users = db.query(User).filter(User.role == UserRole.ADMIN).count() | ||
| """ | ||
| Get system statistics. | ||
| Optimized: Uses a single query with aggregates instead of 3 separate count queries. | ||
| """ | ||
| stats = db.query( | ||
| func.count(User.id).label('total_users'), | ||
| func.sum(case((User.role == UserRole.ADMIN, 1), else_=0)).label('admin_count'), | ||
| func.sum(case((User.is_active == True, 1), else_=0)).label('active_users') | ||
| ).first() | ||
|
Comment on lines
+28
to
+32
|
||
|
|
||
| return { | ||
| "total_users": total_users, | ||
| "admin_count": admin_users, | ||
| "active_users": db.query(User).filter(User.is_active == True).count(), | ||
| "total_users": stats.total_users or 0, | ||
| "admin_count": stats.admin_count or 0, | ||
| "active_users": stats.active_users or 0, | ||
| } | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,3 @@ | ||
| [pytest] | ||
| asyncio_mode = auto | ||
| asyncio_default_fixture_loop_scope = function | ||
|
Comment on lines
+1
to
+3
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 🧩 Analysis chain🏁 Script executed: # Search for pytest-asyncio version requirements in the repository
find . -type f \( -name "*.txt" -o -name "*.toml" -o -name "*.cfg" -o -name "*.py" -o -name "*.lock" -o -name "*.yaml" -o -name "*.yml" \) | head -20Repository: RohanExploit/VishwaGuru Length of output: 917 🏁 Script executed: # Look for dependency files
git ls-files | grep -E "(requirements|setup|pyproject|poetry|pipfile)" | head -20Repository: RohanExploit/VishwaGuru Length of output: 177 🏁 Script executed: # Check for pytest-asyncio in common dependency files
for file in requirements.txt requirements-dev.txt setup.py setup.cfg pyproject.toml poetry.lock Pipfile Pipfile.lock .python-version; do
if [ -f "$file" ]; then
echo "=== $file ==="
cat "$file" | head -50
fi
doneRepository: RohanExploit/VishwaGuru Length of output: 49 🏁 Script executed: echo "=== backend/requirements.txt ===" && cat backend/requirements.txt
echo ""
echo "=== backend/requirements-render.txt ===" && cat backend/requirements-render.txtRepository: RohanExploit/VishwaGuru Length of output: 984 🏁 Script executed: # Search for pytest-asyncio anywhere in the repository
rg "pytest-asyncio" -iRepository: RohanExploit/VishwaGuru Length of output: 49 🏁 Script executed: # Also check functions/requirements.txt and any other requirements files
find . -name "*requirements*.txt" -o -name "Pipfile*" -o -name "pyproject.toml" | grep -v node_modulesRepository: RohanExploit/VishwaGuru Length of output: 156 🏁 Script executed: cat functions/requirements.txtRepository: RohanExploit/VishwaGuru Length of output: 298 Add pytest-asyncio to project dependencies. The pytest.ini file uses pytest-asyncio specific configuration options ( 🤖 Prompt for AI Agents
Comment on lines
+1
to
+3
|
||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
HTTPExceptionandstatusare imported but not used anywhere in this module (and the newfunc/caseimport makes this import block part of the changed hunk). Please remove unused FastAPI imports to avoid lint warnings and keep imports minimal.