Skip to content

Main

health_check()

Performs a basic health check of the API and database availability.

Returns:

Name Type Description
dict

A dictionary containing: - status (str): The general status of the API (e.g., "ok"). - db_ready (bool): True if the SQLite database file exists at DB_PATH.

Source code in api/main.py
68
69
70
71
72
73
74
75
76
77
78
@app.get("/health")
def health_check():
    """
    Performs a basic health check of the API and database availability.

    Returns:
        dict: A dictionary containing:
            - `status` (str): The general status of the API (e.g., "ok").
            - `db_ready` (bool): True if the SQLite database file exists at `DB_PATH`.
    """
    return {"status": "ok", "db_ready": settings.DB_PATH.exists()}

lifespan(app) async

Manages the application lifecycle (startup and shutdown events).

This context manager handles the initialization of critical services before the API starts accepting requests.

Startup Sequence:

  1. Directory Setup: Creates the PLOTS_DIR if it does not exist.
  2. Telemetry: Initializes OpenTelemetry/Tracing setup.
  3. Data Integrity (ETL): Triggers the run_pipeline() function to ensure clean data is available for the agents. If this fails, a critical error is logged.

Parameters:

Name Type Description Default
app FastAPI

The FastAPI application instance.

required

Yields:

Name Type Description
None

Control is yielded back to the application loop.

Source code in api/main.py
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
@asynccontextmanager
async def lifespan(app: FastAPI):
    """
    Manages the application lifecycle (startup and shutdown events).

    This context manager handles the initialization of critical services before the
    API starts accepting requests.

    **Startup Sequence:**

    1.  **Directory Setup:** Creates the `PLOTS_DIR` if it does not exist.
    2.  **Telemetry:** Initializes OpenTelemetry/Tracing setup.
    3.  **Data Integrity (ETL):** Triggers the `run_pipeline()` function to ensure
        clean data is available for the agents. If this fails, a critical error is logged.

    Args:
        app (FastAPI): The FastAPI application instance.

    Yields:
        None: Control is yielded back to the application loop.
    """
    settings.PLOTS_DIR.mkdir(parents=True, exist_ok=True)
    setup_telemetry()

    logger.info("Checking data integrity (ETL)...")
    try:
        run_pipeline()
        logger.info("Data is ready for use.")
    except Exception as e:
        logger.critical(f"Critical failure during data initialization: {e}")

    yield

    logger.info("Shutting down API...")