diff --git a/.env.example b/.env.example index a7d9140..260695f 100644 --- a/.env.example +++ b/.env.example @@ -59,6 +59,11 @@ GLITCHTIP_EMAIL_URL=consolemail:// # Grafana (dashboards) — UI is loopback-only (:3001) unless you add Traefik labels GRAFANA_ADMIN_PASSWORD=CHANGE_ME_grafana_password GRAFANA_ROOT_URL=http://localhost:3001/ +# Bot-observability datasource (Grafana → app Postgres, read-only role). +# The bot provisions the obs_reader role with this password at startup +# (empty = role skipped; datasource then can't connect). +PROCUREMENT_BOT_OBS_READER_PASSWORD= +OBS_READER_USER=obs_reader # OpenPanel (product analytics) — client id/secret from the OpenPanel project OPENPANEL_API_URL=http://openpanel-api:3333 OPENPANEL_CLIENT_ID= diff --git a/backend/app/routers/__init__.py b/backend/app/routers/__init__.py index 1788808..8caef92 100644 --- a/backend/app/routers/__init__.py +++ b/backend/app/routers/__init__.py @@ -2,6 +2,7 @@ from app.routers import ( attribute_definitions, auth, + bot_metrics, brand_materials, brands, drafts, @@ -29,6 +30,7 @@ from app.routers import ( ROUTERS = [ auth.router, + bot_metrics.router, units.router, brands.router, material_categories.router, diff --git a/backend/app/routers/bot_metrics.py b/backend/app/routers/bot_metrics.py new file mode 100644 index 0000000..caebc3f --- /dev/null +++ b/backend/app/routers/bot_metrics.py @@ -0,0 +1,282 @@ +"""Bot observability metrics (read-only) for the ERP admin dashboard page. + +Reads the bot's own tables (bot_traces / bot_trace_events — owned and written +by the Bale bot container) with plain SELECTs only. Admin-gated. Calibration, +demo and test traffic (source != 'bale' / chat_id = 0) is excluded from the +aggregates; the traces endpoint can show everything with ?include_synthetic=1. +""" +from fastapi import APIRouter, Depends, Query +from sqlalchemy import text +from sqlalchemy.ext.asyncio import AsyncSession + +from app.auth import require_role +from app.db.engine import get_db + +router = APIRouter(prefix="/bot-metrics", tags=["bot-metrics"]) + +# real-traffic predicate shared by every aggregate (same rule as the bot's +# tools/monitor.py so the numbers match the weekly digest) +REAL = ("e.trace_id IN (SELECT id FROM bot_traces" + " WHERE coalesce(source,'bale') NOT IN ('calibration','demo')" + " AND coalesce(chat_id,0) <> 0)") + + +async def _rows(db: AsyncSession, sql: str, params: dict | None = None) -> list[dict]: + res = await db.execute(text(sql), params or {}) + return [dict(r._mapping) for r in res.all()] + + +@router.get("/summary") +async def summary( + days: int = Query(7, ge=1, le=90), + db: AsyncSession = Depends(get_db), + _ctx=Depends(require_role("admin")), +): + """Headline KPIs + daily volumes for the dashboard header cards.""" + p = {"days": days} + kpis = (await _rows(db, f""" + SELECT + (SELECT count(*) FROM bot_traces + WHERE coalesce(source,'bale') NOT IN ('calibration','demo') + AND created_at >= now() - (:days * interval '1 day')) AS traces, + (SELECT count(*) FROM bot_trace_events e WHERE {REAL} + AND e.created_at >= now() - (:days * interval '1 day')) AS events, + (SELECT count(*) FROM bot_trace_events e WHERE {REAL} + AND (e.stage = 'jev.failure' OR (e.stage = 'jev.result' AND NOT e.ok) + OR e.stage LIKE 'matching.%.failure') + AND e.created_at >= now() - (:days * interval '1 day')) AS errors, + (SELECT coalesce(round(percentile_cont(0.95) WITHIN GROUP (ORDER BY e.latency_ms)), 0) + FROM bot_trace_events e WHERE {REAL} AND e.stage = 'jev.result' + AND e.latency_ms IS NOT NULL + AND e.created_at >= now() - (:days * interval '1 day')) AS jev_p95_ms, + (SELECT count(*) FROM bot_trace_events e WHERE {REAL} AND e.stage = 'draft.commit' + AND e.created_at >= now() - (:days * interval '1 day')) AS commits, + (SELECT coalesce(sum((e.payload->'usage'->>'input_tokens')::bigint), 0) + FROM bot_trace_events e WHERE {REAL} + AND e.stage IN ('jev.result','matching.arbitration.result','matching.fallback.result') + AND e.payload->'usage' IS NOT NULL + AND e.created_at >= now() - (:days * interval '1 day')) AS tokens_in, + (SELECT coalesce(sum((e.payload->'usage'->>'output_tokens')::bigint), 0) + FROM bot_trace_events e WHERE {REAL} + AND e.stage IN ('jev.result','matching.arbitration.result','matching.fallback.result') + AND e.payload->'usage' IS NOT NULL + AND e.created_at >= now() - (:days * interval '1 day')) AS tokens_out + """, p))[0] + daily = await _rows(db, f""" + SELECT to_char(date_trunc('day', e.created_at), 'YYYY-MM-DD') AS day, + count(*) AS events, + count(DISTINCT e.trace_id) AS traces + FROM bot_trace_events e WHERE {REAL} + AND e.created_at >= now() - (:days * interval '1 day') + GROUP BY 1 ORDER BY 1 + """, p) + return {"data": {"kpis": kpis, "daily": daily, "days": days}} + + +@router.get("/routing") +async def routing( + days: int = Query(7, ge=1, le=90), + db: AsyncSession = Depends(get_db), + _ctx=Depends(require_role("admin")), +): + """Final mention statuses (arbiter + crisis fallback aggregates, plus the + legacy per-mention rows) and the arbiter's actions on Jev's suggestions.""" + p = {"days": days} + statuses = await _rows(db, f""" + SELECT metric, sum(n)::int AS n FROM ( + SELECT unnest(ARRAY['exact','ambiguity','new']) AS metric, 0 AS n + UNION ALL + SELECT 'exact', sum(coalesce((e.payload->'statuses'->>'exact')::int,0)) + FROM bot_trace_events e WHERE {REAL} + AND e.stage IN ('matching.arbitration.result','matching.fallback.result') + AND e.created_at >= now() - (:days * interval '1 day') + UNION ALL + SELECT 'ambiguity', sum(coalesce((e.payload->'statuses'->>'ambiguity')::int,0)) + FROM bot_trace_events e WHERE {REAL} + AND e.stage IN ('matching.arbitration.result','matching.fallback.result') + AND e.created_at >= now() - (:days * interval '1 day') + UNION ALL + SELECT 'new', sum(coalesce((e.payload->'statuses'->>'new')::int,0)) + FROM bot_trace_events e WHERE {REAL} + AND e.stage IN ('matching.arbitration.result','matching.fallback.result') + AND e.created_at >= now() - (:days * interval '1 day') + UNION ALL + SELECT m->>'status', count(*) FROM bot_trace_events e + JOIN bot_traces t ON t.id = e.trace_id, jsonb_array_elements(e.payload->'mentions') m + WHERE e.stage='routing.decision' AND e.payload->>'source'='jev' + AND coalesce(t.source,'bale') NOT IN ('calibration','demo') + AND coalesce(t.chat_id,0) <> 0 + AND e.created_at >= now() - (:days * interval '1 day') + GROUP BY 1 + ) x GROUP BY metric + """, p) + actions = await _rows(db, f""" + SELECT + coalesce(sum((e.payload->'actions'->>'endorsed_jev')::int),0)::int AS endorsed_jev, + coalesce(sum((e.payload->'actions'->>'vetoed_to_new')::int),0)::int AS vetoed_to_new, + coalesce(sum((e.payload->'actions'->>'demoted_to_ambiguity')::int),0)::int AS demoted_to_ambiguity, + coalesce(sum((e.payload->'actions'->>'other')::int),0)::int AS other, + count(*)::int AS invocations + FROM bot_trace_events e WHERE {REAL} AND e.stage='matching.arbitration.result' + AND e.created_at >= now() - (:days * interval '1 day') + """, p) + stages = await _rows(db, f""" + SELECT e.stage, count(*)::int AS n + FROM bot_trace_events e WHERE {REAL} + AND e.stage IN ('jev.request','jev.result','jev.failure', + 'matching.arbitration.result','matching.fallback.result') + AND e.created_at >= now() - (:days * interval '1 day') + GROUP BY 1 ORDER BY n DESC + """, p) + return {"data": {"statuses": statuses, "actions": actions[0] if actions else {}, "stages": stages}} + + +@router.get("/quality") +async def quality( + days: int = Query(7, ge=1, le=90), + db: AsyncSession = Depends(get_db), + _ctx=Depends(require_role("admin")), +): + """User choices as the real A/B proxy: what the user picked when asked.""" + p = {"days": days} + amb = await _rows(db, f""" + SELECT coalesce(e.payload->>'action','?') AS action, + coalesce(e.payload->>'arg','-') AS arg, + count(*)::int AS n + FROM bot_trace_events e WHERE {REAL} AND e.stage='ambiguity.user_choice' + AND e.created_at >= now() - (:days * interval '1 day') + GROUP BY 1,2 ORDER BY n DESC + """, p) + picks = await _rows(db, f""" + SELECT CASE WHEN (e.payload->>'idx')::int = 0 THEN 'top' ELSE 'other' END AS pick, + count(*)::int AS n + FROM bot_trace_events e WHERE {REAL} AND e.stage='user_choice.pick' + AND e.created_at >= now() - (:days * interval '1 day') + GROUP BY 1 + """, p) + drafts = await _rows(db, f""" + SELECT e.stage, count(*)::int AS n + FROM bot_trace_events e WHERE {REAL} + AND e.stage IN ('draft.render','draft.commit','draft.cancel','ambiguity.sent') + AND e.created_at >= now() - (:days * interval '1 day') + GROUP BY 1 ORDER BY n DESC + """, p) + return {"data": {"ambiguities": amb, "picks": picks, "drafts": drafts}} + + +@router.get("/jev") +async def jev_health( + days: int = Query(7, ge=1, le=90), + db: AsyncSession = Depends(get_db), + _ctx=Depends(require_role("admin")), +): + """Jev health: models seen (drift vs the pinned version), tokens, latency.""" + p = {"days": days} + models = await _rows(db, f""" + SELECT coalesce(e.model,'?') AS model, count(*)::int AS n, + max(e.created_at)::text AS last_seen + FROM bot_trace_events e WHERE {REAL} AND e.stage='jev.result' + AND e.created_at >= now() - (:days * interval '1 day') + GROUP BY 1 ORDER BY n DESC + """, p) + latency = await _rows(db, f""" + SELECT e.stage, + coalesce(round(percentile_cont(0.5) WITHIN GROUP (ORDER BY e.latency_ms)),0)::int AS p50, + coalesce(round(percentile_cont(0.95) WITHIN GROUP (ORDER BY e.latency_ms)),0)::int AS p95, + count(*)::int AS n + FROM bot_trace_events e WHERE {REAL} + AND e.stage IN ('jev.result','nlu.result','matching.arbitration.result','matching.fallback.result') + AND e.latency_ms IS NOT NULL + AND e.created_at >= now() - (:days * interval '1 day') + GROUP BY 1 ORDER BY n DESC + """, p) + tokens_daily = await _rows(db, f""" + SELECT to_char(date_trunc('day', e.created_at), 'YYYY-MM-DD') AS day, + coalesce(sum((e.payload->'usage'->>'input_tokens')::bigint),0)::bigint AS tokens_in, + coalesce(sum((e.payload->'usage'->>'output_tokens')::bigint),0)::bigint AS tokens_out + FROM bot_trace_events e WHERE {REAL} + AND e.stage IN ('jev.result','matching.arbitration.result','matching.fallback.result') + AND e.payload->'usage' IS NOT NULL + AND e.created_at >= now() - (:days * interval '1 day') + GROUP BY 1 ORDER BY 1 + """, p) + return {"data": {"models": models, "latency": latency, "tokens_daily": tokens_daily}} + + +@router.get("/stt") +async def stt_health( + days: int = Query(7, ge=1, le=90), + db: AsyncSession = Depends(get_db), + _ctx=Depends(require_role("admin")), +): + """STT providers and the audio-enhancement layer (ok-rate + p50 latency).""" + p = {"days": days} + providers = await _rows(db, f""" + SELECT coalesce(e.payload->>'provider','?') AS provider, + count(*) FILTER (WHERE e.ok)::int AS ok, + count(*)::int AS total, + coalesce(round(percentile_cont(0.5) WITHIN GROUP (ORDER BY e.latency_ms)),0)::int AS p50_ms + FROM bot_trace_events e WHERE {REAL} AND e.stage='stt.result' + AND e.created_at >= now() - (:days * interval '1 day') + GROUP BY 1 ORDER BY ok DESC + """, p) + enhance = await _rows(db, f""" + SELECT count(*) FILTER (WHERE e.ok)::int AS ok, + count(*)::int AS total, + coalesce(round(percentile_cont(0.5) WITHIN GROUP (ORDER BY e.latency_ms)),0)::int AS p50_ms + FROM bot_trace_events e WHERE {REAL} AND e.stage='audio.enhance.result' + AND e.created_at >= now() - (:days * interval '1 day') + """, p) + return {"data": {"providers": providers, "enhance": enhance[0] if enhance else None}} + + +@router.get("/traces") +async def traces( + limit: int = Query(50, ge=1, le=200), + status: str | None = None, + include_synthetic: bool = False, + db: AsyncSession = Depends(get_db), + _ctx=Depends(require_role("admin")), +): + """Latest traces for the explorer table (oldest events come from /traces/{id}).""" + sql = """ + SELECT t.id::text AS id, t.created_at::text AS created_at, + coalesce(t.source,'bale') AS source, t.chat_id::text AS chat_id, + t.kind, coalesce(t.status,'open') AS status, + (SELECT count(*) FROM bot_trace_events e WHERE e.trace_id=t.id)::int AS events + FROM bot_traces t + WHERE (:include_synthetic + OR (coalesce(t.source,'bale') NOT IN ('calibration','demo') AND coalesce(t.chat_id,0) <> 0)) + """ + params: dict = {"include_synthetic": include_synthetic} + if status: + sql += " AND t.status = :status" + params["status"] = status + sql += " ORDER BY t.created_at DESC LIMIT :limit" + params["limit"] = limit + return {"data": await _rows(db, sql, params)} + + +@router.get("/traces/{trace_id}") +async def trace_detail( + trace_id: str, + db: AsyncSession = Depends(get_db), + _ctx=Depends(require_role("admin")), +): + """Full event list of one trace (the drawer/drill-down view).""" + events = await _rows(db, """ + SELECT e.created_at::text AS created_at, e.stage, e.ok, + e.latency_ms, e.model, e.field, e.payload + FROM bot_trace_events e + WHERE e.trace_id::text = :tid + ORDER BY e.created_at ASC, e.id ASC + LIMIT 500 + """, {"tid": trace_id}) + head = await _rows(db, """ + SELECT id::text AS id, created_at::text AS created_at, + coalesce(source,'bale') AS source, chat_id::text AS chat_id, + kind, coalesce(status,'open') AS status, + draft_id::text AS draft_id, committed_quote_id::text AS committed_quote_id + FROM bot_traces WHERE id::text = :tid + """, {"tid": trace_id}) + return {"data": {"trace": head[0] if head else None, "events": events}} diff --git a/bot/app/config.py b/bot/app/config.py index 7ebc7cd..0dab949 100644 --- a/bot/app/config.py +++ b/bot/app/config.py @@ -147,6 +147,10 @@ class Settings(BaseSettings): OBS_ENABLED: bool = True OBS_VOICE_DIR: str = "data/voices" # persistent volume (./bot/data) — keep forever OBS_PROMPT_VERSION: str = "" # manual label for the NLU prompt in use + # Read-only DB role for the Grafana datasource (dashboard phase 1); empty = + # role provisioning is skipped (local dev can point the datasource at the + # postgres user instead). Rotating = change env + restart the bot. + OBS_READER_PASSWORD: str = "" # Jev (TypeSafe) entity matching — phases 2-5. Model version is PINNED # (phase 0 resolved jev-1.13.0) so upstream updates cannot silently change diff --git a/bot/app/db_init.py b/bot/app/db_init.py index 1b42e4d..ff57c15 100644 --- a/bot/app/db_init.py +++ b/bot/app/db_init.py @@ -97,10 +97,48 @@ DDL = [ """, "CREATE INDEX IF NOT EXISTS ix_bot_trace_events_trace ON bot_trace_events (trace_id, created_at)", "CREATE INDEX IF NOT EXISTS ix_bot_trace_events_stage ON bot_trace_events (stage)", + # dashboard queries filter stage + time range together (senior-engineer review + # 2026-10-24; column is `stage` — the review's `event_type` doesn't exist here) + "CREATE INDEX IF NOT EXISTS ix_bot_trace_events_stage_time ON bot_trace_events (stage, created_at DESC)", "CREATE INDEX IF NOT EXISTS ix_bot_trace_events_payload ON bot_trace_events USING GIN (payload jsonb_path_ops)", ] +async def ensure_obs_reader(session: AsyncSession) -> None: + """Read-only role for the Grafana Postgres datasource (dashboard plan phase 1). + + Idempotent; entirely skipped when PROCUREMENT_BOT_OBS_READER_PASSWORD is empty + (local dev may keep using the postgres superuser for the datasource instead). + Password comes from env so rotation never needs a code change. + """ + from app.config import settings + + pwd = (settings.OBS_READER_PASSWORD or "").strip() + if not pwd: + logger.info("obs_reader role skipped (no OBS_READER_PASSWORD configured)") + return + # CREATE ROLE's PASSWORD clause takes a string literal only (no function + # expressions), so the env value is embedded with standard '' escaping — + # bound parameters cannot cross into a DO block. + lit = pwd.replace("'", "''") + await session.execute(text(f""" + DO $$ + BEGIN + IF NOT EXISTS (SELECT 1 FROM pg_roles WHERE rolname = 'obs_reader') THEN + CREATE ROLE obs_reader LOGIN PASSWORD '{lit}'; + ELSE + ALTER ROLE obs_reader WITH LOGIN PASSWORD '{lit}'; + END IF; + END + $$ + """)) + await session.execute(text("GRANT USAGE ON SCHEMA public TO obs_reader")) + await session.execute(text( + "GRANT SELECT ON TABLE public.bot_traces, public.bot_trace_events TO obs_reader")) + await session.commit() + logger.info("obs_reader role ready (USAGE on public + SELECT on bot_traces/bot_trace_events)") + + async def ensure_bot_tables(session: AsyncSession) -> None: for stmt in DDL: await session.execute(text(stmt)) @@ -113,3 +151,4 @@ async def ensure_schema() -> None: async with SessionLocal() as session: await ensure_bot_tables(session) + await ensure_obs_reader(session) diff --git a/bot/docs/DEPLOY-AND-MONITORING.md b/bot/docs/DEPLOY-AND-MONITORING.md index a0a3f82..2e5e925 100644 --- a/bot/docs/DEPLOY-AND-MONITORING.md +++ b/bot/docs/DEPLOY-AND-MONITORING.md @@ -59,14 +59,25 @@ 4. راستی‌آزمایی پایش: `docker compose exec bot python tools/monitor.py --days 1` → قیف باید پر شود و رخدادهای همان پیام‌ها در `bot_trace_events` باشد. 5. چک سهمیه: `usage` در `jev.result` (توکن ورودی/خروجی) + latency p95. +6. **داشبوردها (۲۰۲۶-۱۰-۲۴):** `PROCUREMENT_BOT_OBS_READER_PASSWORD` را در `.env` ست کنید و + یک بار بات را restart کنید تا نقش فقط‌خواندنی `obs_reader` ساخته شود؛ سپس گرفانا را + (ری)استارت کنید — دیتاسورس `BotPostgres` و دو داشبورد «بات — Observability خط لوله» و + «بات — اکسپلورر ترس‌ها» خودکار provision می‌شوند (جزئیات در `monitoring/README.md`). + صفحه‌ی فارسی داخل ERP هم روی مسیر `/bot-monitor` (فقط ادمین) است. + برای دیدن همه‌ی پنل‌ها روی نصب تازه: `docker compose exec bot python tools/seed_observability_demo.py --days 7` + (داده‌ها با `source='demo'` از آمار ترافیک واقعی جدا می‌مانند). ## د) Rollback (بدون دیپلوی — فقط env + restart) | سوئیچ | اثر | |---|---| -| `JEV_ENABLED=false` | بازگشت کامل به مسیر difflib قدیم | +| `JEV_ENABLED=false` | غیرفعال‌شدن Jev — mentionها مستقیم به فالبک DeepSeek می‌روند | | `OBS_ENABLED=false` | خاموش‌شدن ثبت (داده‌های قبلی دست‌نخورده می‌مانند) | +حذف کامل داشبوردها (در صورت نیاز): دو JSON در `monitoring/grafana/dashboards/` + دیتاسورس در +`datasources.yml` + `backend/app/routers/bot_metrics.py` + `frontend/src/pages/BotMonitor/` + `DROP ROLE obs_reader`. +هیچ داده‌ای حذف نمی‌شود. + ## ه) پایش هفتگی ```bash diff --git a/bot/tools/seed_observability_demo.py b/bot/tools/seed_observability_demo.py new file mode 100644 index 0000000..030387a --- /dev/null +++ b/bot/tools/seed_observability_demo.py @@ -0,0 +1,207 @@ +"""Seed DEMO observability traces so every dashboard panel has data (phase 5). + + docker compose exec bot python tools/seed_observability_demo.py [--days 7] + +- Writes traces with source='demo' (excluded from real-traffic aggregates in + monitor.py / bot-metrics / Grafana by default; visible via the ERP page's + «نمایش ترافیک آزمایشی» checkbox or Grafana queries without the filter). +- Covers every current pipeline stage (787824a): input.text/voice, + audio.enhance.result, stt.run/result, nlu.request/result, entities.extracted, + jev.request/result (+ one jev.failure), routing.decision (jev_scored), + matching.arbitration.result, matching.fallback.result, ambiguity.sent, + ambiguity.user_choice, user_choice.pick, draft.render, draft.commit. +- Idempotent per run-day: re-running the same day replaces that day's demo + traces (delete + re-insert of source='demo' rows only — real data untouched, + no-deletion policy applies to real traffic only). +""" +import argparse +import asyncio +import random +import sys +import uuid +from datetime import datetime, timedelta, timezone +from pathlib import Path + +sys.path.insert(0, str(Path(__file__).resolve().parents[1])) + +from sqlalchemy import text + +from app.db import SessionLocal + +rng = random.Random(42) # deterministic demo data + +FIELDS = ["project", "supplier", "material", "brand", "spec", "unit"] +VARIANTS = { + "project": [["منشیانه"], ["دفتر"], ["باق اسپارت"], ["پردیس"]], + "supplier": [["مصطفوی", "مستفدی"], ["فولاد مبارکه"], ["راجین"]], + "material": [["گچ و خاک"], ["سیمان"], ["میلگرد"]], + "brand": [["ذوب آهن"], ["سیمان تهران"]], + "spec": [["سایز 14"], ["50 کیلویی"]], + "unit": [["کیسه"], ["کیلو"], ["تن"]], +} +NAMES = { + "project": ["منشانه", "دفتر", "باغ اسپارت", "پردیس کاشان"], + "supplier": ["بازرگانی مصطفوی", "نمایندگی فولاد مبارکه", "راجین"], + "material": ["گچ و خاک", "سیمان پرتلند", "میلگرد آجدار"], + "brand": ["ذوب آهن اصفهان", "سیمان تهران"], + "spec": ["سایز ۱۴", "۵۰ کیلویی"], + "unit": ["کیسه", "کیلوگرم", "تن"], +} +STAGES_F = ["سایز ۱۴", "۵۰ کیلویی"] + + +def _mentions(n: int) -> list[dict]: + out = [] + for field in (FIELDS[:n]): + out.append({"field": field, "variants": rng.choice(VARIANTS[field])}) + return out + + +async def one_trace(s, chat_id: int, t0: datetime, kind: str) -> None: + tid = uuid.uuid4() + n_ev = 0 + + async def ev(stage: str, *, minutes=0.0, ok=True, latency=None, model=None, + field=None, payload=None) -> None: + nonlocal n_ev + n_ev += 1 + await s.execute(text(""" + INSERT INTO bot_trace_events + (trace_id, stage, ok, latency_ms, model, field, payload, created_at) + VALUES (:t, :stage, :ok, :lat, :model, :field, CAST(:payload AS jsonb), :ts) + """), {"t": tid, "stage": stage, "ok": ok, "lat": latency, "model": model, + "field": field, "payload": __import__("json").dumps(payload or {}, ensure_ascii=False), + "ts": t0 + timedelta(minutes=minutes)}) + + await s.execute(text(""" + INSERT INTO bot_traces (id, chat_id, message_id, kind, source, status, created_at, updated_at) + VALUES (:id, :chat, :msg, :kind, 'demo', :status, :ts, :ts) + """), {"id": tid, "chat": chat_id, "msg": rng.randrange(1000, 99999), "kind": kind, + "status": "committed", "ts": t0}) + + m = 0.0 + # ---- input + if kind == "voice": + await ev("input.voice", minutes=m, payload={ + "path": f"data/voices/demo/{tid}.ogg", "size": 21400, "mime": "audio/ogg"}) + # audio enhancement (new in 787824a) + ok_enh = rng.random() < 0.85 + await ev("audio.enhance.result", minutes=(m := m + 0.1), ok=ok_enh, + latency=rng.randrange(900, 2600), model="audio_enhancer", + payload={"size": 18000, "original_size": 21400} if ok_enh + else {"error": "HTTP 503", "fallback": True}) + await ev("stt.run", minutes=(m := m + 0.1), payload={"providers": ["whisper", "vosk"]}) + transcripts = [] + for p in ("whisper", "vosk"): + ok = rng.random() < 0.92 + t = "بیست و پنج کیسه گچ و خاک از مصطفوی برای منشیانه" if ok else None + await ev("stt.result", minutes=(m := m + 0.1), ok=ok, latency=rng.randrange(700, 2100), + model="whisper-1" if p == "whisper" else "vosk-fa", + payload={"provider": p, "text": t, "error": None if ok else "timeout"}) + if ok: + transcripts.append({"name": p, "text": t}) + payload_text = transcripts[0]["text"] if transcripts else "" + else: + await ev("input.text", minutes=m, payload={"text": "۲۵ کیسه گچ و خاک از مصطفوی برای منشیانه"}) + payload_text = "۲۵ کیسه گچ و خاک از مصطفوی برای منشیانه" + + # ---- NLU + await ev("nlu.request", minutes=(m := m + 0.05), payload={ + "prompt_hash": "demo-hash", "prompt_version": "nlu-v2-entity-mentions"}) + mentions = _mentions(rng.randint(3, 6)) + await ev("nlu.result", minutes=(m := m + 0.9), latency=rng.randrange(1500, 3600), + model="deepseek-chat", payload={ + "intent": "register_quote", "consensus_text": payload_text}) + await ev("entities.extracted", minutes=(m := m + 0.02), payload={"entity_mentions": mentions}) + + # ---- Jev scoring + arbitration/fallback (two-stage, 787824a) + scored: list = [] + statuses = {"exact": 0, "ambiguity": 0, "new": 0} + actions: dict = {} + act_names = ["endorsed_jev", "endorsed_jev", "endorsed_jev", + "demoted_to_ambiguity", "vetoed_to_new"] + for i, men in enumerate(mentions): + field = men["field"] + jev_failed = i == 0 and rng.random() < 0.25 + if jev_failed: + await ev("jev.failure", minutes=(m := m + 0.1), field=field, ok=False, + payload={"error": "jev_unavailable_or_error", "n_mentions": 1}) + # crisis fallback path + status = rng.choice(["exact", "ambiguity", "new"]) + statuses[status] += 1 + continue + await ev("jev.request", minutes=(m := m + 0.05), field=field, + payload={"n_mentions": 1, "n_candidates": rng.randint(6, 20)}) + name = rng.choice(NAMES[field]) + conf = rng.choice([0.55, 0.68, 0.75, 0.86, 0.9, 0.95, 0.99]) + await ev("jev.result", minutes=(m := m + 0.9), field=field, model="jev-1.13.0", + latency=rng.randrange(600, 1300), + payload={"answers": {"mention_0": {"choice": name, "confidence": conf}}, + "usage": {"input_tokens": rng.randint(400, 2600), + "output_tokens": rng.randint(60, 1300)}}) + scored.append({"variants": men["variants"], "jev_choice": name, "confidence": conf}) + if scored: + await ev("routing.decision", minutes=(m := m + 0.02), payload={ + "source": "jev_scored", "mentions": scored}) + for _ in scored: + action = rng.choice(act_names) + actions[action] = actions.get(action, 0) + 1 + statuses["exact" if action == "endorsed_jev" + else "ambiguity" if action == "demoted_to_ambiguity" else "new"] += 1 + await ev("matching.arbitration.result", minutes=(m := m + 1.1), + latency=rng.randrange(1200, 3200), model="deepseek-chat", + payload={"statuses": statuses, "actions": actions, + "n_items": len(scored), "n_resolved": len(scored), + "usage": {"input_tokens": rng.randint(800, 3000), + "output_tokens": rng.randint(100, 900)}}) + else: + await ev("matching.fallback.result", minutes=(m := m + 1.2), + latency=rng.randrange(1400, 3400), model="deepseek-chat", + payload={"statuses": statuses, "n_mentions": len(mentions), + "n_resolved": max(len(mentions) - 1, 0), + "usage": {"input_tokens": rng.randint(900, 3200), + "output_tokens": rng.randint(120, 950)}}) + + # ---- ambiguity + user choice (the A/B proxy panels) + if statuses["ambiguity"] > 0: + await ev("ambiguity.sent", minutes=(m := m + 0.1), payload={ + "amb_id": "em_demo", "kind": "entity_match", + "candidates": [{"id": "x1", "name": "منشانه", "prob": 0.62}]}) + await ev("ambiguity.user_choice", minutes=(m := m + 2.5), payload={ + "amb_id": "em_demo", "action": "pick", "arg": "0", + "candidates": [{"id": "x1", "name": "منشانه"}]}) + await ev("user_choice.pick", minutes=(m := m + 0.05), payload={ + "field": "project", "idx": 0, "choice": "منشانه"}) + + # ---- draft lifecycle + await ev("draft.render", minutes=(m := m + 0.2), payload={"fresh": True, "missing": []}) + await ev("draft.commit", minutes=(m := m + 3.0), payload={ + "quote_id": str(uuid.uuid4()), "items": len(mentions) - 3, "newly_created": [], + "only_resolved": False}) + + +async def run(days: int) -> None: + async with SessionLocal() as s: + # idempotent per run-day: wipe only this script's demo rows for those days + await s.execute(text(""" + DELETE FROM bot_traces + WHERE source = 'demo' + AND created_at >= now() - (:days * interval '1 day') + """), {"days": days}) + base = datetime.now(timezone.utc).replace(hour=10, minute=0, second=0, microsecond=0) + n = 0 + for d in range(days): + for _ in range(rng.randint(2, 5)): # 2-5 traces/day + day0 = base - timedelta(days=d) + t0 = day0 + timedelta(minutes=rng.randrange(0, 600)) + await one_trace(s, rng.choice([1461166742, 1839839922]), t0, + rng.choice(["voice", "text"])) + n += 1 + await s.commit() + print(f"seeded {n} demo traces over {days} day(s) (source='demo')") + + +if __name__ == "__main__": + ap = argparse.ArgumentParser() + ap.add_argument("--days", type=int, default=7) + asyncio.run(run(ap.parse_args().days)) diff --git a/frontend/package-lock.json b/frontend/package-lock.json index f7d6e03..a3e2503 100644 --- a/frontend/package-lock.json +++ b/frontend/package-lock.json @@ -28,6 +28,7 @@ "react-dom": "^19.2.8", "react-hook-form": "^7.85.0", "react-router-dom": "^7.18.2", + "recharts": "^3.8.0", "sonner": "^2.0.8", "tailwind-merge": "^3.6.0", "tailwindcss": "^4.3.3", @@ -1366,6 +1367,32 @@ "integrity": "sha512-JtyZR+mqgBibTo8xea3B6ZRmzZiM/YeVBtUkas6zMuXjAlfIFIW2FgqeM9eLyvEaYX66vr6DJMK+4U6LV0KhNw==", "license": "MIT" }, + "node_modules/@reduxjs/toolkit": { + "version": "2.13.0", + "resolved": "https://registry.npmjs.org/@reduxjs/toolkit/-/toolkit-2.13.0.tgz", + "integrity": "sha512-f6jwVRfwW30+tnllz4YcsHHpPYYOE7abcHkyUCovD+P7DyhdAI5prz2Fh/8TLhakjpN+7aIEion1s5L/tvlYlA==", + "license": "MIT", + "dependencies": { + "@standard-schema/spec": "^1.0.0", + "@standard-schema/utils": "^0.3.0", + "immer": "^11.0.0", + "redux": "^5.0.1", + "redux-thunk": "^3.1.0", + "reselect": "^5.1.0" + }, + "peerDependencies": { + "react": "^16.9.0 || ^17.0.0 || ^18 || ^19", + "react-redux": "^7.2.1 || ^8.1.3 || ^9.0.0" + }, + "peerDependenciesMeta": { + "react": { + "optional": true + }, + "react-redux": { + "optional": true + } + } + }, "node_modules/@rolldown/binding-android-arm64": { "version": "1.2.3", "resolved": "https://registry.npmjs.org/@rolldown/binding-android-arm64/-/binding-android-arm64-1.2.3.tgz", @@ -1714,6 +1741,18 @@ "node": ">=18" } }, + "node_modules/@standard-schema/spec": { + "version": "1.1.0", + "resolved": "https://registry.npmjs.org/@standard-schema/spec/-/spec-1.1.0.tgz", + "integrity": "sha512-l2aFy5jALhniG5HgqrD6jXLi/rUWrKvqN/qJx6yoJsgKhblVd+iqqU4RCXavm/jPityDo5TCvKMnpjKnOriy0w==", + "license": "MIT" + }, + "node_modules/@standard-schema/utils": { + "version": "0.3.0", + "resolved": "https://registry.npmjs.org/@standard-schema/utils/-/utils-0.3.0.tgz", + "integrity": "sha512-e7Mew686owMaPJVNNLs55PUvgz371nKgwsc4vxE49zsODpJEnxgxRo2y/OKrqueavXgZNMDVj3DdHFlaSAeU8g==", + "license": "MIT" + }, "node_modules/@tailwindcss/node": { "version": "4.3.3", "resolved": "https://registry.npmjs.org/@tailwindcss/node/-/node-4.3.3.tgz", @@ -2003,6 +2042,69 @@ "integrity": "sha512-nl09VhutdjINdWyXxHWN/w9zlNCfr60JUqJbd24YXUuCwgeL0TpFSdElCwb6cxfB6ybE19Gjj4g0jsgkXxKv1Q==", "license": "MIT" }, + "node_modules/@types/d3-array": { + "version": "3.2.2", + "resolved": "https://registry.npmjs.org/@types/d3-array/-/d3-array-3.2.2.tgz", + "integrity": "sha512-hOLWVbm7uRza0BYXpIIW5pxfrKe0W+D5lrFiAEYR+pb6w3N2SwSMaJbXdUfSEv+dT4MfHBLtn5js0LAWaO6otw==", + "license": "MIT" + }, + "node_modules/@types/d3-color": { + "version": "3.1.3", + "resolved": "https://registry.npmjs.org/@types/d3-color/-/d3-color-3.1.3.tgz", + "integrity": "sha512-iO90scth9WAbmgv7ogoq57O9YpKmFBbmoEoCHDB2xMBY0+/KVrqAaCDyCE16dUspeOvIxFFRI+0sEtqDqy2b4A==", + "license": "MIT" + }, + "node_modules/@types/d3-ease": { + "version": "3.0.2", + "resolved": "https://registry.npmjs.org/@types/d3-ease/-/d3-ease-3.0.2.tgz", + "integrity": "sha512-NcV1JjO5oDzoK26oMzbILE6HW7uVXOHLQvHshBUW4UMdZGfiY6v5BeQwh9a9tCzv+CeefZQHJt5SRgK154RtiA==", + "license": "MIT" + }, + "node_modules/@types/d3-interpolate": { + "version": "3.0.4", + "resolved": "https://registry.npmjs.org/@types/d3-interpolate/-/d3-interpolate-3.0.4.tgz", + "integrity": "sha512-mgLPETlrpVV1YRJIglr4Ez47g7Yxjl1lj7YKsiMCb27VJH9W8NVM6Bb9d8kkpG/uAQS5AmbA48q2IAolKKo1MA==", + "license": "MIT", + "dependencies": { + "@types/d3-color": "*" + } + }, + "node_modules/@types/d3-path": { + "version": "3.1.1", + "resolved": "https://registry.npmjs.org/@types/d3-path/-/d3-path-3.1.1.tgz", + "integrity": "sha512-VMZBYyQvbGmWyWVea0EHs/BwLgxc+MKi1zLDCONksozI4YJMcTt8ZEuIR4Sb1MMTE8MMW49v0IwI5+b7RmfWlg==", + "license": "MIT" + }, + "node_modules/@types/d3-scale": { + "version": "4.0.9", + "resolved": "https://registry.npmjs.org/@types/d3-scale/-/d3-scale-4.0.9.tgz", + "integrity": "sha512-dLmtwB8zkAeO/juAMfnV+sItKjlsw2lKdZVVy6LRr0cBmegxSABiLEpGVmSJJ8O08i4+sGR6qQtb6WtuwJdvVw==", + "license": "MIT", + "dependencies": { + "@types/d3-time": "*" + } + }, + "node_modules/@types/d3-shape": { + "version": "3.2.0", + "resolved": "https://registry.npmjs.org/@types/d3-shape/-/d3-shape-3.2.0.tgz", + "integrity": "sha512-kVd74ta9eof3eJOvbNd1vGKS/XERRyQbT26Og63hIsvDO84cjD5gEOhsXf26w3FSoNlPVz84DOFcKv/oou+fMw==", + "license": "MIT", + "dependencies": { + "@types/d3-path": "*" + } + }, + "node_modules/@types/d3-time": { + "version": "3.0.4", + "resolved": "https://registry.npmjs.org/@types/d3-time/-/d3-time-3.0.4.tgz", + "integrity": "sha512-yuzZug1nkAAaBlBBikKZTgzCeA+k1uy4ZFwWANOfKw5z5LRhV0gNA7gNkKm7HoK+HRN0wX3EkxGk0fpbWhmB7g==", + "license": "MIT" + }, + "node_modules/@types/d3-timer": { + "version": "3.0.2", + "resolved": "https://registry.npmjs.org/@types/d3-timer/-/d3-timer-3.0.2.tgz", + "integrity": "sha512-Ps3T8E8dZDam6fUyNiMkekK3XUsaUEik+idO9/YjPtfj2qruF8tFBXS7XhtE4iIXBLxhmLjP3SXpLhVf21I9Lw==", + "license": "MIT" + }, "node_modules/@types/node": { "version": "24.13.3", "resolved": "https://registry.npmjs.org/@types/node/-/node-24.13.3.tgz", @@ -2033,6 +2135,12 @@ "@types/react": "^19.2.0" } }, + "node_modules/@types/use-sync-external-store": { + "version": "0.0.6", + "resolved": "https://registry.npmjs.org/@types/use-sync-external-store/-/use-sync-external-store-0.0.6.tgz", + "integrity": "sha512-zFDAD+tlpf2r4asuHEj0XH6pY6i0g5NeAHPn+15wk3BV6JA69eERFXC1gyGThDkVa1zCyKr5jox1+2LbV/AMLg==", + "license": "MIT" + }, "node_modules/@vitejs/plugin-react": { "version": "6.0.5", "resolved": "https://registry.npmjs.org/@vitejs/plugin-react/-/plugin-react-6.0.5.tgz", @@ -2127,6 +2235,133 @@ "devOptional": true, "license": "MIT" }, + "node_modules/d3-array": { + "version": "3.2.4", + "resolved": "https://registry.npmjs.org/d3-array/-/d3-array-3.2.4.tgz", + "integrity": "sha512-tdQAmyA18i4J7wprpYq8ClcxZy3SC31QMeByyCFyRt7BVHdREQZ5lpzoe5mFEYZUWe+oq8HBvk9JjpibyEV4Jg==", + "license": "ISC", + "dependencies": { + "internmap": "1 - 2" + }, + "engines": { + "node": ">=12" + } + }, + "node_modules/d3-color": { + "version": "3.1.0", + "resolved": "https://registry.npmjs.org/d3-color/-/d3-color-3.1.0.tgz", + "integrity": "sha512-zg/chbXyeBtMQ1LbD/WSoW2DpC3I0mpmPdW+ynRTj/x2DAWYrIY7qeZIHidozwV24m4iavr15lNwIwLxRmOxhA==", + "license": "ISC", + "engines": { + "node": ">=12" + } + }, + "node_modules/d3-ease": { + "version": "3.0.1", + "resolved": "https://registry.npmjs.org/d3-ease/-/d3-ease-3.0.1.tgz", + "integrity": "sha512-wR/XK3D3XcLIZwpbvQwQ5fK+8Ykds1ip7A2Txe0yxncXSdq1L9skcG7blcedkOX+ZcgxGAmLX1FrRGbADwzi0w==", + "license": "BSD-3-Clause", + "engines": { + "node": ">=12" + } + }, + "node_modules/d3-format": { + "version": "3.1.2", + "resolved": "https://registry.npmjs.org/d3-format/-/d3-format-3.1.2.tgz", + "integrity": "sha512-AJDdYOdnyRDV5b6ArilzCPPwc1ejkHcoyFarqlPqT7zRYjhavcT3uSrqcMvsgh2CgoPbK3RCwyHaVyxYcP2Arg==", + "license": "ISC", + "engines": { + "node": ">=12" + } + }, + "node_modules/d3-interpolate": { + "version": "3.0.1", + "resolved": "https://registry.npmjs.org/d3-interpolate/-/d3-interpolate-3.0.1.tgz", + "integrity": "sha512-3bYs1rOD33uo8aqJfKP3JWPAibgw8Zm2+L9vBKEHJ2Rg+viTR7o5Mmv5mZcieN+FRYaAOWX5SJATX6k1PWz72g==", + "license": "ISC", + "dependencies": { + "d3-color": "1 - 3" + }, + "engines": { + "node": ">=12" + } + }, + "node_modules/d3-path": { + "version": "3.1.0", + "resolved": "https://registry.npmjs.org/d3-path/-/d3-path-3.1.0.tgz", + "integrity": "sha512-p3KP5HCf/bvjBSSKuXid6Zqijx7wIfNW+J/maPs+iwR35at5JCbLUT0LzF1cnjbCHWhqzQTIN2Jpe8pRebIEFQ==", + "license": "ISC", + "engines": { + "node": ">=12" + } + }, + "node_modules/d3-scale": { + "version": "4.0.2", + "resolved": "https://registry.npmjs.org/d3-scale/-/d3-scale-4.0.2.tgz", + "integrity": "sha512-GZW464g1SH7ag3Y7hXjf8RoUuAFIqklOAq3MRl4OaWabTFJY9PN/E1YklhXLh+OQ3fM9yS2nOkCoS+WLZ6kvxQ==", + "license": "ISC", + "dependencies": { + "d3-array": "2.10.0 - 3", + "d3-format": "1 - 3", + "d3-interpolate": "1.2.0 - 3", + "d3-time": "2.1.1 - 3", + "d3-time-format": "2 - 4" + }, + "engines": { + "node": ">=12" + } + }, + "node_modules/d3-shape": { + "version": "3.2.0", + "resolved": "https://registry.npmjs.org/d3-shape/-/d3-shape-3.2.0.tgz", + "integrity": "sha512-SaLBuwGm3MOViRq2ABk3eLoxwZELpH6zhl3FbAoJ7Vm1gofKx6El1Ib5z23NUEhF9AsGl7y+dzLe5Cw2AArGTA==", + "license": "ISC", + "dependencies": { + "d3-path": "^3.1.0" + }, + "engines": { + "node": ">=12" + } + }, + "node_modules/d3-time": { + "version": "3.1.0", + "resolved": "https://registry.npmjs.org/d3-time/-/d3-time-3.1.0.tgz", + "integrity": "sha512-VqKjzBLejbSMT4IgbmVgDjpkYrNWUYJnbCGo874u7MMKIWsILRX+OpX/gTk8MqjpT1A/c6HY2dCA77ZN0lkQ2Q==", + "license": "ISC", + "dependencies": { + "d3-array": "2 - 3" + }, + "engines": { + "node": ">=12" + } + }, + "node_modules/d3-time-format": { + "version": "4.1.0", + "resolved": "https://registry.npmjs.org/d3-time-format/-/d3-time-format-4.1.0.tgz", + "integrity": "sha512-dJxPBlzC7NugB2PDLwo9Q8JiTR3M3e4/XANkreKSUxF8vvXKqm1Yfq4Q5dl8budlunRVlUUaDUgFt7eA8D6NLg==", + "license": "ISC", + "dependencies": { + "d3-time": "1 - 3" + }, + "engines": { + "node": ">=12" + } + }, + "node_modules/d3-timer": { + "version": "3.0.1", + "resolved": "https://registry.npmjs.org/d3-timer/-/d3-timer-3.0.1.tgz", + "integrity": "sha512-ndfJ/JxxMd3nw31uyKoY2naivF+r29V+Lc0svZxe1JvvIRmi8hUsrMvdOwgS1o6uBHmiz91geQ0ylPP0aj1VUA==", + "license": "ISC", + "engines": { + "node": ">=12" + } + }, + "node_modules/decimal.js-light": { + "version": "2.5.1", + "resolved": "https://registry.npmjs.org/decimal.js-light/-/decimal.js-light-2.5.1.tgz", + "integrity": "sha512-qIMFpTMZmny+MMIitAB6D7iVPEorVw6YQRWkvarTkT4tBeSLLiHzcwj6q0MmYSFCiVpiqPJTJEYIrpcPzVEIvg==", + "license": "MIT" + }, "node_modules/detect-libc": { "version": "2.1.2", "resolved": "https://registry.npmjs.org/detect-libc/-/detect-libc-2.1.2.tgz", @@ -2155,6 +2390,24 @@ "node": ">=10.13.0" } }, + "node_modules/es-toolkit": { + "version": "1.52.0", + "resolved": "https://registry.npmjs.org/es-toolkit/-/es-toolkit-1.52.0.tgz", + "integrity": "sha512-XTNEJQh1tY1ZJVcf6ayP/2n4ZPyaHlW2FWs7xvw5ddPuhUVjLD3olQVQS7kf58JbAB48iL0uL/jerTrjtV3lDA==", + "license": "MIT", + "workspaces": [ + "docs", + "benchmarks", + "tests/types", + "tests/browser-compat" + ] + }, + "node_modules/eventemitter3": { + "version": "5.0.4", + "resolved": "https://registry.npmjs.org/eventemitter3/-/eventemitter3-5.0.4.tgz", + "integrity": "sha512-mlsTRyGaPBjPedk6Bvw+aqbsXDtoAyAzm5MO7JgU+yVRyMQ5O8bD4Kcci7BS85f93veegeCPkL8R4GLClnjLFw==", + "license": "MIT" + }, "node_modules/fdir": { "version": "6.5.0", "resolved": "https://registry.npmjs.org/fdir/-/fdir-6.5.0.tgz", @@ -2201,6 +2454,25 @@ "integrity": "sha512-RbJ5/jmFcNNCcDV5o9eTnBLJ/HszWV0P73bc+Ff4nS/rJj+YaS6IGyiOL0VoBYX+l1Wrl3k63h/KrH+nhJ0XvQ==", "license": "ISC" }, + "node_modules/immer": { + "version": "11.2.9", + "resolved": "https://registry.npmjs.org/immer/-/immer-11.2.9.tgz", + "integrity": "sha512-IQ4UEtXjZjyefyWjtLEDXUSrF6T2hwjKPi6BIJl3bY/2XXW24d6cNGdXJSBOA/+KNnuiXkZY6DR0C3efolhitA==", + "license": "MIT", + "funding": { + "type": "opencollective", + "url": "https://opencollective.com/immer" + } + }, + "node_modules/internmap": { + "version": "2.0.3", + "resolved": "https://registry.npmjs.org/internmap/-/internmap-2.0.3.tgz", + "integrity": "sha512-5Hh7Y1wQbvY5ooGgPbDaL5iYLAPzMTUrjMulskHLH6wnv/A+1q5rgEaiuqEjB+oxGXIVZs1FF+R/KPN3ZSQYYg==", + "license": "ISC", + "engines": { + "node": ">=12" + } + }, "node_modules/jiti": { "version": "2.7.0", "resolved": "https://registry.npmjs.org/jiti/-/jiti-2.7.0.tgz", @@ -2645,6 +2917,36 @@ "react": "^16.8.0 || ^17 || ^18 || ^19" } }, + "node_modules/react-is": { + "version": "19.3.1", + "resolved": "https://registry.npmjs.org/react-is/-/react-is-19.3.1.tgz", + "integrity": "sha512-RRieF6IyxQBkcK6pP8xOHprLTPW6QjRkbGdImtqNme+/hYVwCZkLQLdlZGOhUqXwkS9IEynaGvPJtv6XB/cz0Q==", + "license": "MIT", + "peer": true + }, + "node_modules/react-redux": { + "version": "9.3.0", + "resolved": "https://registry.npmjs.org/react-redux/-/react-redux-9.3.0.tgz", + "integrity": "sha512-KQopgqFo/p/fgmAs5qz6p5RWaNAzq40WAu7fJIXnQpYxFPbJYtsJPWvGeF2rOBaY/kEuV77AVsX8TsQzKm+A/g==", + "license": "MIT", + "dependencies": { + "@types/use-sync-external-store": "^0.0.6", + "use-sync-external-store": "^1.4.0" + }, + "peerDependencies": { + "@types/react": "^18.2.25 || ^19", + "react": "^18.0 || ^19", + "redux": "^5.0.0" + }, + "peerDependenciesMeta": { + "@types/react": { + "optional": true + }, + "redux": { + "optional": true + } + } + }, "node_modules/react-remove-scroll": { "version": "2.7.2", "resolved": "https://registry.npmjs.org/react-remove-scroll/-/react-remove-scroll-2.7.2.tgz", @@ -2752,6 +3054,57 @@ } } }, + "node_modules/recharts": { + "version": "3.10.1", + "resolved": "https://registry.npmjs.org/recharts/-/recharts-3.10.1.tgz", + "integrity": "sha512-QXFrvt6IVcw7eeZCoyXTwkIJAX3Dv1nyVhMicXJ47GsGDDpcN8z6o644DibE9XjpBTThtsomLKnTV6lc+cVFUA==", + "license": "MIT", + "workspaces": [ + "www" + ], + "dependencies": { + "@reduxjs/toolkit": "^1.9.0 || 2.x.x", + "clsx": "^2.1.1", + "decimal.js-light": "^2.5.1", + "es-toolkit": "^1.39.3", + "eventemitter3": "^5.0.1", + "immer": "^11.1.8", + "react-redux": "8.x.x || 9.x.x", + "reselect": "5.2.0", + "tiny-invariant": "^1.3.3", + "use-sync-external-store": "^1.2.2", + "victory-vendor": "^37.0.2" + }, + "engines": { + "node": ">=18" + }, + "peerDependencies": { + "react": "^16.8.0 || ^17.0.0 || ^18.0.0 || ^19.0.0", + "react-dom": "^16.0.0 || ^17.0.0 || ^18.0.0 || ^19.0.0", + "react-is": "^16.8.0 || ^17.0.0 || ^18.0.0 || ^19.0.0" + } + }, + "node_modules/redux": { + "version": "5.0.1", + "resolved": "https://registry.npmjs.org/redux/-/redux-5.0.1.tgz", + "integrity": "sha512-M9/ELqF6fy8FwmkpnF0S3YKOqMyoWJ4+CS5Efg2ct3oY9daQvd/Pc71FpGZsVsbl3Cpb+IIcjBDUnnyBdQbq4w==", + "license": "MIT" + }, + "node_modules/redux-thunk": { + "version": "3.1.0", + "resolved": "https://registry.npmjs.org/redux-thunk/-/redux-thunk-3.1.0.tgz", + "integrity": "sha512-NW2r5T6ksUKXCabzhL9z+h206HQw/NJkcLm1GPImRQ8IzfXwRGqjVhKJGauHirT0DAuyy6hjdnMZaRoAcy0Klw==", + "license": "MIT", + "peerDependencies": { + "redux": "^5.0.0" + } + }, + "node_modules/reselect": { + "version": "5.2.0", + "resolved": "https://registry.npmjs.org/reselect/-/reselect-5.2.0.tgz", + "integrity": "sha512-AgZ3UOZm3YndfrJ4OYjgrT7bmCm/1iqkjvEfH/oYjzh6PD2qw4QuT3jjnXIrpdt4MTpMXclMT3lXbmRY+XRakw==", + "license": "MIT" + }, "node_modules/rolldown": { "version": "1.2.3", "resolved": "https://registry.npmjs.org/rolldown/-/rolldown-1.2.3.tgz", @@ -2884,6 +3237,12 @@ "url": "https://opencollective.com/webpack" } }, + "node_modules/tiny-invariant": { + "version": "1.3.3", + "resolved": "https://registry.npmjs.org/tiny-invariant/-/tiny-invariant-1.3.3.tgz", + "integrity": "sha512-+FbBPE1o9QAYvviau/qC5SE3caw21q3xkvWKBtja5vgqOWIHHJ3ioaq1VPfn/Szqctz2bU/oYeKd9/z5BL+PVg==", + "license": "MIT" + }, "node_modules/tinyglobby": { "version": "0.2.17", "resolved": "https://registry.npmjs.org/tinyglobby/-/tinyglobby-0.2.17.tgz", @@ -2970,6 +3329,37 @@ } } }, + "node_modules/use-sync-external-store": { + "version": "1.7.0", + "resolved": "https://registry.npmjs.org/use-sync-external-store/-/use-sync-external-store-1.7.0.tgz", + "integrity": "sha512-6L+EeigHMQhdaIPNIFUKwfWJSwWFQ8gJbJ2DLOs5sDIegTwR9fRxvnM3uciHKjIZhFz+KAv2emhWMRvDmMcY8A==", + "license": "MIT", + "peerDependencies": { + "react": "^16.8.0 || ^17.0.0 || ^18.0.0 || ^19.0.0" + } + }, + "node_modules/victory-vendor": { + "version": "37.3.6", + "resolved": "https://registry.npmjs.org/victory-vendor/-/victory-vendor-37.3.6.tgz", + "integrity": "sha512-SbPDPdDBYp+5MJHhBCAyI7wKM3d5ivekigc2Dk2s7pgbZ9wIgIBYGVw4zGHBml/qTFbexrofXW6Gu4noGxrOwQ==", + "license": "MIT AND ISC", + "dependencies": { + "@types/d3-array": "^3.0.3", + "@types/d3-ease": "^3.0.0", + "@types/d3-interpolate": "^3.0.1", + "@types/d3-scale": "^4.0.2", + "@types/d3-shape": "^3.1.0", + "@types/d3-time": "^3.0.0", + "@types/d3-timer": "^3.0.0", + "d3-array": "^3.1.6", + "d3-ease": "^3.0.1", + "d3-interpolate": "^3.0.1", + "d3-scale": "^4.0.2", + "d3-shape": "^3.1.0", + "d3-time": "^3.0.0", + "d3-timer": "^3.0.1" + } + }, "node_modules/vite": { "version": "8.2.1", "resolved": "https://registry.npmjs.org/vite/-/vite-8.2.1.tgz", diff --git a/frontend/package.json b/frontend/package.json index 399a320..c759395 100644 --- a/frontend/package.json +++ b/frontend/package.json @@ -30,6 +30,7 @@ "react-dom": "^19.2.8", "react-hook-form": "^7.85.0", "react-router-dom": "^7.18.2", + "recharts": "^3.8.0", "sonner": "^2.0.8", "tailwind-merge": "^3.6.0", "tailwindcss": "^4.3.3", diff --git a/frontend/src/App.tsx b/frontend/src/App.tsx index d9eb7f0..15e9301 100644 --- a/frontend/src/App.tsx +++ b/frontend/src/App.tsx @@ -43,6 +43,9 @@ const OrganizationPage = lazy(() => const AdminPanelPage = lazy(() => import("./pages/Admin/AdminPanel").then((m) => ({ default: m.AdminPanel })), ); +const BotMonitorPage = lazy(() => + import("./pages/BotMonitor/BotMonitorPage").then((m) => ({ default: m.BotMonitorPage })), +); function Loading() { return ( @@ -220,6 +223,14 @@ export default function App() { } /> + + + + } + /> } /> diff --git a/frontend/src/components/layout/MobileTabBar.tsx b/frontend/src/components/layout/MobileTabBar.tsx index fcb4db0..192f343 100644 --- a/frontend/src/components/layout/MobileTabBar.tsx +++ b/frontend/src/components/layout/MobileTabBar.tsx @@ -80,7 +80,7 @@ export function MobileTabBar() { const role = useAuthStore((s) => s.user?.role); const { pathname } = useLocation(); - if (pathname === "/login" || pathname === "/admin") return null; + if (pathname === "/login" || pathname === "/admin" || pathname === "/bot-monitor") return null; const isPM = role === "pm"; const isOffice = role === "admin" || role === "procurement_manager"; diff --git a/frontend/src/lib/api.ts b/frontend/src/lib/api.ts index 7fdfdea..8adf788 100644 --- a/frontend/src/lib/api.ts +++ b/frontend/src/lib/api.ts @@ -1604,6 +1604,79 @@ export interface UserRow { created_at: string; } +// ============================================================ +// مانیتورینگ بات (bot-metrics) — شکل پاسخ‌های فقط-خواندنی +// ============================================================ +export interface BotMetricsKpis { + traces: number; + events: number; + errors: number; + jev_p95_ms: number | null; + commits: number; + tokens_in: number; + tokens_out: number; +} +export interface BotMetricsSummary { + kpis: BotMetricsKpis; + daily: { day: string; events: number; traces: number }[]; + days: number; +} +export interface BotMetricsRouting { + statuses: { metric: string; n: number }[]; + actions: { + endorsed_jev: number; + vetoed_to_new: number; + demoted_to_ambiguity: number; + other: number; + invocations: number; + }; + stages: { stage: string; n: number }[]; +} +export interface BotMetricsQuality { + ambiguities: { action: string; arg: string; n: number }[]; + picks: { pick: "top" | "other"; n: number }[]; + drafts: { stage: string; n: number }[]; +} +export interface BotMetricsJev { + models: { model: string; n: number; last_seen: string }[]; + latency: { stage: string; p50: number; p95: number; n: number }[]; + tokens_daily: { day: string; tokens_in: number; tokens_out: number }[]; +} +export interface BotMetricsStt { + providers: { provider: string; ok: number; total: number; p50_ms: number }[]; + enhance: { ok: number; total: number; p50_ms: number } | null; +} +export interface BotTraceRow { + id: string; + created_at: string; + source: string; + chat_id: string; + kind: string; + status: string; + events: number; +} +export interface BotTraceDetail { + trace: { + id: string; + created_at: string; + source: string; + chat_id: string; + kind: string; + status: string; + draft_id: string | null; + committed_quote_id: string | null; + } | null; + events: { + created_at: string; + stage: string; + ok: boolean; + latency_ms: number | null; + model: string | null; + field: string | null; + payload: Record; + }[]; +} + export const apiV26 = { auth: { // ورود درون‌صفحه‌ای: بک‌اند با grant_type=password از Casdoor توکن می‌گیرد @@ -1796,4 +1869,30 @@ export const apiV26 = { attributes: [], }), }, + + // مانیتورینگ بات بله — فقط-خواندنی از جدول‌های bot_traces/bot_trace_events + // (فقط ادمین؛ بک‌اند با require_role("admin") گارد می‌کند). آمار تجمیعی فقط + // ترافیک واقعی را می‌شمارد (calibration/demo/test حذف می‌شوند). + botMetrics: { + summary: (days: number) => + getData(`/bot-metrics/summary?days=${days}`), + routing: (days: number) => + getData(`/bot-metrics/routing?days=${days}`), + quality: (days: number) => + getData(`/bot-metrics/quality?days=${days}`), + jev: (days: number) => + getData(`/bot-metrics/jev?days=${days}`), + stt: (days: number) => + getData(`/bot-metrics/stt?days=${days}`), + traces: (opts?: { limit?: number; status?: string | null; include_synthetic?: boolean }) => { + const q = new URLSearchParams(); + if (opts?.limit) q.set("limit", String(opts.limit)); + if (opts?.status) q.set("status", opts.status); + if (opts?.include_synthetic) q.set("include_synthetic", "true"); + const qs = q.toString(); + return getData(`/bot-metrics/traces${qs ? `?${qs}` : ""}`); + }, + traceDetail: (traceId: string) => + getData(`/bot-metrics/traces/${encodeURIComponent(traceId)}`), + }, }; diff --git a/frontend/src/pages/Admin/AdminPanel.tsx b/frontend/src/pages/Admin/AdminPanel.tsx index ff37a2d..3cbe0c4 100644 --- a/frontend/src/pages/Admin/AdminPanel.tsx +++ b/frontend/src/pages/Admin/AdminPanel.tsx @@ -1,6 +1,6 @@ import { useCallback, useEffect, useState } from "react"; import { useNavigate } from "react-router-dom"; -import { ShieldCheck, Warehouse as WarehouseIcon } from "lucide-react"; +import { ShieldCheck, Warehouse as WarehouseIcon, Bot as BotIcon } from "lucide-react"; import { toast } from "sonner"; import { Header, PageLayout } from "../../components/layout/Header"; import { EmptyState } from "../../components/EmptyState"; @@ -170,13 +170,22 @@ export function AdminPanel() { )} - +
+ + +
); diff --git a/frontend/src/pages/BotMonitor/BotMonitorPage.tsx b/frontend/src/pages/BotMonitor/BotMonitorPage.tsx new file mode 100644 index 0000000..df5aac5 --- /dev/null +++ b/frontend/src/pages/BotMonitor/BotMonitorPage.tsx @@ -0,0 +1,592 @@ +import { useState } from "react"; +import { useQuery, useQueryClient } from "@tanstack/react-query"; +import { Bot, X } from "lucide-react"; +import { + ResponsiveContainer, + BarChart, + Bar, + XAxis, + YAxis, + Tooltip, + CartesianGrid, + PieChart, + Pie, + Cell, +} from "recharts"; +import { Header, PageLayout } from "../../components/layout/Header"; +import { EmptyState } from "../../components/EmptyState"; +import { AppSwitcherMenu } from "../../components/AppSwitcherMenu"; +import { inventorySubheader } from "../Inventory/panelMenu"; +import { useAuthStore } from "../../store/auth"; +import { apiV26, type BotTraceRow, type BotTraceDetail } from "../../lib/api"; +import { faNum, fmtDateTime } from "../../lib/formatters"; + +// داشبورد مانیتورینگ بات بله (فقط ادمین) — خواندنی از bot-metrics. +// داده: همان جدول‌های Observability بات؛ گرفانا همان اطلاعات را بیرون از ERP +// نشان می‌دهد، این صفحه نسخه‌ی فارسی/RTL داخل خود سیستم است. +// آمار فقط ترافیک واقعی را می‌شمارد (calibration/demo/test حذف می‌شوند). + +const DAY_OPTIONS = [1, 7, 30, 90] as const; + +const STATUS_LABELS: Record = { + exact: "اتصال دقیق", + ambiguity: "ابهام (پرسش از کاربر)", + new: "موجودیت جدید", +}; + +const STATUS_COLORS: Record = { + exact: "#16a34a", + ambiguity: "#d97706", + new: "#64748b", +}; + +const STAGE_LABELS: Record = { + "jev.request": "درخواست Jev", + "jev.result": "پاسخ Jev", + "jev.failure": "خطای Jev", + "matching.arbitration.result": "داور DeepSeek", + "matching.fallback.result": "فالبک بحرانی", + "draft.render": "نمایش پیش‌نویس", + "draft.commit": "ثبت نهایی", + "draft.cancel": "لغو", + "ambiguity.sent": "ارسال ابهام", +}; + +function Card({ + label, + value, + hint, + tone = "default", +}: { + label: string; + value: string; + hint?: string; + tone?: "default" | "warn" | "good"; +}) { + const toneCls = + tone === "warn" + ? "border-red-500/25 bg-red-500/5" + : tone === "good" + ? "border-emerald-500/25 bg-emerald-500/5" + : "border-line bg-panel"; + return ( +
+
{label}
+
{value}
+ {hint ?
{hint}
: null} +
+ ); +} + +function Panel({ + title, + children, + className = "", +}: { + title: string; + children: React.ReactNode; + className?: string; +}) { + return ( +
+

{title}

+ {children} +
+ ); +} + +const tooltipStyle = { + backgroundColor: "var(--color-panel, #111827)", + border: "1px solid rgba(148,163,184,.25)", + borderRadius: 8, + fontSize: 12, +}; + +export function BotMonitorPage() { + const user = useAuthStore((s) => s.user); + const isAdmin = user?.role === "admin"; + const [days, setDays] = useState(7); + const [showSynthetic, setShowSynthetic] = useState(false); + const [openTrace, setOpenTrace] = useState(null); + const qc = useQueryClient(); + + const summary = useQuery({ + queryKey: ["bot-metrics", "summary", days], + queryFn: () => apiV26.botMetrics.summary(days), + enabled: isAdmin, + }); + const routing = useQuery({ + queryKey: ["bot-metrics", "routing", days], + queryFn: () => apiV26.botMetrics.routing(days), + enabled: isAdmin, + }); + const quality = useQuery({ + queryKey: ["bot-metrics", "quality", days], + queryFn: () => apiV26.botMetrics.quality(days), + enabled: isAdmin, + }); + const jev = useQuery({ + queryKey: ["bot-metrics", "jev", days], + queryFn: () => apiV26.botMetrics.jev(days), + enabled: isAdmin, + }); + const stt = useQuery({ + queryKey: ["bot-metrics", "stt", days], + queryFn: () => apiV26.botMetrics.stt(days), + enabled: isAdmin, + }); + const traces = useQuery({ + queryKey: ["bot-metrics", "traces", showSynthetic], + queryFn: () => apiV26.botMetrics.traces({ limit: 50, include_synthetic: showSynthetic }), + enabled: isAdmin, + }); + const traceDetail = useQuery({ + queryKey: ["bot-metrics", "trace", openTrace], + queryFn: () => apiV26.botMetrics.traceDetail(openTrace!), + enabled: isAdmin && !!openTrace, + }); + + if (!isAdmin) { + return ( +
+
} + menu={} + /> + + + +
+ ); + } + + const k = summary.data?.kpis; + const pieData = (routing.data?.statuses ?? []).map((s) => ({ + name: STATUS_LABELS[s.metric] ?? s.metric, + value: s.n, + key: s.metric, + })); + const actions = routing.data?.actions; + const top = quality.data?.picks.find((p) => p.pick === "top")?.n ?? 0; + const other = quality.data?.picks.find((p) => p.pick === "other")?.n ?? 0; + const pinnedModel = "jev-1.13.0"; + const drifted = (jev.data?.models ?? []).some((m) => m.model !== "?" && m.model !== pinnedModel); + const loading = summary.isLoading || routing.isLoading; + + return ( +
+
} + menu={} + actions={ +
+ {DAY_OPTIONS.map((d) => ( + + ))} + +
+ } + /> + + {loading ? ( +
در حال بارگذاری…
+ ) : ( + <> + {/* ردیف KPI */} +
+ + + 0 ? "warn" : "good"} + /> + + + + +
+ + {/* نمودارها */} +
+ + {(summary.data?.daily ?? []).length === 0 ? ( + + ) : ( +
+ + + + + + + + + +
+ )} +
+ + + {pieData.length === 0 || pieData.every((p) => p.value === 0) ? ( + + ) : ( +
+
+ + + + {pieData.map((d) => ( + + ))} + + + + +
+
    + {pieData.map((d) => ( +
  • + + + {d.name} + + {faNum(d.value)} +
  • + ))} +
+
+ )} +
+
+ +
+ {/* داور و کیفیت */} + + + + + + + + + + + + + + + + + + + + +
تأیید پیشنهاد Jev{faNum(actions?.endorsed_jev ?? 0)}
وتو → ساخت جدید{faNum(actions?.vetoed_to_new ?? 0)}
تنزل → ابهام{faNum(actions?.demoted_to_ambiguity ?? 0)}
فراخوانی داور{faNum(actions?.invocations ?? 0)}
+
+ + +
+
+ گزینه‌ی اول (پیشنهاد مدل) + {faNum(top)} +
+
+ گزینه‌ی غیراول + {faNum(other)} +
+ {(quality.data?.ambiguities ?? []).map((a, i) => ( +
+ + {a.action === "pick" ? "انتخاب کاندیدا" : a.action === "new" ? "«هیچ‌کدام»" : a.action} + {a.arg !== "-" ? ` (${a.arg})` : ""} + + {faNum(a.n)} +
+ ))} +
+
+ + + + + + + + + + + + {(stt.data?.providers ?? []).map((p) => ( + + + + + + ))} + {stt.data?.enhance ? ( + + + + + + ) : null} + +
پرووایدرموفقp50
{p.provider} + {faNum(p.ok)}/{faNum(p.total)} + {p.p50_ms ? `${faNum(p.p50_ms)}ms` : "—"}
پیش‌پردازش صدا + {faNum(stt.data.enhance.ok)}/{faNum(stt.data.enhance.total)} + {stt.data.enhance.p50_ms ? `${faNum(stt.data.enhance.p50_ms)}ms` : "—"}
+
+
+ +
+ + + + {(jev.data?.latency ?? []).map((l) => ( + + + + + + ))} + {(jev.data?.latency ?? []).length === 0 ? ( + + + + ) : null} + +
{STAGE_LABELS[l.stage] ?? l.stage}{faNum(l.p50)} / {faNum(l.p95)}{faNum(l.n)}×
داده‌ای ثبت نشده
+
+ + + {(jev.data?.tokens_daily ?? []).length === 0 ? ( + + ) : ( +
+ + + + + + + + + + +
+ )} +
+
+ + {/* جدول ترس‌ها */} + + + {traces.isLoading ? ( +
در حال بارگذاری…
+ ) : (traces.data ?? []).length === 0 ? ( + + ) : ( +
+ + + + + + + + + + + + + {(traces.data ?? []).map((t: BotTraceRow) => ( + setOpenTrace(t.id)} + className="cursor-pointer border-b border-line last:border-0 hover:bg-slate-bg" + title={t.id} + > + + + + + + + + ))} + +
زمانمنبعچتنوعوضعیترخداد
{fmtDateTime(t.created_at)}{t.source}{t.chat_id}{t.kind}{t.status}{faNum(t.events)}
+
+ )} +
+ + )} +
+ + {/* درایور جزئیات ترس */} + {openTrace ? ( + { + setOpenTrace(null); + void qc.removeQueries({ queryKey: ["bot-metrics", "trace"] }); + }} + /> + ) : null} +
+ ); +} + +function TraceDrawer({ + traceId, + detail, + loading, + onClose, +}: { + traceId: string; + detail: BotTraceDetail | null; + loading: boolean; + onClose: () => void; +}) { + return ( +
+
+ +
+ ); +} diff --git a/monitoring/README.md b/monitoring/README.md index 8d73e13..ba8ac49 100644 --- a/monitoring/README.md +++ b/monitoring/README.md @@ -87,12 +87,48 @@ costs; low value for a small internal team at launch). If wanted later: enable OpenPanel replay with **10–25% sampling** and review retention — budget +1 GB storage and re-check RAM headroom first. +## Bot observability dashboards (Bale bot) + +Two Grafana dashboards read the bot's own tables (`bot_traces` / +`bot_trace_events`) directly — no metrics pipeline, no data duplication: + +- **بات — Observability خط لوله** (`bot-observability.json`): KPI row (real + traffic, events, Jev/fallback errors, Jev p95, tokens, commits, monthly + archive guard), events/day, per-stage p50/p95 latency, final mention + statuses (exact/ambiguity/new — arbiter + crisis-fallback aggregates plus + the legacy per-mention rows), DeepSeek arbiter actions on Jev suggestions, + tokens/day, model drift vs the pinned `jev-1.13.0`, user-choice A/B proxy, + STT + audio-enhancement health, archive size guard (>5 GB). +- **بات — اکسپلورر ترس‌ها** (`bot-trace-explorer.json`): latest traces table + + full event list of one trace (paste its `trace_id` in the dashboard + variable). + +Setup (one-time): + +1. `PROCUREMENT_BOT_OBS_READER_PASSWORD=` in `.env` — the **bot** + provisions the read-only role `obs_reader` at startup (`USAGE` on schema + `public` + `SELECT` on the two tables only; empty value = role skipped). +2. Start/restart grafana after the bot ran once — the `BotPostgres` datasource + (uid `bot-postgres`) is provisioned from `datasources.yml` via the grafana + service env; grafana reaches postgres over the `shared` (erp_shared) + network. + +Aggregate panels count **real traffic only** (`source` not in +calibration/demo and `chat_id ≠ 0`). To fill every panel on a fresh install, +seed synthetic traces: `docker compose exec bot python +tools/seed_observability_demo.py --days 7` (marked `source='demo'`). + +The same data is also available inside the ERP itself (admin-only, Persian +RTL): **/bot-monitor** — served by `GET /api/v1/bot-metrics/*` +(`backend/app/routers/bot_metrics.py`). + ## Alerting `monitoring/prometheus/alerts.yml` defines: API down, 5xx > 5%, p95 > 2 s, root disk < 10%, host RAM < 10%, Postgres > 80 connections. They appear in Grafana alerting; wire a notification channel (email/Telegram) from the -Grafana UI when ready. +Grafana UI when ready. (Bot observability alerts are deliberately NOT part +of this update — display only, per owner decision 2026-10-24.) ## Lean mode (if RAM gets tight) diff --git a/monitoring/docker-compose.monitoring.yml b/monitoring/docker-compose.monitoring.yml index 7f5c863..38dc4df 100644 --- a/monitoring/docker-compose.monitoring.yml +++ b/monitoring/docker-compose.monitoring.yml @@ -94,6 +94,12 @@ services: GF_SECURITY_ADMIN_PASSWORD: ${GRAFANA_ADMIN_PASSWORD:?set GRAFANA_ADMIN_PASSWORD in .env} GF_USERS_ALLOW_SIGN_UP: "false" GF_SERVER_ROOT_URL: ${GRAFANA_ROOT_URL:-http://localhost:3001/} + # Bot-observability Postgres datasource (provisioned from datasources.yml) + OBS_DB_HOST: postgres + OBS_DB_PORT: "5432" + OBS_DB_NAME: procurement + OBS_DB_USER: ${OBS_READER_USER:-obs_reader} + OBS_DB_PASSWORD: ${PROCUREMENT_BOT_OBS_READER_PASSWORD:-} volumes: - grafana_data:/var/lib/grafana - ./grafana/provisioning:/etc/grafana/provisioning:ro @@ -102,6 +108,7 @@ services: - "127.0.0.1:3001:3000" networks: - monitoring + - shared # reaches the app postgres for the bot_traces/bot_trace_events datasource restart: unless-stopped # ------------------------------------------------------------------ logs -- diff --git a/monitoring/grafana/dashboards/bot-observability.json b/monitoring/grafana/dashboards/bot-observability.json new file mode 100644 index 0000000..c65693f --- /dev/null +++ b/monitoring/grafana/dashboards/bot-observability.json @@ -0,0 +1,144 @@ +{ + "annotations": { "list": [] }, + "editable": true, + "graphTooltip": 1, + "refresh": "5m", + "schemaVersion": 41, + "tags": ["erp", "bot"], + "time": { "from": "now-7d", "to": "now" }, + "timezone": "browser", + "title": "بات — Observability خط لوله", + "uid": "bot-observability", + "version": 1, + "panels": [ + { "type": "row", "title": "نمای کلی", "gridPos": { "h": 1, "w": 24, "x": 0, "y": 0 }, "id": 100 }, + { + "type": "stat", "title": "روندها (ترافیک واقعی)", "id": 1, + "datasource": { "type": "postgres", "uid": "bot-postgres" }, + "gridPos": { "h": 4, "w": 4, "x": 0, "y": 1 }, + "targets": [{ "refId": "A", "format": "table", "rawSql": "SELECT count(*) AS value FROM bot_traces WHERE coalesce(source,'bale') NOT IN ('calibration','demo') AND $__timeFilter(created_at)" }], + "fieldConfig": { "defaults": { "color": { "mode": "thresholds" }, "thresholds": { "steps": [ { "color": "green", "value": null } ] } }, "overrides": [] } + }, + { + "type": "stat", "title": "رخدادها", "id": 2, + "datasource": { "type": "postgres", "uid": "bot-postgres" }, + "gridPos": { "h": 4, "w": 4, "x": 4, "y": 1 }, + "targets": [{ "refId": "A", "format": "table", "rawSql": "SELECT count(*) AS value FROM bot_trace_events e JOIN bot_traces t ON e.trace_id=t.id WHERE coalesce(t.source,'bale') NOT IN ('calibration','demo') AND $__timeFilter(e.created_at)" }] + }, + { + "type": "stat", "title": "خطای Jev / فالبک", "id": 3, + "datasource": { "type": "postgres", "uid": "bot-postgres" }, + "gridPos": { "h": 4, "w": 4, "x": 8, "y": 1 }, + "targets": [{ "refId": "A", "format": "table", "rawSql": "SELECT count(*) AS value FROM bot_trace_events e JOIN bot_traces t ON e.trace_id=t.id WHERE (e.stage='jev.failure' OR (e.stage='jev.result' AND NOT e.ok) OR e.stage LIKE 'matching.%.failure') AND coalesce(t.source,'bale') NOT IN ('calibration','demo') AND $__timeFilter(e.created_at)" }], + "fieldConfig": { "defaults": { "color": { "mode": "thresholds" }, "thresholds": { "steps": [ { "color": "green", "value": null }, { "color": "red", "value": 1 } ] } }, "overrides": [] } + }, + { + "type": "stat", "title": "p95 تأخیر Jev (ms)", "id": 4, + "datasource": { "type": "postgres", "uid": "bot-postgres" }, + "gridPos": { "h": 4, "w": 4, "x": 12, "y": 1 }, + "targets": [{ "refId": "A", "format": "table", "rawSql": "SELECT coalesce(round(percentile_cont(0.95) WITHIN GROUP (ORDER BY e.latency_ms)),0) AS value FROM bot_trace_events e JOIN bot_traces t ON e.trace_id=t.id WHERE e.stage='jev.result' AND e.latency_ms IS NOT NULL AND coalesce(t.source,'bale') NOT IN ('calibration','demo') AND $__timeFilter(e.created_at)" }], + "fieldConfig": { "defaults": { "unit": "ms" }, "overrides": [] } + }, + { + "type": "stat", "title": "ثبت نهایی پیش‌نویس", "id": 5, + "datasource": { "type": "postgres", "uid": "bot-postgres" }, + "gridPos": { "h": 4, "w": 4, "x": 16, "y": 1 }, + "targets": [{ "refId": "A", "format": "table", "rawSql": "SELECT count(*) AS value FROM bot_trace_events e JOIN bot_traces t ON e.trace_id=t.id WHERE e.stage='draft.commit' AND coalesce(t.source,'bale') NOT IN ('calibration','demo') AND $__timeFilter(e.created_at)" }] + }, + { + "type": "stat", "title": "رخداد این ماه (گارد آرشیو ۱۰۰k)", "id": 6, + "datasource": { "type": "postgres", "uid": "bot-postgres" }, + "gridPos": { "h": 4, "w": 4, "x": 20, "y": 1 }, + "targets": [{ "refId": "A", "format": "table", "rawSql": "SELECT count(*) AS value FROM bot_trace_events WHERE created_at >= date_trunc('month', now())" }], + "fieldConfig": { "defaults": { "color": { "mode": "thresholds" }, "thresholds": { "steps": [ { "color": "green", "value": null }, { "color": "orange", "value": 50000 }, { "color": "red", "value": 100000 } ] } }, "overrides": [] } + }, + + { "type": "row", "title": "حجم و تأخیر", "gridPos": { "h": 1, "w": 24, "x": 0, "y": 5 }, "id": 101 }, + { + "type": "timeseries", "title": "رخداد در روز (ترافیک واقعی)", "id": 10, + "datasource": { "type": "postgres", "uid": "bot-postgres" }, + "gridPos": { "h": 8, "w": 12, "x": 0, "y": 6 }, + "targets": [{ "refId": "A", "format": "time_series", "rawSql": "SELECT $__timeGroupAlias(e.created_at,'1d'), count(*) AS value FROM bot_trace_events e JOIN bot_traces t ON e.trace_id=t.id WHERE coalesce(t.source,'bale') NOT IN ('calibration','demo') AND $__timeFilter(e.created_at) GROUP BY 1 ORDER BY 1" }], + "fieldConfig": { "defaults": { "custom": { "drawStyle": "bars", "fillOpacity": 60, "lineWidth": 1 } }, "overrides": [] } + }, + { + "type": "timeseries", "title": "تأخیر p50/p95 به تفکیک مرحله", "id": 11, + "datasource": { "type": "postgres", "uid": "bot-postgres" }, + "gridPos": { "h": 8, "w": 12, "x": 12, "y": 6 }, + "targets": [{ "refId": "A", "format": "time_series", "rawSql": "SELECT $__timeGroupAlias(e.created_at,'1h'), round(percentile_cont(0.5) WITHIN GROUP (ORDER BY e.latency_ms)) AS p50, round(percentile_cont(0.95) WITHIN GROUP (ORDER BY e.latency_ms)) AS p95, e.stage AS metric FROM bot_trace_events e JOIN bot_traces t ON e.trace_id=t.id WHERE e.stage IN ('jev.result','nlu.result','stt.result','audio.enhance.result','matching.arbitration.result') AND e.latency_ms IS NOT NULL AND coalesce(t.source,'bale') NOT IN ('calibration','demo') AND $__timeFilter(e.created_at) GROUP BY 1, e.stage ORDER BY 1" }], + "fieldConfig": { "defaults": { "unit": "ms" }, "overrides": [] } + }, + + { "type": "row", "title": "تطبیق موجودیت (Jev + داور DeepSeek)", "gridPos": { "h": 1, "w": 24, "x": 0, "y": 14 }, "id": 102 }, + { + "type": "piechart", "title": "وضعیت نهایی mentionها (exact/ambiguity/new)", "id": 20, + "datasource": { "type": "postgres", "uid": "bot-postgres" }, + "gridPos": { "h": 8, "w": 8, "x": 0, "y": 15 }, + "targets": [{ "refId": "A", "format": "table", "rawSql": "SELECT 'exact' AS metric, sum(coalesce((e.payload->'statuses'->>'exact')::int,0)) AS value FROM bot_trace_events e JOIN bot_traces t ON e.trace_id=t.id WHERE e.stage IN ('matching.arbitration.result','matching.fallback.result') AND coalesce(t.source,'bale') NOT IN ('calibration','demo') AND $__timeFilter(e.created_at) UNION ALL SELECT 'ambiguity', sum(coalesce((e.payload->'statuses'->>'ambiguity')::int,0)) FROM bot_trace_events e JOIN bot_traces t ON e.trace_id=t.id WHERE e.stage IN ('matching.arbitration.result','matching.fallback.result') AND coalesce(t.source,'bale') NOT IN ('calibration','demo') AND $__timeFilter(e.created_at) UNION ALL SELECT 'new', sum(coalesce((e.payload->'statuses'->>'new')::int,0)) FROM bot_trace_events e JOIN bot_traces t ON e.trace_id=t.id WHERE e.stage IN ('matching.arbitration.result','matching.fallback.result') AND coalesce(t.source,'bale') NOT IN ('calibration','demo') AND $__timeFilter(e.created_at) UNION ALL SELECT m->>'status', count(*) FROM bot_trace_events e JOIN bot_traces t ON e.trace_id=t.id, jsonb_array_elements(e.payload->'mentions') m WHERE e.stage='routing.decision' AND e.payload->>'source'='jev' AND coalesce(t.source,'bale') NOT IN ('calibration','demo') AND $__timeFilter(e.created_at) GROUP BY 1" }], + "fieldConfig": { "defaults": {}, "overrides": [] }, + "options": { "pieType": "donut", "legend": { "displayMode": "table", "placement": "right" } } + }, + { + "type": "table", "title": "اقدامات داور DeepSeek روی پیشنهاد Jev", "id": 21, + "datasource": { "type": "postgres", "uid": "bot-postgres" }, + "gridPos": { "h": 8, "w": 8, "x": 8, "y": 15 }, + "targets": [{ "refId": "A", "format": "table", "rawSql": "SELECT coalesce(e.payload->'actions'->>'endorsed_jev','0')::int AS \"تأیید Jev\", coalesce(e.payload->'actions'->>'vetoed_to_new','0')::int AS \"وتو → جدید\", coalesce(e.payload->'actions'->>'demoted_to_ambiguity','0')::int AS \"تنزل → ابهام\", coalesce(e.payload->'actions'->>'other','0')::int AS \"سایر\", count(*) OVER () AS \"فراخوانی‌ها\" FROM bot_trace_events e JOIN bot_traces t ON e.trace_id=t.id WHERE e.stage='matching.arbitration.result' AND coalesce(t.source,'bale') NOT IN ('calibration','demo') AND $__timeFilter(e.created_at)" }], + "fieldConfig": { "defaults": {}, "overrides": [] } + }, + { + "type": "timeseries", "title": "توکن مصرفی در زمان (ورودی/خروجی)", "id": 22, + "datasource": { "type": "postgres", "uid": "bot-postgres" }, + "gridPos": { "h": 8, "w": 8, "x": 16, "y": 15 }, + "targets": [{ "refId": "A", "format": "time_series", "rawSql": "SELECT $__timeGroupAlias(e.created_at,'1d'), sum(coalesce((e.payload->'usage'->>'input_tokens')::bigint,0)) AS input, sum(coalesce((e.payload->'usage'->>'output_tokens')::bigint,0)) AS output FROM bot_trace_events e JOIN bot_traces t ON e.trace_id=t.id WHERE e.stage IN ('jev.result','matching.arbitration.result','matching.fallback.result') AND e.payload->'usage' IS NOT NULL AND coalesce(t.source,'bale') NOT IN ('calibration','demo') AND $__timeFilter(e.created_at) GROUP BY 1 ORDER BY 1" }], + "fieldConfig": { "defaults": { "custom": { "drawStyle": "bars", "fillOpacity": 50 } }, "overrides": [] } + }, + { + "type": "table", "title": "نسخه مدل Jev دیده‌شده (پین: jev-1.13.0)", "id": 23, + "datasource": { "type": "postgres", "uid": "bot-postgres" }, + "gridPos": { "h": 6, "w": 8, "x": 0, "y": 23 }, + "targets": [{ "refId": "A", "format": "table", "rawSql": "SELECT coalesce(e.model,'?') AS \"مدل\", count(*) AS \"تعداد\", max(e.created_at)::text AS \"آخرین بار\" FROM bot_trace_events e JOIN bot_traces t ON e.trace_id=t.id WHERE e.stage='jev.result' AND coalesce(t.source,'bale') NOT IN ('calibration','demo') AND $__timeFilter(e.created_at) GROUP BY 1 ORDER BY 2 DESC" }] + }, + { + "type": "timeseries", "title": "فراخوانی مراحل تطبیق (Jev/داور/فالبک)", "id": 24, + "datasource": { "type": "postgres", "uid": "bot-postgres" }, + "gridPos": { "h": 6, "w": 8, "x": 8, "y": 23 }, + "targets": [{ "refId": "A", "format": "time_series", "rawSql": "SELECT $__timeGroupAlias(e.created_at,'1d'), count(*) AS value, e.stage AS metric FROM bot_trace_events e JOIN bot_traces t ON e.trace_id=t.id WHERE e.stage IN ('jev.request','matching.arbitration.result','matching.fallback.result') AND coalesce(t.source,'bale') NOT IN ('calibration','demo') AND $__timeFilter(e.created_at) GROUP BY 1, e.stage ORDER BY 1" }], + "fieldConfig": { "defaults": { "custom": { "drawStyle": "bars", "fillOpacity": 40, "stacking": { "mode": "normal" } } }, "overrides": [] } + }, + { + "type": "stat", "title": "حجم Observability (گارد آرشیو ۵GB)", "id": 25, + "datasource": { "type": "postgres", "uid": "bot-postgres" }, + "gridPos": { "h": 6, "w": 8, "x": 16, "y": 23 }, + "targets": [{ "refId": "A", "format": "table", "rawSql": "SELECT (pg_total_relation_size('bot_trace_events') + pg_total_relation_size('bot_traces')) AS value" }], + "fieldConfig": { "defaults": { "unit": "bytes", "color": { "mode": "thresholds" }, "thresholds": { "steps": [ { "color": "green", "value": null }, { "color": "orange", "value": 3000000000 }, { "color": "red", "value": 5000000000 } ] } }, "overrides": [] } + }, + + { "type": "row", "title": "کیفیت و ورودی", "gridPos": { "h": 1, "w": 24, "x": 0, "y": 29 }, "id": 103 }, + { + "type": "table", "title": "انتخاب کاربر (A/B واقعی)", "id": 30, + "datasource": { "type": "postgres", "uid": "bot-postgres" }, + "gridPos": { "h": 8, "w": 8, "x": 0, "y": 30 }, + "targets": [{ "refId": "A", "format": "table", "rawSql": "SELECT payload->>'action' AS \"اقدام\", coalesce(payload->>'arg','-') AS \"آرگومان\", count(*) AS \"تعداد\" FROM bot_trace_events e JOIN bot_traces t ON e.trace_id=t.id WHERE e.stage='ambiguity.user_choice' AND coalesce(t.source,'bale') NOT IN ('calibration','demo') AND $__timeFilter(e.created_at) GROUP BY 1,2 UNION ALL SELECT CASE WHEN (payload->>'idx')::int = 0 THEN 'انتخاب: گزینه اول (پیشنهاد مدل)' ELSE 'انتخاب: گزینه غیراول' END, '-', count(*) FROM bot_trace_events e JOIN bot_traces t ON e.trace_id=t.id WHERE e.stage='user_choice.pick' AND coalesce(t.source,'bale') NOT IN ('calibration','demo') AND $__timeFilter(e.created_at) GROUP BY 1" }] + }, + { + "type": "table", "title": "سلامت STT به تفکیک پرووایدر", "id": 31, + "datasource": { "type": "postgres", "uid": "bot-postgres" }, + "gridPos": { "h": 8, "w": 8, "x": 8, "y": 30 }, + "targets": [{ "refId": "A", "format": "table", "rawSql": "SELECT coalesce(e.payload->>'provider','?') AS \"پرووایدر\", count(*) FILTER (WHERE e.ok) AS \"موفق\", count(*) AS \"کل\", round(100.0*count(*) FILTER (WHERE e.ok)/greatest(count(*),1),1)::text || '%' AS \"نرخ موفقیت\", coalesce(round(percentile_cont(0.5) WITHIN GROUP (ORDER BY e.latency_ms))::text,'—') AS \"p50 (ms)\" FROM bot_trace_events e JOIN bot_traces t ON e.trace_id=t.id WHERE e.stage='stt.result' AND coalesce(t.source,'bale') NOT IN ('calibration','demo') AND $__timeFilter(e.created_at) GROUP BY 1 ORDER BY 2 DESC" }] + }, + { + "type": "timeseries", "title": "پیش‌پردازش صدا (audio.enhance)", "id": 32, + "datasource": { "type": "postgres", "uid": "bot-postgres" }, + "gridPos": { "h": 8, "w": 8, "x": 16, "y": 30 }, + "targets": [{ "refId": "A", "format": "time_series", "rawSql": "SELECT $__timeGroupAlias(e.created_at,'1d'), count(*) FILTER (WHERE e.ok) AS value, 'موفق' AS metric FROM bot_trace_events e JOIN bot_traces t ON e.trace_id=t.id WHERE e.stage='audio.enhance.result' AND coalesce(t.source,'bale') NOT IN ('calibration','demo') AND $__timeFilter(e.created_at) GROUP BY 1 UNION ALL SELECT $__timeGroupAlias(e.created_at,'1d'), count(*) FILTER (WHERE NOT e.ok), 'ناموفق (بازگشت به صدای خام)' FROM bot_trace_events e JOIN bot_traces t ON e.trace_id=t.id WHERE e.stage='audio.enhance.result' AND coalesce(t.source,'bale') NOT IN ('calibration','demo') AND $__timeFilter(e.created_at) GROUP BY 1" }], + "fieldConfig": { "defaults": { "custom": { "drawStyle": "bars", "fillOpacity": 50, "stacking": { "mode": "normal" } } }, "overrides": [] } + }, + { + "type": "table", "title": "چرخه‌ی پیش‌نویس", "id": 33, + "datasource": { "type": "postgres", "uid": "bot-postgres" }, + "gridPos": { "h": 5, "w": 24, "x": 0, "y": 38 }, + "targets": [{ "refId": "A", "format": "table", "rawSql": "SELECT e.stage AS \"مرحله\", count(*) AS \"تعداد\" FROM bot_trace_events e JOIN bot_traces t ON e.trace_id=t.id WHERE e.stage IN ('draft.render','draft.commit','draft.cancel','ambiguity.sent') AND coalesce(t.source,'bale') NOT IN ('calibration','demo') AND $__timeFilter(e.created_at) GROUP BY 1 ORDER BY 2 DESC" }] + } + ], + "templating": { "list": [] } +} diff --git a/monitoring/grafana/dashboards/bot-trace-explorer.json b/monitoring/grafana/dashboards/bot-trace-explorer.json new file mode 100644 index 0000000..3c7ba98 --- /dev/null +++ b/monitoring/grafana/dashboards/bot-trace-explorer.json @@ -0,0 +1,43 @@ +{ + "annotations": { "list": [] }, + "editable": true, + "graphTooltip": 1, + "refresh": "30s", + "schemaVersion": 41, + "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": 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": { + "list": [ + { + "name": "trace_id", + "label": "شناسه ترس", + "type": "textbox", + "hide": 0, + "current": { "selected": false, "text": "", "value": "" }, + "options": [], + "query": "", + "description": "trace_id را از جدول بالا کپی و اینجا بچسبانید" + } + ] + } +} diff --git a/monitoring/grafana/provisioning/datasources/datasources.yml b/monitoring/grafana/provisioning/datasources/datasources.yml index 2c0808d..fa7b0f4 100644 --- a/monitoring/grafana/provisioning/datasources/datasources.yml +++ b/monitoring/grafana/provisioning/datasources/datasources.yml @@ -13,3 +13,22 @@ datasources: access: proxy url: http://loki:3100 editable: false + + # Bot observability (bot_traces / bot_trace_events) — read-only role, reached + # over the shared erp_shared network. Env vars come from the grafana service + # environment in docker-compose.monitoring.yml. + - name: BotPostgres + uid: bot-postgres + type: postgres + access: proxy + url: ${OBS_DB_HOST}:${OBS_DB_PORT} + user: ${OBS_DB_USER} + secureJsonData: + password: ${OBS_DB_PASSWORD} + jsonData: + database: ${OBS_DB_NAME} + sslmode: disable + postgresVersion: 1800 + maxOpenConns: 4 + connMaxLifetime: 14400 + editable: false