use std::sync::Arc;
use warp::{Filter, Rejection, Reply};
use serde_json::json;
use crate::monitoring::blockchain_metrics;
use crate::monitoring::blockchain_alerts;
use crate::monitoring::metrics;
pub fn create_routes() -> impl Filter<Extract = impl Reply, Error = Rejection> + Clone {
let metrics_route = warp::path!("metrics")
.and(warp::get())
.and_then(|| async { Ok::<_, warp::Rejection>(handle_metrics().await) });
let prometheus_route = warp::path!("metrics" / "prometheus")
.and(warp::get())
.and_then(|| async { Ok::<_, warp::Rejection>(handle_prometheus().await) });
let blockchain_metrics_route = warp::path!("metrics" / "blockchain")
.and(warp::get())
.and_then(|| async { Ok::<_, warp::Rejection>(handle_blockchain_metrics().await) });
let blockchain_historical_route = warp::path!("metrics" / "blockchain" / "historical" / String)
.and(warp::get())
.and_then(|id: String| async move { Ok::<_, warp::Rejection>(handle_blockchain_historical(id).await) });
let alerts_route = warp::path!("metrics" / "alerts")
.and(warp::get())
.and_then(|| async { Ok::<_, warp::Rejection>(handle_alerts().await) });
let alert_history_route = warp::path!("metrics" / "alerts" / "history")
.and(warp::get())
.and_then(|| async { Ok::<_, warp::Rejection>(handle_alert_history().await) });
let acknowledge_alert_route = warp::path!("metrics" / "alerts" / "acknowledge" / String)
.and(warp::post())
.and_then(|id: String| async move { Ok::<_, warp::Rejection>(handle_acknowledge_alert(id).await) });
metrics_route
.or(prometheus_route)
.or(blockchain_metrics_route)
.or(blockchain_historical_route)
.or(alerts_route)
.or(alert_history_route)
.or(acknowledge_alert_route)
}
async fn handle_metrics() -> impl Reply {
let blockchain = blockchain_metrics::get_metrics_json();
warp::reply::json(&json!({
"status": "success",
"data": {
"blockchain": blockchain
}
}))
}
async fn handle_prometheus() -> impl Reply {
let metrics_text = metrics::export_metrics();
warp::reply::with_header(
metrics_text,
"content-type",
"text/plain; version=0.0.4",
)
}
async fn handle_blockchain_metrics() -> impl Reply {
let blockchain = blockchain_metrics::get_metrics_json();
warp::reply::json(&json!({
"status": "success",
"data": blockchain
}))
}
async fn handle_blockchain_historical(metric_name: String) -> impl Reply {
let historical_data = blockchain_metrics::get_historical_data(&metric_name);
match historical_data {
Some(data) => {
warp::reply::json(&json!({
"status": "success",
"metric": metric_name,
"data": data
}))
}
None => {
warp::reply::json(&json!({
"status": "error",
"message": format!("Historical data not available for metric: {}", metric_name)
}))
}
}
}
async fn handle_alerts() -> impl Reply {
let active_alerts = blockchain_alerts::get_active_alerts();
warp::reply::json(&json!({
"status": "success",
"count": active_alerts.len(),
"data": active_alerts
}))
}
async fn handle_alert_history() -> impl Reply {
let history = blockchain_alerts::get_alert_history();
warp::reply::json(&json!({
"status": "success",
"count": history.len(),
"data": history
}))
}
async fn handle_acknowledge_alert(alert_id: String) -> impl Reply {
blockchain_alerts::acknowledge_alert(&alert_id);
warp::reply::json(&json!({
"status": "success",
"message": format!("Alert {} acknowledged", alert_id)
}))
}