perf: add patch_index flag to recalculate_elevation_hysteresis

Allows bulk callers to skip per-activity index.json rewrites and
batch the update themselves, reducing O(n²) index churn to O(n).
This commit is contained in:
Davide Scaini
2026-05-23 21:05:00 +02:00
parent 02edb0b0f9
commit 56932f7f25
+15 -12
View File
@@ -299,7 +299,9 @@ def recalculate_elevation(
} }
def recalculate_elevation_hysteresis(user_dir: Path, activity_id: str) -> dict: def recalculate_elevation_hysteresis(
user_dir: Path, activity_id: str, *, patch_index: bool = True
) -> dict:
"""Recompute elevation gain/loss from the original recorded elevation data. """Recompute elevation gain/loss from the original recorded elevation data.
Algorithm Algorithm
@@ -370,17 +372,18 @@ def recalculate_elevation_hysteresis(user_dir: Path, activity_id: str) -> dict:
detail["elevation_loss_m"] = loss_r detail["elevation_loss_m"] = loss_r
json_path.write_text(json.dumps(detail, indent=2, ensure_ascii=False), encoding="utf-8") json_path.write_text(json.dumps(detail, indent=2, ensure_ascii=False), encoding="utf-8")
# Patch index.json summary # Patch index.json summary (skip for bulk callers who batch this themselves)
index_path = user_dir / "index.json" if patch_index:
if index_path.exists(): index_path = user_dir / "index.json"
index = json.loads(index_path.read_text(encoding="utf-8")) if index_path.exists():
for s in index.get("activities", []): index = json.loads(index_path.read_text(encoding="utf-8"))
if s.get("id") == activity_id: for s in index.get("activities", []):
s["elevation_gain_m"] = gain_r if s.get("id") == activity_id:
break s["elevation_gain_m"] = gain_r
index_path.write_text( break
json.dumps(index, indent=2, ensure_ascii=False), encoding="utf-8" index_path.write_text(
) json.dumps(index, indent=2, ensure_ascii=False), encoding="utf-8"
)
return { return {
"elevation_gain_m": gain_r, "elevation_gain_m": gain_r,