LichessStatTgWeb/LichessWebServices/events.py
vrubelroman d24cc1a2bd structured events.log, count games in the window they finished in, immediate reopened notice
- events.py (bot): one grep-able line per event into data/events.log (10 MB x 5 rotation,
  survives container recreation): user_start, cmd, text/callback/non_text, addgamer_input/
  addgamer_result, addtoken_result, gamer_added, gamer_deleted, period_set, lang_set,
  check (decision=request|skip_unchanged|skip_closed, games, notified, lag), check_error,
  closed_notice, reopened_notice, blocked, token_rejected, status_refresh, admin_notify,
  tg_error; a SUMMARY line at midnight, also shown by admin /status as "Вчера".
  Service side: EVT lichess_429 (kind=concurrency|burst|unknown) and lichess_error in its log.
- /games/{username}/period?by_finish=true: query Lichess from since-3h and keep games with
  since < lastMoveAt <= until. Periodic checks use it, so a game created 14:58 and finished
  15:06 is reported in the 15:00-16:00 window instead of being lost on the boundary.
- reopened-account notice is sent as soon as the status flips, not after the next game.

Co-Authored-By: Claude Fable 5.1 <noreply@anthropic.com>
2026-10-20 22:51:11 +00:00

46 lines
1.5 KiB
Python

"""
Structured event lines for the stats service, same format as the bot's
events.py (LichessClientTG_bot/events.py):
2026-10-20T22:15:12Z EVT lichess_429 endpoint=games kind=burst user=Magnus
The service has no data volume, so instead of a file the lines go to the
stdout logger "events" at INFO (i.e. into the container log), where they are
still grep-able: `docker logs lichess-api 2>&1 | grep "EVT lichess_429"`.
Values: ints as-is, True/False as 1/0, None as "-", strings truncated to 80
chars and repr-quoted when they contain a space or "=". event() never raises.
"""
import logging
import time
_events_logger = logging.getLogger("events")
MAX_STR_LEN = 80
def _fmt_value(value) -> str:
if value is None:
return "-"
if value is True:
return "1"
if value is False:
return "0"
if isinstance(value, int):
return str(value)
if isinstance(value, float):
return f"{value:.1f}"
s = str(value)
if len(s) > MAX_STR_LEN:
s = s[:MAX_STR_LEN]
if " " in s or "=" in s or s == "":
return repr(s)
return s
def event(name: str, /, **fields) -> None:
"""Write one `EVT <name> k=v ...` line to the container log. Never raises. `name` is positional-only so a field may also be called name=."""
try:
ts = time.strftime("%Y-%m-%dT%H:%M:%SZ", time.gmtime())
parts = [ts, "EVT", name] + [f"{k}={_fmt_value(v)}" for k, v in fields.items()]
_events_logger.info(" ".join(parts))
except Exception:
pass