2
2
import logging
3
3
4
4
from django .core .management .base import BaseCommand
5
- from django .db .models import Prefetch , Q
5
+ from django .db .models import F , Prefetch , Q
6
6
7
7
from telemetry .influx import Influx
8
8
from telemetry .models import FastLap , Lap , Session
@@ -17,68 +17,71 @@ def add_arguments(self, parser):
17
17
parser .add_argument (
18
18
"-d" ,
19
19
"--delete-influx" ,
20
- help = "delete old influx data" ,
20
+ help = "Delete old telemetry data from InfluxDB within the specified date range " ,
21
21
action = "store_true" ,
22
22
)
23
23
24
24
parser .add_argument (
25
25
"--delete-sessions" ,
26
- help = "delete sessions" ,
26
+ help = "Delete all sessions from the database (use with caution) " ,
27
27
action = "store_true" ,
28
28
)
29
29
30
30
parser .add_argument (
31
31
"-s" ,
32
32
"--start" ,
33
- help = "start date for deletion" ,
33
+ help = "Start date for deletion in YYYY-MM-DD format " ,
34
34
type = str ,
35
35
default = None ,
36
36
)
37
37
parser .add_argument (
38
38
"-e" ,
39
39
"--end" ,
40
- help = "end date for deletion" ,
40
+ help = "End date for deletion in YYYY-MM-DD format " ,
41
41
type = str ,
42
42
default = None ,
43
43
)
44
44
45
45
parser .add_argument (
46
46
"--fix-rbr-sessions" ,
47
- help = "fix data " ,
47
+ help = "Fix Richard Burns Rally sessions with incorrect end times " ,
48
48
action = "store_true" ,
49
49
)
50
50
51
51
parser .add_argument (
52
52
"--fix-fastlaps" ,
53
- help = "fix data " ,
53
+ help = "Clean up orphaned FastLap records that have no associated Laps " ,
54
54
action = "store_true" ,
55
55
)
56
56
57
57
parser .add_argument (
58
58
"--fix-fastlaps-data" ,
59
- help = "fix fastlaps data" ,
59
+ help = "Validate and clean up FastLap data structures and segments " ,
60
60
action = "store_true" ,
61
61
)
62
62
63
63
parser .add_argument (
64
64
"--dump-session" ,
65
- help = "dump session" ,
65
+ help = "Debug utility to dump session telemetry data " ,
66
66
action = "store_true" ,
67
67
)
68
68
69
69
parser .add_argument (
70
70
"--fix-cars" ,
71
- help = "check for cars with duplicate names within the same game" ,
71
+ help = "Identify and resolve duplicate car names within the same game" ,
72
72
action = "store_true" ,
73
73
)
74
74
75
75
parser .add_argument (
76
76
"--fix-session-car-track" ,
77
- help = "fix sessions missing car or track by using data from first lap" ,
77
+ help = "Repair sessions with missing car/ track info by using data from their first lap" ,
78
78
action = "store_true" ,
79
79
)
80
80
81
81
def handle (self , * args , ** options ):
82
+ """
83
+ Main command handler that routes to specific maintenance tasks based on command line arguments.
84
+ """
82
85
if options ["delete_influx" ]:
83
86
self .influx = Influx ()
84
87
self .delete_influx (options ["start" ], options ["end" ])
@@ -175,6 +178,10 @@ def fix_session_car_track(self):
175
178
print (f"Updated { processed } sessions with car/track data from their laps" )
176
179
177
180
def dump_session (self ):
181
+ """
182
+ Debug utility to dump session telemetry data.
183
+ Currently disabled - needs implementation with proper Telemetry model.
184
+ """
178
185
# TODO: Revisit this method. The Telemetry model is not defined.
179
186
# Uncomment and fix the code below once the Telemetry model is available.
180
187
# session_id = "1689266594"
@@ -242,25 +249,46 @@ def fix_fastlaps_data(self):
242
249
break
243
250
244
251
def fix_rbr_sessions (self ):
245
- # get all sessions for Richard Burns Rally
246
- sessions = Session .objects .filter (game__name = "Richard Burns Rally" )
247
- for session in sessions :
248
- # get all laps for this session
249
- laps = session .laps .all ()
250
- # iterate over all laps
251
- for lap in laps :
252
- print (f"fixing lap { lap .id } end: { lap .end } " )
253
- # set the end time of the lap to the start + the lap time
254
- lap .end = lap .start + datetime .timedelta (seconds = lap .time + 60 )
255
- print (f"--> { lap .end } " )
256
- lap .number = 0
257
- # save the lap
258
- lap .save ()
252
+ """Fix Richard Burns Rally laps where end time equals start time."""
253
+ batch_size = 100
254
+
255
+ # Get all RBR laps where end equals start
256
+ laps = Lap .objects .filter (session__game__name = "Richard Burns Rally" , end = F ("start" ))
257
+
258
+ total_laps = laps .count ()
259
+ logging .info (f"Found { total_laps } RBR laps to fix" )
260
+
261
+ processed = 0
262
+ for lap in laps .iterator (chunk_size = batch_size ):
263
+ # Set end time to start + lap time + 60 second buffer
264
+ lap .end = lap .start + datetime .timedelta (seconds = lap .time + 60 )
265
+ lap .number = 0
266
+ lap .save ()
267
+
268
+ processed += 1
269
+ if processed % batch_size == 0 :
270
+ logging .info (f"Processed { processed } /{ total_laps } laps" )
271
+
272
+ logging .info (f"Completed fixing { processed } RBR laps" )
259
273
260
274
def delete_sessions (self , start , end ):
275
+ """
276
+ Delete all sessions from the database.
277
+
278
+ Args:
279
+ start: Start date (unused)
280
+ end: End date (unused)
281
+ """
261
282
Session .objects .all ().delete ()
262
283
263
284
def delete_influx (self , start , end ):
285
+ """
286
+ Delete telemetry data from InfluxDB within a specified date range.
287
+
288
+ Args:
289
+ start (str): Start date in YYYY-MM-DD format. Defaults to 30 days ago if not specified.
290
+ end (str): End date in YYYY-MM-DD format. Defaults to start + 1 day if not specified.
291
+ """
264
292
if start :
265
293
start = datetime .datetime .strptime (start , "%Y-%m-%d" )
266
294
else :
0 commit comments