Add global segment rescan: POST /api/me/segment-rescan + Rescan all button

This commit is contained in:
Davide Scaini
2026-05-13 08:17:18 +02:00
parent f2075e29d2
commit d7fd585e77
2 changed files with 89 additions and 4 deletions
+58
View File
@@ -2584,6 +2584,64 @@ async def trigger_detect(
return JSONResponse({"ok": True, "efforts_found": total}) return JSONResponse({"ok": True, "efforts_found": total})
@app.post("/api/me/segment-rescan")
async def me_segment_rescan(
bincio_session: Optional[str] = Cookie(default=None),
) -> JSONResponse:
"""Retroactively detect efforts for ALL segments across ALL activities for the logged-in user."""
user = _require_user(bincio_session)
dd = _get_data_dir()
user_dir = dd / user.handle
acts_dir = user_dir / "activities"
from datetime import datetime as _datetime
from bincio.segments.detect import track_from_timeseries_json, detect_one
import json as _json
segments = _seg_store.list_segments(dd)
if not segments:
return JSONResponse({"ok": True, "efforts_found": 0})
total = 0
for detail_path in sorted(acts_dir.glob("*.json")):
if ".timeseries." in detail_path.name:
continue
try:
detail = _json.loads(detail_path.read_text(encoding="utf-8"))
except Exception:
continue
ts_url = detail.get("timeseries_url")
if not ts_url:
continue
ts_path = user_dir / ts_url
if not ts_path.exists():
continue
try:
ts = _json.loads(ts_path.read_text(encoding="utf-8"))
except Exception:
continue
started_raw = detail.get("started_at")
if not started_raw:
continue
try:
started_at = _datetime.fromisoformat(started_raw.replace("Z", "+00:00"))
except Exception:
continue
track = track_from_timeseries_json(
ts, detail.get("id", detail_path.stem),
detail.get("sport", "other"), started_at,
)
if track is None:
continue
for seg in segments:
efforts = detect_one(track, seg)
for effort in efforts:
_seg_store.add_effort(dd, user.handle, seg.id, effort)
total += len(efforts)
return JSONResponse({"ok": True, "efforts_found": total})
@app.get("/api/activities/{activity_id}/segment_efforts") @app.get("/api/activities/{activity_id}/segment_efforts")
async def activity_segment_efforts( async def activity_segment_efforts(
activity_id: str, activity_id: str,
+31 -4
View File
@@ -23,6 +23,8 @@
let loading = false; let loading = false;
let selectedId: string | null = null; let selectedId: string | null = null;
let fetchTimer: ReturnType<typeof setTimeout> | null = null; let fetchTimer: ReturnType<typeof setTimeout> | null = null;
let rescanning = false;
let rescanMsg: string | null = null;
const TILE_STYLE = 'https://tiles.openfreemap.org/styles/positron'; const TILE_STYLE = 'https://tiles.openfreemap.org/styles/positron';
const SEG_COLOR = '#f59e0b'; const SEG_COLOR = '#f59e0b';
@@ -123,6 +125,20 @@
['case', ['==', ['get', 'id'], selectedId ?? ''], 5, 3]); ['case', ['==', ['get', 'id'], selectedId ?? ''], 5, 3]);
} }
async function rescanAll() {
rescanning = true;
rescanMsg = null;
try {
const r = await fetch('/api/me/segment-rescan', { method: 'POST', credentials: 'include' });
const d = await r.json();
if (r.ok) rescanMsg = `Found ${d.efforts_found} effort${d.efforts_found !== 1 ? 's' : ''}.`;
else rescanMsg = d.detail ?? 'Rescan failed.';
} catch {
rescanMsg = 'Could not reach server.';
}
rescanning = false;
}
async function deleteSegment(id: string) { async function deleteSegment(id: string) {
if (!confirm('Delete this segment? This cannot be undone.')) return; if (!confirm('Delete this segment? This cannot be undone.')) return;
try { try {
@@ -144,10 +160,21 @@
<div class="flex items-center justify-between px-4 py-3 border-b shrink-0" <div class="flex items-center justify-between px-4 py-3 border-b shrink-0"
style="border-color: var(--border)"> style="border-color: var(--border)">
<h1 class="text-lg font-bold text-white">Segments</h1> <h1 class="text-lg font-bold text-white">Segments</h1>
<a href="{base}segments/new/" <div class="flex items-center gap-2">
class="px-3 py-1.5 rounded-lg text-sm bg-blue-600 hover:bg-blue-500 text-white transition-colors"> {#if rescanMsg}
+ New segment <span class="text-xs text-zinc-400">{rescanMsg}</span>
</a> {/if}
<button
on:click={rescanAll}
disabled={rescanning}
title="Rescan all your activities against all segments"
class="px-3 py-1.5 rounded-lg text-sm border border-zinc-700 text-zinc-400 hover:border-zinc-500 hover:text-white transition-colors disabled:opacity-40"
>{rescanning ? 'Scanning…' : 'Rescan all'}</button>
<a href="{base}segments/new/"
class="px-3 py-1.5 rounded-lg text-sm bg-blue-600 hover:bg-blue-500 text-white transition-colors">
+ New segment
</a>
</div>
</div> </div>
<!-- Map --> <!-- Map -->