explore: shard tracks into per-year files for progressive loading

bake_tracks now writes tracks_YYYY.json shards + tracks_index.json manifest
instead of a single monolithic tracks.json. API /api/me/tracks returns the
manifest; /api/me/tracks/{year} serves individual shards. Explore.svelte
fetches the two most recent years eagerly then streams the rest in the
background so the map renders immediately with recent data.
This commit is contained in:
Davide Scaini
2026-05-14 18:34:53 +02:00
parent 8af6b7b04e
commit 5167f2a988
3 changed files with 119 additions and 14 deletions
+21 -5
View File
@@ -43,23 +43,39 @@ def _wipe_user_activities(user_dir: Path) -> int:
if d.exists():
shutil.rmtree(d)
for name in ("index.json", "athlete.json", ".bincio_cache.json", "tracks.json"):
for name in ("index.json", "athlete.json", ".bincio_cache.json", "tracks.json", "tracks_index.json"):
f = user_dir / name
if f.exists():
f.unlink()
deleted += 1
for shard in user_dir.glob("tracks_*.json"):
shard.unlink(missing_ok=True)
deleted += 1
return deleted
@router.get("/api/me/tracks")
async def me_tracks(bincio_session: str | None = Cookie(default=None)) -> Response:
"""Return the pre-baked tracks.json for the logged-in user (Explore page)."""
"""Return the tracks manifest (years list + total) for the logged-in user."""
user = deps._require_user(bincio_session)
tracks_path = deps._get_data_dir() / user.handle / "tracks.json"
if not tracks_path.exists():
index_path = deps._get_data_dir() / user.handle / "tracks_index.json"
if not index_path.exists():
raise HTTPException(404, "Tracks not yet baked — upload an activity first")
return Response(content=tracks_path.read_bytes(), media_type="application/json")
return Response(content=index_path.read_bytes(), media_type="application/json")
@router.get("/api/me/tracks/{year}")
async def me_tracks_year(year: str, bincio_session: str | None = Cookie(default=None)) -> Response:
"""Return the pre-baked tracks shard for a specific year."""
user = deps._require_user(bincio_session)
if not year.isdigit() or len(year) != 4:
raise HTTPException(400, "year must be a 4-digit string")
shard_path = deps._get_data_dir() / user.handle / f"tracks_{year}.json"
if not shard_path.exists():
raise HTTPException(404, f"No tracks shard for year {year}")
return Response(content=shard_path.read_bytes(), media_type="application/json")
@router.get("/api/me/storage")