fix: optimize toggle_favourite endpoint performance#1094
fix: optimize toggle_favourite endpoint performance#1094harsh1519 wants to merge 3 commits intoAOSSIE-Org:mainfrom
Conversation
📝 WalkthroughWalkthroughAdds Changes
sequenceDiagram
participant C as Client
participant R as API Route (toggle_favourite)
participant DB as Database
Note over R,DB: Toggle favourite then fetch updated record
C->>R: POST /images/{id}/toggle_favourite
R->>DB: UPDATE images SET isFavourite = NOT isFavourite WHERE id = {id}
DB-->>R: OK (rows affected)
R->>DB: SELECT id, path, folder_id, thumbnailPath, metadata, isTagged, isFavourite FROM images WHERE id = {id}
DB-->>R: image row (or NULL)
alt image found
R-->>C: 200 OK with updated image
else not found
R-->>C: 404 Image not found after toggle
end
Estimated code review effort🎯 2 (Simple) | ⏱️ ~10 minutes Poem
🚥 Pre-merge checks | ✅ 2 | ❌ 1❌ Failed checks (1 warning)
✅ Passed checks (2 passed)
✏️ Tip: You can configure your own custom pre-merge checks in the settings. ✨ Finishing touches
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
Caution
Some comments are outside the diff and can’t be posted inline due to platform limitations.
⚠️ Outside diff range comments (1)
backend/app/routes/images.py (1)
99-125: Preserve intended 404s by not swallowingHTTPException.
The broadexcept Exceptionwill catch the 404 you raise and return a 500 instead.🐛 Proposed fix
try: success = db_toggle_image_favourite_status(image_id) if not success: raise HTTPException( status_code=404, detail="Image not found or failed to toggle" ) # Fetch updated status to return image = db_get_image_by_id(image_id) if not image: raise HTTPException( status_code=404, detail="Image not found after toggle" ) return { "success": True, "image_id": image_id, "isFavourite": image.get("isFavourite", False), } - except Exception as e: + except HTTPException: + raise + except Exception as e: logger.error(f"error in /toggle-favourite route: {e}") raise HTTPException(status_code=500, detail=f"Internal server error: {e}")
🤖 Fix all issues with AI agents
In `@backend/app/database/images.py`:
- Around line 425-446: The db_get_image_by_id function currently calls
json.loads(row[4]) which can raise json.JSONDecodeError and cause unrelated
endpoints to 500; wrap the metadata parsing in a try/except that catches
json.JSONDecodeError (or Exception) and falls back to an empty dict for the
"metadata" field so the function returns normally; update the code around
json.loads(row[4]) in db_get_image_by_id to perform the safe parse with fallback
and keep other returned fields unchanged.
Related to #977
The /toggle-favourite endpoint previously fetched all images from the database
to determine the favourite status of a single image, causing O(n) complexity
and unnecessary memory usage.
This PR updates the route to fetch only the specific image by ID and return
the updated favourite status.
Summary by CodeRabbit
Performance Improvements
Bug Fixes
✏️ Tip: You can customize this high-level summary in your review settings.