From dbdf50f66959217360c5694aa25bba39278386ba Mon Sep 17 00:00:00 2001 From: Jerboa-app Date: Sun, 25 Feb 2024 12:06:38 +0000 Subject: [PATCH] stats spawns a thread to compute --- src/web/stats.rs | 106 ++++++++++++++++++++++++++--------------------- 1 file changed, 58 insertions(+), 48 deletions(-) diff --git a/src/web/stats.rs b/src/web/stats.rs index 7dc2396..5699d5e 100644 --- a/src/web/stats.rs +++ b/src/web/stats.rs @@ -25,47 +25,14 @@ pub struct Stats pub last_save: Instant } -use axum:: -{ - http::{Request, StatusCode}, - response::Response, - extract::{State, ConnectInfo}, - middleware::Next -}; - -pub async fn get_ip_info(token: String, sip: String) -> IpDetails -{ - let config = IpInfoConfig { - token: Some(token), - ..Default::default() - }; - - let mut ipinfo = IpInfo::new(config) - .expect("should construct"); - - let res = ipinfo.lookup(&sip).await; - - match res { - Ok(r) => r, - Err(e) => - { - crate::debug(format!("Error getting ip details {}", e), None); - IpDetails { ip: sip, ..Default::default()} - } - } -} - -use crate::config::read_stats_config; -use crate::util::write_file; -pub async fn log_stats -( - ConnectInfo(addr): ConnectInfo, - State(state): State>>, - request: Request, - next: Next -) -> Result +impl Stats { - + pub async fn process_hit + ( + addr: SocketAddr, + state: Arc>, + uri: String + ) { let start_time = Instant::now(); @@ -106,12 +73,10 @@ pub async fn log_stats compute_time ), Some("PERFORMANCE".to_string())); - let response = next.run(request).await; - - return Ok(response) + return } }, - Err(e) => {} + Err(_e) => {} } } } @@ -124,7 +89,7 @@ pub async fn log_stats let last = chrono::offset::Utc::now().to_rfc3339(); - let hit = Hit { details, path: request.uri().to_string(), count, last }; + let hit = Hit { details, path: uri, count, last }; crate::debug(format!("[Hit] {:?}", hit), None); @@ -161,12 +126,57 @@ pub async fn log_stats compute_time, write_time ), Some("PERFORMANCE".to_string())); - } +} +use axum:: +{ + http::{Request, StatusCode}, + response::Response, + extract::{State, ConnectInfo}, + middleware::Next +}; + +pub async fn get_ip_info(token: String, sip: String) -> IpDetails +{ + let config = IpInfoConfig { + token: Some(token), + ..Default::default() + }; + + let mut ipinfo = IpInfo::new(config) + .expect("should construct"); + + let res = ipinfo.lookup(&sip).await; - let response = next.run(request).await; + match res { + Ok(r) => r, + Err(e) => + { + crate::debug(format!("Error getting ip details {}", e), None); + IpDetails { ip: sip, ..Default::default()} + } + } +} - Ok(response) +use crate::config::read_stats_config; +use crate::util::write_file; +pub async fn log_stats +( + ConnectInfo(addr): ConnectInfo, + State(state): State>>, + request: Request, + next: Next +) -> Result +{ + let uri = request.uri().to_string(); + tokio::spawn + (async move + { + Stats::process_hit(addr, state, uri).await + } + ); + + Ok(next.run(request).await) } \ No newline at end of file