changeset 48:616a145a1bb3

Honor includebots in queries
author Lewin Bormann <lbo@spheniscida.de>
date Tue, 19 Jul 2022 22:05:20 -0700
parents c2436a591e56
children 3b266ff9308e
files src/logsdb.rs src/main.rs
diffstat 2 files changed, 24 insertions(+), 7 deletions(-) [+]
line wrap: on
line diff
--- a/src/logsdb.rs	Tue Jul 19 21:42:01 2022 -0700
+++ b/src/logsdb.rs	Tue Jul 19 22:05:20 2022 -0700
@@ -20,6 +20,7 @@
     pub from: OffsetDateTime,
     pub to: OffsetDateTime,
     pub tz_offset: i64,
+    pub include_bots: bool,
 }
 
 impl<'p> LogsDBSession<'p, Sqlite> {
@@ -116,26 +117,30 @@
     ) -> Result<(Vec<String>, Vec<u32>, Vec<u32>), Error> {
         let domain = ctx.domain.as_ref().map(String::as_str).unwrap_or("%");
         let tz_offset = ctx.tz_offset;
+        let include_bots = if ctx.include_bots { 1 } else { 0 };
         let mut results = sqlx::query(
             r#"
 SELECT DATE(atime + ?, 'unixepoch') AS rqdate, COUNT(requestlog.id) AS rqcount, sesscount
 FROM requestlog
 JOIN (
     SELECT DATE(start + ?, 'unixepoch') AS sessdate, COUNT(*) AS sesscount
-    FROM sessions WHERE sessions.domain LIKE ? GROUP BY sessdate)
+    FROM sessions WHERE sessions.domain LIKE ? AND sessions.is_bot <= ? GROUP BY sessdate)
 AS sc ON (rqdate = sessdate)
-WHERE atime+? > ? AND atime+? < ? AND requestlog.domain LIKE ?
+JOIN sessions ON (sessions.id = requestlog.session)
+WHERE atime+? > ? AND atime+? < ? AND requestlog.domain LIKE ? AND sessions.is_bot <= ?
 GROUP BY rqdate
 ORDER BY rqdate ASC;"#,
         )
         .bind(tz_offset)
         .bind(tz_offset)
         .bind(domain)
+        .bind(include_bots)
         .bind(tz_offset)
         .bind(ctx.from.unix_timestamp())
         .bind(tz_offset)
         .bind(ctx.to.unix_timestamp())
         .bind(domain)
+        .bind(include_bots)
         .fetch(&mut *self.0);
 
         // Result table: date / visit count / session count
@@ -166,11 +171,12 @@
         ctx: &LogsQueryContext,
     ) -> Result<Vec<(String, i64)>, Error> {
         let tz_offset = ctx.tz_offset;
+        let include_bots = if ctx.include_bots { 1 } else { 0 };
         let result = sqlx::query(
             r#"
 SELECT origin_country, COUNT(*) AS count
 FROM sessions
-WHERE start+? > ? AND start+? < ? AND domain LIKE ?
+WHERE start+? > ? AND start+? < ? AND domain LIKE ? AND is_bot <= ?
 GROUP BY origin_country
 ORDER BY count DESC;"#,
         )
@@ -179,6 +185,7 @@
         .bind(tz_offset)
         .bind(ctx.to.unix_timestamp())
         .bind(ctx.domain.as_ref().map(String::as_str).unwrap_or("%"))
+        .bind(include_bots)
         .fetch_all(&mut *self.0)
         .await?;
 
@@ -194,11 +201,13 @@
         n: i64,
     ) -> Result<Vec<(String, i64)>, Error> {
         let tz_offset = ctx.tz_offset;
+        let include_bots = if ctx.include_bots { 1 } else { 0 };
         let result = sqlx::query(
             r#"
 SELECT path, COUNT(*) AS pathcount
 FROM RequestLog
-WHERE atime+? > ? AND atime+? < ? AND domain LIKE ?
+JOIN Sessions ON (Sessions.id = Requestlog.session)
+WHERE atime+? > ? AND atime+? < ? AND RequestLog.domain LIKE ? AND Sessions.is_bot <= ?
 GROUP BY path
 ORDER BY pathcount DESC
 LIMIT ?;"#,
@@ -208,6 +217,7 @@
         .bind(tz_offset)
         .bind(ctx.to.unix_timestamp())
         .bind(ctx.domain.as_ref().map(String::as_str).unwrap_or("%"))
+        .bind(include_bots)
         .bind(n)
         .fetch(&mut *self.0);
 
@@ -231,6 +241,7 @@
         ctx: &LogsQueryContext,
     ) -> Result<Vec<(String, f32)>, Error> {
         let tz_offset = ctx.tz_offset;
+        let include_bots = if ctx.include_bots { 1 } else { 0 };
         let result = sqlx::query(
             r#"
 SELECT d, CAST(nreq AS REAL)/nses
@@ -238,13 +249,14 @@
     (SELECT DATE(atime, 'unixepoch') AS d,
             COUNT(*) AS nreq
     FROM RequestLog
-    WHERE atime+? > ? AND atime+? < ? AND domain LIKE ?
+    JOIN Sessions ON (Sessions.id = RequestLog.session)
+    WHERE atime+? > ? AND atime+? < ? AND RequestLog.domain LIKE ? AND is_bot <= ?
     GROUP BY d)
 JOIN
     (SELECT DATE(start, 'unixepoch') AS d2,
             COUNT(*) as nses
      FROM Sessions
-     WHERE start+? > ? AND start+? < ? AND domain LIKE ?
+     WHERE start+? > ? AND start+? < ? AND Sessions.domain LIKE ? AND is_bot <= ?
      GROUP BY d2)
 ON (d = d2)
 ORDER BY d ASC"#,
@@ -254,11 +266,13 @@
         .bind(tz_offset)
         .bind(ctx.to.unix_timestamp())
         .bind(ctx.domain.as_ref().map(String::as_str).unwrap_or("%"))
+        .bind(include_bots)
         .bind(tz_offset)
         .bind(ctx.from.unix_timestamp())
         .bind(tz_offset)
         .bind(ctx.to.unix_timestamp())
         .bind(ctx.domain.as_ref().map(String::as_str).unwrap_or("%"))
+        .bind(include_bots)
         .fetch(&mut *self.0);
 
         Ok(result
--- a/src/main.rs	Tue Jul 19 21:42:01 2022 -0700
+++ b/src/main.rs	Tue Jul 19 22:05:20 2022 -0700
@@ -188,7 +188,7 @@
 }
 
 /// Main analytics page for logged-in users.
-#[rocket::get("/?<domain>&<from>&<duration>", rank = 1)]
+#[rocket::get("/?<domain>&<from>&<duration>&<includebots>", rank = 1)]
 async fn route_index_loggedin(
     mut conn: Connection<LogsDB>,
     mut config_conn: Connection<ConfigDB>,
@@ -197,6 +197,7 @@
     domain: Option<String>,
     from: Option<&str>,
     duration: Option<&str>,
+    includebots: Option<bool>,
 ) -> Template {
     let f;
     if let Some(ref flash) = flash {
@@ -236,12 +237,14 @@
             0
         }
     };
+    let includebots = includebots.unwrap_or(false);
 
     let ctx = logsdb::LogsQueryContext {
         domain: domain.clone(),
         from: begin,
         to: end,
         tz_offset,
+        include_bots: includebots,
     };
 
     let vissess = match LogsDBSession(&mut conn)