Files
foxhunt/services/ml_training_service/tests/integration_regime_persistence.rs
jgrusewski 83629f9ca8 feat(deployment): Complete Runpod GPU deployment infrastructure
Implement comprehensive Runpod deployment with S3 volume mount architecture for
FP32 ML model training on Tesla V100 GPUs.

## Infrastructure Components

### Deployment Scripts (scripts/)
- runpod_deploy.sh: Master deployment orchestrator (8-step workflow)
- runpod_upload.sh: S3 upload for binaries and test data
- upload_env_to_runpod.sh: Secure .env credentials upload
- runpod_deploy_test.sh: Prerequisites validation

### Docker Configuration
- Dockerfile.runpod: Multi-stage CUDA 12.1 runtime (~2GB, no binaries)
- entrypoint.sh: Volume verification and training execution
- Architecture: Volume mount (NO S3 downloads in pods)

### S3 Configuration
- Bucket: se3zdnb5o4 (Iceland region: eur-is-1)
- Endpoint: https://s3api-eur-is-1.runpod.io
- Structure: binaries/, test_data/, models/, .env

### OpenTofu Infrastructure (terraform/runpod/)
- main.tf: Pod and volume resources
- variables.tf: Configuration variables
- outputs.tf: Pod connection info
- Security: NO credentials in state (uses volume .env)

## Deployment Assets Uploaded

### Training Binaries (77MB)
- train_tft_parquet (23M) - TFT-225 features
- train_mamba2_parquet (22M) - MAMBA-2 state space
- train_dqn (22M) - Deep Q-Network
- train_ppo (13M) - Proximal Policy Optimization

### Test Data (13.8 MB)
- 9 Parquet files: ES.FUT, NQ.FUT, 6E.FUT, ZN.FUT (180-day datasets)

### Credentials
- .env file (1.5 KB, private access, chmod 600)

## Documentation

### Deployment Guides
- RUNPOD_DEPLOYMENT_READY_SUMMARY.md: Complete deployment status
- RUNPOD_VOLUME_DEPLOYMENT_GUIDE.md: Step-by-step guide (42KB)
- RUNPOD_DEPLOYMENT_QUICK_START.md: Quick reference
- RUNPOD_UPLOAD_GUIDE.md: S3 upload instructions
- RUNPOD_VOLUME_CONFIGURATION_COMPLETE.md: S3 setup report
- RUNPOD_S3_PARQUET_UPLOAD_REPORT.md: Data upload verification

### Architecture Documentation
- RUNPOD_VOLUME_MOUNT_ARCHITECTURE.md: Volume mount design
- RUNPOD_S3_ARCHITECTURE_DIAGRAM.txt: S3 API vs filesystem access
- DOCKERFILE_RUNPOD_FINAL_SUMMARY.md: Docker image specification

### Decision Documentation
- RUNPOD_DEPLOYMENT_CHECKLIST.md: Go/no-go decision matrix (27KB)
- RUNPOD_DEPLOYMENT_DECISION_TREE.md: Decision workflow
- FP32_RUNPOD_DEPLOYMENT_READY.md: FP32 deployment readiness

## QAT Enhancements

### Core QAT Infrastructure
- ml/src/memory_optimization/qat.rs: Enhanced QAT observer (+226 lines)
- ml/src/memory_optimization/auto_batch_size.rs: OOM recovery (+84 lines)
- ml/src/tft/qat_tft.rs: QAT TFT wrapper (+154 lines)
- ml/src/trainers/tft.rs: QAT training integration (+433 lines)
- ml/src/qat_metrics_exporter.rs: NEW - QAT metrics export

### QAT Testing
- ml/tests/qat_integration_tests.rs: NEW - Integration test suite
- ml/tests/qat_gradient_clipping_test.rs: NEW - Gradient clipping tests
- ml/tests/qat_device_consistency_test.rs: Device mismatch tests (+205 lines)
- ml/tests/qat_accuracy_validation_test.rs: Accuracy validation
- ml/tests/qat_tft_integration_test.rs: TFT QAT integration

### QAT Documentation
- ml/docs/QAT_GUIDE.md: Comprehensive QAT guide (+616 lines)
- ml/docs/QAT_GRADIENT_CHECKPOINTING_WORKAROUND.md: NEW - Workaround guide
- QAT_BLOCKERS_ROOT_CAUSE_ANALYSIS.md: P0 blocker analysis (44KB)
- QAT_ACCURACY_VALIDATION_REPORT.md: Accuracy comparison
- QAT_GRADIENT_CLIPPING_VALIDATION_REPORT.md: Clipping validation

### QAT Monitoring
- config/grafana/dashboards/qat-training-metrics.json: NEW - Grafana dashboard

## AWS CLI Configuration

### Credentials Setup
- ~/.aws/credentials: Runpod profile configured
  - Access Key: user_2xxA3XcIFj16yfL3aBon9niiSpr
  - Secret Key: (from RUNPOD_S3_SECRET)
- ~/.aws/config: Iceland region (eur-is-1)

## Production Readiness

### FP32 Models:  READY FOR DEPLOYMENT
- DQN: 15-20s training, ~6MB GPU memory
- PPO: 7-10s training, ~145MB GPU memory
- MAMBA-2: 2-3 min training, ~164MB GPU memory
- TFT-225: 3-5 min training, ~500MB GPU memory
- Total GPU Budget: 815MB (fits on 4GB+ Tesla V100)

### QAT Models: 🔴 BLOCKED
- 24 tests implemented but DO NOT COMPILE (11 errors)
- 3 P0 blockers: device mismatch, gradient checkpointing, OOM recovery
- Timeline: 1-2 weeks to fix (13h P0 fixes + validation)

### Wave D Features:  OPERATIONAL
- 225 features fully integrated
- Feature extraction: 5.10μs/bar (196x faster than target)
- Wave D backtest: Sharpe 2.00, Win Rate 60%, Drawdown 15%
- Database migration 045: Applied cleanly, zero conflicts

## Cost Analysis

### One-Time Setup
- Network Volume: $4/month (50GB SSD)
- Upload costs: FREE (S3 API included)

### Per Training Run (TFT-225)
- GPU: Tesla V100-PCIE-16GB @ $0.29/hr
- Training Time: ~4 hours
- Cost per run: $1.16

### Monthly (20 Training Runs)
- Storage: $4.00/month
- Training: $23.20/month (20 runs × $1.16)
- Total: $27.20/month

## Security

### Credentials Management
-  NO credentials in Docker image
-  NO credentials in Terraform state
-  .env gitignored and not committed
-  .env file private on S3 (HTTP 401 on public access)
-  Docker Hub repository PRIVATE (jgrusewski/foxhunt)

### Access Control
- S3 API: Local client uploads only
- Volume mount: Pod filesystem access only
- Authentication: AWS CLI with Runpod profile required

## Next Steps

1.  COMPLETE: Build Docker image
2.  PENDING: Push to Docker Hub
3.  PENDING: Deploy pod via Runpod console
4.  PENDING: Validate training on Tesla V100

## Performance Targets

- Build time: 5-10 min
- Upload time: ~20 sec (90MB total)
- Pod startup: ~30 sec
- Training time: 3-5 min (TFT-225)
- Total deployment: ~40 min from start to first training run

## Test Status

- FP32 tests: 597/608 passing (98.2%)
- QAT tests: 0/24 passing (compilation errors)
- Overall: 2,062/2,086 passing (98.8% excluding QAT)

🤖 Generated with Claude Code (https://claude.com/claude-code)

Co-Authored-By: Claude <noreply@anthropic.com>
2025-10-24 01:11:43 +02:00

676 lines
21 KiB
Rust

//! Integration Test: Database Regime Persistence
//!
//! **Agent IMPL-24**: Verifies that regime_states, regime_transitions, and
//! adaptive_strategy_metrics are properly populated during ML training operations.
//!
//! ## Test Coverage
//! - Regime state persistence during feature extraction
//! - Regime transition tracking across multiple bars
//! - Adaptive strategy metrics population
//! - Database schema validation (constraints, indices)
//! - Grafana dashboard compatibility (query validation)
//! - Multi-symbol regime tracking
//!
//! ## Dependencies
//! - Requires PostgreSQL running with migration 045 applied
//! - Uses real DatabasePool (no mocks)
//! - Tests actual SQL queries used by Grafana dashboards
use anyhow::Result;
use chrono::{DateTime, Utc};
use common::database::DatabasePool;
use common::regime_persistence::RegimePersistenceManager;
use sqlx::PgPool;
/// Helper to create test database pool
async fn setup_test_db() -> Result<DatabasePool> {
let database_url = std::env::var("DATABASE_URL").unwrap_or_else(|_| {
"postgresql://foxhunt:foxhunt_dev_password@localhost:5432/foxhunt".to_string()
});
use common::database::{LocalDatabaseConfig, PoolConfig, PerformanceConfig};
let config = LocalDatabaseConfig {
url: database_url,
pool: PoolConfig {
max_connections: 5,
min_connections: 1,
connect_timeout_ms: 10000,
acquire_timeout_ms: 10000,
max_lifetime_seconds: 3600,
idle_timeout_seconds: 600,
},
performance: PerformanceConfig {
query_timeout_micros: 100_000,
enable_prewarming: false,
enable_prepared_statements: true,
enable_slow_query_logging: false,
slow_query_threshold_micros: 50_000,
},
};
DatabasePool::new(config).await.map_err(|e| anyhow::anyhow!("Failed to create pool: {}", e))
}
/// Helper to get the underlying PgPool for raw SQL queries
async fn get_pg_pool() -> Result<PgPool> {
let database_url = std::env::var("DATABASE_URL").unwrap_or_else(|_| {
"postgresql://foxhunt:foxhunt_dev_password@localhost:5432/foxhunt".to_string()
});
Ok(PgPool::connect(&database_url).await?)
}
/// Helper to clear regime tables for clean testing
async fn clear_regime_tables(pg_pool: &PgPool) -> Result<()> {
// Clear in reverse dependency order
sqlx::query!("DELETE FROM adaptive_strategy_metrics").execute(pg_pool).await?;
sqlx::query!("DELETE FROM regime_transitions").execute(pg_pool).await?;
sqlx::query!("DELETE FROM regime_states").execute(pg_pool).await?;
Ok(())
}
/// Generate mock regime features for testing
///
/// # Arguments
/// * `cusum_mean` - CUSUM mean (feature 201)
/// * `cusum_std` - CUSUM std (feature 202)
/// * `adx` - ADX value (feature 211)
/// * `position_mult` - Position multiplier (feature 221)
/// * `stop_mult` - Stop-loss multiplier (feature 222)
fn generate_regime_features(
cusum_mean: f64,
cusum_std: f64,
adx: f64,
position_mult: f64,
stop_mult: f64,
) -> [f64; 24] {
[
// CUSUM Statistics (features 201-210)
cusum_mean,
cusum_std,
0.5, // cusum_s_plus
-0.3, // cusum_s_minus
0.0, // cusum_range
0.0, // cusum_crossings
0.0, // cusum_max
0.0, // cusum_min
0.0, // cusum_mean_abs
0.0, // cusum_trend
// ADX & Directional (features 211-215)
adx,
0.0, // plus_di
0.0, // minus_di
0.0, // adx_slope
0.0, // di_diff
// Transition Probabilities (features 216-220)
0.7, // prob_stay
0.2, // prob_volatile
0.1, // prob_trending
0.0, // prob_ranging
0.0, // prob_normal
// Adaptive Metrics (features 221-224)
position_mult,
stop_mult,
0.0, // regime_sharpe (calculated during backtest)
0.0, // risk_utilization (calculated during backtest)
]
}
#[tokio::test]
#[ignore = "Requires PostgreSQL with migration 045 applied"]
async fn test_regime_states_persisted_during_training() -> Result<()> {
let pool = setup_test_db().await?;
let pg_pool = get_pg_pool().await?;
// 1. Clear regime tables
clear_regime_tables(&pg_pool).await?;
// 2. Create regime persistence manager
let pool_clone = pool.clone();
let mut manager = RegimePersistenceManager::new(pool_clone);
// 3. Simulate ML training with Wave D features for ES.FUT
let symbols = vec!["ES.FUT", "NQ.FUT"];
let base_timestamp = Utc::now();
for (idx, symbol) in symbols.iter().enumerate() {
let timestamp = base_timestamp + chrono::Duration::seconds(idx as i64 * 60);
// Generate features for volatile regime (cusum_std > 2.0)
let features = generate_regime_features(
0.5, // cusum_mean
3.0, // cusum_std (VOLATILE)
35.0, // adx
0.8, // position_mult (reduced for volatile)
3.5, // stop_mult (wider stops)
);
manager
.process_regime_features(symbol, &features, timestamp)
.await?;
}
// 4. Verify regime_states populated
let state_count = sqlx::query_scalar!(
r#"SELECT COUNT(*) as "count!" FROM regime_states"#
)
.fetch_one(&pg_pool)
.await?;
assert!(
state_count > 0,
"No regime states persisted! Expected at least 2, got {}",
state_count
);
assert_eq!(
state_count, 2,
"Expected 2 regime states (ES.FUT + NQ.FUT), got {}",
state_count
);
// 5. Verify ES.FUT regime state details
let es_state = pool.get_latest_regime("ES.FUT").await?;
assert_eq!(es_state.symbol, "ES.FUT");
assert_eq!(es_state.regime, "Volatile"); // cusum_std=3.0 > 2.0
assert!(es_state.confidence > 0.0 && es_state.confidence <= 1.0);
assert!(es_state.cusum_s_plus.is_some());
assert!(es_state.cusum_s_minus.is_some());
assert!(es_state.adx.is_some());
assert_eq!(es_state.adx.unwrap(), 35.0);
// 6. Verify adaptive_strategy_metrics populated
let metrics_count = sqlx::query_scalar!(
r#"SELECT COUNT(*) as "count!" FROM adaptive_strategy_metrics WHERE symbol = 'ES.FUT'"#
)
.fetch_one(&pg_pool)
.await?;
assert!(
metrics_count > 0,
"No adaptive metrics persisted for ES.FUT!"
);
// 7. Verify metrics values
let metrics = sqlx::query!(
r#"
SELECT
position_multiplier,
stop_loss_multiplier,
regime
FROM adaptive_strategy_metrics
WHERE symbol = 'ES.FUT'
ORDER BY event_timestamp DESC
LIMIT 1
"#
)
.fetch_one(&pg_pool)
.await?;
assert_eq!(metrics.position_multiplier, 0.8);
assert_eq!(metrics.stop_loss_multiplier, 3.5);
assert!(
metrics.position_multiplier > 0.0 && metrics.position_multiplier <= 2.0,
"Position multiplier out of range: {}",
metrics.position_multiplier
);
assert!(
metrics.stop_loss_multiplier >= 1.0 && metrics.stop_loss_multiplier <= 5.0,
"Stop-loss multiplier out of range: {}",
metrics.stop_loss_multiplier
);
Ok(())
}
#[tokio::test]
#[ignore = "Requires PostgreSQL with migration 045 applied"]
async fn test_regime_transitions_tracked() -> Result<()> {
let pool = setup_test_db().await?;
let pg_pool = get_pg_pool().await?;
clear_regime_tables(&pg_pool).await?;
let pool_clone = pool.clone();
let mut manager = RegimePersistenceManager::new(pool_clone);
let symbol = "TRANSITION.TEST";
let base_timestamp = Utc::now();
// First regime: Volatile (3 bars)
for i in 0..3 {
let timestamp = base_timestamp + chrono::Duration::seconds(i * 60);
let features = generate_regime_features(0.5, 3.0, 35.0, 0.8, 3.5);
manager
.process_regime_features(symbol, &features, timestamp)
.await?;
}
// Second regime: Trending (2 bars)
for i in 3..5 {
let timestamp = base_timestamp + chrono::Duration::seconds(i * 60);
// Trending: cusum_mean > 1.5 && adx > 25
let features = generate_regime_features(2.0, 1.0, 30.0, 1.2, 2.0);
manager
.process_regime_features(symbol, &features, timestamp)
.await?;
}
// Third regime: Ranging (1 bar)
let timestamp = base_timestamp + chrono::Duration::seconds(5 * 60);
// Ranging: adx < 20 && cusum_std < 1.0
let features = generate_regime_features(0.2, 0.5, 15.0, 1.0, 1.5);
manager
.process_regime_features(symbol, &features, timestamp)
.await?;
// Verify transitions were recorded
let transition_count = sqlx::query_scalar!(
r#"SELECT COUNT(*) as "count!" FROM regime_transitions WHERE symbol = $1"#,
symbol
)
.fetch_one(&pg_pool)
.await?;
assert!(
transition_count >= 2,
"Expected at least 2 transitions (Volatile->Trending, Trending->Ranging), got {}",
transition_count
);
// Get transition details
let transitions = pool.get_regime_transitions(symbol, 10).await?;
assert!(!transitions.is_empty(), "No transitions recorded!");
// Verify first transition: Volatile -> Trending
let first_transition = &transitions
.iter()
.find(|t| t.from_regime == "Volatile" && t.to_regime == "Trending");
assert!(
first_transition.is_some(),
"Expected Volatile->Trending transition"
);
let transition = first_transition.unwrap();
assert_eq!(transition.duration_bars, Some(3)); // 3 bars in Volatile regime
// Verify transition matrix query works (used by Grafana)
let matrix = sqlx::query!(
r#"
SELECT
from_regime,
to_regime,
COUNT(*) as "count!"
FROM regime_transitions
WHERE symbol = $1
GROUP BY from_regime, to_regime
ORDER BY from_regime, to_regime
"#,
symbol
)
.fetch_all(&pg_pool)
.await?;
assert!(!matrix.is_empty(), "No transition matrix data!");
assert!(matrix.len() >= 2, "Expected at least 2 transition pairs");
Ok(())
}
#[tokio::test]
#[ignore = "Requires PostgreSQL with migration 045 applied"]
async fn test_grafana_can_query_regime_states() -> Result<()> {
let pool = setup_test_db().await?;
let pg_pool = get_pg_pool().await?;
clear_regime_tables(&pg_pool).await?;
let mut manager = RegimePersistenceManager::new(pool);
let symbol = "ES.FUT";
let base_timestamp = Utc::now();
// Populate some regime states
for i in 0..10 {
let timestamp = base_timestamp + chrono::Duration::seconds(i * 60);
let features = generate_regime_features(0.5, 3.0, 35.0, 0.8, 3.5);
manager
.process_regime_features(symbol, &features, timestamp)
.await?;
}
// Test Grafana-style query: regime distribution by symbol
let regime_distribution = sqlx::query!(
r#"
SELECT
symbol,
regime,
COUNT(*) as "count!",
AVG(confidence) as "avg_confidence!"
FROM regime_states
WHERE event_timestamp >= NOW() - INTERVAL '1 hour'
GROUP BY symbol, regime
ORDER BY symbol, regime
"#
)
.fetch_all(&pg_pool)
.await?;
assert!(!regime_distribution.is_empty(), "Expected regime data");
// Verify data structure is correct for Grafana
for row in regime_distribution {
assert!(!row.symbol.is_empty());
assert!(!row.regime.is_empty());
assert!(row.count > 0);
assert!(row.avg_confidence >= 0.0 && row.avg_confidence <= 1.0);
}
// Test time-series query (used by Grafana timeseries panel)
let timeseries = sqlx::query!(
r#"
SELECT
event_timestamp,
regime,
confidence,
adx
FROM regime_states
WHERE symbol = $1
ORDER BY event_timestamp DESC
LIMIT 100
"#,
symbol
)
.fetch_all(&pg_pool)
.await?;
assert!(!timeseries.is_empty(), "Expected time-series data");
assert!(timeseries.len() <= 100, "Query limit not enforced");
Ok(())
}
#[tokio::test]
#[ignore = "Requires PostgreSQL with migration 045 applied"]
async fn test_regime_state_has_valid_timestamp() -> Result<()> {
let pool = setup_test_db().await?;
let pg_pool = get_pg_pool().await?;
clear_regime_tables(&pg_pool).await?;
let pool_clone = pool.clone();
let mut manager = RegimePersistenceManager::new(pool_clone);
let symbol = "TIMESTAMP.TEST";
let timestamp = Utc::now();
let features = generate_regime_features(0.5, 3.0, 35.0, 0.8, 3.5);
manager
.process_regime_features(symbol, &features, timestamp)
.await?;
// Verify timestamp is stored correctly
let state = pool.get_latest_regime(symbol).await?;
let time_diff = (Utc::now() - state.event_timestamp)
.num_seconds()
.abs();
assert!(
time_diff < 60,
"Timestamp should be recent (within 60 seconds), got diff: {} seconds",
time_diff
);
Ok(())
}
#[tokio::test]
#[ignore = "Requires PostgreSQL with migration 045 applied"]
async fn test_confidence_scores_in_valid_range() -> Result<()> {
let pool = setup_test_db().await?;
let pg_pool = get_pg_pool().await?;
clear_regime_tables(&pg_pool).await?;
let pool_clone = pool.clone();
let mut manager = RegimePersistenceManager::new(pool_clone);
let symbol = "CONFIDENCE.TEST";
// Test various ADX values (confidence = adx / 50.0)
let test_cases = vec![
(10.0, 0.2), // Low ADX
(25.0, 0.5), // Medium ADX
(50.0, 1.0), // High ADX
(100.0, 1.0), // Out of range (clamped to 1.0)
];
for (idx, (adx, expected_confidence)) in test_cases.iter().enumerate() {
let timestamp = Utc::now() + chrono::Duration::seconds(idx as i64 * 60);
let features = generate_regime_features(0.5, 3.0, *adx, 0.8, 3.5);
manager
.process_regime_features(symbol, &features, timestamp)
.await?;
let state = pool.get_latest_regime(symbol).await?;
assert!(
state.confidence >= 0.0 && state.confidence <= 1.0,
"Confidence out of range [0.0, 1.0]: {}",
state.confidence
);
assert!(
(state.confidence - expected_confidence).abs() < 0.01,
"Expected confidence {}, got {}",
expected_confidence,
state.confidence
);
}
Ok(())
}
#[tokio::test]
#[ignore = "Requires PostgreSQL with migration 045 applied"]
async fn test_adaptive_metrics_update_on_backtest() -> Result<()> {
let pool = setup_test_db().await?;
let pg_pool = get_pg_pool().await?;
clear_regime_tables(&pg_pool).await?;
let pool_clone = pool.clone();
let mut manager = RegimePersistenceManager::new(pool_clone);
let symbol = "BACKTEST.TEST";
let regime = "Trending";
let timestamp = Utc::now();
// Initial metrics population
let features = generate_regime_features(2.0, 1.0, 30.0, 1.2, 2.0);
manager
.process_regime_features(symbol, &features, timestamp)
.await?;
// Simulate trade execution updates
manager
.update_trade_metrics(symbol, regime, timestamp, 1000, true)
.await?; // +$1000, winner
manager
.update_trade_metrics(symbol, regime, timestamp, -500, false)
.await?; // -$500, loser
manager
.update_trade_metrics(symbol, regime, timestamp, 750, true)
.await?; // +$750, winner
// Verify performance metrics
let performance = pool.get_regime_performance(Some(symbol), 24).await?;
let trending_perf = performance
.iter()
.find(|p| p.regime == Some("Trending".to_string()))
.expect("Expected Trending regime performance");
assert_eq!(trending_perf.total_trades, Some(3));
assert_eq!(trending_perf.win_rate, Some(2.0 / 3.0)); // 2 winners out of 3
assert_eq!(trending_perf.total_pnl, Some(rust_decimal::Decimal::from(1250))); // 1000 - 500 + 750
Ok(())
}
#[tokio::test]
#[ignore = "Requires PostgreSQL with migration 045 applied"]
async fn test_database_coverage_by_symbol() -> Result<()> {
let pool = setup_test_db().await?;
let pg_pool = get_pg_pool().await?;
clear_regime_tables(&pg_pool).await?;
let mut manager = RegimePersistenceManager::new(pool);
let symbols = vec!["ES.FUT", "NQ.FUT", "6E.FUT", "ZN.FUT"];
// Populate regime states for all symbols
for (idx, symbol) in symbols.iter().enumerate() {
let timestamp = Utc::now() + chrono::Duration::seconds(idx as i64 * 60);
let features = generate_regime_features(0.5, 3.0, 35.0, 0.8, 3.5);
manager
.process_regime_features(symbol, &features, timestamp)
.await?;
}
// Verify regime coverage by symbol
let coverage = sqlx::query!(
r#"
SELECT
symbol,
COUNT(*) as "count!"
FROM regime_states
GROUP BY symbol
ORDER BY symbol
"#
)
.fetch_all(&pg_pool)
.await?;
assert_eq!(coverage.len(), 4, "Expected 4 symbols");
for (symbol, row) in symbols.iter().zip(coverage.iter()) {
assert_eq!(row.symbol, *symbol);
assert!(row.count > 0);
}
Ok(())
}
#[tokio::test]
#[ignore = "Requires PostgreSQL with migration 045 applied"]
async fn test_latest_adaptive_metrics_query() -> Result<()> {
let pool = setup_test_db().await?;
let pg_pool = get_pg_pool().await?;
clear_regime_tables(&pg_pool).await?;
let mut manager = RegimePersistenceManager::new(pool);
let symbol = "METRICS.LATEST";
// Insert multiple metrics over time
for i in 0..5 {
let timestamp = Utc::now() + chrono::Duration::seconds(i * 60);
let features = generate_regime_features(2.0, 1.0, 30.0, 1.2 + (i as f64 * 0.1), 2.0);
manager
.process_regime_features(symbol, &features, timestamp)
.await?;
}
// Query latest adaptive metrics (Grafana dashboard query)
let latest_metrics = sqlx::query!(
r#"
SELECT
symbol,
regime,
position_multiplier,
stop_loss_multiplier,
event_timestamp
FROM adaptive_strategy_metrics
WHERE symbol = $1
ORDER BY event_timestamp DESC
LIMIT 10
"#,
symbol
)
.fetch_all(&pg_pool)
.await?;
assert!(!latest_metrics.is_empty(), "Expected metrics");
assert!(latest_metrics.len() <= 10, "Query limit not enforced");
// Verify ordering (newest first)
let mut prev_timestamp: Option<DateTime<Utc>> = None;
for metric in &latest_metrics {
if let Some(prev) = prev_timestamp {
assert!(
metric.event_timestamp <= prev,
"Metrics not ordered by timestamp DESC"
);
}
prev_timestamp = Some(metric.event_timestamp);
}
// Verify latest has highest position multiplier
let latest = &latest_metrics[0];
assert_eq!(latest.position_multiplier, 1.6); // 1.2 + 0.4
Ok(())
}
#[tokio::test]
#[ignore = "Requires PostgreSQL with migration 045 applied"]
async fn test_transition_probability_calculation() -> Result<()> {
let pool = setup_test_db().await?;
let pg_pool = get_pg_pool().await?;
clear_regime_tables(&pg_pool).await?;
let mut manager = RegimePersistenceManager::new(pool);
let symbol = "PROB.TEST";
let base_timestamp = Utc::now();
// Create transition pattern: Volatile -> Trending -> Volatile -> Trending
let regime_sequence = vec![
(0.5, 3.0, 35.0), // Volatile
(2.0, 1.0, 30.0), // Trending
(0.5, 3.0, 35.0), // Volatile
(2.0, 1.0, 30.0), // Trending
(0.5, 3.0, 35.0), // Volatile
];
for (i, (cusum_mean, cusum_std, adx)) in regime_sequence.iter().enumerate() {
let timestamp = base_timestamp + chrono::Duration::seconds(i as i64 * 60);
let features = generate_regime_features(*cusum_mean, *cusum_std, *adx, 1.0, 2.0);
manager
.process_regime_features(symbol, &features, timestamp)
.await?;
}
// Verify transition probabilities using SQL function
let transition_matrix = sqlx::query!(
r#"
SELECT
from_regime,
to_regime,
transition_count,
transition_probability
FROM get_regime_transition_matrix($1, 24)
"#,
symbol
)
.fetch_all(&pg_pool)
.await?;
assert!(!transition_matrix.is_empty(), "No transition matrix data!");
// Verify probabilities sum to ~1.0 for each from_regime
use std::collections::HashMap;
let mut prob_sums: HashMap<String, f64> = HashMap::new();
for row in &transition_matrix {
*prob_sums.entry(row.from_regime.clone().unwrap_or_default()).or_insert(0.0) +=
row.transition_probability.unwrap_or(0.0);
}
for (from_regime, sum) in prob_sums {
assert!(
(sum - 1.0).abs() < 0.01,
"Transition probabilities from {} should sum to 1.0, got {}",
from_regime,
sum
);
}
Ok(())
}