""" Routing-Counter für Archivar v7 — empirische Daten für V8-Refactoring. Zählt JEDE Routing-Entscheidung und JEDEN Bypass. In-Memory (nicht persistent). Nach Neustart beginnt Zählung neu. Usage:: from archivar.routing_counter import routing_counter routing_counter.increment('dm_routing_skipped') routing_counter.log_counts() """ from __future__ import annotations import logging import threading from typing import Dict logger = logging.getLogger(__name__) class RoutingCounter: """Thread-safe In-Memory-Counter für Routing-Entscheidungen.""" def __init__(self) -> None: self._lock = threading.Lock() self._counts: Dict[str, int] = {} def increment(self, reason: str) -> None: """Inkrementiere den Zähler für einen Grund (thread-safe).""" with self._lock: self._counts[reason] = self._counts.get(reason, 0) + 1 def get_counts(self) -> Dict[str, int]: """Gibt eine Kopie aller Zählerstände zurück.""" with self._lock: return dict(self._counts) def log_counts(self) -> None: """Logge alle Zählerstände auf INFO-Level.""" counts = self.get_counts() total = sum(counts.values()) logger.info( "Routing counter (total=%d): %s", total, ", ".join(f"{k}={v}" for k, v in sorted(counts.items())), ) def reset(self) -> None: """Setze alle Zähler zurück.""" with self._lock: self._counts.clear() # Modul-Level-Singleton — wird von adapter.py und router.py importiert routing_counter = RoutingCounter()