mirror of
https://github.com/kuhyx/utils.git
synced 2026-11-18 21:34:34 +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>
70 lines
2.3 KiB
Python
70 lines
2.3 KiB
Python
# Copyright (c) 2026 Krzysztof Rudnicki
|
|
"""Append-only activity.jsonl; one flock'd, fsync'd line per event."""
|
|
|
|
from __future__ import annotations
|
|
|
|
from datetime import UTC, datetime
|
|
import fcntl
|
|
import json
|
|
import os
|
|
from typing import Any
|
|
|
|
from focus_project import _api, _clock
|
|
from focus_project._paths import activity_path, data_dir
|
|
|
|
SOURCES = frozenset({"timer", "tutor", "sql_drills", "leetcode", "browser"})
|
|
KINDS = frozenset({"start", "checkin", "stop", "heartbeat"})
|
|
DEVICES = frozenset({"pc", "phone"})
|
|
# Clock skew tolerated on a stamped event; a bigger lead is rejected. Whatever
|
|
# is accepted is still clipped at "now" by ``minutes_for``.
|
|
MAX_SKEW_S = 120.0
|
|
|
|
|
|
def record_activity( # pylint: disable=too-many-arguments,too-many-positional-arguments
|
|
source: str,
|
|
kind: str,
|
|
ts: float | None = None,
|
|
project: str | None = None,
|
|
dev: str = "pc",
|
|
meta: dict[str, Any] | None = None,
|
|
) -> None:
|
|
"""Append one event. ``project`` defaults to the project active at ``ts``.
|
|
|
|
Raises:
|
|
ValueError: unknown source/kind/dev, or ``ts`` more than
|
|
:data:`MAX_SKEW_S` ahead of the clock (nothing is written).
|
|
"""
|
|
for value, allowed, label in (
|
|
(source, SOURCES, "source"),
|
|
(kind, KINDS, "kind"),
|
|
(dev, DEVICES, "dev"),
|
|
):
|
|
if value not in allowed:
|
|
msg = f"unknown {label} {value!r}; expected one of {sorted(allowed)}"
|
|
raise ValueError(msg)
|
|
now = _clock.now()
|
|
when = now if ts is None else ts
|
|
if when > now + MAX_SKEW_S:
|
|
msg = f"ts is {when - now:.0f} s ahead of the clock (max {MAX_SKEW_S:.0f} s)"
|
|
raise ValueError(msg)
|
|
if project is None:
|
|
found = _api.active(datetime.fromtimestamp(when, tz=UTC).astimezone().date())
|
|
project = found.id if found else None
|
|
line = json.dumps(
|
|
{
|
|
"ts": when,
|
|
"source": source,
|
|
"kind": kind,
|
|
"project": project,
|
|
"dev": dev,
|
|
"meta": meta or {},
|
|
},
|
|
separators=(",", ":"),
|
|
)
|
|
data_dir().mkdir(parents=True, exist_ok=True)
|
|
with activity_path().open("a", encoding="utf-8") as handle:
|
|
fcntl.flock(handle, fcntl.LOCK_EX)
|
|
handle.write(line + "\n")
|
|
handle.flush()
|
|
os.fsync(handle.fileno())
|