"""Onglet Administration : métriques du RAG (volume, latence, langues, abstention).""" from __future__ import annotations import hmac import gradio as gr from config import settings from core.metrics.logger import fetch_recent, summary_metrics from core.optimization.cache import answer_cache, embedding_cache from core.retrieval.vector_store import get_vector_store from core.ui.components import ( _e, render_admin_dashboard, render_admin_denied, render_admin_placeholder, ) from core.ui.i18n import t DENIED = "__denied__" def _check_password(password: str) -> bool: if not settings.admin_password: return True # aucun mot de passe configuré -> accès libre (démo) return hmac.compare_digest((password or "").encode(), settings.admin_password.encode()) def collect_dashboard_data() -> dict: store = get_vector_store() return { "metrics": summary_metrics(), "corpus": store.corpus_stats(), "answer_cache": answer_cache.stats(), "embedding_cache": embedding_cache.stats(), "recent": fetch_recent(limit=25), } def render_admin_body(lang: str, state) -> str: if state is None: return render_admin_placeholder(lang) if state == DENIED: return render_admin_denied(lang) + render_admin_placeholder(lang) return render_admin_dashboard(lang, state) def refresh_dashboard(password: str, ui_lang: str): if not _check_password(password): return render_admin_body(ui_lang, DENIED), DENIED data = collect_dashboard_data() return render_admin_body(ui_lang, data), data def render_admin_head(lang: str) -> str: return ( f'
{_e(t(lang, "admin_desc"))}