Skip to content

Commit dcbf462

Browse files
committed
404 for api
1 parent 8365c48 commit dcbf462

File tree

2 files changed

+19
-7
lines changed

2 files changed

+19
-7
lines changed

api/telemetry_loader.py

+13-3
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77

88
from telemetry.analyzer import Analyzer
99
from telemetry.influx import Influx
10-
from telemetry.models import Lap
10+
from telemetry.models import Lap, Session
1111

1212

1313
class TelemetryLoader:
@@ -72,8 +72,11 @@ def process_dataframe(self, df):
7272
def get_lap_df(self, lap_id, measurement="laps_cc", bucket="racing"):
7373
# make sure the lap_id is an integer
7474
lap_id = int(lap_id)
75-
# fetch the lap from the database
76-
lap = Lap.objects.get(id=lap_id)
75+
try:
76+
# fetch the lap from the database
77+
lap = Lap.objects.get(id=lap_id)
78+
except Lap.DoesNotExist:
79+
return None
7780

7881
influx = Influx()
7982

@@ -95,6 +98,13 @@ def get_lap_df(self, lap_id, measurement="laps_cc", bucket="racing"):
9598
def get_session_df(self, session_id, measurement="laps_cc", bucket="racing"):
9699
# make sure the session_id is an integer
97100
session_id = int(session_id)
101+
102+
# First check if session exists in database
103+
try:
104+
Session.objects.get(id=session_id)
105+
except Session.DoesNotExist:
106+
return None
107+
98108
file_path = f"{self.temp_dir}/session_{session_id}_df.csv.gz"
99109

100110
if self.caching and os.path.exists(file_path):

api/views.py

+6-4
Original file line numberDiff line numberDiff line change
@@ -13,15 +13,16 @@ class SessionView(View):
1313
def get(self, request, session_id=0, *args, **kwargs):
1414
df = self.telemetry_loader.get_session_df(session_id)
1515

16+
if df is None:
17+
return JsonResponse({"error": f"Session with ID {session_id} does not exist"}, status=404)
18+
1619
# Compression:
1720
# Compress the JSON response. Django can be configured to gzip responses, which can significantly reduce the response size.
1821
# Make sure your frontend can handle the decompression, which is typically handled automatically by modern browsers.
1922

2023
# render the dataframe as json
2124
df_json = df.to_json(orient="split", date_format="iso")
22-
2325
df_dict = json.loads(df_json)
24-
2526
return JsonResponse(df_dict, safe=False)
2627

2728

@@ -32,8 +33,9 @@ class LapView(View):
3233
def get(self, request, lap_id=0, *args, **kwargs):
3334
df = self.telemetry_loader.get_lap_df(lap_id)
3435

35-
df_json = df.to_json(orient="split", date_format="iso")
36+
if df is None:
37+
return JsonResponse({"error": f"Lap with ID {lap_id} does not exist"}, status=404)
3638

39+
df_json = df.to_json(orient="split", date_format="iso")
3740
df_dict = json.loads(df_json)
38-
3941
return JsonResponse(df_dict, safe=False)

0 commit comments

Comments
 (0)