Skip to content

~95% on SimpleQA (e.g. Qwen3.6-27B on a 3090). Supports all local and cloud LLMs (llama.cpp, Ollama, Google, ...). 10+ search engines - arXiv, PubMed, your private documents. Everything Local & Encrypted.

3.7 KB
Raw Blame
"""Bounded, per-user diagnostics for best-effort metric writes.

Only fixed source/reason categories and counts are retained. This module stays
independent of database and metrics imports so optional/programmatic callers
can report a failure without initializing either subsystem.
"""

from collections import Counter, OrderedDict
from threading import Lock
from typing import Literal, TypedDict

from loguru import logger

MetricWriteSource = Literal["tokens", "search", "rate_limits"]
MetricWriteReason = Literal[
    "write_error",
    "missing_credentials",
    "pending_transaction",
    "database_unavailable",
]

_SOURCES: tuple[MetricWriteSource, ...] = ("tokens", "search", "rate_limits")
METRIC_WRITE_REASON_LABELS: dict[MetricWriteReason, str] = {
    "write_error": "Database write failures",
    "missing_credentials": "Missing database credentials",
    "pending_transaction": "Updates skipped while another write was pending",
    "database_unavailable": "Metrics storage unavailable",
}
MAX_TRACKED_USERS = 512


class MetricWriteSnapshot(TypedDict):
    dropped_writes: int
    by_source: dict[MetricWriteSource, int]
    by_reason: dict[MetricWriteReason, int]


class MetricWriteHealth:
    """Count dropped updates for at most ``max_users`` recent writers.

    Counts are process-local lower bounds, reset by restart or eviction. Reads
    never allocate an entry or expose another user's counts.
    """

    def __init__(self, max_users: int = MAX_TRACKED_USERS) -> None:
        if max_users < 1:
            raise ValueError("max_users must be positive")
        self._max_users = max_users
        self._lock = Lock()
        self._users: OrderedDict[
            str, Counter[tuple[MetricWriteSource, MetricWriteReason]]
        ] = OrderedDict()

    def record(
        self,
        username: str | None,
        source: MetricWriteSource,
        reason: MetricWriteReason,
    ) -> None:
        # ``type(...) is str`` rather than ``isinstance``: a ``str`` subclass
        # can alias ``__hash__``/``__eq__`` and land in another user's bucket.
        if type(username) is not str:
            if username is not None:
                logger.debug(
                    "Not counting a dropped metric write: username type {} is not str",
                    type(username).__name__,
                )
            return
        if not username:
            return
        if source not in _SOURCES or reason not in METRIC_WRITE_REASON_LABELS:
            return
        with self._lock:
            counts = self._users.setdefault(username, Counter())
            counts[(source, reason)] += 1
            self._users.move_to_end(username)
            if len(self._users) > self._max_users:
                self._users.popitem(last=False)

    def snapshot(self, username: str | None) -> MetricWriteSnapshot:
        counts: Counter[tuple[MetricWriteSource, MetricWriteReason]] = Counter()
        with self._lock:
            if type(username) is str:
                counts = self._users.get(username, counts).copy()
        return {
            "dropped_writes": sum(counts.values()),
            "by_source": {
                source: sum(n for (s, _), n in counts.items() if s == source)
                for source in _SOURCES
            },
            "by_reason": {
                reason: sum(n for (_, r), n in counts.items() if r == reason)
                for reason in METRIC_WRITE_REASON_LABELS
            },
        }


_health = MetricWriteHealth()


def record_metric_write_drop(
    username: str | None, source: MetricWriteSource, reason: MetricWriteReason
) -> None:
    _health.record(username, source, reason)


def get_metric_write_health(username: str) -> MetricWriteSnapshot:
    return _health.snapshot(username)