Files
foxhunt/config/src/database.rs
jgrusewski b94299260a 🎯 Wave 17-7: Eliminate 99.2% of warnings (5,564 → 43)
## Achievements
- Fixed deprecated chrono::timestamp_nanos() usage
- Applied cargo fix for auto-fixable warnings
- Reduced warnings from 1,168 to 43 (96.3% this wave)
- Overall reduction: 5,564 → 43 (99.2% total)

## Changes
- ml/src/risk/advanced_risk_engine.rs: Fix deprecated timestamp_nanos()
- ml/src/risk/var_models.rs: Simplify DateTime handling
- risk/src/safety/: Make Redis optional for tests
- Multiple files: Remove unused imports via cargo fix

## Remaining Warnings (43 - All Justified)
- 41 dead code warnings (future functionality)
- 1 unused Result in test code
- 1 unused field warning

## Success Metrics
 High-priority warnings: 0
 Deprecated APIs: 0
 Compilation: SUCCESS
 Build time: ~2 minutes

Report: /tmp/wave17_agent7_warnings_final.md
2025-09-30 18:32:51 +02:00

1031 lines
39 KiB
Rust

//! Database configuration for PostgreSQL connections and connection pooling.
//!
//! This module provides comprehensive database configuration structures for managing
//! PostgreSQL connections, connection pools, and transaction settings in the Foxhunt
//! HFT trading system. It supports connection pooling, timeout management, and
//! transaction isolation levels optimized for high-frequency trading workloads.
use serde::{Deserialize, Serialize};
use std::time::Duration;
#[cfg(feature = "postgres")]
use sqlx::Row;
/// Main database configuration structure for PostgreSQL connections.
///
/// Provides comprehensive database connection settings including connection pooling,
/// timeouts, logging, and transaction management. Optimized for high-frequency
/// trading workloads with appropriate defaults for low-latency operations.
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct DatabaseConfig {
/// PostgreSQL connection URL (e.g., "postgresql://user:pass@host:port/database")
pub url: String,
/// Maximum number of connections in the pool
pub max_connections: u32,
/// Minimum number of connections to maintain in the pool
pub min_connections: u32,
/// Timeout for establishing new database connections
pub connect_timeout: std::time::Duration,
/// Timeout for individual query execution
pub query_timeout: std::time::Duration,
/// Enable detailed query logging for debugging
pub enable_query_logging: bool,
/// Application name to identify connections in PostgreSQL logs
pub application_name: Option<String>,
/// Connection pool configuration settings
pub pool: PoolConfig,
/// Transaction management configuration
pub transaction: TransactionConfig,
}
impl DatabaseConfig {
/// Creates a new DatabaseConfig with sensible defaults for development.
///
/// Returns a configuration suitable for local development with a PostgreSQL
/// database running on localhost. Production deployments should override
/// these settings through environment variables or configuration files.
pub fn new() -> Self {
Self {
url: "postgresql://localhost/foxhunt".to_string(),
max_connections: 10,
min_connections: 1,
connect_timeout: Duration::from_secs(30),
query_timeout: Duration::from_secs(60),
enable_query_logging: false,
application_name: Some("foxhunt".to_string()),
pool: PoolConfig::default(),
transaction: TransactionConfig::default(),
}
}
/// Validates the database configuration for correctness.
///
/// Performs basic validation checks on the configuration parameters to ensure
/// they are valid before attempting to establish database connections.
///
/// # Errors
///
/// Returns an error string if the configuration is invalid, such as:
/// - Empty database URL
/// - Invalid connection parameters
pub fn validate(&self) -> Result<(), String> {
if self.url.is_empty() {
return Err("Database URL cannot be empty".to_string());
}
Ok(())
}
}
/// Database connection pool configuration.
///
/// Manages the behavior of the connection pool including connection lifecycle,
/// timeouts, and health checking. Optimized for high-frequency trading workloads
/// where connection availability and low latency are critical.
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct PoolConfig {
/// Minimum number of connections to maintain in the pool
pub min_connections: u32,
/// Maximum number of connections allowed in the pool
pub max_connections: u32,
/// Timeout in seconds for acquiring a connection from the pool
pub acquire_timeout_secs: u64,
/// Maximum lifetime in seconds for a connection before it's recycled
pub max_lifetime_secs: u64,
/// Timeout in seconds before idle connections are closed
pub idle_timeout_secs: u64,
/// Whether to test connections before returning them from the pool
pub test_before_acquire: bool,
/// Database URL for pool connections
pub database_url: String,
/// Enable periodic health checks for pool connections
pub health_check_enabled: bool,
/// Interval in seconds between health checks
pub health_check_interval_secs: u64,
}
impl Default for PoolConfig {
fn default() -> Self {
Self {
min_connections: 1,
max_connections: 10,
acquire_timeout_secs: 30,
max_lifetime_secs: 1800,
idle_timeout_secs: 600,
test_before_acquire: true,
database_url: "postgresql://localhost/foxhunt".to_string(),
health_check_enabled: true,
health_check_interval_secs: 60,
}
}
}
/// Database transaction configuration and retry policies.
///
/// Configures transaction behavior including isolation levels, timeouts,
/// and retry mechanisms. Critical for maintaining data consistency in
/// high-frequency trading operations while handling transient failures.
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct TransactionConfig {
/// PostgreSQL transaction isolation level (e.g., "READ_COMMITTED", "SERIALIZABLE")
pub isolation_level: String,
/// Default timeout duration for transactions
pub timeout: Duration,
/// Default timeout in seconds for transactions
pub default_timeout_secs: u64,
/// Enable automatic retry on transaction failures
pub enable_retry: bool,
/// Maximum number of retry attempts for failed transactions
pub max_retries: u32,
/// Delay in milliseconds between retry attempts
pub retry_delay_ms: u64,
/// Maximum number of nested savepoints allowed
pub max_savepoints: u32,
}
impl Default for TransactionConfig {
fn default() -> Self {
Self {
isolation_level: "READ_COMMITTED".to_string(),
timeout: Duration::from_secs(30),
default_timeout_secs: 30,
enable_retry: true,
max_retries: 3,
retry_delay_ms: 100,
max_savepoints: 10,
}
}
}
/// Database loader for symbol configurations with PostgreSQL integration.
///
/// Provides high-performance loading and caching of symbol configurations
/// from the PostgreSQL database. Supports real-time updates through PostgreSQL
/// NOTIFY/LISTEN for configuration hot-reload capabilities.
#[cfg(feature = "postgres")]
pub struct PostgresSymbolConfigLoader {
/// Database connection pool
pool: sqlx::PgPool,
/// Configuration cache timeout
cache_timeout: Duration,
/// PostgreSQL listener for configuration changes
listener: Option<sqlx::postgres::PgListener>,
}
#[cfg(feature = "postgres")]
impl PostgresSymbolConfigLoader {
/// Creates a new PostgreSQL symbol configuration loader.
pub async fn new(database_url: &str) -> Result<Self, sqlx::Error> {
let pool = sqlx::PgPool::connect(database_url).await?;
Ok(Self {
pool,
cache_timeout: Duration::from_secs(300), // 5 minutes
listener: None,
})
}
/// Creates a new loader with an existing connection pool.
pub fn with_pool(pool: sqlx::PgPool) -> Self {
Self {
pool,
cache_timeout: Duration::from_secs(300),
listener: None,
}
}
/// Loads a symbol configuration by symbol name.
pub async fn load_symbol_config(&self, symbol: &str) -> Result<Option<crate::symbol_config::SymbolConfig>, sqlx::Error> {
// Simplified implementation using basic sqlx::query instead of macros
let query = "
SELECT
sc.id,
sc.symbol,
sc.description,
sc.classification,
sc.primary_exchange,
sc.currency,
sc.tick_size,
sc.lot_size,
sc.min_order_size,
sc.max_order_size,
sc.sector,
sc.industry,
sc.market_cap,
sc.avg_daily_volume,
sc.margin_requirement,
sc.position_limit,
sc.risk_multiplier,
sc.is_active,
sc.data_source,
sc.created_at,
sc.updated_at,
sc.last_validated
FROM symbol_config sc
WHERE sc.symbol = $1 AND sc.is_active = true
";
let row = sqlx::query(query)
.bind(symbol)
.fetch_optional(&self.pool)
.await?;
if let Some(row) = row {
// Create a basic symbol config from the row
let symbol_name: String = row.get("symbol");
let description: String = row.get("description");
let classification_str: String = row.get("classification");
let classification = match classification_str.as_str() {
"EQUITY" => crate::symbol_config::AssetClassification::Equity,
"FUTURE" => crate::symbol_config::AssetClassification::Future,
"FOREX" => crate::symbol_config::AssetClassification::Forex,
"CRYPTO" => crate::symbol_config::AssetClassification::Crypto,
"COMMODITY" => crate::symbol_config::AssetClassification::Commodity,
"FIXED_INCOME" => crate::symbol_config::AssetClassification::FixedIncome,
"OPTION" => crate::symbol_config::AssetClassification::Option,
"ETF" => crate::symbol_config::AssetClassification::Etf,
"INDEX" => crate::symbol_config::AssetClassification::Index,
"DERIVATIVE" => crate::symbol_config::AssetClassification::Derivative,
_ => crate::symbol_config::AssetClassification::Equity,
};
let mut config = crate::symbol_config::SymbolConfig::new(symbol_name, classification);
config.description = description;
config.primary_exchange = row.get("primary_exchange");
config.currency = row.get("currency");
// Handle decimal conversions safely
if let Ok(tick_size) = row.try_get::<rust_decimal::Decimal, _>("tick_size") {
if let Ok(f) = tick_size.try_into() {
config.tick_size = f;
}
}
Ok(Some(config))
} else {
Ok(None)
}
}
/// Loads all active symbol configurations.
pub async fn load_all_symbols(&self) -> Result<Vec<crate::symbol_config::SymbolConfig>, sqlx::Error> {
let query = "
SELECT symbol, description, classification
FROM symbol_config
WHERE is_active = true
ORDER BY symbol
";
let rows = sqlx::query(query)
.fetch_all(&self.pool)
.await?;
let mut configs = Vec::new();
for row in rows {
let symbol_name: String = row.get("symbol");
let description: String = row.get("description");
let classification_str: String = row.get("classification");
let classification = match classification_str.as_str() {
"EQUITY" => crate::symbol_config::AssetClassification::Equity,
"FUTURE" => crate::symbol_config::AssetClassification::Future,
"FOREX" => crate::symbol_config::AssetClassification::Forex,
"CRYPTO" => crate::symbol_config::AssetClassification::Crypto,
"COMMODITY" => crate::symbol_config::AssetClassification::Commodity,
"FIXED_INCOME" => crate::symbol_config::AssetClassification::FixedIncome,
"OPTION" => crate::symbol_config::AssetClassification::Option,
"ETF" => crate::symbol_config::AssetClassification::Etf,
"INDEX" => crate::symbol_config::AssetClassification::Index,
"DERIVATIVE" => crate::symbol_config::AssetClassification::Derivative,
_ => crate::symbol_config::AssetClassification::Equity,
};
let mut config = crate::symbol_config::SymbolConfig::new(symbol_name, classification);
config.description = description;
configs.push(config);
}
Ok(configs)
}
/// Loads symbols filtered by asset classification.
pub async fn load_symbols_by_classification(
&self,
classification: crate::symbol_config::AssetClassification
) -> Result<Vec<crate::symbol_config::SymbolConfig>, sqlx::Error> {
let class_str = classification.regulatory_class();
let query = "
SELECT symbol, description, classification
FROM symbol_config
WHERE is_active = true AND classification = $1
ORDER BY symbol
";
let rows = sqlx::query(query)
.bind(class_str)
.fetch_all(&self.pool)
.await?;
let mut configs = Vec::new();
for row in rows {
let symbol_name: String = row.get("symbol");
let description: String = row.get("description");
let mut config = crate::symbol_config::SymbolConfig::new(symbol_name, classification.clone());
config.description = description;
configs.push(config);
}
Ok(configs)
}
/// Saves or updates a symbol configuration.
pub async fn save_symbol_config(
&self,
config: &crate::symbol_config::SymbolConfig
) -> Result<(), sqlx::Error> {
let query = "
INSERT INTO symbol_config (
symbol, description, classification, primary_exchange, currency
) VALUES ($1, $2, $3, $4, $5)
ON CONFLICT (symbol) DO UPDATE SET
description = EXCLUDED.description,
classification = EXCLUDED.classification,
primary_exchange = EXCLUDED.primary_exchange,
currency = EXCLUDED.currency,
updated_at = NOW()
";
sqlx::query(query)
.bind(&config.symbol)
.bind(&config.description)
.bind(config.classification.regulatory_class())
.bind(&config.primary_exchange)
.bind(&config.currency)
.execute(&self.pool)
.await?;
Ok(())
}
/// Initializes PostgreSQL NOTIFY/LISTEN for configuration hot-reload.
pub async fn enable_hot_reload(&mut self) -> Result<(), sqlx::Error> {
let mut listener = sqlx::postgres::PgListener::connect_with(&self.pool).await?;
listener.listen("symbol_config_changed").await?;
self.listener = Some(listener);
Ok(())
}
/// Checks for configuration change notifications.
pub async fn check_for_updates(&mut self) -> Result<Option<String>, sqlx::Error> {
if let Some(listener) = &mut self.listener {
if let Some(notification) = listener.try_recv().await? {
return Ok(Some(notification.payload().to_string()));
}
}
Ok(None)
}
}
/// Database integration for comprehensive asset classification system.
///
/// Provides PostgreSQL-backed storage and retrieval for asset classification
/// configurations with support for pattern matching, caching, and hot-reload.
#[cfg(feature = "postgres")]
pub struct PostgresAssetClassificationLoader {
/// Database connection pool
pool: sqlx::PgPool,
/// Configuration cache timeout
cache_timeout: Duration,
/// PostgreSQL listener for configuration changes
listener: Option<sqlx::postgres::PgListener>,
}
#[cfg(feature = "postgres")]
impl PostgresAssetClassificationLoader {
/// Creates a new PostgreSQL asset classification loader.
pub async fn new(database_url: &str) -> Result<Self, sqlx::Error> {
let pool = sqlx::PgPool::connect(database_url).await?;
Ok(Self {
pool,
cache_timeout: Duration::from_secs(300), // 5 minutes
listener: None,
})
}
/// Creates a new loader with an existing connection pool.
pub fn with_pool(pool: sqlx::PgPool) -> Self {
Self {
pool,
cache_timeout: Duration::from_secs(300),
listener: None,
}
}
/// Loads all active asset configurations ordered by priority.
pub async fn load_asset_configurations(&self) -> Result<Vec<crate::asset_classification::AssetConfig>, sqlx::Error> {
let query = "
SELECT
id,
name,
symbol_pattern,
asset_class_data,
volatility_profile,
trading_parameters,
priority,
is_active,
created_at,
updated_at,
trading_hours,
settlement_config
FROM asset_configurations
WHERE is_active = true
ORDER BY priority DESC
";
let rows = sqlx::query(query)
.fetch_all(&self.pool)
.await?;
let mut configs = Vec::new();
for row in rows {
if let Ok(config) = self.row_to_asset_config(row) {
configs.push(config);
}
}
Ok(configs)
}
/// Loads a specific asset configuration by ID.
pub async fn load_asset_configuration_by_id(&self, id: uuid::Uuid) -> Result<Option<crate::asset_classification::AssetConfig>, sqlx::Error> {
let query = "
SELECT
id,
name,
symbol_pattern,
asset_class_data,
volatility_profile,
trading_parameters,
priority,
is_active,
created_at,
updated_at,
trading_hours,
settlement_config
FROM asset_configurations
WHERE id = $1
";
let row = sqlx::query(query)
.bind(id)
.fetch_optional(&self.pool)
.await?;
if let Some(row) = row {
Ok(Some(self.row_to_asset_config(row)?))
} else {
Ok(None)
}
}
/// Saves or updates an asset configuration.
pub async fn save_asset_configuration(&self, config: &crate::asset_classification::AssetConfig) -> Result<(), sqlx::Error> {
let query = "
INSERT INTO asset_configurations (
id, name, symbol_pattern, asset_class_data, volatility_profile,
trading_parameters, priority, is_active, created_at, updated_at,
trading_hours, settlement_config
) VALUES ($1, $2, $3, $4, $5, $6, $7, $8, $9, $10, $11, $12)
ON CONFLICT (id) DO UPDATE SET
name = EXCLUDED.name,
symbol_pattern = EXCLUDED.symbol_pattern,
asset_class_data = EXCLUDED.asset_class_data,
volatility_profile = EXCLUDED.volatility_profile,
trading_parameters = EXCLUDED.trading_parameters,
priority = EXCLUDED.priority,
is_active = EXCLUDED.is_active,
updated_at = NOW(),
trading_hours = EXCLUDED.trading_hours,
settlement_config = EXCLUDED.settlement_config
";
let asset_class_json = serde_json::to_value(&config.asset_class)
.map_err(|e| sqlx::Error::Encode(Box::new(e)))?;
let volatility_json = serde_json::to_value(&config.volatility_profile)
.map_err(|e| sqlx::Error::Encode(Box::new(e)))?;
let trading_params_json = serde_json::to_value(&config.trading_parameters)
.map_err(|e| sqlx::Error::Encode(Box::new(e)))?;
let trading_hours_json = serde_json::to_value(&config.trading_hours)
.map_err(|e| sqlx::Error::Encode(Box::new(e)))?;
let settlement_json = serde_json::to_value(&config.settlement_config)
.map_err(|e| sqlx::Error::Encode(Box::new(e)))?;
sqlx::query(query)
.bind(config.id)
.bind(&config.name)
.bind(&config.symbol_pattern)
.bind(asset_class_json)
.bind(volatility_json)
.bind(trading_params_json)
.bind(config.priority as i32)
.bind(config.is_active)
.bind(config.created_at)
.bind(config.updated_at)
.bind(trading_hours_json)
.bind(settlement_json)
.execute(&self.pool)
.await?;
Ok(())
}
/// Loads explicit symbol mappings.
pub async fn load_symbol_mappings(&self) -> Result<std::collections::HashMap<String, crate::asset_classification::AssetClass>, sqlx::Error> {
let query = "
SELECT symbol, asset_class_data
FROM symbol_mappings
WHERE is_active = true AND (expires_at IS NULL OR expires_at > NOW())
";
let rows = sqlx::query(query)
.fetch_all(&self.pool)
.await?;
let mut mappings = std::collections::HashMap::new();
for row in rows {
let symbol: String = row.get("symbol");
let asset_class_json: serde_json::Value = row.get("asset_class_data");
if let Ok(asset_class) = serde_json::from_value::<crate::asset_classification::AssetClass>(asset_class_json) {
mappings.insert(symbol.to_uppercase(), asset_class);
}
}
Ok(mappings)
}
/// Saves a symbol mapping.
pub async fn save_symbol_mapping(
&self,
symbol: &str,
asset_class: &crate::asset_classification::AssetClass,
source: &str,
confidence_score: f64,
expires_at: Option<chrono::DateTime<chrono::Utc>>
) -> Result<(), sqlx::Error> {
let query = "
INSERT INTO symbol_mappings (
symbol, asset_class_data, source, confidence_score, expires_at
) VALUES ($1, $2, $3, $4, $5)
ON CONFLICT (symbol) DO UPDATE SET
asset_class_data = EXCLUDED.asset_class_data,
source = EXCLUDED.source,
confidence_score = EXCLUDED.confidence_score,
expires_at = EXCLUDED.expires_at,
updated_at = NOW()
";
let asset_class_json = serde_json::to_value(asset_class)
.map_err(|e| sqlx::Error::Encode(Box::new(e)))?;
sqlx::query(query)
.bind(symbol.to_uppercase())
.bind(asset_class_json)
.bind(source)
.bind(confidence_score)
.bind(expires_at)
.execute(&self.pool)
.await?;
Ok(())
}
/// Loads volatility profiles.
pub async fn load_volatility_profiles(&self) -> Result<std::collections::HashMap<String, crate::asset_classification::VolatilityProfile>, sqlx::Error> {
let query = "
SELECT
name,
base_annual_volatility,
stress_volatility_multiplier,
intraday_pattern,
volatility_persistence,
jump_risk
FROM volatility_profiles
WHERE is_active = true
";
let rows = sqlx::query(query)
.fetch_all(&self.pool)
.await?;
let mut profiles = std::collections::HashMap::new();
for row in rows {
let name: String = row.get("name");
let base_volatility: rust_decimal::Decimal = row.get("base_annual_volatility");
let stress_multiplier: rust_decimal::Decimal = row.get("stress_volatility_multiplier");
let persistence: rust_decimal::Decimal = row.get("volatility_persistence");
let intraday_json: serde_json::Value = row.get("intraday_pattern");
let jump_risk_json: serde_json::Value = row.get("jump_risk");
if let (Ok(base_vol), Ok(stress_mult), Ok(persist), Ok(intraday), Ok(jump_risk)) = (
f64::try_from(base_volatility),
f64::try_from(stress_multiplier),
f64::try_from(persistence),
serde_json::from_value::<Vec<f64>>(intraday_json),
serde_json::from_value::<crate::asset_classification::JumpRiskProfile>(jump_risk_json)
) {
let profile = crate::asset_classification::VolatilityProfile {
base_annual_volatility: base_vol,
stress_volatility_multiplier: stress_mult,
intraday_pattern: intraday,
volatility_persistence: persist,
jump_risk,
};
profiles.insert(name, profile);
}
}
Ok(profiles)
}
/// Caches symbol classification for performance.
pub async fn cache_symbol_classification(
&self,
symbol: &str,
asset_class: &crate::asset_classification::AssetClass,
configuration_id: Option<uuid::Uuid>
) -> Result<(), sqlx::Error> {
let query = "
INSERT INTO asset_classification_cache (symbol, asset_class_data, configuration_id)
VALUES ($1, $2, $3)
ON CONFLICT (symbol) DO UPDATE SET
asset_class_data = EXCLUDED.asset_class_data,
configuration_id = EXCLUDED.configuration_id,
cached_at = NOW(),
expires_at = NOW() + INTERVAL '1 hour'
";
let asset_class_json = serde_json::to_value(asset_class)
.map_err(|e| sqlx::Error::Encode(Box::new(e)))?;
sqlx::query(query)
.bind(symbol.to_uppercase())
.bind(asset_class_json)
.bind(configuration_id)
.execute(&self.pool)
.await?;
Ok(())
}
/// Retrieves cached symbol classification.
pub async fn get_cached_classification(&self, symbol: &str) -> Result<Option<crate::asset_classification::AssetClass>, sqlx::Error> {
let query = "
SELECT asset_class_data
FROM asset_classification_cache
WHERE symbol = $1 AND expires_at > NOW()
";
let row = sqlx::query(query)
.bind(symbol.to_uppercase())
.fetch_optional(&self.pool)
.await?;
if let Some(row) = row {
let asset_class_json: serde_json::Value = row.get("asset_class_data");
Ok(serde_json::from_value(asset_class_json).ok())
} else {
Ok(None)
}
}
/// Cleans up expired cache entries.
pub async fn cleanup_cache(&self) -> Result<u64, sqlx::Error> {
let query = "DELETE FROM asset_classification_cache WHERE expires_at < NOW()";
let result = sqlx::query(query).execute(&self.pool).await?;
Ok(result.rows_affected())
}
/// Logs asset classification changes for audit.
pub async fn log_classification_change(
&self,
symbol: &str,
old_classification: Option<&crate::asset_classification::AssetClass>,
new_classification: &crate::asset_classification::AssetClass,
changed_by: &str,
reason: &str
) -> Result<(), sqlx::Error> {
let query = "
INSERT INTO asset_classification_audit (
symbol, old_classification, new_classification, changed_by, change_reason
) VALUES ($1, $2, $3, $4, $5)
";
let old_json = old_classification.map(|c| serde_json::to_value(c).ok()).flatten();
let new_json = serde_json::to_value(new_classification)
.map_err(|e| sqlx::Error::Encode(Box::new(e)))?;
sqlx::query(query)
.bind(symbol)
.bind(old_json)
.bind(new_json)
.bind(changed_by)
.bind(reason)
.execute(&self.pool)
.await?;
Ok(())
}
/// Enables PostgreSQL NOTIFY/LISTEN for configuration hot-reload.
pub async fn enable_hot_reload(&mut self) -> Result<(), sqlx::Error> {
let mut listener = sqlx::postgres::PgListener::connect_with(&self.pool).await?;
listener.listen("config_change").await?;
self.listener = Some(listener);
Ok(())
}
/// Checks for configuration change notifications.
pub async fn check_for_config_updates(&mut self) -> Result<Option<String>, sqlx::Error> {
if let Some(listener) = &mut self.listener {
if let Some(notification) = listener.try_recv().await? {
return Ok(Some(notification.payload().to_string()));
}
}
Ok(None)
}
/// Converts a database row to AssetConfig.
fn row_to_asset_config(&self, row: sqlx::postgres::PgRow) -> Result<crate::asset_classification::AssetConfig, sqlx::Error> {
let id: uuid::Uuid = row.get("id");
let name: String = row.get("name");
let symbol_pattern: String = row.get("symbol_pattern");
let priority: i32 = row.get("priority");
let is_active: bool = row.get("is_active");
let created_at: chrono::DateTime<chrono::Utc> = row.get("created_at");
let updated_at: chrono::DateTime<chrono::Utc> = row.get("updated_at");
let asset_class_json: serde_json::Value = row.get("asset_class_data");
let volatility_json: serde_json::Value = row.get("volatility_profile");
let trading_params_json: serde_json::Value = row.get("trading_parameters");
let trading_hours_json: Option<serde_json::Value> = row.get("trading_hours");
let settlement_json: serde_json::Value = row.get("settlement_config");
let asset_class = serde_json::from_value(asset_class_json)
.map_err(|e| sqlx::Error::Decode(Box::new(e)))?;
let volatility_profile = serde_json::from_value(volatility_json)
.map_err(|e| sqlx::Error::Decode(Box::new(e)))?;
let trading_parameters = serde_json::from_value(trading_params_json)
.map_err(|e| sqlx::Error::Decode(Box::new(e)))?;
let trading_hours = trading_hours_json
.map(|json| serde_json::from_value(json).ok())
.flatten();
let settlement_config = serde_json::from_value(settlement_json)
.map_err(|e| sqlx::Error::Decode(Box::new(e)))?;
Ok(crate::asset_classification::AssetConfig {
id,
name,
symbol_pattern,
compiled_pattern: None, // Will be compiled when loaded
asset_class,
volatility_profile,
trading_parameters,
priority: priority as u32,
is_active,
created_at,
updated_at,
trading_hours,
settlement_config,
})
}
}
/// General-purpose PostgreSQL configuration loader for various configuration types.
///
/// Provides a unified interface for loading configurations from PostgreSQL with
/// support for hot-reload through NOTIFY/LISTEN and caching for performance.
#[cfg(feature = "postgres")]
pub struct PostgresConfigLoader {
/// Database connection pool
pool: sqlx::PgPool,
/// Configuration cache timeout
cache_timeout: Duration,
}
#[cfg(feature = "postgres")]
impl PostgresConfigLoader {
/// Creates a new PostgreSQL configuration loader.
pub async fn new(database_url: &str) -> Result<Self, sqlx::Error> {
let pool = sqlx::PgPool::connect(database_url).await?;
Ok(Self {
pool,
cache_timeout: Duration::from_secs(300), // 5 minutes
})
}
/// Creates a new loader with an existing connection pool.
pub fn with_pool(pool: sqlx::PgPool) -> Self {
Self {
pool,
cache_timeout: Duration::from_secs(300),
}
}
/// Get the underlying connection pool.
pub fn pool(&self) -> &sqlx::PgPool {
&self.pool
}
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn test_database_config_new() {
let config = DatabaseConfig::new();
assert!(!config.url.is_empty());
assert_eq!(config.max_connections, 10);
assert_eq!(config.min_connections, 1);
assert!(config.application_name.is_some());
}
#[test]
fn test_database_config_validate_success() {
let config = DatabaseConfig::new();
assert!(config.validate().is_ok());
}
#[test]
fn test_database_config_validate_empty_url() {
let mut config = DatabaseConfig::new();
config.url = String::new();
assert!(config.validate().is_err());
}
#[test]
fn test_pool_config_default() {
let pool_config = PoolConfig::default();
assert_eq!(pool_config.min_connections, 1);
assert_eq!(pool_config.max_connections, 10);
assert!(pool_config.test_before_acquire);
}
#[test]
fn test_transaction_config_default() {
let tx_config = TransactionConfig::default();
assert_eq!(tx_config.isolation_level, "READ_COMMITTED");
assert_eq!(tx_config.default_timeout_secs, 30);
assert!(tx_config.enable_retry);
}
#[test]
fn test_transaction_config_serialization() {
let tx_config = TransactionConfig::default();
let serialized = serde_json::to_string(&tx_config).unwrap();
let deserialized: TransactionConfig = serde_json::from_str(&serialized).unwrap();
assert_eq!(tx_config.isolation_level, deserialized.isolation_level);
}
#[test]
fn test_database_config_with_custom_values() {
let mut config = DatabaseConfig::new();
config.max_connections = 50;
config.min_connections = 5;
config.enable_query_logging = true;
assert_eq!(config.max_connections, 50);
assert_eq!(config.min_connections, 5);
assert!(config.enable_query_logging);
}
#[test]
fn test_pool_config_timeouts() {
let mut pool_config = PoolConfig::default();
pool_config.acquire_timeout_secs = 30;
pool_config.max_lifetime_secs = 1800;
pool_config.idle_timeout_secs = 600;
assert_eq!(pool_config.acquire_timeout_secs, 30);
assert_eq!(pool_config.max_lifetime_secs, 1800);
assert_eq!(pool_config.idle_timeout_secs, 600);
}
#[test]
fn test_transaction_config_isolation_levels() {
let levels = vec![
"READ_UNCOMMITTED",
"READ_COMMITTED",
"REPEATABLE_READ",
"SERIALIZABLE",
];
for level in levels {
let mut tx_config = TransactionConfig::default();
tx_config.isolation_level = level.to_string();
assert_eq!(tx_config.isolation_level, level);
}
}
#[test]
fn test_database_config_clone() {
let config1 = DatabaseConfig::new();
let config2 = config1.clone();
assert_eq!(config1.url, config2.url);
assert_eq!(config1.max_connections, config2.max_connections);
assert_eq!(config1.min_connections, config2.min_connections);
}
#[test]
fn test_pool_config_validation() {
let pool_config = PoolConfig::default();
assert!(pool_config.min_connections <= pool_config.max_connections);
}
#[test]
fn test_database_url_format() {
let config = DatabaseConfig::new();
assert!(config.url.starts_with("postgresql://"));
}
#[test]
fn test_transaction_config_retry_settings() {
let mut tx_config = TransactionConfig::default();
tx_config.enable_retry = true;
tx_config.max_retries = 5;
assert!(tx_config.enable_retry);
assert_eq!(tx_config.max_retries, 5);
tx_config.enable_retry = false;
assert!(!tx_config.enable_retry);
}
#[test]
fn test_pool_config_connection_settings() {
let mut pool_config = PoolConfig::default();
pool_config.test_before_acquire = true;
pool_config.acquire_timeout_secs = 30;
assert!(pool_config.test_before_acquire);
assert_eq!(pool_config.acquire_timeout_secs, 30);
}
#[test]
fn test_database_config_application_name() {
let config = DatabaseConfig::new();
assert_eq!(config.application_name, Some("foxhunt".to_string()));
}
#[test]
fn test_database_config_query_logging() {
let mut config = DatabaseConfig::new();
config.enable_query_logging = true;
assert!(config.enable_query_logging);
}
#[test]
fn test_pool_config_connection_limits() {
let mut pool_config = PoolConfig::default();
pool_config.max_connections = 100;
pool_config.min_connections = 10;
assert_eq!(pool_config.max_connections, 100);
assert_eq!(pool_config.min_connections, 10);
}
#[test]
fn test_transaction_timeout() {
let mut tx_config = TransactionConfig::default();
tx_config.default_timeout_secs = 60;
assert_eq!(tx_config.default_timeout_secs, 60);
assert_eq!(tx_config.timeout, Duration::from_secs(60));
}
#[test]
fn test_database_config_connect_timeout() {
let config = DatabaseConfig::new();
assert_eq!(config.connect_timeout, Duration::from_secs(30));
}
#[test]
fn test_database_config_query_timeout() {
let config = DatabaseConfig::new();
assert_eq!(config.query_timeout, Duration::from_secs(60));
}
#[test]
fn test_pool_config_test_before_acquire() {
let mut pool_config = PoolConfig::default();
pool_config.test_before_acquire = false;
assert!(!pool_config.test_before_acquire);
pool_config.test_before_acquire = true;
assert!(pool_config.test_before_acquire);
}
}