use actix_web::{web, HttpResponse}; use std::sync::Arc; use tokio::sync::Mutex; use serde::Deserialize; use crate::db::Database; #[derive(Deserialize)] pub struct MetricsQuery { start_block: Option, end_block: Option, } pub async fn get_metrics( db: web::Data>>, path: web::Path, query: web::Query, ) -> HttpResponse { let process_id = path.into_inner(); let db = db.lock().await; let start = query.start_block.unwrap_or(0); let end = query.end_block.unwrap_or(i32::MAX); match db.get_metrics_for_period(&process_id, start, end).await { Ok(metrics) => { let total_sent: i64 = metrics.iter().map(|m| m.bytes_sent).sum(); let total_received: i64 = metrics.iter().map(|m| m.bytes_received).sum(); let total_messages: i32 = metrics.iter().map(|m| m.message_count).sum(); HttpResponse::Ok().json(serde_json::json!({ "process_id": process_id, "period": { "start_block": start, "end_block": end }, "metrics": { "total_bytes_sent": total_sent, "total_bytes_received": total_received, "total_mb": (total_sent + total_received) / 1_048_576, "message_count": total_messages }, "details": metrics })) }, Err(e) => HttpResponse::InternalServerError().json(serde_json::json!({ "error": format!("Failed to fetch metrics: {}", e) })) } }