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.

24.0 KB
Raw Blame
"""
Search call tracking system for metrics collection.
Similar to token_counter.py but tracks search engine usage.
"""

from typing import Any, Dict, List, Optional

from loguru import logger
from sqlalchemy import case, func

from ..utilities.thread_context import get_search_context
from ..utilities.metric_write_health import (
    MetricWriteReason,
    record_metric_write_drop,
)
from ..database.models import SearchCall
from ..database.sqlcipher_utils import retry_on_db_lock
from .database import MetricsDatabase
from .query_utils import get_research_mode_condition, get_time_filter_condition

# ``success_status`` of a search that was not sent because its engine was
# disabled or cooling down for the research (see engine_availability).
# Recorded so outages stay visible, but not counted as a search attempt:
# it has no response time, and it is excluded from call counts, averages
# and success rates.
SEARCH_STATUS_SKIPPED = "skipped_unavailable"


def _session_has_pending_orm_state(session: Any) -> bool:
    """True if *session* has unflushed ORM work ``begin_nested()`` would flush.

    ``Session.begin_nested()`` flushes ``new``/``dirty``/``deleted`` before
    CREATE SAVEPOINT. A flush failure (SQLite writer lock held elsewhere)
    invalidates the caller's transaction; catching it cannot restore
    ``session.is_active``. Best-effort telemetry must skip in that case.
    """
    try:
        for attr in ("new", "dirty", "deleted"):
            collection = getattr(session, attr, None)
            if collection and len(collection) > 0:
                return True
    except Exception:
        return False
    return False


class SearchTracker:
    """Track search engine calls and performance metrics."""

    def __init__(self, db: Optional[MetricsDatabase] = None):
        """Initialize the search tracker."""
        self.db = db or MetricsDatabase()

    @staticmethod
    def record_search(
        engine_name: str,
        query: str,
        results_count: int = 0,
        response_time_ms: int = 0,
        success: bool = True,
        error_message: Optional[str] = None,
        status: Optional[str] = None,
        error_type: Optional[str] = None,
    ) -> None:
        """Record a completed search operation directly to database.

        ``status`` overrides the success/error status, e.g.
        ``SEARCH_STATUS_SKIPPED`` for a search skipped because its engine
        was unavailable; ``error_message`` then carries the reason.
        Pass ``error_type`` separately when the exception message has been
        sanitized. Plain text alone cannot identify an exception class.
        """
        password = None

        # Extract research context (thread-safe)
        context = get_search_context()

        # Skip metrics recording in programmatic mode or when no context is set
        if context is None:
            logger.warning(
                "Skipping search metrics recording - no research context available "
                "(likely in programmatic mode)"
            )
            return

        research_id = context.get("research_id")

        # Convert research_id to string if it's an integer (for backward compatibility)
        if isinstance(research_id, int):
            research_id = str(research_id)
        research_query = context.get("research_query")
        research_mode = context.get("research_mode", "unknown")
        research_phase = context.get("research_phase", "search")
        search_iteration = context.get("search_iteration", 0)

        # Determine success status
        success_status = status or ("success" if success else "error")
        if status == SEARCH_STATUS_SKIPPED:
            error_type = "engine_unavailable"
        elif error_message and not error_type:
            error_type = (
                type(error_message).__name__
                if isinstance(error_message, Exception)
                else "unknown_error"
            )

        # Record search call in database - only from background threads
        username = None
        dropped_reason: MetricWriteReason | None = None
        try:
            # Get username from context for thread-safe database
            username = context.get("username")
            if not username:
                logger.warning(
                    "Cannot save search metrics - no username in research "
                    f"context. Search engine: {engine_name}"
                )
                return

            # Get password from context
            password = context.get("user_password")
            if not password:
                dropped_reason = "missing_credentials"
                logger.warning(
                    "Cannot save search metrics - no password in research "
                    f"context. Search engine: {engine_name}, "
                    f"username: {username}"
                )
                return

            try:

                def _write_search(session: Any) -> None:
                    search_call = SearchCall(
                        research_id=research_id,
                        research_query=research_query,
                        research_mode=research_mode,
                        research_phase=research_phase,
                        search_iteration=search_iteration,
                        search_engine=engine_name,
                        query=query,
                        results_count=results_count,
                        response_time_ms=response_time_ms,
                        success=int(success_status == "success"),
                        success_status=success_status,
                        error_type=error_type,
                        error_message=str(error_message)
                        if error_message
                        else None,
                    )
                    session.add(search_call)

                    logger.debug(
                        f"Search call recorded to encrypted DB: {engine_name} - "
                        f"{results_count} results in {response_time_ms}ms"
                    )

                # Two paths, because get_user_db_session does not commit
                # on exit and metrics_writer.get_session() is a second
                # QueuePool connection:
                #
                # * in_scope(): reuse the thread-local session and write
                #   inside a SAVEPOINT (telemetry_savepoint()). A nested
                #   transaction isolates metrics failures from the caller's
                #   session (preventing the caller's staged work from being
                #   rolled back) and never calls session.commit(). The row
                #   joins the caller's open write transaction, or commits
                #   at once on its own when the scope has not written.
                #   Session.begin_nested() flushes ALL pending ORM state
                #   before CREATE SAVEPOINT. If that flush fails (e.g.
                #   another connection holds SQLite's writer lock),
                #   SQLAlchemy invalidates the caller's transaction and
                #   catching the exception cannot restore it. Best-effort
                #   telemetry therefore skips when the caller already has
                #   unflushed work — the write cannot be isolated.
                # * otherwise: metrics_writer commits+closes its own
                #   session. A thread-local write here would be rolled
                #   back by the next get_session() at scope depth 0.
                from ..database.thread_local_session import (
                    thread_session_manager,
                )

                if thread_session_manager.in_scope():
                    from ..database.session_context import (
                        telemetry_savepoint,
                    )

                    # Borrow directly: session provisioning can replace a
                    # different owner's session or recover a failed one by
                    # rolling back the caller's transaction.
                    session = thread_session_manager.get_scoped_session(
                        username
                    )
                    if session is None:
                        dropped_reason = "database_unavailable"
                        # Unlike rollback-coupled loss, an owner mismatch is
                        # an anomaly worth surfacing (as rate telemetry does).
                        logger.warning(
                            "Skipping in-scope search metrics without a "
                            "matching owner session"
                        )
                        return
                    if _session_has_pending_orm_state(session):
                        dropped_reason = "pending_transaction"
                        logger.debug(
                            "Skipping in-scope search metrics write; "
                            "caller session has pending ORM state "
                            "that Session.begin_nested() would flush "
                            "before creating a SAVEPOINT"
                        )
                        return
                    with telemetry_savepoint(session):
                        _write_search(session)
                else:
                    from ..database.thread_metrics import metrics_writer

                    # Set password for this thread
                    metrics_writer.set_user_password(username, password)

                    def _write_owned_search() -> None:
                        with metrics_writer.get_session(username) as session:
                            _write_search(session)

                    retry_on_db_lock(_write_owned_search)
            except Exception as e:
                dropped_reason = "write_error"
                # Deferred import to avoid potential circular imports during module initialization
                from ..security.log_sanitizer import scrub_error

                safe_msg = scrub_error(e, password)
                logger.warning(f"Failed to write search metrics: {safe_msg}")

        except Exception as e:
            dropped_reason = "write_error"
            # Deferred import to avoid potential circular imports during module initialization
            from ..security.log_sanitizer import scrub_error

            safe_msg = scrub_error(e, password)
            logger.warning(f"Failed to record search call: {safe_msg}")
        finally:
            # A write and its context-manager cleanup can both fail. Count
            # this recording attempt once after all nested handlers finish.
            if dropped_reason is not None:
                record_metric_write_drop(username, "search", dropped_reason)

    def get_search_metrics(
        self,
        period: str = "30d",
        research_mode: str = "all",
        username: Optional[str] = None,
        password: Optional[str] = None,
    ) -> Dict[str, Any]:
        """Get search engine usage metrics."""
        with self.db.get_session(
            username=username, password=password
        ) as session:
            try:
                # Build base query with filters
                query = session.query(SearchCall).filter(
                    SearchCall.search_engine.isnot(None)
                )

                # Apply time filter
                time_condition = get_time_filter_condition(
                    period, SearchCall.timestamp
                )
                if time_condition is not None:
                    query = query.filter(time_condition)

                # Apply research mode filter
                mode_condition = get_research_mode_condition(
                    research_mode, SearchCall.research_mode
                )
                if mode_condition is not None:
                    query = query.filter(mode_condition)

                # Get search engine statistics using ORM aggregation
                skipped = SearchCall.success_status == SEARCH_STATUS_SKIPPED
                search_stats = session.query(
                    SearchCall.search_engine,
                    func.sum(case((skipped, 0), else_=1)).label("call_count"),
                    func.avg(
                        case((skipped, None), else_=SearchCall.response_time_ms)
                    ).label("avg_response_time"),
                    func.sum(SearchCall.results_count).label("total_results"),
                    func.avg(
                        case((skipped, None), else_=SearchCall.results_count)
                    ).label("avg_results_per_call"),
                    func.sum(
                        case(
                            (SearchCall.success_status == "success", 1), else_=0
                        )
                    ).label("success_count"),
                    func.sum(
                        case((SearchCall.success_status == "error", 1), else_=0)
                    ).label("error_count"),
                    func.sum(case((skipped, 1), else_=0)).label(
                        "skipped_count"
                    ),
                ).filter(SearchCall.search_engine.isnot(None))

                # Apply same filters to stats query
                if time_condition is not None:
                    search_stats = search_stats.filter(time_condition)
                if mode_condition is not None:
                    search_stats = search_stats.filter(mode_condition)

                search_stats = (
                    search_stats.group_by(SearchCall.search_engine)
                    .order_by(func.count().desc())
                    .all()
                )

                # Get recent search calls
                recent_calls_query = session.query(SearchCall)
                if time_condition is not None:
                    recent_calls_query = recent_calls_query.filter(
                        time_condition
                    )
                if mode_condition is not None:
                    recent_calls_query = recent_calls_query.filter(
                        mode_condition
                    )

                recent_calls = (
                    recent_calls_query.order_by(SearchCall.timestamp.desc())
                    .limit(20)
                    .all()
                )

                return {
                    "search_engine_stats": [
                        {
                            "engine": stat.search_engine,
                            "call_count": stat.call_count,
                            "avg_response_time": stat.avg_response_time or 0,
                            "total_results": stat.total_results or 0,
                            "avg_results_per_call": stat.avg_results_per_call
                            or 0,
                            "success_rate": (
                                (stat.success_count / stat.call_count * 100)
                                if stat.call_count
                                else 0
                            ),
                            "error_count": stat.error_count or 0,
                            "skipped_count": stat.skipped_count or 0,
                        }
                        for stat in search_stats
                    ],
                    "recent_calls": [
                        {
                            "engine": call.search_engine,
                            "query": (
                                call.query[:100] + "..."
                                if len(call.query or "") > 100
                                else call.query
                            ),
                            "results_count": call.results_count,
                            "response_time_ms": call.response_time_ms,
                            "success_status": call.success_status,
                            "timestamp": str(call.timestamp),
                        }
                        for call in recent_calls
                    ],
                }

            except Exception:
                logger.warning("Error getting search metrics")
                return {"search_engine_stats": [], "recent_calls": []}

    def get_research_search_metrics(
        self,
        research_id: str,
        username: Optional[str] = None,
        password: Optional[str] = None,
    ) -> Dict[str, Any]:
        """Get search metrics for a specific research session."""
        with self.db.get_session(
            username=username, password=password
        ) as session:
            try:
                # Get all search calls for this research
                search_calls = (
                    session.query(SearchCall)
                    .filter(SearchCall.research_id == research_id)
                    .order_by(SearchCall.timestamp.asc())
                    .all()
                )

                # Get search engine stats for this research
                skipped = SearchCall.success_status == SEARCH_STATUS_SKIPPED
                engine_stats = (
                    session.query(
                        SearchCall.search_engine,
                        func.sum(case((skipped, 0), else_=1)).label(
                            "call_count"
                        ),
                        func.avg(
                            case(
                                (skipped, None),
                                else_=SearchCall.response_time_ms,
                            )
                        ).label("avg_response_time"),
                        func.sum(SearchCall.results_count).label(
                            "total_results"
                        ),
                        func.sum(
                            case(
                                (SearchCall.success_status == "success", 1),
                                else_=0,
                            )
                        ).label("success_count"),
                        func.sum(case((skipped, 1), else_=0)).label(
                            "skipped_count"
                        ),
                    )
                    .filter(SearchCall.research_id == research_id)
                    .group_by(SearchCall.search_engine)
                    .order_by(func.count().desc())
                    .all()
                )

                # Calculate totals; skipped searches were never sent.
                attempted = [
                    call
                    for call in search_calls
                    if call.success_status != SEARCH_STATUS_SKIPPED
                ]
                total_searches = len(attempted)
                skipped_searches = len(search_calls) - total_searches
                total_results = sum(
                    call.results_count or 0 for call in search_calls
                )
                avg_response_time = (
                    sum(call.response_time_ms or 0 for call in attempted)
                    / total_searches
                    if total_searches > 0
                    else 0
                )
                successful_searches = sum(
                    1 for call in attempted if call.success_status == "success"
                )
                success_rate = (
                    (successful_searches / total_searches * 100)
                    if total_searches > 0
                    else 0
                )

                return {
                    "total_searches": total_searches,
                    "skipped_searches": skipped_searches,
                    "total_results": total_results,
                    "avg_response_time": round(avg_response_time),
                    "success_rate": round(success_rate, 1),
                    "search_calls": [
                        {
                            "engine": call.search_engine,
                            "query": call.query,
                            "results_count": call.results_count,
                            "response_time_ms": call.response_time_ms,
                            "success_status": call.success_status,
                            "timestamp": str(call.timestamp),
                        }
                        for call in search_calls
                    ],
                    "engine_stats": [
                        {
                            "engine": stat.search_engine,
                            "call_count": stat.call_count,
                            "avg_response_time": stat.avg_response_time or 0,
                            "total_results": stat.total_results or 0,
                            "success_rate": (
                                (stat.success_count / stat.call_count * 100)
                                if stat.call_count
                                else 0
                            ),
                            "skipped_count": stat.skipped_count or 0,
                        }
                        for stat in engine_stats
                    ],
                }

            except Exception:
                logger.warning("Error getting research search metrics")
                return {
                    "total_searches": 0,
                    "skipped_searches": 0,
                    "total_results": 0,
                    "avg_response_time": 0,
                    "success_rate": 0,
                    "search_calls": [],
                    "engine_stats": [],
                }

    def get_search_time_series(
        self,
        period: str = "30d",
        research_mode: str = "all",
        username: Optional[str] = None,
        password: Optional[str] = None,
    ) -> List[Dict[str, Any]]:
        """Get search activity time series data for charting.

        Args:
            period: Time period to filter by ('7d', '30d', '3m', '1y', 'all')
            research_mode: Research mode to filter by ('quick', 'detailed', 'all')
            username: Username for database access
            password: Password for database access

        Returns:
            List of time series data points with search engine activity
        """
        with self.db.get_session(
            username=username, password=password
        ) as session:
            try:
                # Build base query
                query = session.query(SearchCall).filter(
                    SearchCall.search_engine.isnot(None),
                    SearchCall.timestamp.isnot(None),
                )

                # Apply time filter
                time_condition = get_time_filter_condition(
                    period, SearchCall.timestamp
                )
                if time_condition is not None:
                    query = query.filter(time_condition)

                # Apply research mode filter
                mode_condition = get_research_mode_condition(
                    research_mode, SearchCall.research_mode
                )
                if mode_condition is not None:
                    query = query.filter(mode_condition)

                # Get all search calls ordered by time
                search_calls = query.order_by(SearchCall.timestamp.asc()).all()

                # Create time series data
                time_series = []
                for call in search_calls:
                    if call.success_status == SEARCH_STATUS_SKIPPED:
                        # Not search activity: nothing was sent.
                        continue
                    time_series.append(
                        {
                            "timestamp": (
                                str(call.timestamp) if call.timestamp else None
                            ),
                            "search_engine": call.search_engine,
                            "results_count": call.results_count or 0,
                            "response_time_ms": call.response_time_ms or 0,
                            "success_status": call.success_status,
                            "query": (
                                call.query[:50] + "..."
                                if call.query and len(call.query) > 50
                                else call.query
                            ),
                        }
                    )

                return time_series

            except Exception:
                logger.warning("Error getting search time series")
                return []


def get_search_tracker() -> SearchTracker:
    """Create a SearchTracker instance.

    Returns a fresh instance each time. Callers should pass username/password
    to the query methods so the correct per-user database is accessed.
    """
    return SearchTracker()