Skip to content

Commit d7a75b5

Browse files
committed
maintenance script cleanup
1 parent 1754f47 commit d7a75b5

File tree

1 file changed

+53
-25
lines changed

1 file changed

+53
-25
lines changed

telemetry/management/commands/maintenance.py

+53-25
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
import logging
33

44
from django.core.management.base import BaseCommand
5-
from django.db.models import Prefetch, Q
5+
from django.db.models import F, Prefetch, Q
66

77
from telemetry.influx import Influx
88
from telemetry.models import FastLap, Lap, Session
@@ -17,68 +17,71 @@ def add_arguments(self, parser):
1717
parser.add_argument(
1818
"-d",
1919
"--delete-influx",
20-
help="delete old influx data",
20+
help="Delete old telemetry data from InfluxDB within the specified date range",
2121
action="store_true",
2222
)
2323

2424
parser.add_argument(
2525
"--delete-sessions",
26-
help="delete sessions",
26+
help="Delete all sessions from the database (use with caution)",
2727
action="store_true",
2828
)
2929

3030
parser.add_argument(
3131
"-s",
3232
"--start",
33-
help="start date for deletion",
33+
help="Start date for deletion in YYYY-MM-DD format",
3434
type=str,
3535
default=None,
3636
)
3737
parser.add_argument(
3838
"-e",
3939
"--end",
40-
help="end date for deletion",
40+
help="End date for deletion in YYYY-MM-DD format",
4141
type=str,
4242
default=None,
4343
)
4444

4545
parser.add_argument(
4646
"--fix-rbr-sessions",
47-
help="fix data",
47+
help="Fix Richard Burns Rally sessions with incorrect end times",
4848
action="store_true",
4949
)
5050

5151
parser.add_argument(
5252
"--fix-fastlaps",
53-
help="fix data",
53+
help="Clean up orphaned FastLap records that have no associated Laps",
5454
action="store_true",
5555
)
5656

5757
parser.add_argument(
5858
"--fix-fastlaps-data",
59-
help="fix fastlaps data",
59+
help="Validate and clean up FastLap data structures and segments",
6060
action="store_true",
6161
)
6262

6363
parser.add_argument(
6464
"--dump-session",
65-
help="dump session",
65+
help="Debug utility to dump session telemetry data",
6666
action="store_true",
6767
)
6868

6969
parser.add_argument(
7070
"--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",
7272
action="store_true",
7373
)
7474

7575
parser.add_argument(
7676
"--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",
7878
action="store_true",
7979
)
8080

8181
def handle(self, *args, **options):
82+
"""
83+
Main command handler that routes to specific maintenance tasks based on command line arguments.
84+
"""
8285
if options["delete_influx"]:
8386
self.influx = Influx()
8487
self.delete_influx(options["start"], options["end"])
@@ -175,6 +178,10 @@ def fix_session_car_track(self):
175178
print(f"Updated {processed} sessions with car/track data from their laps")
176179

177180
def dump_session(self):
181+
"""
182+
Debug utility to dump session telemetry data.
183+
Currently disabled - needs implementation with proper Telemetry model.
184+
"""
178185
# TODO: Revisit this method. The Telemetry model is not defined.
179186
# Uncomment and fix the code below once the Telemetry model is available.
180187
# session_id = "1689266594"
@@ -242,25 +249,46 @@ def fix_fastlaps_data(self):
242249
break
243250

244251
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")
259273

260274
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+
"""
261282
Session.objects.all().delete()
262283

263284
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+
"""
264292
if start:
265293
start = datetime.datetime.strptime(start, "%Y-%m-%d")
266294
else:

0 commit comments

Comments
 (0)