use nym_sphinx::DestinationAddressBytes;
use time::OffsetDateTime;
use crate::types::SessionType;
pub type GatewayStatsReceiver = tokio::sync::mpsc::UnboundedReceiver<GatewayStatsEvent>;
#[derive(Clone)]
pub struct GatewayStatsReporter {
stats_tx: tokio::sync::mpsc::UnboundedSender<GatewayStatsEvent>,
}
impl GatewayStatsReporter {
pub fn new(stats_tx: tokio::sync::mpsc::UnboundedSender<GatewayStatsEvent>) -> Self {
Self { stats_tx }
}
pub fn report(&self, event: GatewayStatsEvent) {
self.stats_tx.send(event).unwrap_or_else(|err| {
log::error!("Failed to report gateway stat event : {err}");
});
}
}
pub enum GatewayStatsEvent {
SessionStatsEvent(GatewaySessionEvent),
}
#[derive(Debug, Clone, Copy)]
pub enum GatewaySessionEvent {
SessionStart {
start_time: OffsetDateTime,
client: DestinationAddressBytes,
},
SessionStop {
stop_time: OffsetDateTime,
client: DestinationAddressBytes,
},
SessionRemember {
session_type: SessionType,
client: DestinationAddressBytes,
},
}
impl GatewaySessionEvent {
pub fn new_session_start(client: DestinationAddressBytes) -> GatewaySessionEvent {
GatewaySessionEvent::SessionStart {
start_time: OffsetDateTime::now_utc(),
client,
}
}
pub fn new_session_stop(client: DestinationAddressBytes) -> GatewaySessionEvent {
GatewaySessionEvent::SessionStop {
stop_time: OffsetDateTime::now_utc(),
client,
}
}
pub fn new_session_remember(
session_type: SessionType,
client: DestinationAddressBytes,
) -> GatewaySessionEvent {
GatewaySessionEvent::SessionRemember {
session_type,
client,
}
}
}