feat(monitoring): multi-model dashboard v2.1 — tabs, per-model records, voice playback + MinIO dual-write

Problem: the dashboard was Jev-centric; NLU/arbiter/fallback DeepSeek, STT
providers and the audio-enhancer were barely visible, and stored texts/voices
had no real access path.

Bot (additive; reply path untouched):
- nlu.result now records latency_ms + the DeepSeek usage tokens
- matching.arbitration/fallback.result fill the model + latency_ms columns
  (payload unchanged; SQL coalesces column/payload so old rows still count)
- storage.py: MinIO dual-write layer — every voice (raw + denoised) and user
  attachment is mirrored fire-and-forget into bucket bot-media; the blocking
  minio SDK runs via asyncio.to_thread (senior-engineer review) so the event
  loop is never stalled; empty MINIO_* env = layer off; bucket auto-created
- requirements: minio>=7.2

Backend (admin-gated, read-only):
- GET /bot-metrics/models — one record per model (jev, deepseek-nlu,
  deepseek-arbiter, deepseek-fallback, audio-enhancer + dynamic per-provider
  STT) with calls/ok-rate/p50/p95/tokens/last-seen + version-drift vs the pin
- GET /bot-metrics/models/{id}/daily — per-model daily calls/fails/tokens/p50
- GET /bot-metrics/stages — every stage tagged with its Persian pipeline
  section (صوت/درک پیام/تطبیق/ابهام و انتخاب/پیش‌نویس)
- /traces now carries input_kind + a 120-char text excerpt (consensus →
  input.text → first STT transcript)
- media serving: POST /media-url mints a short-lived HMAC URL (audio/a tags
  cannot send Authorization); GET /media streams from MinIO bot-media with
  the read-only ./bot/data mount as fallback for pre-MinIO files; hard ref
  validation (voices|attachments only, no .., extension whitelist)
- router exempted from the global Bearer dependency (files.py pattern) —
  every endpoint keeps its own require_role('admin')

Frontend /bot-monitor (RTL, recharts):
- four tabs: نمای کلی / مدل‌ها (records table + per-model expandable daily
  charts: calls/fails, tokens, p50) / بخش‌ها (sections grouped) / پیام‌ها
- message drawer rebuilt: inline <audio controls> for raw + denoised voice,
  download links, every STT transcript + consensus side-by-side, user
  attachments, then the event timeline
- terminology: «ترس» → «پرونده‌ی پیام» with tooltips + a one-line legend

Grafana: bot-observability gains a «کارکرد مدل‌ها» table (stage→model
mapping for legacy rows); the explorer gains a message text column.

Seed: real (tiny) ogg files written + mirrored to MinIO, NLU usage tokens,
column-level model/latency on matching events.

Verified locally: /models returns all 7 records incl. per-STT and NLU tokens;
media 200 via signed URL (MinIO) and 401 without; ../ and .py rejected (400);
disk fallback serves a legacy file; pm-role 403 on every new endpoint;
bucket auto-created with 14 mirrored objects; tsc + vite build clean;
Grafana re-provisions 24 panels.
This commit is contained in:
2026-10-25 22:02:40 +03:30
parent 0adadd2cf8
commit cb621ca5d0
18 changed files with 2028 additions and 543 deletions
@@ -1,29 +1,86 @@
{
"annotations": { "list": [] },
"annotations": {
"list": []
},
"editable": true,
"graphTooltip": 1,
"refresh": "30s",
"schemaVersion": 41,
"tags": ["erp", "bot"],
"time": { "from": "now-24h", "to": "now" },
"tags": [
"erp",
"bot"
],
"time": {
"from": "now-24h",
"to": "now"
},
"timezone": "browser",
"title": "بات — اکسپلورر ترس‌ها",
"uid": "bot-trace-explorer",
"version": 1,
"panels": [
{
"type": "table", "title": "آخرین ترس‌ها", "id": 1,
"datasource": { "type": "postgres", "uid": "bot-postgres" },
"gridPos": { "h": 12, "w": 24, "x": 0, "y": 0 },
"targets": [{ "refId": "A", "format": "table", "rawSql": "SELECT t.id::text AS \"trace_id\", t.created_at AS \"زمان\", coalesce(t.source,'bale') AS \"منبع\", t.chat_id AS \"چت\", t.kind AS \"نوع\", coalesce(t.status,'—') AS \"وضعیت\", (SELECT count(*) FROM bot_trace_events e WHERE e.trace_id=t.id) AS \"رخدادها\" FROM bot_traces t WHERE ($trace_id = '' OR t.id::text = $trace_id) AND $__timeFilter(t.created_at) ORDER BY t.created_at DESC LIMIT 100" }],
"fieldConfig": { "defaults": { "custom": { "align": "auto" } }, "overrides": [] }
"type": "table",
"title": "آخرین ترس‌ها",
"id": 1,
"datasource": {
"type": "postgres",
"uid": "bot-postgres"
},
"gridPos": {
"h": 12,
"w": 24,
"x": 0,
"y": 0
},
"targets": [
{
"refId": "A",
"format": "table",
"rawSql": "SELECT t.id::text AS \"trace_id\", t.created_at AS \"زمان\", coalesce(t.source,'bale') AS \"منبع\", t.chat_id AS \"چت\", t.kind AS \"نوع\", coalesce(t.status,'—') AS \"وضعیت\", (SELECT count(*) FROM bot_trace_events e WHERE e.trace_id=t.id) AS \"رخدادها\", , left(coalesce((SELECT e2.payload->>'consensus_text' FROM bot_trace_events e2 WHERE e2.trace_id=t.id AND e2.stage='nlu.result' LIMIT 1), (SELECT e2.payload->>'text' FROM bot_trace_events e2 WHERE e2.trace_id=t.id AND e2.stage='input.text' LIMIT 1), ''), 90) AS \"متن/گزیده\"\n FROM bot_traces t WHERE ($trace_id = '' OR t.id::text = $trace_id) AND $__timeFilter(t.created_at) ORDER BY t.created_at DESC LIMIT 100"
}
],
"fieldConfig": {
"defaults": {
"custom": {
"align": "auto"
}
},
"overrides": []
}
},
{
"type": "table", "title": "رویدادهای ترس انتخاب‌شده (به ترتیب زمان)", "id": 2,
"datasource": { "type": "postgres", "uid": "bot-postgres" },
"gridPos": { "h": 14, "w": 24, "x": 0, "y": 12 },
"targets": [{ "refId": "A", "format": "table", "rawSql": "SELECT e.created_at AS \"زمان\", e.stage AS \"مرحله\", e.ok AS \"ok\", coalesce(e.latency_ms::text,'—') AS \"تأخیر (ms)\", coalesce(e.model,'—') AS \"مدل\", coalesce(e.field,'—') AS \"فیلد\", left(e.payload::text, 700) AS \"payload\" FROM bot_trace_events e WHERE e.trace_id::text = $trace_id ORDER BY e.created_at ASC LIMIT 500" }],
"fieldConfig": { "defaults": { "custom": { "align": "auto", "cellOptions": { "type": "text" } } }, "overrides": [] }
"type": "table",
"title": "رویدادهای ترس انتخاب‌شده (به ترتیب زمان)",
"id": 2,
"datasource": {
"type": "postgres",
"uid": "bot-postgres"
},
"gridPos": {
"h": 14,
"w": 24,
"x": 0,
"y": 12
},
"targets": [
{
"refId": "A",
"format": "table",
"rawSql": "SELECT e.created_at AS \"زمان\", e.stage AS \"مرحله\", e.ok AS \"ok\", coalesce(e.latency_ms::text,'—') AS \"تأخیر (ms)\", coalesce(e.model,'—') AS \"مدل\", coalesce(e.field,'—') AS \"فیلد\", left(e.payload::text, 700) AS \"payload\" FROM bot_trace_events e WHERE e.trace_id::text = $trace_id ORDER BY e.created_at ASC LIMIT 500"
}
],
"fieldConfig": {
"defaults": {
"custom": {
"align": "auto",
"cellOptions": {
"type": "text"
}
}
},
"overrides": []
}
}
],
"templating": {
@@ -33,11 +90,15 @@
"label": "شناسه ترس",
"type": "textbox",
"hide": 0,
"current": { "selected": false, "text": "", "value": "" },
"current": {
"selected": false,
"text": "",
"value": ""
},
"options": [],
"query": "",
"description": "trace_id را از جدول بالا کپی و اینجا بچسبانید"
}
]
}
}
}