4NK_certificator/src/api/metrics.rs
4NK Dev 4acfa96a5c feat: initial commit - 4NK Certificator service
- Bitcoin mainnet anchoring service for 4NK relay data volumes
- REST API for processes, metrics, and anchors
- PostgreSQL database with SQLx
- Docker support with compose file
- Configuration via TOML
- Periodic anchoring based on block intervals
- Conditional payment system (priceMoSats, btcAddress)
- Process state snapshot with cryptographic hash
- Health check endpoint

Version: 0.1.0 (MVP)
2025-10-01 09:35:33 +00:00

51 lines
1.6 KiB
Rust

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<i32>,
end_block: Option<i32>,
}
pub async fn get_metrics(
db: web::Data<Arc<Mutex<Database>>>,
path: web::Path<String>,
query: web::Query<MetricsQuery>,
) -> 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)
}))
}
}