Skip to content

Commit bfe029a

Browse files
fixes tests and runs formatting
1 parent d1a5bf6 commit bfe029a

31 files changed

+863
-590
lines changed

lib/controllers/environment.py

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
ControllerInterface,
33
controller_exception_handler,
44
)
5-
from lib.views.environment import EnvironmentSummary
5+
from lib.views.environment import EnvironmentSimulation
66
from lib.models.environment import EnvironmentModel
77
from lib.services.environment import EnvironmentService
88

@@ -36,26 +36,26 @@ async def get_rocketpy_env_binary(
3636
Raises:
3737
HTTP 404 Not Found: If the env is not found in the database.
3838
"""
39-
env = await self.get_env_by_id(env_id)
39+
env = await self.get_environment_by_id(env_id)
4040
env_service = EnvironmentService.from_env_model(env)
41-
return env_service.get_env_binary()
41+
return env_service.get_environment_binary()
4242

4343
@controller_exception_handler
4444
async def get_environment_simulation(
4545
self, env_id: str
46-
) -> EnvironmentSummary:
46+
) -> EnvironmentSimulation:
4747
"""
4848
Simulate a rocket environment.
4949
5050
Args:
5151
env_id: str.
5252
5353
Returns:
54-
EnvironmentSummary
54+
EnvironmentSimulation
5555
5656
Raises:
5757
HTTP 404 Not Found: If the env does not exist in the database.
5858
"""
59-
env = await self.get_env_by_id(env_id)
59+
env = await self.get_environment_by_id(env_id)
6060
env_service = EnvironmentService.from_env_model(env)
61-
return env_service.get_env_summary()
61+
return env_service.get_environment_simulation()

lib/controllers/flight.py

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
ControllerInterface,
33
controller_exception_handler,
44
)
5-
from lib.views.flight import FlightSummary, FlightUpdated
5+
from lib.views.flight import FlightSimulation, FlightUpdated
66
from lib.models.flight import FlightModel
77
from lib.models.environment import EnvironmentModel
88
from lib.models.rocket import RocketModel
@@ -87,22 +87,22 @@ async def get_rocketpy_flight_binary(
8787
return flight_service.get_flight_binary()
8888

8989
@controller_exception_handler
90-
async def simulate_flight(
90+
async def get_flight_simulation(
9191
self,
9292
flight_id: str,
93-
) -> FlightSummary:
93+
) -> FlightSimulation:
9494
"""
9595
Simulate a rocket flight.
9696
9797
Args:
9898
flight_id: str
9999
100100
Returns:
101-
Flight summary view.
101+
Flight simulation view.
102102
103103
Raises:
104104
HTTP 404 Not Found: If the flight does not exist in the database.
105105
"""
106106
flight = await self.get_flight_by_id(flight_id=flight_id)
107107
flight_service = FlightService.from_flight_model(flight)
108-
return flight_service.get_flight_summary()
108+
return flight_service.get_flight_simmulation()

lib/controllers/motor.py

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
ControllerInterface,
33
controller_exception_handler,
44
)
5-
from lib.views.motor import MotorSummary
5+
from lib.views.motor import MotorSimulation
66
from lib.models.motor import MotorModel
77
from lib.services.motor import MotorService
88

@@ -41,21 +41,19 @@ async def get_rocketpy_motor_binary(
4141
return motor_service.get_motor_binary()
4242

4343
@controller_exception_handler
44-
async def simulate_motor(
45-
self, motor_id: str
46-
) -> MotorSummary:
44+
async def get_motor_simulation(self, motor_id: str) -> MotorSimulation:
4745
"""
4846
Simulate a rocketpy motor.
4947
5048
Args:
5149
motor_id: str
5250
5351
Returns:
54-
views.MotorSummary
52+
views.MotorSimulation
5553
5654
Raises:
5755
HTTP 404 Not Found: If the motor does not exist in the database.
5856
"""
5957
motor = await self.get_motor_by_id(motor_id)
6058
motor_service = MotorService.from_motor_model(motor)
61-
return motor_service.get_motor_summary()
59+
return motor_service.get_motor_simulation()

lib/controllers/rocket.py

Lines changed: 6 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
ControllerInterface,
33
controller_exception_handler,
44
)
5-
from lib.views.rocket import RocketSummary
5+
from lib.views.rocket import RocketSimulation
66
from lib.models.rocket import RocketModel
77
from lib.services.rocket import RocketService
88

@@ -20,9 +20,7 @@ def __init__(self):
2020
super().__init__(models=[RocketModel])
2121

2222
@controller_exception_handler
23-
async def get_rocketpy_rocket_binary(
24-
self, rocket_id: str
25-
) -> bytes:
23+
async def get_rocketpy_rocket_binary(self, rocket_id: str) -> bytes:
2624
"""
2725
Get a rocketpy.Rocket object as dill binary.
2826
@@ -40,22 +38,22 @@ async def get_rocketpy_rocket_binary(
4038
return rocket_service.get_rocket_binary()
4139

4240
@controller_exception_handler
43-
async def simulate_rocket(
41+
async def get_rocket_simulation(
4442
self,
4543
rocket_id: str,
46-
) -> RocketSummary:
44+
) -> RocketSimulation:
4745
"""
4846
Simulate a rocketpy rocket.
4947
5048
Args:
5149
rocket_id: str
5250
5351
Returns:
54-
views.RocketSummary
52+
views.RocketSimulation
5553
5654
Raises:
5755
HTTP 404 Not Found: If the rocket does not exist in the database.
5856
"""
5957
rocket = await self.get_rocket_by_id(rocket_id)
6058
rocket_service = RocketService.from_rocket_model(rocket)
61-
return rocket_service.get_rocket_summary()
59+
return rocket_service.get_rocket_simulation()

lib/models/environment.py

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,14 @@ class EnvironmentModel(ApiBaseModel):
1111
elevation: Optional[int] = 1
1212

1313
# Optional parameters
14-
atmospheric_model_type: Literal['standard_atmosphere', 'custom_atmosphere', 'wyoming_sounding', 'forecast', 'reanalysis', 'ensemble'] = 'standard_atmosphere'
14+
atmospheric_model_type: Literal[
15+
'standard_atmosphere',
16+
'custom_atmosphere',
17+
'wyoming_sounding',
18+
'forecast',
19+
'reanalysis',
20+
'ensemble',
21+
] = 'standard_atmosphere'
1522
atmospheric_model_file: Optional[str] = None
1623
date: Optional[datetime.datetime] = (
1724
datetime.datetime.today() + datetime.timedelta(days=1)

lib/models/interface.py

Lines changed: 17 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,18 @@
11
from typing import Self, Optional
22
from abc import abstractmethod, ABC
3-
from pydantic import BaseModel, PrivateAttr, ConfigDict
3+
from pydantic import (
4+
BaseModel,
5+
PrivateAttr,
6+
ConfigDict,
7+
model_validator,
8+
)
49
from bson import ObjectId
510

611

712
class ApiBaseModel(BaseModel, ABC):
813
_id: Optional[ObjectId] = PrivateAttr(default=None)
914
model_config = ConfigDict(
1015
extra="allow",
11-
json_encoders={ObjectId: str},
1216
use_enum_values=True,
1317
validate_default=True,
1418
validate_all_in_root=True,
@@ -21,6 +25,17 @@ def set_id(self, value):
2125
def get_id(self):
2226
return self._id
2327

28+
@model_validator(mode='after')
29+
def validate_computed_id(self):
30+
"""Validate _id after model instantiation"""
31+
if self._id is not None:
32+
if not isinstance(self._id, ObjectId):
33+
try:
34+
self._id = ObjectId(str(self._id))
35+
except Exception as e:
36+
raise ValueError(f"Invalid ObjectId: {e}")
37+
return self
38+
2439
@property
2540
@abstractmethod
2641
def NAME(): # pylint: disable=invalid-name, no-method-argument

lib/models/motor.py

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -48,8 +48,12 @@ class MotorModel(ApiBaseModel):
4848
throat_radius: Optional[float] = None
4949

5050
# Optional parameters
51-
interpolation_method: Literal['linear', 'spline', 'akima', 'polynomial', 'shepard', 'rbf'] = 'linear'
52-
coordinate_system_orientation: Literal['nozzle_to_combustion_chamber', 'combustion_chamber_to_nozzle'] = 'nose_to_combustion_chamber'
51+
interpolation_method: Literal[
52+
'linear', 'spline', 'akima', 'polynomial', 'shepard', 'rbf'
53+
] = 'linear'
54+
coordinate_system_orientation: Literal[
55+
'nozzle_to_combustion_chamber', 'combustion_chamber_to_nozzle'
56+
] = 'nozzle_to_combustion_chamber'
5357
reshape_thrust_curve: Union[bool, tuple] = False
5458

5559
# Computed parameters
@@ -62,7 +66,9 @@ def validate_motor_kind(self):
6266
self._motor_kind not in (MotorKinds.SOLID, MotorKinds.GENERIC)
6367
and self.tanks is None
6468
):
65-
raise ValueError("Tanks must be provided for liquid and hybrid motors.")
69+
raise ValueError(
70+
"Tanks must be provided for liquid and hybrid motors."
71+
)
6672
return self
6773

6874
@computed_field

lib/models/rocket.py

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,9 @@ class RocketModel(ApiBaseModel):
2626
] = (0, 0, 0)
2727
power_off_drag: List[Tuple[float, float]] = [(0, 0)]
2828
power_on_drag: List[Tuple[float, float]] = [(0, 0)]
29-
coordinate_system_orientation: Literal['tail_to_nose', 'nose_to_tail'] = 'tail_to_nose'
29+
coordinate_system_orientation: Literal['tail_to_nose', 'nose_to_tail'] = (
30+
'tail_to_nose'
31+
)
3032
nose: NoseCone
3133
fins: List[Fins]
3234

lib/models/sub/aerosurfaces.py

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,9 @@ class Fins(BaseModel):
3939
tip_chord: Optional[float] = None
4040
cant_angle: Optional[float] = None
4141
rocket_radius: Optional[float] = None
42-
airfoil: Optional[Tuple[List[Tuple[float, float]], Literal['radians', 'degrees']]] = None
42+
airfoil: Optional[
43+
Tuple[List[Tuple[float, float]], Literal['radians', 'degrees']]
44+
] = None
4345

4446
def get_additional_parameters(self):
4547
return {

lib/repositories/environment.py

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -22,12 +22,18 @@ async def create_environment(self, environment: EnvironmentModel) -> str:
2222
return await self.insert(environment.model_dump())
2323

2424
@repository_exception_handler
25-
async def read_environment_by_id(self, environment_id: str) -> Optional[EnvironmentModel]:
25+
async def read_environment_by_id(
26+
self, environment_id: str
27+
) -> Optional[EnvironmentModel]:
2628
return await self.find_by_id(data_id=environment_id)
2729

2830
@repository_exception_handler
29-
async def update_environment_by_id(self, environment_id: str, environment: EnvironmentModel):
30-
await self.update_by_id(environment.model_dump(), data_id=environment_id)
31+
async def update_environment_by_id(
32+
self, environment_id: str, environment: EnvironmentModel
33+
):
34+
await self.update_by_id(
35+
environment.model_dump(), data_id=environment_id
36+
)
3137

3238
@repository_exception_handler
3339
async def delete_environment_by_id(self, environment_id: str):

lib/repositories/interface.py

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -40,9 +40,12 @@ async def wrapper(self, *args, **kwargs):
4040
return await method(self, *args, **kwargs)
4141
except PyMongoError as e:
4242
logger.exception(f"{method.__name__} - caught PyMongoError: {e}")
43-
raise e from e
43+
raise
4444
except RepositoryNotInitializedException as e:
45-
raise e from e
45+
logger.exception(
46+
f"{method.__name__} - Repository not initialized: {e}"
47+
)
48+
raise
4649
except Exception as e:
4750
logger.exception(
4851
f"{method.__name__} - caught unexpected error: {e}"

lib/routes/environment.py

Lines changed: 20 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
from opentelemetry import trace
77

88
from lib.views.environment import (
9-
EnvironmentSummary,
9+
EnvironmentSimulation,
1010
EnvironmentCreated,
1111
EnvironmentRetrieved,
1212
EnvironmentUpdated,
@@ -29,7 +29,9 @@
2929

3030

3131
@router.post("/")
32-
async def create_environment(environment: EnvironmentModel) -> EnvironmentCreated:
32+
async def create_environment(
33+
environment: EnvironmentModel,
34+
) -> EnvironmentCreated:
3335
"""
3436
Creates a new environment
3537
@@ -55,7 +57,9 @@ async def read_environment(environment_id: str) -> EnvironmentRetrieved:
5557

5658

5759
@router.put("/{environment_id}")
58-
async def update_environment(environment_id: str, environment: EnvironmentModel) -> EnvironmentUpdated:
60+
async def update_environment(
61+
environment_id: str, environment: EnvironmentModel
62+
) -> EnvironmentUpdated:
5963
"""
6064
Updates an existing environment
6165
@@ -67,7 +71,9 @@ async def update_environment(environment_id: str, environment: EnvironmentModel)
6771
"""
6872
with tracer.start_as_current_span("update_becho"):
6973
controller = EnvironmentController()
70-
return await controller.put_environment_by_id(environment_id, environment)
74+
return await controller.put_environment_by_id(
75+
environment_id, environment
76+
)
7177

7278

7379
@router.delete("/{environment_id}")
@@ -107,7 +113,9 @@ async def get_rocketpy_environment_binary(environment_id: str):
107113
'Content-Disposition': f'attachment; filename="rocketpy_environment_{environment_id}.dill"'
108114
}
109115
controller = EnvironmentController()
110-
binary = await controller.get_rocketpy_environment_binary(environment_id)
116+
binary = await controller.get_rocketpy_environment_binary(
117+
environment_id
118+
)
111119
return Response(
112120
content=binary,
113121
headers=headers,
@@ -116,14 +124,16 @@ async def get_rocketpy_environment_binary(environment_id: str):
116124
)
117125

118126

119-
@router.get("/{environment_id}/summary")
120-
async def get_environment_simulation(environment_id: str) -> EnvironmentSummary:
127+
@router.get("/{environment_id}/simulate")
128+
async def get_environment_simulation(
129+
environment_id: str,
130+
) -> EnvironmentSimulation:
121131
"""
122-
Loads rocketpy.environment simulation
132+
Simulates an environment
123133
124134
## Args
125-
``` environment_id: str ```
135+
``` environment_id: Environment ID```
126136
"""
127137
with tracer.start_as_current_span("get_environment_simulation"):
128138
controller = EnvironmentController()
129-
return await controller.get_environment_summary(environment_id)
139+
return await controller.get_environment_simulation(environment_id)

0 commit comments

Comments
 (0)