screen-locker/screen_locker/_progress_row.py
Krzysztof kuhy Rudnicki 4ed64b463c
Some checks are pending
md-naming / markdown naming convention (push) Waiting to run
pre-commit / pre-commit (${{ matrix.group }}) (flutter, flutter-analyze, ) (push) Waiting to run
pre-commit / pre-commit (${{ matrix.group }}) (mypy, mypy, ) (push) Waiting to run
pre-commit / pre-commit (${{ matrix.group }}) (pylint, pylint, ) (push) Waiting to run
pre-commit / pre-commit (${{ matrix.group }}) (rest, , mypy,pylint,flutter-analyze,dependency-freshness) (push) Waiting to run
Tests / test (push) Waiting to run
Tests / lock-popup-safety (push) Waiting to run
Tests / flutter test (shard ${{ matrix.shard }}) (0) (push) Waiting to run
Tests / flutter test (shard ${{ matrix.shard }}) (1) (push) Waiting to run
Tests / flutter test (shard ${{ matrix.shard }}) (2) (push) Waiting to run
Tests / flutter test (shard ${{ matrix.shard }}) (3) (push) Waiting to run
Tests / flutter test (shard ${{ matrix.shard }}) (4) (push) Waiting to run
Tests / flutter test (shard ${{ matrix.shard }}) (5) (push) Waiting to run
Tests / flutter test (shard ${{ matrix.shard }}) (6) (push) Waiting to run
Tests / flutter test (shard ${{ matrix.shard }}) (7) (push) Waiting to run
Tests / flutter test (shard ${{ matrix.shard }}) (8) (push) Waiting to run
Tests / flutter test (shard ${{ matrix.shard }}) (9) (push) Waiting to run
Tests / flutter-coverage (push) Blocked by required conditions
Tests / web-coverage (push) Waiting to run
repo-contract / repo contract (push) Waiting to run
Write a progress row for a short run day
When a verified RunnerUp day is real but too short to qualify, the
ledger now gets one signed "progress" row saying how far it got:
metres over the distance bar or seconds over the time bar, whichever
fraction is larger, from the verified TCX only. Several short runs in
a day are summed; a longer second run writes a new row with a larger
done. The row is inert: no reader uses it yet, and no bonus, shutdown
time, stamp or web payload changes.

Qualifying days write only their normal credit. Manual and StrongLifts
days and the rooted-DB path write nothing.

Failure modes: if the row cannot be signed or written, the workout
verdict and everything else proceed exactly as before and the failure
is logged at warning or above. An unreadable TCX is "could not check"
and writes nothing. The bars appear only inside the signed row, never
in a log line or message.

Co-Authored-By: Claude Fable 5.2 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01WzxdWVfmEXxPymCs5bo3Zs
2026-11-22 18:23:46 +01:00

90 lines
3.5 KiB
Python

"""The workout's signed "progress" row: how far a too-short verified day got.
Writer only. The row is inert today -- no reader in ``earned_time`` looks at
``kind: "progress"`` yet -- and it never touches a bonus, a shutdown time, a
stamp or the web payload. It records ``done`` of ``target`` for one day, in
integers, from the verified RunnerUp TCX only::
{"kind": "progress", "entry_id": "progress:<day>:<done>:<target>",
"day": <day>, "amount": 0,
"detail": {"source": "workout", "for_day": <day>,
"done": "<digits>", "target": "<digits>"}}
The larger of the two fractions (distance / distance bar, moving time / time
bar) is the one recorded, so ``done``/``target`` is metres over metres or
seconds over seconds. The bars come from ``_constants`` and appear only inside
the signed row: they are never logged or shown (the constants file keeps a
deliberate leeway hidden). The ADVERTISED bars are the targets, not the
looser accept bars, so a row never reveals the leeway.
Signing, locking and idempotence on ``entry_id`` are the credit rows' own
(:func:`screen_locker._workout_ledger.append_rows`); a second short run the
same day has a larger ``done`` and so a new id.
"""
from __future__ import annotations
import logging
from typing import Any
from screen_locker._constants import MIN_RUN_DISTANCE_KM, MIN_WORKOUT_DURATION_MINUTES
from screen_locker._workout_ledger import append_rows
_logger = logging.getLogger(__name__)
SOURCE = "workout"
def progress_pair(metres: float, moving_seconds: float) -> tuple[int, int] | None:
"""``(done, target)`` of the leading fraction, or ``None`` with nothing done.
Both fractions are compared exactly, by cross-multiplying integers; a tie
goes to distance. ``done`` is floored and capped at its ``target``.
"""
distance_target = round(MIN_RUN_DISTANCE_KM * 1000)
time_target = MIN_WORKOUT_DURATION_MINUTES * 60
distance_done = min(int(max(metres, 0.0)), distance_target)
time_done = min(int(max(moving_seconds, 0.0)), time_target)
if distance_done * time_target >= time_done * distance_target:
done, target = distance_done, distance_target
else:
done, target = time_done, time_target
return (done, target) if done > 0 else None
def progress_row(for_day: str, done: int, target: int) -> dict[str, Any] | None:
"""The unsigned progress row; ``None`` when nothing was done."""
if done <= 0:
return None
return {
"kind": "progress",
"entry_id": f"progress:{for_day}:{done}:{target}",
"day": for_day,
"amount": 0,
"detail": {
"source": SOURCE,
"for_day": for_day,
"done": str(done),
"target": str(target),
},
}
def record_progress(for_day: str, metres: float, moving_seconds: float) -> None:
"""Append the day's progress row; never raises, never changes a verdict.
Call only for a day verified from RunnerUp TCX data and judged too short.
A row that cannot be signed or written is logged at warning or above by
``append_rows``; any other failure is logged here.
"""
try:
pair = progress_pair(metres, moving_seconds)
row = progress_row(for_day, *pair) if pair is not None else None
if row is not None:
append_rows([row])
except Exception: # pylint: disable=broad-exception-caught # a progress row must never change the verdict
_logger.exception(
"Could not write the progress row for %s; the workout verdict is unchanged",
for_day,
)