Skip to content

Commit

Permalink
fixes async issues
Browse files Browse the repository at this point in the history
  • Loading branch information
GabrielBarberini committed May 6, 2024
1 parent 729f667 commit a7afebd
Show file tree
Hide file tree
Showing 5 changed files with 84 additions and 102 deletions.
8 changes: 8 additions & 0 deletions lib/__init__.py
Original file line number Diff line number Diff line change
@@ -1,2 +1,10 @@
# lib/__init__.py
import logging
from .api import app

logging.basicConfig(
level=logging.INFO,
filename='app.log',
filemode='a',
format='%(asctime)s - %(levelname)s - %(message)s',
)
7 changes: 4 additions & 3 deletions lib/api.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,16 +2,17 @@
This is the main API file for the RocketPy API.
"""

import logging

from fastapi import FastAPI, Request, status
from fastapi.exceptions import RequestValidationError
from fastapi.middleware.cors import CORSMiddleware
from fastapi.openapi.utils import get_openapi
from fastapi.responses import RedirectResponse, JSONResponse

from lib import logging
from lib.routes import flight, environment, motor, rocket

logger = logging.getLogger(__name__)

app = FastAPI(
swagger_ui_parameters={
"defaultModelsExpandDepth": 0,
Expand Down Expand Up @@ -84,7 +85,7 @@ async def validation_exception_handler(
request: Request, exc: RequestValidationError
):
exc_str = f"{exc}".replace("\n", " ").replace(" ", " ")
logging.error(f"{request}: {exc_str}")
logger.error(f"{request}: {exc_str}")
content = {"status_code": 10422, "message": exc_str, "data": None}
return JSONResponse(
content=content, status_code=status.HTTP_422_UNPROCESSABLE_ENTITY
Expand Down
105 changes: 45 additions & 60 deletions lib/controllers/environment.py
Original file line number Diff line number Diff line change
@@ -1,11 +1,10 @@
from typing import Union
from datetime import datetime

import logging
import jsonpickle
from rocketpy.environment.environment import Environment as RocketPyEnvironment
from fastapi import HTTPException, status

from lib import logging
from lib.controllers import parse_error
from lib.models.environment import Env
from lib.repositories.environment import EnvRepository
Expand All @@ -19,6 +18,8 @@
EnvPickle,
)

logger = logging.getLogger(__name__)


class EnvController:
"""
Expand Down Expand Up @@ -69,23 +70,22 @@ async def create_env(self) -> "Union[EnvCreated, HTTPException]":
Returns:
views.EnvCreated
"""
env_repo = EnvRepository(environment=self.env)
try:
await env_repo.create_env()
created_env = await EnvRepository(
environment=self.env
).create_env()
except Exception as e:
exc_str = parse_error(e)
logging.error(
f"[{datetime.now()}] controllers.environment.create_env: {exc_str}"
)
logger.error(f"controllers.environment.create_env: {exc_str}")
raise HTTPException(
status_code=status.HTTP_500_INTERNAL_SERVER_ERROR,
detail=f"Failed to create environment: {e}",
) from e
else:
return EnvCreated(env_id=env_repo.env_id)
return EnvCreated(env_id=created_env.env_id)
finally:
logging.info(
f"[{datetime.now()}] Call to controllers.environment.create_env completed; params: Env {hash(self.env)}"
logger.info(
f"Call to controllers.environment.create_env completed; params: Env {hash(self.env)}"
)

@staticmethod
Expand All @@ -102,14 +102,11 @@ async def get_env_by_id(env_id: int) -> "Union[Env, HTTPException]":
Raises:
HTTP 404 Not Found: If the env is not found in the database.
"""
env_repo = EnvRepository(env_id=env_id)
try:
read_env = await env_repo.get_env()
read_env = await EnvRepository(env_id=env_id).get_env()
except Exception as e:
exc_str = parse_error(e)
logging.error(
f"[{datetime.now()}] controllers.environment.get_env_by_id: {exc_str}"
)
logger.error(f"controllers.environment.get_env_by_id: {exc_str}")
raise HTTPException(
status_code=status.HTTP_500_INTERNAL_SERVER_ERROR,
detail=f"Failed to read environment: {e}",
Expand All @@ -122,8 +119,8 @@ async def get_env_by_id(env_id: int) -> "Union[Env, HTTPException]":
detail="Environment not found",
)
finally:
logging.info(
f"[{datetime.now()}] Call to controllers.environment.get_env_by_id completed; params: EnvID {env_id}"
logger.info(
f"Call to controllers.environment.get_env_by_id completed; params: EnvID {env_id}"
)

@classmethod
Expand All @@ -143,31 +140,27 @@ async def get_rocketpy_env_as_jsonpickle(
Raises:
HTTP 404 Not Found: If the env is not found in the database.
"""
env_repo = EnvRepository(env_id=env_id)
try:
read_env = await env_repo.get_env()
read_env = await cls.get_env_by_id(env_id)
except HTTPException as e:
raise e from e
except Exception as e:
exc_str = parse_error(e)
logging.error(
f"[{datetime.now()}] controllers.environment.get_rocketpy_env_as_jsonpickle: {exc_str}"
logger.error(
f"controllers.environment.get_rocketpy_env_as_jsonpickle: {exc_str}"
)
raise HTTPException(
status_code=status.HTTP_500_INTERNAL_SERVER_ERROR,
detail=f"Failed to read environment: {e}",
) from e
else:
if read_env:
rocketpy_env = cls.get_rocketpy_env(read_env)
return EnvPickle(
jsonpickle_rocketpy_env=jsonpickle.encode(rocketpy_env)
)
raise HTTPException(
status_code=status.HTTP_404_NOT_FOUND,
detail="Environment not found",
rocketpy_env = await cls.get_rocketpy_env(read_env)
return EnvPickle(
jsonpickle_rocketpy_env=jsonpickle.encode(rocketpy_env)
)
finally:
logging.info(
f"[{datetime.now()}] Call to controllers.environment.get_rocketpy_env_as_jsonpickle completed; params: EnvID {env_id}"
logger.info(
f"Call to controllers.environment.get_rocketpy_env_as_jsonpickle completed; params: EnvID {env_id}"
)

async def update_env(
Expand All @@ -185,30 +178,25 @@ async def update_env(
Raises:
HTTP 404 Not Found: If the env is not found in the database.
"""
env_repo = EnvRepository(environment=self.env, env_id=env_id)
try:
read_env = await env_repo.get_env()
if read_env:
await env_repo.update_env()
else:
raise HTTPException(
status_code=status.HTTP_404_NOT_FOUND,
detail="Environment not found",
)
await EnvController.get_env_by_id(env_id)
updated_env = await EnvRepository(
environment=self.env, env_id=env_id
).update_env()
except HTTPException as e:
raise e from e
except Exception as e:
exc_str = parse_error(e)
logging.error(
f"[{datetime.now()}] controllers.environment.update_env: {exc_str}"
)
logger.error(f"controllers.environment.update_env: {exc_str}")
raise HTTPException(
status_code=status.HTTP_500_INTERNAL_SERVER_ERROR,
detail=f"Failed to update environment: {e}",
) from e
else:
return EnvUpdated(new_env_id=env_repo.env_id)
return EnvUpdated(new_env_id=updated_env.env_id)
finally:
logging.info(
f"[{datetime.now()}] Call to controllers.environment.update_env completed; params: EnvID {env_id}, Env {hash(self.env)}"
logger.info(
f"Call to controllers.environment.update_env completed; params: EnvID {env_id}, Env {hash(self.env)}"
)

@staticmethod
Expand All @@ -225,23 +213,20 @@ async def delete_env(env_id: str) -> "Union[EnvDeleted, HTTPException]":
Raises:
HTTP 404 Not Found: If the env is not found in the database.
"""
env_repo = EnvRepository(env_id=env_id)
try:
await env_repo.delete_env()
await EnvRepository(env_id=env_id).delete_env()
except Exception as e:
exc_str = parse_error(e)
logging.error(
f"[{datetime.now()}] controllers.environment.delete_env: {exc_str}"
)
logger.error(f"controllers.environment.delete_env: {exc_str}")
raise HTTPException(
status_code=status.HTTP_500_INTERNAL_SERVER_ERROR,
detail=f"Failed to delete environment: {e}",
) from e
else:
return EnvDeleted(deleted_env_id=env_id)
finally:
logging.info(
f"[{datetime.now()}] Call to controllers.environment.delete_env completed; params: EnvID {env_id}"
logger.info(
f"Call to controllers.environment.delete_env completed; params: EnvID {env_id}"
)

@classmethod
Expand All @@ -258,9 +243,9 @@ async def simulate(cls, env_id: int) -> "Union[EnvSummary, HTTPException]":
Raises:
HTTP 404 Not Found: If the env does not exist in the database.
"""
read_env = await cls.get_env_by_id(env_id)
try:
rocketpy_env = cls.get_rocketpy_env(read_env)
read_env = await cls.get_env_by_id(env_id)
rocketpy_env = await cls.get_rocketpy_env(read_env)
env_simulation_numbers = EnvData.parse_obj(
rocketpy_env.all_info_returned()
)
Expand All @@ -270,18 +255,18 @@ async def simulate(cls, env_id: int) -> "Union[EnvSummary, HTTPException]":
env_summary = EnvSummary(
env_data=env_simulation_numbers, env_plots=env_simulation_plots
)
except HTTPException as e:
raise e from e
except Exception as e:
exc_str = parse_error(e)
logging.error(
f"[{datetime.now()}] controllers.environment.simulate: {exc_str}"
)
logger.error(f"controllers.environment.simulate: {exc_str}")
raise HTTPException(
status_code=status.HTTP_500_INTERNAL_SERVER_ERROR,
detail=f"Failed to simulate environment: {e}",
) from e
else:
return env_summary
finally:
logging.info(
f"[{datetime.now()}] Call to controllers.environment.simulate completed; params: EnvID {env_id}"
logger.info(
f"Call to controllers.environment.simulate completed; params: EnvID {env_id}"
)
61 changes: 26 additions & 35 deletions lib/repositories/environment.py
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
import logging
from datetime import datetime
from typing import Union
from lib import logging
from lib.repositories import parse_error
from lib.models.environment import Env
from lib.repositories.repo import Repository

logger = logging.getLogger(__name__)


class EnvRepository(Repository):
"""
Expand All @@ -22,7 +23,7 @@ def __init__(self, environment: Env = None, env_id: str = None):
if env_id:
self._env_id = env_id
else:
self._env_id = hash(self._env)
self._env_id = str(hash(self._env))

@property
def env(self) -> "Env":
Expand All @@ -45,55 +46,49 @@ async def create_env(self):
Creates a non-existing models.Env in the database
Returns:
None
self
"""
env_exists = await self.get_env()
if env_exists:
return

try:
environment_to_dict = self.env.dict()
environment_to_dict["env_id"] = self.env_id
await self.collection.insert_one(environment_to_dict)
except Exception as e:
exc_str = parse_error(e)
logging.error(
f"[{datetime.now()}] repositories.environment.create_env: {exc_str}"
)
logger.error(f"repositories.environment.create_env: {exc_str}")
raise Exception(f"Error creating environment: {str(e)}") from e
else:
return self
finally:
logging.info(
f"[{datetime.now()}] Call to repositories.environment.create_env completed; states: Env {hash(self.env)}, EnvID {self.env_id}"
logger.info(
f"Call to repositories.environment.create_env completed; states: EnvID {self.env_id}"
)
self.close_connection()
await self.close_connection()

async def update_env(self):
"""
Updates a models.Env in the database
Returns:
None
self
"""
try:
environment_to_dict = self.env.dict()
environment_to_dict["env_id"] = hash(self.env)
environment_to_dict["env_id"] = str(hash(self.env))
await self.collection.update_one(
{"env_id": self.env_id}, {"$set": environment_to_dict}
)
self.env_id = environment_to_dict["env_id"]
except Exception as e:
exc_str = parse_error(e)
logging.error(
f"[{datetime.now()}] repositories.environment.update_env: {exc_str}"
)
logger.error(f"repositories.environment.update_env: {exc_str}")
raise Exception(f"Error updating environment: {str(e)}") from e
else:
return
return self
finally:
logging.info(
f"[{datetime.now()}] Call to repositories.environment.update_env completed; states: Env {hash(self.env)}, EnvID {self.env_id}"
logger.info(
f"Call to repositories.environment.update_env completed; states: Env {hash(self.env)}, EnvID {self.env_id}"
)
self.close_connection()
await self.close_connection()

async def get_env(self) -> "Union[Env, None]":
"""
Expand All @@ -105,17 +100,15 @@ async def get_env(self) -> "Union[Env, None]":
try:
read_env = await self.collection.find_one({"env_id": self.env_id})
except Exception as e:
logging.error(
f"[{datetime.now()}] repositories.environment.get_env: {str(e)}"
)
logger.error(f"repositories.environment.get_env: {str(e)}")
raise Exception(f"Error getting environment: {str(e)}") from e
else:
return Env.parse_obj(read_env) if read_env else None
finally:
logging.info(
f"[{datetime.now()}] Call to repositories.environment.get_env completed; states: Env {hash(self.env)}, EnvID {self.env_id}"
logger.info(
f"Call to repositories.environment.get_env completed; states: Env {hash(self.env)}, EnvID {self.env_id}"
)
self.close_connection()
await self.close_connection()

async def delete_env(self):
"""
Expand All @@ -127,12 +120,10 @@ async def delete_env(self):
try:
await self.collection.delete_one({"env_id": self.env_id})
except Exception as e:
logging.error(
f"[{datetime.now()}] repositories.environment.delete_env: {str(e)}"
)
logger.error(f"repositories.environment.delete_env: {str(e)}")
raise Exception(f"Error deleting environment: {str(e)}") from e
finally:
logging.info(
f"[{datetime.now()}] Call to repositories.environment.delete_env completed; states: Env {hash(self.env)}, EnvID {self.env_id}"
logger.info(
f"Call to repositories.environment.delete_env completed; states: Env {hash(self.env)}, EnvID {self.env_id}"
)
self.close_connection()
await self.close_connection()
Loading

0 comments on commit a7afebd

Please sign in to comment.