Add guided running stats explorer with validated queries and comparisons

This commit is contained in:
Peter Stockings
2026-10-02 15:48:28 +10:00
parent 794e74166a
commit f951e888bd
8 changed files with 694 additions and 0 deletions
+49
View File
@@ -29,6 +29,7 @@ from .extensions import db, limiter
from .history import activity_page, latest_mapped_run, overview, records_for
from .models import Activity, Friendship, GarminConnection, User
from .services import activity_data, import_cache, import_upload, route_data
from .stats import DEFAULT, METRICS, explore, parse_query, query_text
web = Blueprint("web", __name__)
DUMMY_HASH = generate_password_hash("not-a-real-account-password")
@@ -193,6 +194,54 @@ def dashboard():
)
@web.get("/stats")
@login_required
def stats():
error = None
results = None
try:
query = parse_query(request.args)
page = int(request.args.get("page", "1"))
results = explore(current_user.id, current_user.timezone, query, page)
except (ValueError, OverflowError) as exc:
error = str(exc)
query = dict(DEFAULT)
return render_template(
"stats.html",
query=query,
query_text=query_text(query),
metrics=METRICS,
results=results,
error=error,
submitted_query=request.args.get("query", ""),
), 400 if error else 200
@web.get("/stats/runs")
@login_required
def stats_runs():
try:
query = parse_query(request.args)
bucket = (
datetime.strptime(request.args.get("bucket", ""), "%d %b %Y").replace(tzinfo=UTC).date()
)
results = explore(
current_user.id,
current_user.timezone,
query,
int(request.args.get("page", "1")),
selected_bucket=bucket,
)
except (ValueError, OverflowError) as exc:
return jsonify(error=str(exc)), 400
return jsonify(
rows=results["rows"],
count=results["evidence_count"],
page=results["page"],
pages=results["pages"],
)
@web.route("/friends", methods=["GET", "POST"])
@login_required
@limiter.limit("20 per hour", methods=["POST"])