feat(spp_api_v2_simulation): add simulation REST API#72
Conversation
Summary of ChangesHello, 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 introduces a new module that significantly expands the OpenSPP API V2 capabilities by providing a dedicated RESTful interface for managing and interacting with simulation scenarios. This enhancement allows external systems to programmatically create, update, delete, and execute simulation scenarios, retrieve detailed run results, compare different simulation outcomes, and perform population aggregations with demographic breakdowns, all while ensuring privacy protection through k-anonymity. Highlights
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.
Semgrep OSS found more than 20 potential problems in the proposed changes. Check the Files changed tab for more details.
Codecov Report❌ Patch coverage is Additional details and impacted files@@ Coverage Diff @@
## 19.0 #72 +/- ##
==========================================
+ Coverage 55.79% 58.18% +2.38%
==========================================
Files 162 196 +34
Lines 9291 11210 +1919
==========================================
+ Hits 5184 6522 +1338
- Misses 4107 4688 +581
Flags with carried forward coverage won't be shown. Click here to find out more.
🚀 New features to boost your workflow:
|
There was a problem hiding this comment.
Code Review
This pull request introduces a new module, spp_api_v2_simulation, to provide a comprehensive REST API for managing and executing simulation scenarios. The changes are extensive, adding new models, services, routers, Pydantic schemas, and tests to support CRUD operations on scenarios, runs, and comparisons, as well as analytics and aggregation endpoints.
The overall structure is good, but there are a few significant areas for improvement. The most critical issue is the duplication of business logic between the newly introduced routers and a SimulationApiService that appears to be unused by the API endpoints. This should be refactored to have the routers delegate logic to the service layer, making the code more maintainable. Additionally, there is considerable duplication among the Pydantic schema definitions, which should be consolidated. I've also noted a security concern regarding potential information leakage in exception handling and a minor point about an empty ir.model.access.csv file.
Note: Security Review did not run due to the size of the PR.
| _logger = logging.getLogger(__name__) | ||
|
|
||
|
|
||
| class SimulationApiService: |
There was a problem hiding this comment.
There is a significant amount of duplicated logic between the API routers (e.g., in spp_api_v2_simulation/routers/scenario.py) and this service class. The routers currently implement business logic for creating, updating, and fetching simulation data directly, while this service contains a parallel, unused implementation of the same logic.
To improve maintainability and adhere to a clear separation of concerns, the routers should be refactored to be a thin layer that handles HTTP requests/responses and delegates all business logic to this service. This will create a single source of truth for the simulation operations and make the codebase cleaner and easier to manage.
| @@ -0,0 +1,267 @@ | |||
| # Part of OpenSPP. See LICENSE file for full copyright and licensing details. | |||
| """Pydantic schemas for Simulation API.""" | |||
There was a problem hiding this comment.
This file defines several Pydantic schemas (e.g., CreateScenarioRequest, ScenarioResponse, ComparisonResponse) that are duplicates or slight variations of schemas defined in other files within this directory (e.g., scenario.py, comparison.py). The API routers appear to use the schemas from the other, more specific files, which suggests that many of the schemas in this file are unused in the context of the API.
This duplication can lead to confusion and maintenance issues. It is highly recommended to consolidate the schemas, remove the duplicates, and ensure there is a single, authoritative definition for each schema. This file should likely only contain the schemas that are actually used from it, such as TemplateResponse and TemplateListResponse.
| except Exception as e: | ||
| _logger.exception("Failed to create comparison") | ||
| raise HTTPException( | ||
| status_code=status.HTTP_500_INTERNAL_SERVER_ERROR, | ||
| detail=f"Failed to create comparison: {str(e)}", | ||
| ) from None |
There was a problem hiding this comment.
In the generic except Exception as e: block, the exception details str(e) are included in the HTTP response's detail message. This can potentially leak sensitive internal information, such as database details or library internals, to the API client, which is a security risk.
The current implementation already logs the full exception with _logger.exception, which is excellent for debugging. The client-facing error message, however, should be generic to avoid leaking implementation details. This same issue is present in the exception handlers in run.py and scenario.py as well.
| except Exception as e: | |
| _logger.exception("Failed to create comparison") | |
| raise HTTPException( | |
| status_code=status.HTTP_500_INTERNAL_SERVER_ERROR, | |
| detail=f"Failed to create comparison: {str(e)}", | |
| ) from None | |
| except Exception as e: | |
| _logger.exception("Failed to create comparison") | |
| raise HTTPException( | |
| status_code=status.HTTP_500_INTERNAL_SERVER_ERROR, | |
| detail="Failed to create comparison", | |
| ) from e |
| @@ -0,0 +1 @@ | |||
| id,name,model_id:id,group_id:id,perm_read,perm_write,perm_create,perm_unlink | |||
There was a problem hiding this comment.
This ir.model.access.csv file only contains a header row and is otherwise empty. While API access is correctly handled via scopes, an empty access file is unusual. If no new models require specific access rights for regular Odoo users, this file could be removed to avoid confusion. If it's intended as a placeholder for future rules, adding a comment to that effect would be helpful for other developers.
Prevent internal exception messages from leaking to API callers via HTTPException detail strings. Full exception info is still logged via _logger.exception for server-side diagnosis.
e0859a4 to
c36f076
Compare
Summary
New module providing REST API for simulation scenario management:
Dependencies: spp_api_v2, spp_simulation, spp_aggregation
Test plan
./scripts/test_single_module.sh spp_api_v2_simulation