this_env/env/methods/
get_requests_logs.rs1use rusqlite::{Connection, Result as SqlResult};
7use crate::env::structs::EnvRequestLog;
8
9pub fn get_request_logs(conn: &Connection, domain: Option<&str>, count: Option<usize>, offset: Option<usize>) -> SqlResult<Vec<EnvRequestLog>> {
10 let query = match (domain, count) {
11 (Some(_), Some(_)) => "SELECT method, path, host, timestamp, ip, headers, decision, reason, domain FROM env_request_logs WHERE domain = ? ORDER BY timestamp DESC LIMIT ? OFFSET ?",
12 (Some(_), None) => "SELECT method, path, host, timestamp, ip, headers, decision, reason, domain FROM env_request_logs WHERE domain = ? ORDER BY timestamp DESC",
13 (None, Some(_)) => "SELECT method, path, host, timestamp, ip, headers, decision, reason, domain FROM env_request_logs ORDER BY timestamp DESC LIMIT ? OFFSET ?",
14 (None, None) => "SELECT method, path, host, timestamp, ip, headers, decision, reason, domain FROM env_request_logs ORDER BY timestamp DESC",
15 };
16
17 log::debug!("Fetching request logs with domain filter: {:?}, count: {:?}, offset: {:?}", domain, count, offset);
18 let mut stmt = conn.prepare(query)?;
19
20 let map_row = |row: &rusqlite::Row| -> rusqlite::Result<EnvRequestLog> {
21 Ok(EnvRequestLog {
22 method: row.get(0)?,
23 path: row.get(1)?,
24 host: row.get(2)?,
25 timestamp: row.get(3)?,
26 ip: row.get(4)?,
27 headers: row.get(5)?,
28 decision: row.get(6)?,
29 reason: row.get(7)?,
30 domain: row.get(8)?,
31 })
32 };
33
34 let rows = match (domain, count) {
35 (Some(d), Some(c)) => stmt.query_map(rusqlite::params![d, c as i64, offset.unwrap_or(0) as i64], map_row)?,
36 (Some(d), None) => stmt.query_map([d], map_row)?,
37 (None, Some(c)) => stmt.query_map(rusqlite::params![c as i64, offset.unwrap_or(0) as i64], map_row)?,
38 (None, None) => stmt.query_map([], map_row)?,
39 };
40
41 let mut logs = Vec::new();
42 for log in rows {
43 logs.push(log?);
44 }
45 log::debug!("Fetched {} total logs", logs.len());
46 Ok(logs)
47}