Draichi commited on
Commit
6eb3b0c
1 Parent(s): 265fb29

fix(formula1_databases): session_id and datetime +1 minute diff

Browse files
Files changed (1) hide show
  1. notebooks/formula1_databases.py +10 -11
notebooks/formula1_databases.py CHANGED
@@ -206,6 +206,7 @@ class FastF1ToSQL:
206
  placeholders = ':' + ', :'.join(session_data.keys())
207
  query = f"INSERT INTO Sessions ({columns}) VALUES ({placeholders})"
208
  self.cursor.execute(query, session_data)
 
209
 
210
  def insert_drivers(self, session: Session) -> None:
211
  """
@@ -233,8 +234,7 @@ class FastF1ToSQL:
233
  session (Session): The FastF1 session object.
234
  """
235
  laps_df = session.laps.copy()
236
- # Assuming this is called right after insert_session
237
- laps_df['session_id'] = self.cursor.lastrowid
238
  laps_df['lap_start_time_in_datetime'] = pd.to_datetime(
239
  laps_df['LapStartDate'])
240
  laps_df['pin_in_time_in_datetime'] = self._session_start_date + \
@@ -317,8 +317,7 @@ class FastF1ToSQL:
317
  session (Session): The FastF1 session containing weather data.
318
  """
319
  weather_data = cast(pd.DataFrame, session.weather_data)
320
- # Assuming this is called right after insert_session
321
- weather_data['session_id'] = self.cursor.lastrowid
322
 
323
  weather_data['datetime'] = self._session_start_date + \
324
  weather_data['Time']
@@ -376,11 +375,11 @@ class FastF1ToSQL:
376
  laps = session.laps.pick_driver(driver)
377
  lap = laps.loc[laps['LapStartTime'] <= time].iloc[-1]
378
 
379
- if self.cursor.lastrowid is None:
380
  raise ValueError("No ID was generated")
381
 
382
  self.cursor.execute("SELECT lap_id FROM Laps WHERE session_id = ? AND driver_name = ? AND lap_number = ?",
383
- (self.cursor.lastrowid, driver, lap['LapNumber']))
384
  return self.cursor.fetchone()[0]
385
 
386
  def create_data_analysis_views(self) -> None:
@@ -410,7 +409,7 @@ class FastF1ToSQL:
410
  JOIN Tracks t ON s.track_id = t.track_id
411
  JOIN Event e ON s.event_id = e.event_id
412
  LEFT JOIN Weather w ON s.session_id = w.session_id
413
- AND l.lap_start_time_in_datetime BETWEEN w.datetime AND datetime(w.datetime, '+5 minutes')
414
  GROUP BY l.driver_name, e.event_id, s.session_id;
415
 
416
  -- 2. Tyre Performance Analysis with Weather
@@ -433,7 +432,7 @@ class FastF1ToSQL:
433
  JOIN Tracks t ON s.track_id = t.track_id
434
  JOIN Event e ON s.event_id = e.event_id
435
  LEFT JOIN Weather w ON s.session_id = w.session_id
436
- AND l.lap_start_time_in_datetime BETWEEN w.datetime AND datetime(w.datetime, '+5 minutes')
437
  GROUP BY l.driver_name, e.event_id, s.session_id, l.tyre_compound;
438
 
439
  -- 3. Weather Impact Analysis
@@ -454,7 +453,7 @@ class FastF1ToSQL:
454
  JOIN Tracks t ON s.track_id = t.track_id
455
  JOIN Event e ON s.event_id = e.event_id
456
  JOIN Laps l ON s.session_id = l.session_id
457
- AND l.lap_start_time_in_datetime BETWEEN w.datetime AND datetime(w.datetime, '+5 minutes')
458
  GROUP BY e.event_id, s.session_id;
459
 
460
  -- 4. Event Performance Overview
@@ -475,7 +474,7 @@ class FastF1ToSQL:
475
  JOIN Sessions s ON e.event_id = s.event_id
476
  JOIN Laps l ON s.session_id = l.session_id
477
  LEFT JOIN Weather w ON s.session_id = w.session_id
478
- AND l.lap_start_time_in_datetime BETWEEN w.datetime AND datetime(w.datetime, '+5 minutes')
479
  GROUP BY e.event_id, s.session_id;
480
 
481
  -- 5. Telemetry Analysis with Weather
@@ -505,7 +504,7 @@ class FastF1ToSQL:
505
  JOIN Event e ON s.event_id = e.event_id
506
  JOIN Telemetry tel ON l.lap_id = tel.lap_id
507
  LEFT JOIN Weather w ON s.session_id = w.session_id
508
- AND tel.datetime BETWEEN w.datetime AND datetime(w.datetime, '+5 minutes')
509
  GROUP BY l.lap_id;
510
  ''')
511
  self.conn.commit()
 
206
  placeholders = ':' + ', :'.join(session_data.keys())
207
  query = f"INSERT INTO Sessions ({columns}) VALUES ({placeholders})"
208
  self.cursor.execute(query, session_data)
209
+ self._session_id = self.cursor.lastrowid
210
 
211
  def insert_drivers(self, session: Session) -> None:
212
  """
 
234
  session (Session): The FastF1 session object.
235
  """
236
  laps_df = session.laps.copy()
237
+ laps_df['session_id'] = self._session_id
 
238
  laps_df['lap_start_time_in_datetime'] = pd.to_datetime(
239
  laps_df['LapStartDate'])
240
  laps_df['pin_in_time_in_datetime'] = self._session_start_date + \
 
317
  session (Session): The FastF1 session containing weather data.
318
  """
319
  weather_data = cast(pd.DataFrame, session.weather_data)
320
+ weather_data['session_id'] = self._session_id
 
321
 
322
  weather_data['datetime'] = self._session_start_date + \
323
  weather_data['Time']
 
375
  laps = session.laps.pick_driver(driver)
376
  lap = laps.loc[laps['LapStartTime'] <= time].iloc[-1]
377
 
378
+ if self._session_id is None:
379
  raise ValueError("No ID was generated")
380
 
381
  self.cursor.execute("SELECT lap_id FROM Laps WHERE session_id = ? AND driver_name = ? AND lap_number = ?",
382
+ (self._session_id, driver, lap['LapNumber']))
383
  return self.cursor.fetchone()[0]
384
 
385
  def create_data_analysis_views(self) -> None:
 
409
  JOIN Tracks t ON s.track_id = t.track_id
410
  JOIN Event e ON s.event_id = e.event_id
411
  LEFT JOIN Weather w ON s.session_id = w.session_id
412
+ AND l.lap_start_time_in_datetime BETWEEN w.datetime AND datetime(w.datetime, '+1 minutes')
413
  GROUP BY l.driver_name, e.event_id, s.session_id;
414
 
415
  -- 2. Tyre Performance Analysis with Weather
 
432
  JOIN Tracks t ON s.track_id = t.track_id
433
  JOIN Event e ON s.event_id = e.event_id
434
  LEFT JOIN Weather w ON s.session_id = w.session_id
435
+ AND l.lap_start_time_in_datetime BETWEEN w.datetime AND datetime(w.datetime, '+1 minutes')
436
  GROUP BY l.driver_name, e.event_id, s.session_id, l.tyre_compound;
437
 
438
  -- 3. Weather Impact Analysis
 
453
  JOIN Tracks t ON s.track_id = t.track_id
454
  JOIN Event e ON s.event_id = e.event_id
455
  JOIN Laps l ON s.session_id = l.session_id
456
+ AND l.lap_start_time_in_datetime BETWEEN w.datetime AND datetime(w.datetime, '+1 minutes')
457
  GROUP BY e.event_id, s.session_id;
458
 
459
  -- 4. Event Performance Overview
 
474
  JOIN Sessions s ON e.event_id = s.event_id
475
  JOIN Laps l ON s.session_id = l.session_id
476
  LEFT JOIN Weather w ON s.session_id = w.session_id
477
+ AND l.lap_start_time_in_datetime BETWEEN w.datetime AND datetime(w.datetime, '+1 minutes')
478
  GROUP BY e.event_id, s.session_id;
479
 
480
  -- 5. Telemetry Analysis with Weather
 
504
  JOIN Event e ON s.event_id = e.event_id
505
  JOIN Telemetry tel ON l.lap_id = tel.lap_id
506
  LEFT JOIN Weather w ON s.session_id = w.session_id
507
+ AND tel.datetime BETWEEN w.datetime AND datetime(w.datetime, '+1 minutes')
508
  GROUP BY l.lap_id;
509
  ''')
510
  self.conn.commit()