simulation
check_container_status(docker_container, simulation_id, db)
¶
Check the status of a Docker container and update the status of an associated simulation in the database accordingly.
This function waits for the completion of a Docker container and retrieves its exit code. If the exit code indicates an error, an HTTPException is raised with the container logs. Otherwise, it updates the simulation's status to finished, sets the end date, and commits the changes to the database.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
docker_container
|
DockerContainer
|
Docker container object representing the container to be monitored |
required |
simulation_id
|
int
|
The unique identifier of the simulation associated with the Docker container |
required |
db
|
Session
|
Database session object used for querying and persisting updates. Dependency injection. |
required |
Returns:
Type | Description |
---|---|
None
|
None |
Source code in backend/app/simulation/router.py
delete_simulation(token, simulation_id, db=Depends(get_db_session))
async
¶
Deletes a simulation from the database based on the given simulation ID. The user must be authenticated via a token for the operation to proceed.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
token
|
Annotated[str, Depends(oauth2_scheme)]
|
The authenticated token required to access the endpoint. |
required |
simulation_id
|
int
|
The ID of the simulation to be deleted. |
required |
db
|
Session
|
The database session is used to retrieve and delete the simulation record. Dependency injection. |
Depends(get_db_session)
|
Returns:
Type | Description |
---|---|
MessageResponse
|
A response indicating whether the simulation was successfully deleted. |
Raises:
Type | Description |
---|---|
HTTPException
|
If the user is not authenticated |
Source code in backend/app/simulation/router.py
get_simulation(simulation_id, token, db=Depends(get_db_session))
async
¶
Fetches and returns simulation data based on the specified simulation ID. The request must include a valid authentication token. The function verifies user rights before retrieving the simulation. If the simulation does not exist or the user lacks the necessary permissions, appropriate HTTP exceptions are raised.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
simulation_id
|
int
|
The ID of the simulation to be fetched. |
required |
token
|
Annotated[str, Depends(oauth2_scheme)]
|
The authentication token for validating the request. |
required |
db
|
Session
|
Database session to access the database. Dependency injection. |
Depends(get_db_session)
|
Returns:
Type | Description |
---|---|
DataResponse
|
A |
Raises:
Type | Description |
---|---|
HTTPException
|
If the user is not authorized. |
HTTPException
|
If the specified simulation does not exist. |
HTTPException
|
If the user does not have access to the simulation. |
Source code in backend/app/simulation/router.py
get_simulation_status(simulation_id, token, db=Depends(get_db_session))
async
¶
This function retrieves the status of a specific simulation based on the provided simulation ID. It performs authentication and authorization checks to ensure proper access control. If the simulation exists and the user has the required access permissions, it fetches and returns the simulation's status wrapped in a success response.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
simulation_id
|
int
|
ID of the simulation to retrieve status for. |
required |
token
|
Annotated[str, Depends(oauth2_scheme)]
|
Access token provided by the client for authorization. |
required |
db
|
Session
|
Database session is required to access simulation data. Dependency injection. |
Depends(get_db_session)
|
Returns:
Type | Description |
---|---|
MessageResponse
|
A |
Raises:
Type | Description |
---|---|
HTTPException
|
|
Source code in backend/app/simulation/router.py
get_simulations(scenario_id, token, db=Depends(get_db_session))
async
¶
Retrieve a list of simulations associated with a specific scenario.
This function fetches simulations from the database that are linked to the
given scenario_id
. It authenticates the request using a token and validates
the user's rights for the specified scenario. If no simulations are found,
an appropriate HTTP error is raised.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
scenario_id
|
int
|
Identifier of the scenario for which simulations are requested. |
required |
token
|
Annotated[str, Depends(oauth2_scheme)]
|
A string token used for authentication and authorization. |
required |
db
|
Session
|
A database session used for querying the simulations. Dependency injection. |
Depends(get_db_session)
|
Returns:
Type | Description |
---|---|
DataResponse
|
A DataResponse object containing the list of simulations matched to the specified scenario, the total count of simulations, and a success flag. |
Source code in backend/app/simulation/router.py
start_simulation(scenario_id, background_tasks, token, db=Depends(get_db_session))
async
¶
Starts a new simulation for a given scenario and ensures any currently running simulations for the same scenario are stopped. It creates a new simulation entry in the database, generates a unique task ID, and initiates the simulation task.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
scenario_id
|
int
|
The ID of the scenario for which the simulation is being started. |
required |
background_tasks
|
BackgroundTasks
|
The background tasks object provided by FastAPI's dependency injection. |
required |
token
|
Annotated[str, Depends(oauth2_scheme)]
|
The authentication token used to authenticate and authorize the user. |
required |
db
|
Session
|
The session used for database interaction, provided by dependency injection. |
Depends(get_db_session)
|
Returns:
Type | Description |
---|---|
MessageResponse
|
A response containing success information and details of the initiated task. |
Raises:
Type | Description |
---|---|
HTTPException
|
If authentication or authorization fails. |
Source code in backend/app/simulation/router.py
stop_simulation(simulation_id, token, db=Depends(get_db_session))
async
¶
Stops a simulation with the specified simulation ID. This operation first validates the user's authentication and authorization using the provided token, then retrieves the simulation from the database. If the simulation is found and the user has the appropriate rights, the simulation task is manually stopped through the 'revoke' method. A success message is returned upon successful completion.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
simulation_id
|
int
|
The identifier of the simulation to be stopped. |
required |
token
|
Annotated[str, Depends(oauth2_scheme)]
|
A token to validate user authentication and authorization. |
required |
db
|
Session
|
The database session is used to access and query the database. Dependency injection. |
Depends(get_db_session)
|
Returns:
Type | Description |
---|---|
MessageResponse
|
A message response indicating a success message and the ID of the stopped simulation. |
Raises:
Type | Description |
---|---|
HTTPException
|
Raises a 401 Unauthorized error if the token is missing or invalid, or if the user is not authorized. Raises a 404 Not Found error if the specified simulation ID does not exist. |
Source code in backend/app/simulation/router.py
stop_simulations(scenario_id, token, db=Depends(get_db_session))
async
¶
Stop active simulations for a specified scenario.
This endpoint allows stopping all running simulations associated with a specific scenario. Authorization and user permission checks are performed before proceeding. If more than one simulation is found, a conflict error is reported. Additionally, background tasks associated with the simulations are terminated.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
scenario_id
|
int
|
ID of the scenario whose simulations are to be stopped. |
required |
token
|
Annotated[str, Depends(oauth2_scheme)]
|
Authentication token for user validation. |
required |
db
|
Session
|
Database session for querying and updating simulation data. |
Depends(get_db_session)
|
Returns:
Type | Description |
---|---|
MessageResponse
|
Response object indicating the success or failure of the operation along with details of any encountered errors. |
Source code in backend/app/simulation/router.py
validate_user_rights(token, scenario_id, db)
¶
Validates a user's rights to access a specific scenario within a project. The function first validates whether the user is the owner of the given scenario and subsequently verifies ownership of the associated project. Raises appropriate HTTP exceptions if the user is unauthorized or if the specified scenario or project does not exist.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
token
|
Str
|
Authentication token of the user. |
required |
scenario_id
|
Int
|
ID of the scenario to validate access for. |
required |
db
|
Session
|
Database session/connection object used for querying related data. Dependency injection. |
required |
Returns:
Type | Description |
---|---|
Bool
|
A boolean indicating whether the user is authorized to access the scenario. |
Raises:
Type | Description |
---|---|
HTTPException
|
If the user is unauthorized or the specified scenario or project is not found. |