refactor: api/services/recommended_app_service.py to session#32162
refactor: api/services/recommended_app_service.py to session#32162asukaminato0721 wants to merge 1 commit intolanggenius:mainfrom
Conversation
Summary of ChangesHello @asukaminato0721, I'm Gemini Code Assist1! I'm currently reviewing this pull request and will post my feedback shortly. In the meantime, here's a summary to help you and other reviewers quickly get up to speed! This pull request refactors the Highlights
🧠 New Feature in Public Preview: You can now enable Memory to help Gemini Code Assist learn from your team's feedback. This makes future code reviews more consistent and personalized to your project's style. Click here to enable Memory in your admin console. Changelog
Activity
Using Gemini Code AssistThe full guide for Gemini Code Assist can be found on our documentation page, here are some quick tips. Invoking Gemini You can request assistance from Gemini at any point by creating a comment using either
Customization To customize Gemini Code Assist for GitHub experience, repository maintainers can create a configuration file and/or provide a custom code review style guide (such as PEP-8 for Python) by creating and adding files to a Limitations & Feedback Gemini Code Assist may make mistakes. Please leave feedback on any instances where its feedback is incorrect or counter productive. You can react with 👍 and 👎 on @gemini-code-assist comments. If you're interested in giving your feedback about your experience with Gemini Code Assist for Github and other Google products, sign up here. You can also get AI-powered code generation, chat, as well as code reviews directly in the IDE at no cost with the Gemini Code Assist IDE Extension. Footnotes
|
There was a problem hiding this comment.
Code Review
This pull request refactors RecommendedAppService to use the session_factory for database operations, which is a positive step towards better session management. However, I've identified a performance regression where a database session is created inside a loop, and a pre-existing race condition that should be addressed. My review includes suggestions to optimize the database queries for better performance and to fix the race condition for improved reliability.
There was a problem hiding this comment.
Pull request overview
Refactors RecommendedAppService to use the project’s pure SQLAlchemy session_factory instead of Flask-SQLAlchemy’s db.session, aligning with the ongoing DB session migration effort (issue #24115).
Changes:
- Replace
db.sessionqueries withsession_factory.create_session()for trial-app lookups. - Wrap trial record mutation in a
session.begin()transaction context.
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
Pyrefly DiffNo changes detected. |
There was a problem hiding this comment.
Pull request overview
Copilot reviewed 1 out of 1 changed files in this pull request and generated 3 comments.
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
| if FeatureService.get_system_features().enable_trial_app: | ||
| apps = result["recommended_apps"] | ||
| for app in apps: | ||
| app_id = app["app_id"] | ||
| trial_app_model = db.session.query(TrialApp).where(TrialApp.app_id == app_id).first() | ||
| if trial_app_model: | ||
| app["can_trial"] = True | ||
| else: | ||
| app["can_trial"] = False | ||
| with session_factory.create_session() as session: | ||
| trial_app_model = session.query(TrialApp).where(TrialApp.app_id == app_id).first() |
There was a problem hiding this comment.
There are existing unit tests for RecommendedAppService, but none exercise the enable_trial_app branches touched here (the session_factory lookups / can_trial flag behavior). Add tests that set FeatureService.get_system_features().enable_trial_app = True and mock session_factory.create_session() so the trial-app behavior is covered and protected during the db.session -> session_factory migration.
| for app in apps: | ||
| app_id = app["app_id"] | ||
| trial_app_model = db.session.query(TrialApp).where(TrialApp.app_id == app_id).first() | ||
| if trial_app_model: | ||
| app["can_trial"] = True | ||
| else: | ||
| app["can_trial"] = False | ||
| with session_factory.create_session() as session: | ||
| trial_app_model = session.query(TrialApp).where(TrialApp.app_id == app_id).first() | ||
| if trial_app_model: |
There was a problem hiding this comment.
get_recommended_apps_and_categories opens a new SQLAlchemy Session and runs a query for each app in the loop. This creates an N+1 query pattern and can put unnecessary pressure on the connection pool. Consider creating a single session for the whole block and fetching all matching TrialApp.app_id values in one IN (...) query, then setting can_trial via set membership.
| result: dict = retrieval_instance.get_recommend_app_detail(app_id) | ||
| if FeatureService.get_system_features().enable_trial_app: | ||
| app_id = result["id"] | ||
| trial_app_model = db.session.query(TrialApp).where(TrialApp.app_id == app_id).first() | ||
| if trial_app_model: | ||
| result["can_trial"] = True | ||
| else: | ||
| result["can_trial"] = False | ||
| with session_factory.create_session() as session: | ||
| trial_app_model = session.query(TrialApp).where(TrialApp.app_id == app_id).first() |
There was a problem hiding this comment.
get_recommend_app_detail can return None (e.g., remote retrieval non-200). When enable_trial_app is enabled, this code will raise a TypeError by subscripting result["id"]. Add a guard to return None early when result is falsy before using it in the trial-app lookup.
Pyrefly DiffNo changes detected. |
Pyrefly DiffNo changes detected. |
Pyrefly DiffNo changes detected. |
There was a problem hiding this comment.
Pull request overview
Copilot reviewed 1 out of 1 changed files in this pull request and generated 1 comment.
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
You can also share your feedback on Copilot code review. Take the survey.
| with session_factory.create_session() as session: | ||
| trial_app_model = session.query(TrialApp).where(TrialApp.app_id == app_id).first() | ||
| if trial_app_model: | ||
| result["can_trial"] = True | ||
| else: |
0bb07c6 to
262ec62
Compare
Pyrefly DiffNo changes detected. |
Important
Fixes #<issue number>.Summary
part of #24115
Screenshots
Checklist
make lintandmake type-check(backend) andcd web && npx lint-staged(frontend) to appease the lint gods