-
-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathflight.py
More file actions
316 lines (271 loc) · 8 KB
/
flight.py
File metadata and controls
316 lines (271 loc) · 8 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
"""
Flight routes with dependency injection for improved performance.
"""
import json
from fastapi import (
APIRouter,
File,
HTTPException,
Response,
UploadFile,
status,
)
from opentelemetry import trace
from src.views.flight import (
FlightSimulation,
FlightCreated,
FlightRetrieved,
FlightImported,
)
from src.models.environment import EnvironmentModel
from src.models.flight import FlightModel, FlightWithReferencesRequest
from src.models.rocket import RocketModel
from src.dependencies import FlightControllerDep
router = APIRouter(
prefix="/flights",
tags=["FLIGHT"],
responses={
404: {"description": "Not found"},
422: {"description": "Unprocessable Entity"},
500: {"description": "Internal Server Error"},
},
)
tracer = trace.get_tracer(__name__)
MAX_RPY_UPLOAD_BYTES = 10 * 1024 * 1024 # 10 MB
@router.post("/", status_code=201)
async def create_flight(
flight: FlightModel,
controller: FlightControllerDep,
) -> FlightCreated:
"""
Creates a new flight
## Args
``` models.Flight JSON ```
"""
with tracer.start_as_current_span("create_flight"):
return await controller.post_flight(flight)
@router.post("/from-references", status_code=201)
async def create_flight_from_references(
payload: FlightWithReferencesRequest,
controller: FlightControllerDep,
) -> FlightCreated:
"""
Creates a flight using existing rocket and environment references.
## Args
```
environment_id: str
rocket_id: str
flight: Flight-only fields JSON
```
"""
with tracer.start_as_current_span("create_flight_from_references"):
return await controller.create_flight_from_references(payload)
@router.get("/{flight_id}")
async def read_flight(
flight_id: str,
controller: FlightControllerDep,
) -> FlightRetrieved:
"""
Reads an existing flight
## Args
``` flight_id: str ```
"""
with tracer.start_as_current_span("read_flight"):
return await controller.get_flight_by_id(flight_id)
@router.put("/{flight_id}", status_code=204)
async def update_flight(
flight_id: str,
flight: FlightModel,
controller: FlightControllerDep,
) -> None:
"""
Updates an existing flight
## Args
```
flight_id: str
flight: models.flight JSON
```
"""
with tracer.start_as_current_span("update_flight"):
return await controller.put_flight_by_id(flight_id, flight)
@router.put("/{flight_id}/from-references", status_code=204)
async def update_flight_from_references(
flight_id: str,
payload: FlightWithReferencesRequest,
controller: FlightControllerDep,
) -> None:
"""
Updates a flight using existing rocket and environment references.
## Args
```
flight_id: str
environment_id: str
rocket_id: str
flight: Flight-only fields JSON
```
"""
with tracer.start_as_current_span("update_flight_from_references"):
return await controller.update_flight_from_references(
flight_id, payload
)
@router.delete("/{flight_id}", status_code=204)
async def delete_flight(
flight_id: str,
controller: FlightControllerDep,
) -> None:
"""
Deletes an existing flight
## Args
``` flight_id: str ```
"""
with tracer.start_as_current_span("delete_flight"):
return await controller.delete_flight_by_id(flight_id)
@router.get(
"/{flight_id}/rocketpy",
responses={
200: {
"description": "Portable .rpy JSON file download",
"content": {"application/json": {}},
}
},
status_code=200,
response_class=Response,
)
async def get_rocketpy_flight_rpy(
flight_id: str,
controller: FlightControllerDep,
):
"""
Export a rocketpy Flight as a portable ``.rpy`` JSON file.
The ``.rpy`` format is architecture-, OS-, and
Python-version-agnostic.
## Args
``` flight_id: str ```
"""
with tracer.start_as_current_span("get_rocketpy_flight_rpy"):
headers = {
'Content-Disposition': (
'attachment; filename=' f'"rocketpy_flight_{flight_id}.rpy"'
),
}
rpy = await controller.get_rocketpy_flight_rpy(flight_id)
return Response(
content=rpy,
headers=headers,
media_type="application/json",
status_code=200,
)
@router.post(
"/upload",
status_code=201,
responses={
201: {"description": "Flight imported from .rpy file"},
413: {"description": "Uploaded .rpy file exceeds size limit"},
422: {"description": "Invalid .rpy file"},
},
)
async def import_flight_from_rpy(
file: UploadFile = File(...),
controller: FlightControllerDep = None, # noqa: B008
) -> FlightImported:
"""
Upload a ``.rpy`` JSON file containing a RocketPy Flight.
The file is deserialized and decomposed into its
constituent objects (Environment, Motor, Rocket, Flight).
Each object is persisted as a normal JSON model and the
corresponding IDs are returned. Maximum upload size is 10 MB.
## Args
``` file: .rpy JSON upload ```
"""
with tracer.start_as_current_span("import_flight_from_rpy"):
content = await file.read(MAX_RPY_UPLOAD_BYTES + 1)
if len(content) > MAX_RPY_UPLOAD_BYTES:
raise HTTPException(
status_code=status.HTTP_413_REQUEST_ENTITY_TOO_LARGE,
detail="Uploaded .rpy file exceeds 10 MB limit.",
)
return await controller.import_flight_from_rpy(content)
@router.get(
"/{flight_id}/notebook",
responses={
200: {
"description": "Jupyter notebook file download",
"content": {"application/x-ipynb+json": {}},
}
},
status_code=200,
response_class=Response,
)
async def get_flight_notebook(
flight_id: str,
controller: FlightControllerDep,
):
"""
Export a flight as a Jupyter notebook (.ipynb).
The notebook loads the flight's ``.rpy`` file and calls
``flight.all_info()`` for interactive exploration.
## Args
``` flight_id: str ```
"""
with tracer.start_as_current_span("get_flight_notebook"):
notebook = await controller.get_flight_notebook(flight_id)
content = json.dumps(notebook, indent=1).encode()
filename = f"flight_{flight_id}.ipynb"
headers = {
"Content-Disposition": (f'attachment; filename="{filename}"'),
}
return Response(
content=content,
headers=headers,
media_type="application/x-ipynb+json",
status_code=200,
)
@router.put("/{flight_id}/environment", status_code=204)
async def update_flight_environment(
flight_id: str,
environment: EnvironmentModel,
controller: FlightControllerDep,
) -> None:
"""
Updates flight environment
## Args
```
flight_id: Flight ID
environment: env object as JSON
```
"""
with tracer.start_as_current_span("update_flight_environment"):
return await controller.update_environment_by_flight_id(
flight_id, environment=environment
)
@router.put("/{flight_id}/rocket", status_code=204)
async def update_flight_rocket(
flight_id: str,
rocket: RocketModel,
controller: FlightControllerDep,
) -> None:
"""
Updates flight rocket.
## Args
```
flight_id: Flight ID
rocket: RocketModel object as JSON
```
"""
with tracer.start_as_current_span("update_flight_rocket"):
return await controller.update_rocket_by_flight_id(
flight_id,
rocket=rocket,
)
@router.get("/{flight_id}/simulate")
async def get_flight_simulation(
flight_id: str,
controller: FlightControllerDep,
) -> FlightSimulation:
"""
Simulates a flight
## Args
``` flight_id: Flight ID ```
"""
with tracer.start_as_current_span("get_flight_simulation"):
return await controller.get_flight_simulation(flight_id)