mirror of
https://github.com/kuhyx/utils.git
synced 2026-11-17 22:05:17 +01:00
main was red on file length, the noqa ban, codespell, test naming and dart coverage; this change touches every one of those jobs. - sickdays: set_enabled is keyword-only; the panel test splits in two with shared probes; the sync wire format moves to DOCS-sync.md. - earned_time: _focus types the project as a Protocol, no noqa; the gate-maturity section moves to DOCS-maturity.md; codespell knows the `checkin` event name. - focus_project: real fixes for every noqa (local-day clocks, typed fakes, logged broad catches, split helpers). The public API that focus-guard calls keeps its signatures; their PLR0913 exemption lives in pyproject with the reason, not in noqa. - crdt-sync: pylint gets nacl/argon2, docstrings, and allows the public E2eLayout.of; _blobs moves to _e2e_blobs.py. - crdt_sync_dart: Argon2Params and the header errors move to e2e_params.dart (re-exported); tests split and reach 100% lines. - sql_drills: test helpers get a leading underscore, as in earned_time. Todo: fda71ed1 Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
91 lines
3.1 KiB
Python
91 lines
3.1 KiB
Python
# Copyright (c) 2026 Krzysztof Rudnicki
|
|
"""The harvester: turn credited focus minutes into signed ledger rows.
|
|
|
|
Run by ``focus-project-harvest.service`` (``.path`` on activity.jsonl plus a
|
|
one-minute ``.timer``). Harvests every project day of the last
|
|
:data:`BACKFILL_DAYS` days, so work recorded late (a phone synced days
|
|
after the fact) is paid once a run sees it; ``credit_to`` is idempotent, so
|
|
settled days cost one read and write nothing. Failure modes, by path:
|
|
|
|
Overtime past a day's target banks for the next day (``carry``): that day's
|
|
carry row pays it from the day's start, own-work rows pay the rest.
|
|
|
|
* a source that errors: logged, contributes 0, the run continues (fail closed);
|
|
* an unreadable registry, signing key or ledger: logged, exit status 1, so
|
|
the unit shows ``failed`` instead of silently crediting nothing.
|
|
"""
|
|
|
|
from __future__ import annotations
|
|
|
|
from datetime import UTC, date, datetime, timedelta
|
|
import logging
|
|
import sys
|
|
|
|
from focus_project import _api, _clock, carry, ledger
|
|
from focus_project._errors import RegistryError
|
|
|
|
BACKFILL_DAYS = 31 # bounds the per-run cost; the relay accepts events <= 48 h old
|
|
_LOG = logging.getLogger("focus_project.harvest")
|
|
|
|
|
|
def project_days(today: date) -> list[date]:
|
|
"""Every project day in the backfill window up to ``today``, oldest first."""
|
|
oldest = today - timedelta(days=BACKFILL_DAYS - 1)
|
|
days: set[date] = set()
|
|
for project in _api.list_projects():
|
|
day = max(project.start, oldest)
|
|
while day <= min(project.last_day, today):
|
|
days.add(day)
|
|
day += timedelta(days=1)
|
|
return sorted(days)
|
|
|
|
|
|
def harvest_day(day: date, now: float) -> int:
|
|
"""Credit ``day``; return the minutes added (0 with no project)."""
|
|
project = _api.active(day)
|
|
if project is None:
|
|
return 0
|
|
daily = project.daily_on(day)
|
|
bank = carry.balance(day, now)
|
|
added = ledger.credit_to(project, day, bank.paid_bank(daily), now=now, carry=True)
|
|
added += ledger.credit_to(project, day, bank.paid_own(daily), now=now)
|
|
if added:
|
|
_LOG.info(
|
|
"credited %d min to %s on %s (total %d, %d from the bank)",
|
|
added,
|
|
project.id,
|
|
day,
|
|
bank.counted(daily),
|
|
bank.paid_bank(daily),
|
|
)
|
|
return added
|
|
|
|
|
|
def harvest(now: float | None = None) -> int:
|
|
"""Harvest the backfill window; 0 on success, 1 if any path failed."""
|
|
moment = _clock.now() if now is None else now
|
|
today = datetime.fromtimestamp(moment, tz=UTC).astimezone().date()
|
|
try:
|
|
days = project_days(today)
|
|
except (RegistryError, OSError) as exc:
|
|
_LOG.error("harvest registry unreadable: %s", exc)
|
|
return 1
|
|
status = 0
|
|
for day in days:
|
|
try:
|
|
harvest_day(day, moment)
|
|
except (RegistryError, ledger.LedgerError, OSError) as exc:
|
|
_LOG.error("harvest %s failed: %s", day, exc)
|
|
status = 1
|
|
return status
|
|
|
|
|
|
def main() -> None:
|
|
"""Entry point of ``python -m focus_project.harvest``."""
|
|
logging.basicConfig(level=logging.INFO, format="%(levelname)s %(message)s")
|
|
sys.exit(harvest())
|
|
|
|
|
|
if __name__ == "__main__":
|
|
main()
|