from __future__ import annotations from typing import Any import pandas as pd import plotly.graph_objects as go from src.i18n import t from src.schemas import Kpis, MethodScore def empty_plot(title: str = "—") -> go.Figure: fig = go.Figure() fig.update_layout(template="plotly_white", height=340, title=title) return fig def kpi_html(kpis: dict[str, Any] | Kpis, lang: str) -> str: data = kpis.model_dump() if isinstance(kpis, Kpis) else kpis def card(label: str, value: str, sub: str) -> str: return ( f"

{label}

" f"

{value}

{sub}

" ) return ( "
" + card(t(lang, "kpi_docs"), str(data.get("n_docs") or 0), data.get("engine_profile") or "policy") + card(t(lang, "kpi_spans"), str(data.get("n_spans") or 0), t(lang, "optimized")) + card( t(lang, "kpi_precision"), f"{100 * float(data.get('policy_precision') or 0):.0f}%", f"baseline {100 * float(data.get('baseline_precision') or 0):.0f}%", ) + card( t(lang, "kpi_recall"), f"{100 * float(data.get('policy_recall') or 0):.0f}%", f"baseline {100 * float(data.get('baseline_recall') or 0):.0f}%", ) + card( t(lang, "kpi_left"), str(data.get("leftover_exports") or 0), f"{data.get('elapsed_ms') or 0} ms", ) + "
" ) def compare_figure(scores: dict[str, MethodScore] | dict[str, Any], title: str) -> go.Figure: if not scores: return empty_plot(title) names, precision, recall, over = [], [], [], [] for key in ("regex_only", "checksum_policy"): item = None for raw_key, value in scores.items(): name = raw_key.value if hasattr(raw_key, "value") else str(raw_key) if name == key or (hasattr(raw_key, "value") and raw_key.value == key): item = value break if str(raw_key).endswith(key): item = value if item is None: continue names.append(key) precision.append(round(100 * float(item.precision if hasattr(item, "precision") else item.get("precision", 0)), 1)) recall.append(round(100 * float(item.recall if hasattr(item, "recall") else item.get("recall", 0)), 1)) over.append(round(100 * float(item.over_redaction if hasattr(item, "over_redaction") else item.get("over_redaction", 0)), 2)) fig = go.Figure() fig.add_bar(name="precision %", x=names, y=precision, marker_color="#0f766e") fig.add_bar(name="recall %", x=names, y=recall, marker_color="#1d4ed8") fig.add_bar(name="over-redaction %", x=names, y=over, marker_color="#b45309") fig.update_layout(template="plotly_white", height=380, barmode="group", title=title) return fig def type_figure(score: MethodScore | dict[str, Any] | None, title: str) -> go.Figure: if score is None: return empty_plot(title) by_type = score.by_type if hasattr(score, "by_type") else (score or {}).get("by_type") or {} if not by_type: return empty_plot(title) names = list(by_type.keys()) precision = [round(100 * float(by_type[name].get("precision", 0)), 1) for name in names] recall = [round(100 * float(by_type[name].get("recall", 0)), 1) for name in names] fig = go.Figure() fig.add_bar(name="precision %", x=names, y=precision, marker_color="#4338ca") fig.add_bar(name="recall %", x=names, y=recall, marker_color="#7c3aed") fig.update_layout(template="plotly_white", height=360, barmode="group", title=title) return fig def leftover_figure(doc_rows: list[dict[str, Any]], title: str) -> go.Figure: if not doc_rows: return empty_plot(title) frame = pd.DataFrame(doc_rows) if "leftover" not in frame.columns: return empty_plot(title) fig = go.Figure(go.Bar(x=list(frame["doc_id"].astype(str)), y=list(frame["leftover"]), marker_color="#be123c")) fig.update_layout(template="plotly_white", height=360, title=title) return fig