1
1
from typing import Union
2
- from datetime import datetime
3
2
4
- import logging
5
3
import jsonpickle
6
4
from rocketpy .environment .environment import Environment as RocketPyEnvironment
7
5
from fastapi import HTTPException , status
8
6
7
+ from lib import logging
9
8
from lib .controllers import parse_error
10
9
from lib .models .environment import Env
11
10
from lib .repositories .environment import EnvRepository
19
18
EnvPickle ,
20
19
)
21
20
21
+ logger = logging .getLogger (__name__ )
22
+
22
23
23
24
class EnvController :
24
25
"""
@@ -69,23 +70,22 @@ async def create_env(self) -> "Union[EnvCreated, HTTPException]":
69
70
Returns:
70
71
views.EnvCreated
71
72
"""
72
- env_repo = EnvRepository (environment = self .env )
73
73
try :
74
- await env_repo .create_env ()
74
+ created_env = await EnvRepository (
75
+ environment = self .env
76
+ ).create_env ()
75
77
except Exception as e :
76
78
exc_str = parse_error (e )
77
- logging .error (
78
- f"[{ datetime .now ()} ] controllers.environment.create_env: { exc_str } "
79
- )
79
+ logger .error (f"controllers.environment.create_env: { exc_str } " )
80
80
raise HTTPException (
81
81
status_code = status .HTTP_500_INTERNAL_SERVER_ERROR ,
82
82
detail = f"Failed to create environment: { e } " ,
83
83
) from e
84
84
else :
85
- return EnvCreated (env_id = env_repo .env_id )
85
+ return EnvCreated (env_id = created_env .env_id )
86
86
finally :
87
- logging .info (
88
- f"[ { datetime . now () } ] Call to controllers.environment.create_env completed; params: Env { hash (self .env )} "
87
+ logger .info (
88
+ f"Call to controllers.environment.create_env completed; params: Env { hash (self .env )} "
89
89
)
90
90
91
91
@staticmethod
@@ -102,14 +102,11 @@ async def get_env_by_id(env_id: int) -> "Union[Env, HTTPException]":
102
102
Raises:
103
103
HTTP 404 Not Found: If the env is not found in the database.
104
104
"""
105
- env_repo = EnvRepository (env_id = env_id )
106
105
try :
107
- read_env = await env_repo .get_env ()
106
+ read_env = await EnvRepository ( env_id = env_id ) .get_env ()
108
107
except Exception as e :
109
108
exc_str = parse_error (e )
110
- logging .error (
111
- f"[{ datetime .now ()} ] controllers.environment.get_env_by_id: { exc_str } "
112
- )
109
+ logger .error (f"controllers.environment.get_env_by_id: { exc_str } " )
113
110
raise HTTPException (
114
111
status_code = status .HTTP_500_INTERNAL_SERVER_ERROR ,
115
112
detail = f"Failed to read environment: { e } " ,
@@ -122,8 +119,8 @@ async def get_env_by_id(env_id: int) -> "Union[Env, HTTPException]":
122
119
detail = "Environment not found" ,
123
120
)
124
121
finally :
125
- logging .info (
126
- f"[ { datetime . now () } ] Call to controllers.environment.get_env_by_id completed; params: EnvID { env_id } "
122
+ logger .info (
123
+ f"Call to controllers.environment.get_env_by_id completed; params: EnvID { env_id } "
127
124
)
128
125
129
126
@classmethod
@@ -143,31 +140,27 @@ async def get_rocketpy_env_as_jsonpickle(
143
140
Raises:
144
141
HTTP 404 Not Found: If the env is not found in the database.
145
142
"""
146
- env_repo = EnvRepository (env_id = env_id )
147
143
try :
148
- read_env = await env_repo .get_env ()
144
+ read_env = await cls .get_env_by_id (env_id )
145
+ except HTTPException as e :
146
+ raise e from e
149
147
except Exception as e :
150
148
exc_str = parse_error (e )
151
- logging .error (
152
- f"[ { datetime . now () } ] controllers.environment.get_rocketpy_env_as_jsonpickle: { exc_str } "
149
+ logger .error (
150
+ f"controllers.environment.get_rocketpy_env_as_jsonpickle: { exc_str } "
153
151
)
154
152
raise HTTPException (
155
153
status_code = status .HTTP_500_INTERNAL_SERVER_ERROR ,
156
154
detail = f"Failed to read environment: { e } " ,
157
155
) from e
158
156
else :
159
- if read_env :
160
- rocketpy_env = cls .get_rocketpy_env (read_env )
161
- return EnvPickle (
162
- jsonpickle_rocketpy_env = jsonpickle .encode (rocketpy_env )
163
- )
164
- raise HTTPException (
165
- status_code = status .HTTP_404_NOT_FOUND ,
166
- detail = "Environment not found" ,
157
+ rocketpy_env = await cls .get_rocketpy_env (read_env )
158
+ return EnvPickle (
159
+ jsonpickle_rocketpy_env = jsonpickle .encode (rocketpy_env )
167
160
)
168
161
finally :
169
- logging .info (
170
- f"[ { datetime . now () } ] Call to controllers.environment.get_rocketpy_env_as_jsonpickle completed; params: EnvID { env_id } "
162
+ logger .info (
163
+ f"Call to controllers.environment.get_rocketpy_env_as_jsonpickle completed; params: EnvID { env_id } "
171
164
)
172
165
173
166
async def update_env (
@@ -185,30 +178,25 @@ async def update_env(
185
178
Raises:
186
179
HTTP 404 Not Found: If the env is not found in the database.
187
180
"""
188
- env_repo = EnvRepository (environment = self .env , env_id = env_id )
189
181
try :
190
- read_env = await env_repo .get_env ()
191
- if read_env :
192
- await env_repo .update_env ()
193
- else :
194
- raise HTTPException (
195
- status_code = status .HTTP_404_NOT_FOUND ,
196
- detail = "Environment not found" ,
197
- )
182
+ await EnvController .get_env_by_id (env_id )
183
+ updated_env = await EnvRepository (
184
+ environment = self .env , env_id = env_id
185
+ ).update_env ()
186
+ except HTTPException as e :
187
+ raise e from e
198
188
except Exception as e :
199
189
exc_str = parse_error (e )
200
- logging .error (
201
- f"[{ datetime .now ()} ] controllers.environment.update_env: { exc_str } "
202
- )
190
+ logger .error (f"controllers.environment.update_env: { exc_str } " )
203
191
raise HTTPException (
204
192
status_code = status .HTTP_500_INTERNAL_SERVER_ERROR ,
205
193
detail = f"Failed to update environment: { e } " ,
206
194
) from e
207
195
else :
208
- return EnvUpdated (new_env_id = env_repo .env_id )
196
+ return EnvUpdated (new_env_id = updated_env .env_id )
209
197
finally :
210
- logging .info (
211
- f"[ { datetime . now () } ] Call to controllers.environment.update_env completed; params: EnvID { env_id } , Env { hash (self .env )} "
198
+ logger .info (
199
+ f"Call to controllers.environment.update_env completed; params: EnvID { env_id } , Env { hash (self .env )} "
212
200
)
213
201
214
202
@staticmethod
@@ -225,23 +213,20 @@ async def delete_env(env_id: str) -> "Union[EnvDeleted, HTTPException]":
225
213
Raises:
226
214
HTTP 404 Not Found: If the env is not found in the database.
227
215
"""
228
- env_repo = EnvRepository (env_id = env_id )
229
216
try :
230
- await env_repo .delete_env ()
217
+ await EnvRepository ( env_id = env_id ) .delete_env ()
231
218
except Exception as e :
232
219
exc_str = parse_error (e )
233
- logging .error (
234
- f"[{ datetime .now ()} ] controllers.environment.delete_env: { exc_str } "
235
- )
220
+ logger .error (f"controllers.environment.delete_env: { exc_str } " )
236
221
raise HTTPException (
237
222
status_code = status .HTTP_500_INTERNAL_SERVER_ERROR ,
238
223
detail = f"Failed to delete environment: { e } " ,
239
224
) from e
240
225
else :
241
226
return EnvDeleted (deleted_env_id = env_id )
242
227
finally :
243
- logging .info (
244
- f"[ { datetime . now () } ] Call to controllers.environment.delete_env completed; params: EnvID { env_id } "
228
+ logger .info (
229
+ f"Call to controllers.environment.delete_env completed; params: EnvID { env_id } "
245
230
)
246
231
247
232
@classmethod
@@ -258,9 +243,9 @@ async def simulate(cls, env_id: int) -> "Union[EnvSummary, HTTPException]":
258
243
Raises:
259
244
HTTP 404 Not Found: If the env does not exist in the database.
260
245
"""
261
- read_env = await cls .get_env_by_id (env_id )
262
246
try :
263
- rocketpy_env = cls .get_rocketpy_env (read_env )
247
+ read_env = await cls .get_env_by_id (env_id )
248
+ rocketpy_env = await cls .get_rocketpy_env (read_env )
264
249
env_simulation_numbers = EnvData .parse_obj (
265
250
rocketpy_env .all_info_returned ()
266
251
)
@@ -270,18 +255,18 @@ async def simulate(cls, env_id: int) -> "Union[EnvSummary, HTTPException]":
270
255
env_summary = EnvSummary (
271
256
env_data = env_simulation_numbers , env_plots = env_simulation_plots
272
257
)
258
+ except HTTPException as e :
259
+ raise e from e
273
260
except Exception as e :
274
261
exc_str = parse_error (e )
275
- logging .error (
276
- f"[{ datetime .now ()} ] controllers.environment.simulate: { exc_str } "
277
- )
262
+ logger .error (f"controllers.environment.simulate: { exc_str } " )
278
263
raise HTTPException (
279
264
status_code = status .HTTP_500_INTERNAL_SERVER_ERROR ,
280
265
detail = f"Failed to simulate environment: { e } " ,
281
266
) from e
282
267
else :
283
268
return env_summary
284
269
finally :
285
- logging .info (
286
- f"[ { datetime . now () } ] Call to controllers.environment.simulate completed; params: EnvID { env_id } "
270
+ logger .info (
271
+ f"Call to controllers.environment.simulate completed; params: EnvID { env_id } "
287
272
)
0 commit comments