Skip to content

Commit

Permalink
stats spawns a thread to compute
Browse files Browse the repository at this point in the history
  • Loading branch information
Jerboa-app committed Feb 25, 2024
1 parent 8da8964 commit dbdf50f
Showing 1 changed file with 58 additions and 48 deletions.
106 changes: 58 additions & 48 deletions src/web/stats.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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<B>
(
ConnectInfo(addr): ConnectInfo<SocketAddr>,
State(state): State<Arc<Mutex<Stats>>>,
request: Request<B>,
next: Next<B>
) -> Result<Response, StatusCode>
impl Stats
{

pub async fn process_hit
(
addr: SocketAddr,
state: Arc<Mutex<Stats>>,
uri: String
)
{
let start_time = Instant::now();

Expand Down Expand Up @@ -106,12 +73,10 @@ pub async fn log_stats<B>
compute_time
), Some("PERFORMANCE".to_string()));

let response = next.run(request).await;

return Ok(response)
return
}
},
Err(e) => {}
Err(_e) => {}
}
}
}
Expand All @@ -124,7 +89,7 @@ pub async fn log_stats<B>

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);

Expand Down Expand Up @@ -161,12 +126,57 @@ pub async fn log_stats<B>
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<B>
(
ConnectInfo(addr): ConnectInfo<SocketAddr>,
State(state): State<Arc<Mutex<Stats>>>,
request: Request<B>,
next: Next<B>
) -> Result<Response, StatusCode>
{

let uri = request.uri().to_string();
tokio::spawn
(async move
{
Stats::process_hit(addr, state, uri).await
}
);

Ok(next.run(request).await)
}

0 comments on commit dbdf50f

Please sign in to comment.