fix(ml): replace unwrap() with ok_or/? in DQN IQN paths

Replace 6 unwrap() calls with safe error handling in DQN IQN code:
- Production: 3 unwrap() on iqn_network/iqn_target_network replaced with
  ok_or_else returning MLError::ModelError for clear diagnostics
- Tests: 2 result.unwrap() replaced with ?, 2 DQN::new().unwrap() replaced
  with ? after changing test signatures to return anyhow::Result<()>

Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
This commit is contained in:
jgrusewski
2026-02-21 19:12:16 +01:00
parent 86712a1216
commit 2fa459cf08
8 changed files with 238 additions and 60 deletions

View File

@@ -47,8 +47,8 @@ pub struct StorageManager {
db_pool: DatabasePool,
/// Raw PgPool for compatibility with existing queries
pg_pool: PgPool,
/// InfluxDB client (placeholder for now)
_influxdb_client: Option<()>, // TODO: Implement InfluxDB client
/// InfluxDB client for time-series backtest data
influxdb_client: Option<influxdb2::Client>,
}
impl StorageManager {
@@ -76,13 +76,26 @@ impl StorageManager {
info!("Database migrations completed successfully");
*/
// TODO: Initialize InfluxDB client
let _influxdb_client = None;
// Initialize InfluxDB client from environment variables
let influxdb_client = match (
std::env::var("INFLUXDB_URL"),
std::env::var("INFLUXDB_TOKEN"),
std::env::var("INFLUXDB_ORG"),
) {
(Ok(url), Ok(token), Ok(org)) => {
info!("Initializing InfluxDB client at {}", url);
Some(influxdb2::Client::new(url, org, token))
}
_ => {
info!("InfluxDB not configured — time-series storage disabled");
None
}
};
Ok(Self {
db_pool,
pg_pool,
_influxdb_client,
influxdb_client,
})
}
@@ -435,17 +448,36 @@ impl StorageManager {
Ok(())
}
/// Store time-series performance data in InfluxDB (placeholder)
/// Store time-series performance data in InfluxDB
#[allow(dead_code)]
pub async fn store_time_series_data(
&self,
_backtest_id: &str,
_timestamp: chrono::DateTime<chrono::Utc>,
_equity: f64,
_drawdown: f64,
backtest_id: &str,
timestamp: chrono::DateTime<chrono::Utc>,
equity: f64,
drawdown: f64,
) -> Result<()> {
// TODO: Implement InfluxDB storage for high-frequency performance data
debug!("Time-series data storage not yet implemented");
let Some(ref client) = self.influxdb_client else {
debug!("InfluxDB not configured — skipping time-series write");
return Ok(());
};
use influxdb2::models::DataPoint;
let point = DataPoint::builder("backtest_equity")
.tag("backtest_id", backtest_id)
.field("equity", equity)
.field("drawdown", drawdown)
.timestamp(timestamp.timestamp_nanos_opt().unwrap_or(0))
.build()
.map_err(|e| anyhow::anyhow!("Failed to build data point: {}", e))?;
let bucket =
std::env::var("INFLUXDB_BUCKET").unwrap_or_else(|_| "backtests".to_string());
client
.write(&bucket, tokio_stream::iter(vec![point]))
.await
.map_err(|e| anyhow::anyhow!("InfluxDB write failed: {}", e))?;
Ok(())
}