Skip to content

Commit

Permalink
Strava object caching has never worked
Browse files Browse the repository at this point in the history
  • Loading branch information
merlinorg committed Feb 11, 2025
1 parent 5979a97 commit 2151f8c
Show file tree
Hide file tree
Showing 3 changed files with 32 additions and 25 deletions.
19 changes: 12 additions & 7 deletions freezing/sync/data/activity.py
Original file line number Diff line number Diff line change
Expand Up @@ -57,8 +57,8 @@ def update_ride_basic(self, strava_activity: Activity, ride: Ride):

ride.average_speed = float(unithelper.mph(strava_activity.average_speed))
ride.maximum_speed = float(unithelper.mph(strava_activity.max_speed))
ride.elapsed_time = strava_activity.elapsed_time.seconds
ride.moving_time = strava_activity.moving_time.seconds
ride.elapsed_time = strava_activity.elapsed_time.total_seconds()
ride.moving_time = strava_activity.moving_time.total_seconds()

location_parts = []
if strava_activity.location_city:
Expand Down Expand Up @@ -117,7 +117,7 @@ def write_ride_efforts(self, strava_activity: Activity, ride: Ride):
effort = RideEffort(
id=se.id,
ride_id=strava_activity.id,
elapsed_time=se.elapsed_time.seconds,
elapsed_time=se.elapsed_time.total_seconds(),
segment_name=se.segment.name,
segment_id=se.segment.id,
)
Expand Down Expand Up @@ -621,10 +621,15 @@ def overlaps_larger(activity, activities):
and (
a.start_date + _overlap_ignore
<= activity.start_date
+ timedelta(seconds=activity.elapsed_time.seconds)
+ timedelta(
seconds=activity.elapsed_time.seconds
) # wrong, should be just elapsed_time
)
and (
a.start_date + timedelta(seconds=a.elapsed_time.seconds)
a.start_date
+ timedelta(
seconds=a.elapsed_time.seconds
) # wrong, should be just elapsed_time
>= activity.start_date + _overlap_ignore
)
]
Expand Down Expand Up @@ -715,8 +720,8 @@ def write_ride(self, activity: Activity) -> Ride:

else:
# If ride has been cropped, we re-fetch it.
if round(ride.distance, 2) != round(
float(unithelper.miles(activity.distance)), 2
if round(ride.distance, 3) != round(
float(unithelper.miles(activity.distance)), 3
):
self.logger.info(
"Queing resync of details for activity {0!r}: "
Expand Down
10 changes: 9 additions & 1 deletion freezing/sync/data/streams.py
Original file line number Diff line number Diff line change
Expand Up @@ -58,10 +58,18 @@ def sync_streams(
sf = CachingStreamFetcher(
cache_basedir=config.STRAVA_ACTIVITY_CACHE_DIR, client=client
)

# Bypass the cache if we appear to be trying to refetch the ride because of change.
# This is a different bypass cache behaviour to efforts, but the code is opaque and
# effects uncertain. This field is set to false when a ride is resynced because its
# distance has changed. So good to avoid cache in that case. However it's also set
# to false in other cases maybe probably.
bypass_cache = not ride.track_fetched

streams = sf.fetch(
athlete_id=ride.athlete_id,
object_id=ride.id,
use_cache=use_cache,
use_cache=use_cache and not bypass_cache,
only_cache=only_cache,
)
if streams:
Expand Down
28 changes: 11 additions & 17 deletions freezing/sync/utils/cache.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,8 +24,8 @@ def __init__(self, cache_basedir: str, client: Client):
self.cache_basedir = cache_basedir
self.client = client

def filename(self, *, athlete_id: int, object_id: int):
return "{}.json".format(athlete_id)
def filename(self, *, object_id: int):
return "{}_{}.json".format(object_id, self.object_type)

def cache_dir(self, athlete_id: int) -> str:
"""
Expand All @@ -48,25 +48,22 @@ def cache_object_json(
"""
directory = self.cache_dir(athlete_id)

object_fname = self.filename(athlete_id=athlete_id, object_id=object_id)
object_fname = self.filename(object_id=object_id)
cache_path = os.path.join(directory, object_fname)

with open(cache_path, "w") as fp:
fp.write(json.dumps(object_json, indent=2))

return cache_path

def get_cached_object_json(
self, athlete_id: int, activity_id: int
) -> Dict[str, Any]:
def get_cached_object_json(self, athlete_id: int, object_id: int) -> Dict[str, Any]:
"""
Retrieves raw object from cached directory.
"""
directory = self.cache_dir(athlete_id)

activity_fname = "{}.json".format(activity_id)

cache_path = os.path.join(directory, activity_fname)
object_fname = self.filename(object_id=object_id)
cache_path = os.path.join(directory, object_fname)

activity_json = None
if os.path.exists(cache_path):
Expand Down Expand Up @@ -108,13 +105,13 @@ def retrieve_object_json(
:param athlete_id:
:param object_id:
:param use_cache: Allow use of cache.
:param only_cache: Only use cache (no download)
:param use_cache: Allow reading from cache.
:param only_cache: Only read from cache (no download)
:return:
"""
if use_cache:
object_json = self.get_cached_object_json(
athlete_id=athlete_id, activity_id=object_id
athlete_id=athlete_id, object_id=object_id
)
else:
object_json = None
Expand All @@ -130,8 +127,8 @@ def retrieve_object_json(
return None

self.logger.info(
"[CACHE-MISS] Fetching {} detail for {!r}".format(
self.object_type, object_id
"[CACHE-{}] Fetching {} detail for {!r}".format(
"MISS" if use_cache else "BYPASS", self.object_type, object_id
)
)

Expand Down Expand Up @@ -208,9 +205,6 @@ def fetch(
class CachingStreamFetcher(CachingAthleteObjectFetcher):
object_type = "streams"

def filename(self, *, athlete_id: int, object_id: int):
return "{}_streams.json".format(athlete_id)

def download_object_json(
self, *, athlete_id: int, object_id: int
) -> Dict[str, Any]:
Expand Down

0 comments on commit 2151f8c

Please sign in to comment.