feat: log bot command invocations to the event stream
Bot commands were the only user-initiated path that didn't surface in the dashboard. They now produce ``command_handled`` / ``command_rate_limited`` / ``command_failed`` rows in ``EventLog`` alongside tracker and action events. Backend - ``EventLog`` gains nullable ``command_tracker_id`` / ``telegram_bot_id`` FKs plus deletion-snapshot name columns (idempotent migration). - New ``_log_command_event`` helper emits one row per invocation at the three branches in ``handle_command``. Logging failures are swallowed so they cannot block the user-visible reply. - Telegram ``from`` is captured in poller and webhook, whitelisted to identity fields by ``_normalize_issuer`` (drops ``language_code`` and any future PII), persisted under ``details.issuer``. - ``/api/status`` resolves live ``CommandTracker`` / ``TelegramBot`` names (mirroring the action pattern) and exposes ``tracker_id``, ``command_tracker_id``, ``telegram_bot_id`` so the frontend can deep-link. Frontend - Event rows are now clickable and open a detail modal with full provenance (bot → chat → issuer → provider), raw ``details`` JSON, and per-entity action buttons. - Buttons use the existing ``requestHighlight`` + ``goto`` crosslink pattern, so clicking lands on the entity's list page with that specific card scrolled into view and pulsing. - Auto-refresh dropdown (Off / 10s / 30s / 1m / 5m) persisted in ``localStorage``; ticker pauses while the tab is hidden. - Event-type filter, dashboard verb labels, and gradients extended for the three new ``command_*`` types. - Filled in pre-existing missing i18n keys (``common.hide`` / ``common.show`` / ``commandConfig.noCommandsForProvider``). Tests - New ``test_command_event_logging.py`` covers subject formatting, issuer normalization, the three event branches, and graceful failure when the DB is unreachable. ``pytest packages/server/tests/`` → 96/96.
This commit is contained in:
@@ -118,6 +118,31 @@ async def get_status(
|
||||
)).all()
|
||||
action_name_map = {aid: aname for aid, aname in action_rows}
|
||||
|
||||
# Live-resolve command tracker and bot names for command_* events
|
||||
# (mirrors the action/tracker pattern above). Falls back to the
|
||||
# snapshot stored on the EventLog when the entity has been deleted.
|
||||
cmd_tracker_ids = {
|
||||
e.command_tracker_id for e in event_rows if e.command_tracker_id is not None
|
||||
}
|
||||
cmd_tracker_name_map: dict[int, str] = {}
|
||||
if cmd_tracker_ids:
|
||||
cmd_tracker_rows = (await session.exec(
|
||||
select(CommandTracker.id, CommandTracker.name).where(
|
||||
CommandTracker.id.in_(cmd_tracker_ids)
|
||||
)
|
||||
)).all()
|
||||
cmd_tracker_name_map = {tid: tname for tid, tname in cmd_tracker_rows}
|
||||
|
||||
bot_ids = {
|
||||
e.telegram_bot_id for e in event_rows if e.telegram_bot_id is not None
|
||||
}
|
||||
bot_name_map: dict[int, str] = {}
|
||||
if bot_ids:
|
||||
bot_rows = (await session.exec(
|
||||
select(TelegramBot.id, TelegramBot.name).where(TelegramBot.id.in_(bot_ids))
|
||||
)).all()
|
||||
bot_name_map = {bid: bname for bid, bname in bot_rows}
|
||||
|
||||
def _display_tracker_name(e: EventLog) -> str:
|
||||
if e.tracker_id is not None and e.tracker_id in tracker_name_map:
|
||||
return tracker_name_map[e.tracker_id]
|
||||
@@ -135,11 +160,30 @@ async def get_status(
|
||||
return f"(deleted) {e.action_name}"
|
||||
return ""
|
||||
|
||||
def _display_command_tracker_name(e: EventLog) -> str:
|
||||
if (
|
||||
e.command_tracker_id is not None
|
||||
and e.command_tracker_id in cmd_tracker_name_map
|
||||
):
|
||||
return cmd_tracker_name_map[e.command_tracker_id]
|
||||
if e.command_tracker_name:
|
||||
return f"(deleted) {e.command_tracker_name}"
|
||||
return ""
|
||||
|
||||
def _display_bot_name(e: EventLog) -> str:
|
||||
if e.telegram_bot_id is not None and e.telegram_bot_id in bot_name_map:
|
||||
return bot_name_map[e.telegram_bot_id]
|
||||
if e.bot_name:
|
||||
return f"(deleted) {e.bot_name}"
|
||||
return ""
|
||||
|
||||
def _display_subject(e: EventLog) -> str:
|
||||
"""The primary label shown on the event row.
|
||||
|
||||
For action events the ``collection_name`` stores the action name;
|
||||
use the live-resolved action name when available so renames show.
|
||||
For command events the ``collection_name`` already stores the
|
||||
rendered ``/cmd args`` string so we just pass it through.
|
||||
"""
|
||||
if e.action_id is not None or (e.event_type or "").startswith("action_"):
|
||||
return _display_action_name(e) or e.collection_name
|
||||
@@ -155,9 +199,14 @@ async def get_status(
|
||||
"id": e.id,
|
||||
"event_type": e.event_type,
|
||||
"collection_name": _display_subject(e),
|
||||
"tracker_id": e.tracker_id,
|
||||
"tracker_name": _display_tracker_name(e),
|
||||
"action_id": e.action_id,
|
||||
"action_name": _display_action_name(e),
|
||||
"command_tracker_id": e.command_tracker_id,
|
||||
"command_tracker_name": _display_command_tracker_name(e),
|
||||
"telegram_bot_id": e.telegram_bot_id,
|
||||
"bot_name": _display_bot_name(e),
|
||||
"provider_name": _display_provider_name(e),
|
||||
"provider_id": e.provider_id,
|
||||
"assets_count": e.assets_count or 0,
|
||||
|
||||
Reference in New Issue
Block a user