diff --git a/VERSION.txt b/VERSION.txt index 7c3272873..8f9174b4d 100644 --- a/VERSION.txt +++ b/VERSION.txt @@ -1 +1 @@ -2.1.1 \ No newline at end of file +2.1.2 \ No newline at end of file diff --git a/docs/changelog.rst b/docs/changelog.rst index bb36ffd09..811f53c8f 100644 --- a/docs/changelog.rst +++ b/docs/changelog.rst @@ -3,6 +3,15 @@ Changelog ========= +v2.1.2 Hotfixes for loading live timing data +============================================ + +- Fix failure to load live timing data due to an error in the + api cache function wrapper. + +- Improve track status loading + + v2.1.1 Add support for recording and using live timing data =========================================================== diff --git a/fastf1/api.py b/fastf1/api.py index 61592ad21..b4e985046 100644 --- a/fastf1/api.py +++ b/fastf1/api.py @@ -160,7 +160,7 @@ def cached_api_request(api_path, response=None, livedata=None): else: # created with different version or force renew --> download again and update logging.info(f"Updating cache for {str(func.__name__)}...") - data = func(api_path, response=None, livedata=None) + data = func(api_path, response=response, livedata=livedata) if data is not None: new_cached = {'version': cls._API_CORE_VERSION, 'data': data} pickle.dump(new_cached, open(cache_file_path, 'wb')) @@ -172,7 +172,7 @@ def cached_api_request(api_path, response=None, livedata=None): else: # cached data does not yet exist for this request, try to download and create cache logging.info("No cached data found. Downloading...") - data = func(api_path, response=None, livedata=None) + data = func(api_path, response=response, livedata=livedata) if data is not None: new_cached = {'version': cls._API_CORE_VERSION, 'data': data} pickle.dump(new_cached, open(cache_file_path, 'wb')) @@ -190,7 +190,7 @@ def cached_api_request(api_path, response=None, livedata=None): cls._has_been_warned = True - return func(api_path, response=None, livedata=None) + return func(api_path, response=response, livedata=livedata) wrapped = cached_api_request wrapped.__doc__ = func.__doc__ # necessary to make docstrings work diff --git a/fastf1/livetiming/data.py b/fastf1/livetiming/data.py index 927160c14..ceace62b2 100644 --- a/fastf1/livetiming/data.py +++ b/fastf1/livetiming/data.py @@ -10,6 +10,16 @@ from fastf1.utils import to_datetime, recursive_dict_get +_track_status_mapping = { + 'AllClear': '1', + 'Yellow': '2', + 'SCDeployed': '4', + 'Red': '5', + 'VSCDeployed': '6', + 'VSCEnding': '7' +} + + class LiveTimingData: """Live timing data object for using saved livetiming data as data source. @@ -126,23 +136,25 @@ def _parse_session_data(self, msg): if 'SessionStatus' not in self.data.keys(): self.data['SessionStatus'] = {'Time': [], 'Status': []} - for entry in msg['StatusSeries']: - if isinstance(entry, str): - continue # TODO sometimes weird entries? - - # convert timestamp to timedelta - status_dt = to_datetime(entry['Utc']) - status_timedelta = status_dt - self._start_date - - # add data to category - if 'TrackStatus' in entry.keys(): - self.data['TrackStatus']['Time'].append(status_timedelta) - self.data['TrackStatus']['Status'].append(entry['TrackStatus']) - self.data['TrackStatus']['Message'].append("") - - elif 'SessionStatus' in entry.keys(): - self.data['SessionStatus']['Time'].append(status_timedelta) - self.data['SessionStatus']['Status'].append(entry['SessionStatus']) + if isinstance(msg['StatusSeries'], dict): + for entry in msg['StatusSeries'].values(): + # convert timestamp to timedelta + status_dt = to_datetime(entry['Utc']) + status_timedelta = status_dt - self._start_date + + # add data to category + if 'TrackStatus' in entry.keys(): + status_value = str(entry['TrackStatus']) + # convert to numeric system used by the api + if not status_value.isnumeric(): + status_value = _track_status_mapping[status_value] + self.data['TrackStatus']['Time'].append(status_timedelta) + self.data['TrackStatus']['Status'].append(status_value) + self.data['TrackStatus']['Message'].append("") + + elif 'SessionStatus' in entry.keys(): + self.data['SessionStatus']['Time'].append(status_timedelta) + self.data['SessionStatus']['Status'].append(entry['SessionStatus']) def _try_set_correct_start_date(self, data): # skim content to find 'Started' session status without actually @@ -166,10 +178,16 @@ def _try_set_correct_start_date(self, data): return # find correct entry in series - for entry in msg['StatusSeries']: - status = recursive_dict_get(entry, 'SessionStatus') - if status == 'Started': - self._start_date = to_datetime(entry['Utc']) + try: + for entry in msg['StatusSeries']: + status = recursive_dict_get(entry, 'SessionStatus') + if status == 'Started': + self._start_date = to_datetime(entry['Utc']) + except AttributeError: + for entry in msg['StatusSeries'].values(): + status = entry.get('SessionStatus', None) + if status == 'Started': + self._start_date = to_datetime(entry['Utc']) def get(self, name): """