use dashmap::DashMap;
use std::sync::atomic::{AtomicU64, Ordering};
use std::sync::Arc;
use std::time::{Duration, Instant};
#[derive(Debug, Default)]
pub struct RouterMetrics {
pub(crate) total_requests: AtomicU64,
pub(crate) successful_requests: AtomicU64,
pub(crate) failed_requests: AtomicU64,
pub(crate) candidate_count_total: AtomicU64,
pub(crate) attempt_count_total: AtomicU64,
pub(crate) engine_selection_total: AtomicU64,
pub(crate) engine_latencies: Arc<DashMap<String, AtomicU64>>,
pub(crate) engine_success_count: Arc<DashMap<String, AtomicU64>>,
pub(crate) engine_failure_count: Arc<DashMap<String, AtomicU64>>,
pub(crate) failure_classification: Arc<DashMap<String, u64>>,
}
impl RouterMetrics {
pub fn new() -> Self {
Self {
total_requests: AtomicU64::new(0),
successful_requests: AtomicU64::new(0),
failed_requests: AtomicU64::new(0),
candidate_count_total: AtomicU64::new(0),
attempt_count_total: AtomicU64::new(0),
engine_selection_total: AtomicU64::new(0),
engine_latencies: Arc::new(DashMap::with_capacity(8)),
engine_success_count: Arc::new(DashMap::with_capacity(8)),
engine_failure_count: Arc::new(DashMap::with_capacity(8)),
failure_classification: Arc::new(DashMap::with_capacity(8)),
}
}
fn latencies(&self) -> &DashMap<String, AtomicU64> {
&self.engine_latencies
}
fn success_count(&self) -> &DashMap<String, AtomicU64> {
&self.engine_success_count
}
fn failure_count(&self) -> &DashMap<String, AtomicU64> {
&self.engine_failure_count
}
fn classification(&self) -> &DashMap<String, u64> {
&self.failure_classification
}
pub(crate) fn classify_error(error_type: &str) -> String {
let lower = error_type.to_lowercase();
if lower.contains("timeout") {
"timeout".to_string()
} else if lower.contains("ssrf") {
"ssrf_protection".to_string()
} else if lower.contains("network") {
"network_error".to_string()
} else if lower.contains("circuit") {
"circuit_breaker".to_string()
} else if lower.contains("browser") {
"browser_error".to_string()
} else {
"other".to_string()
}
}
pub fn record_candidates(&self, count: usize) {
self.candidate_count_total
.fetch_add(count as u64, Ordering::Relaxed);
}
pub fn record_attempt(&self) {
self.attempt_count_total.fetch_add(1, Ordering::Relaxed);
}
pub fn record_engine_selection(&self, _engine_name: &str) {
self.engine_selection_total.fetch_add(1, Ordering::Relaxed);
}
pub fn record_engine_latency(&self, engine_name: &str, duration: Duration) {
let total_ns = duration.as_nanos() as u64;
self.latencies()
.entry(engine_name.to_string())
.or_insert_with(|| AtomicU64::new(0))
.fetch_add(total_ns, Ordering::Relaxed);
}
pub fn record_engine_success(&self, engine_name: &str) {
self.success_count()
.entry(engine_name.to_string())
.or_insert_with(|| AtomicU64::new(0))
.fetch_add(1, Ordering::Relaxed);
}
pub fn record_engine_failure(&self, engine_name: &str, error_type: &str) {
self.failure_count()
.entry(engine_name.to_string())
.or_insert_with(|| AtomicU64::new(0))
.fetch_add(1, Ordering::Relaxed);
let error_category = Self::classify_error(error_type);
self.classification()
.entry(error_category)
.and_modify(|c| *c += 1)
.or_insert(1);
}
pub fn get_avg_latency_ns(&self, engine_name: &str) -> Option<u64> {
let latencies = self.latencies();
let success_count = self.success_count();
if let (Some(total_ns_ref), Some(count_ref)) =
(latencies.get(engine_name), success_count.get(engine_name))
{
let total_ns = total_ns_ref.load(Ordering::Relaxed);
let count = count_ref.load(Ordering::Relaxed);
return total_ns.checked_div(count);
}
None
}
pub fn get_success_rate(&self) -> f64 {
let total = self.total_requests.load(Ordering::Relaxed);
if total == 0 {
return 1.0;
}
self.successful_requests.load(Ordering::Relaxed) as f64 / total as f64
}
}
#[derive(Debug, Clone)]
pub struct EngineStats {
pub success_rate: f64,
pub avg_response_time: Duration,
pub last_used: Option<Instant>,
pub usage_count: u64,
}
impl Default for EngineStats {
fn default() -> Self {
Self {
success_rate: 1.0,
avg_response_time: Duration::from_millis(500),
last_used: None,
usage_count: 0,
}
}
}