Source code for trader.reporting.web.wiring

"""Builds a `trader web` app from `Settings()` alone — no CLI arguments.

Split out from `trader.cli.main` so `trader.reporting.web.asgi` (the
`uvicorn --reload` import target) can build the exact same app without
importing the CLI layer — `reporting/web` staying independent of `cli` keeps
the dependency direction the one `trader.cli.main`'s own module docstring
states ("CLI orchestrates ... so the daemon and a web dashboard can call the
same Core code"), not the reverse.
"""

from pathlib import Path

from fastapi import FastAPI

from trader.config.loader import assemble_database_url
from trader.config.schema import ReportingConfig, load_reporting_config
from trader.config.settings import Settings
from trader.marketdata.yfinance_provider import YFinanceProvider
from trader.persistence.db import create_db_engine, create_session_factory
from trader.persistence.decisions import DecisionRepository
from trader.persistence.outcomes import OutcomeRepository
from trader.persistence.snapshots import SnapshotRepository
from trader.persistence.trades import TradeRepository
from trader.reporting.web.app import create_app

__all__ = ["build_app"]


def _build_reporting_config() -> ReportingConfig:
    """Same "absence is fine, wrongness is not" rule as `main.build_reporting_config`."""
    path = Path("config/reporting.yaml")
    return load_reporting_config(path) if path.exists() else ReportingConfig()


[docs] def build_app() -> FastAPI: """Construct `create_app`'s collaborators the way `build_chat_service` does: from `Settings()` alone, so this needs no Alpaca credentials configured.""" settings = Settings() engine = create_db_engine( assemble_database_url(settings.database_url_template, settings.database_password) ) session_factory = create_session_factory(engine) return create_app( snapshot_repo=SnapshotRepository(session_factory), outcome_repo=OutcomeRepository(session_factory), reporting_config=_build_reporting_config(), provider=YFinanceProvider(), trade_repo=TradeRepository(session_factory), decision_repo=DecisionRepository(session_factory), )