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 model artifacts from the ZenML Model Control Plane.
Functions¶
load_model
cached
¶
Load the production model artifact from the ZenML Model Control Plane.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
name
|
str
|
Model name, e.g. "churn_model". Must match the Model(name=...) in the training pipeline. |
required |
Returns:
| Type | Description |
|---|---|
Any
|
The deserialized model object. |
Raises:
| Type | Description |
|---|---|
Exception
|
If the ZenML server is unreachable or no production artifact exists. |
Source code in src/infrastructure/ml/model_registry.py
get_model_metadata ¶
Return metadata (version, training date, metrics) for a model artifact.