Infrastructure Layer¶
The infrastructure layer implements the repository interfaces defined in the domain layer. It is the only layer that touches DuckDB, pickle files, or external HTTP calls.
DuckDB Adapter¶
src.infrastructure.db.duckdb_adapter ¶
DuckDB adapter – infrastructure layer database connection.
All SQL lives here. Domain layer remains pure Python.
Functions¶
get_connection ¶
Context manager for a DuckDB connection.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
read_only
|
bool
|
If True (default), opens a read-only connection safe for concurrent API workers. Set False for write operations. |
True
|
Yields:
| Type | Description |
|---|---|
DuckDBPyConnection
|
An active DuckDB connection that is closed on exit. |
Source code in src/infrastructure/db/duckdb_adapter.py
Customer Repository (DuckDB)¶
src.infrastructure.repositories.customer_repository ¶
DuckDB implementation of CustomerRepository.
Classes¶
DuckDBCustomerRepository ¶
Bases: CustomerRepository
Reads Customer entities from the DuckDB warehouse.
Source code in src/infrastructure/repositories/customer_repository.py
Functions¶
Fetch a single customer by ID.
Source code in src/infrastructure/repositories/customer_repository.py
Return all customers without a churn_date.
Source code in src/infrastructure/repositories/customer_repository.py
Return a random sample of n customers using DuckDB reservoir sampling.
DuckDB's SAMPLE clause does not support parameterized queries — only literal integer constants are accepted. n is safe to interpolate because callers must clamp it to ≤ 100 before passing it in.
Source code in src/infrastructure/repositories/customer_repository.py
Upsert a customer record.
Source code in src/infrastructure/repositories/customer_repository.py
Functions¶
Usage Repository (DuckDB)¶
src.infrastructure.repositories.usage_repository ¶
DuckDB implementation of UsageRepository.
Classes¶
DuckDBUsageRepository ¶
Bases: UsageRepository
Reads UsageEvent entities from the DuckDB warehouse.
Source code in src/infrastructure/repositories/usage_repository.py
Functions¶
Model Registry¶
src.infrastructure.ml.model_registry ¶
Model registry – loads and caches DVC-tracked model artifacts.
Model files (.pkl) are versioned via DVC and stored in models/. This module is the only place in the codebase that touches pickle files.
Functions¶
load_model
cached
¶
Load a pickled model artifact by name (cached after first load).
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
name
|
str
|
Model name without extension, e.g. "churn_model" or "risk_model". |
required |
Returns:
| Type | Description |
|---|---|
Any
|
The deserialized model object. |
Raises:
| Type | Description |
|---|---|
FileNotFoundError
|
If the artifact does not exist. Run |
Source code in src/infrastructure/ml/model_registry.py
get_model_metadata ¶
Return metadata (version, training date, metrics) for a model artifact.