|
| 1 | +// SPDX-License-Identifier: PMPL-1.0-or-later |
| 2 | +// GNN model training integration with health monitoring |
| 3 | + |
| 4 | +use serde::{Deserialize, Serialize}; |
| 5 | +use std::fs; |
| 6 | +use std::path::Path; |
| 7 | +use chrono::{DateTime, Utc}; |
| 8 | + |
| 9 | +/// Metrics exported from GNN training pipeline |
| 10 | +#[derive(Debug, Clone, Serialize, Deserialize)] |
| 11 | +pub struct GnnTrainingMetrics { |
| 12 | + pub timestamp: String, |
| 13 | + pub training_data_size: usize, |
| 14 | + pub epochs_trained: usize, |
| 15 | + pub nDCG: f32, |
| 16 | + pub MRR: f32, |
| 17 | + pub model_location: String, |
| 18 | + pub status: String, |
| 19 | +} |
| 20 | + |
| 21 | +/// Load GNN training metrics from JSON file |
| 22 | +/// Returns None if file doesn't exist or can't be parsed |
| 23 | +pub fn load_training_metrics(metrics_path: &Path) -> Option<GnnTrainingMetrics> { |
| 24 | + match fs::read_to_string(metrics_path) { |
| 25 | + Ok(json_content) => match serde_json::from_str::<GnnTrainingMetrics>(&json_content) { |
| 26 | + Ok(metrics) => Some(metrics), |
| 27 | + Err(e) => { |
| 28 | + eprintln!("Failed to parse training metrics: {}", e); |
| 29 | + None |
| 30 | + } |
| 31 | + }, |
| 32 | + Err(_) => None, |
| 33 | + } |
| 34 | +} |
| 35 | + |
| 36 | +/// Update HealthStatus with GNN training results |
| 37 | +/// Called after training completes to populate last_validation_nDCG and related fields |
| 38 | +pub fn update_health_with_metrics( |
| 39 | + health: &mut crate::diagnostics::HealthStatus, |
| 40 | + metrics: &GnnTrainingMetrics, |
| 41 | +) { |
| 42 | + health.gnn_model_health.is_loaded = true; |
| 43 | + health.gnn_model_health.last_validation_nDCG = metrics.nDCG; |
| 44 | + health.gnn_model_health.last_validation_MRR = metrics.MRR; |
| 45 | + |
| 46 | + // Mark as meeting threshold if nDCG >= 0.65 |
| 47 | + health.gnn_model_health.nDCG_meets_threshold = metrics.nDCG >= 0.65; |
| 48 | + |
| 49 | + // Update last_trained timestamp |
| 50 | + if let Ok(ts) = DateTime::parse_from_rfc3339(&metrics.timestamp) { |
| 51 | + health.gnn_model_health.last_trained = Some(ts.with_timezone(&Utc)); |
| 52 | + } |
| 53 | + |
| 54 | + // Recompute degradation with updated GNN metrics |
| 55 | + health.compute_degradation_mode(); |
| 56 | +} |
| 57 | + |
| 58 | +#[cfg(test)] |
| 59 | +mod tests { |
| 60 | + use super::*; |
| 61 | + |
| 62 | + #[test] |
| 63 | + fn test_gnn_training_metrics_structure() { |
| 64 | + // Verify that GnnTrainingMetrics can be serialized/deserialized |
| 65 | + let metrics = GnnTrainingMetrics { |
| 66 | + timestamp: "2026-04-25T12:00:00Z".to_string(), |
| 67 | + training_data_size: 1000, |
| 68 | + epochs_trained: 50, |
| 69 | + nDCG: 0.75, |
| 70 | + MRR: 0.68, |
| 71 | + model_location: "models/neural/gnn_ranker".to_string(), |
| 72 | + status: "completed".to_string(), |
| 73 | + }; |
| 74 | + |
| 75 | + // Convert to JSON and back |
| 76 | + let json = serde_json::to_string(&metrics).unwrap(); |
| 77 | + let parsed: GnnTrainingMetrics = serde_json::from_str(&json).unwrap(); |
| 78 | + |
| 79 | + assert_eq!(parsed.nDCG, 0.75); |
| 80 | + assert_eq!(parsed.MRR, 0.68); |
| 81 | + assert_eq!(parsed.training_data_size, 1000); |
| 82 | + } |
| 83 | + |
| 84 | + #[test] |
| 85 | + fn test_update_health_with_metrics() { |
| 86 | + let mut health = crate::diagnostics::HealthStatus::new(); |
| 87 | + |
| 88 | + // Initially GNN is not loaded |
| 89 | + assert!(!health.gnn_model_health.is_loaded); |
| 90 | + assert_eq!(health.gnn_model_health.last_validation_nDCG, 0.0); |
| 91 | + |
| 92 | + let metrics = GnnTrainingMetrics { |
| 93 | + timestamp: "2026-04-25T12:00:00Z".to_string(), |
| 94 | + training_data_size: 1000, |
| 95 | + epochs_trained: 50, |
| 96 | + nDCG: 0.78, |
| 97 | + MRR: 0.72, |
| 98 | + model_location: "models/neural/gnn_ranker".to_string(), |
| 99 | + status: "completed".to_string(), |
| 100 | + }; |
| 101 | + |
| 102 | + update_health_with_metrics(&mut health, &metrics); |
| 103 | + |
| 104 | + // After update, GNN should be loaded with metrics |
| 105 | + assert!(health.gnn_model_health.is_loaded); |
| 106 | + assert_eq!(health.gnn_model_health.last_validation_nDCG, 0.78); |
| 107 | + assert_eq!(health.gnn_model_health.last_validation_MRR, 0.72); |
| 108 | + assert!(health.gnn_model_health.nDCG_meets_threshold); |
| 109 | + } |
| 110 | +} |
0 commit comments