Mercurial > lbo > hg > analyrics
changeset 21:64eae1262165
Ignore log requests by logged-in users
author | Lewin Bormann <lbo@spheniscida.de> |
---|---|
date | Mon, 11 Jul 2022 20:34:11 -0700 |
parents | c6f15ffef3d3 |
children | af328a6c7d9f |
files | src/main.rs |
diffstat | 1 files changed, 53 insertions(+), 50 deletions(-) [+] |
line wrap: on
line diff
--- a/src/main.rs Mon Jul 11 20:25:46 2022 -0700 +++ b/src/main.rs Mon Jul 11 20:34:11 2022 -0700 @@ -419,63 +419,66 @@ ) -> (Status, Either<NamedFile, &'static str>) { let mut conn = LogsDBSession(&mut conn); let mut session_id = None; - // Get session ID from cookie, or start new session. - if let Some(sessioncookie) = cookies.get_private("analyrics_session") { - if let Ok(id) = u32::from_str_radix(sessioncookie.value(), 10) { - session_id = Some(id); - match conn.update_session_time(id).await { - Ok(()) => {} - Err(e) => error!("Couldn't update session time: {}", e), + // Only log if this is not an analytics user. + if cookies.get_private(USER_ID_COOKIE_KEY).is_none() { + // Get session ID from cookie, or start new session. + if let Some(sessioncookie) = cookies.get_private("analyrics_session") { + if let Ok(id) = u32::from_str_radix(sessioncookie.value(), 10) { + session_id = Some(id); + match conn.update_session_time(id).await { + Ok(()) => {} + Err(e) => error!("Couldn't update session time: {}", e), + } + } + } + if session_id.is_none() { + match conn + .start_session(host.as_ref().map(String::as_str).unwrap_or("")) + .await + { + Ok(id) => { + session_id = Some(id); + let c = Cookie::build("analyrics_session", id.to_string()) + .max_age(time::Duration::hours(12)) + .finish(); + cookies.add_private(c); + } + Err(e) => error!("Couldn't start session: {}", e), } } - } - if session_id.is_none() { + + let ntags = tags.len() as u32; + let ua = headers.0.get_one("user-agent").unwrap_or(""); + let ip = ip.to_string(); + let ip = headers.0.get_one("x-real-ip").unwrap_or(&ip); + let host: String = host.unwrap_or( + headers + .0 + .get("host") + .take(1) + .map(|s| s.to_string()) + .collect::<Vec<String>>() + .pop() + .unwrap_or(String::new()), + ); match conn - .start_session(host.as_ref().map(String::as_str).unwrap_or("")) + .log_request( + session_id, + ip, + host, + path.unwrap_or(String::new()), + status.unwrap_or(200), + pagename, + referer, + ua, + ntags, + ) .await { + Err(e) => error!("Couldn't log request: {}", e), Ok(id) => { - session_id = Some(id); - let c = Cookie::build("analyrics_session", id.to_string()) - .max_age(time::Duration::hours(12)) - .finish(); - cookies.add_private(c); + conn.log_tags(id, tags.iter()).await.ok(); } - Err(e) => error!("Couldn't start session: {}", e), - } - } - - let ntags = tags.len() as u32; - let ua = headers.0.get_one("user-agent").unwrap_or(""); - let ip = ip.to_string(); - let ip = headers.0.get_one("x-real-ip").unwrap_or(&ip); - let host: String = host.unwrap_or( - headers - .0 - .get("host") - .take(1) - .map(|s| s.to_string()) - .collect::<Vec<String>>() - .pop() - .unwrap_or(String::new()), - ); - match conn - .log_request( - session_id, - ip, - host, - path.unwrap_or(String::new()), - status.unwrap_or(200), - pagename, - referer, - ua, - ntags, - ) - .await - { - Err(e) => error!("Couldn't log request: {}", e), - Ok(id) => { - conn.log_tags(id, tags.iter()).await.ok(); } }