changeset 31:c25ab9d90bb2

Present views per session
author Lewin Bormann <lbo@spheniscida.de>
date Fri, 15 Jul 2022 08:53:07 -0700
parents 24f2baa54870
children ebe9dbb5b5ea
files assets/index.html.hbs src/logsdb.rs src/main.rs
diffstat 3 files changed, 87 insertions(+), 4 deletions(-) [+]
line wrap: on
line diff
--- a/assets/index.html.hbs	Fri Jul 15 08:19:13 2022 -0700
+++ b/assets/index.html.hbs	Fri Jul 15 08:53:07 2022 -0700
@@ -55,11 +55,13 @@
     </div>
     <div class="plotrow row2">
         <div class="plotframe halfwidth"><canvas id="topPaths" height="100"></canvas> </div>
-        <div class="plotframe halfwidth"> </div>
+        <div class="plotframe halfwidth"><canvas id="requestsBySession" height="100"></canvas> </div>
     </div>
 
     <script>
-        let plots = {"visitsAndSessions": {{{ chartconfig.visitsAndSessions }}}, "topPaths": {{{ chartconfig.topPaths }}} };
+        let plots = {"visitsAndSessions": {{{ chartconfig.visitsAndSessions }}},
+                     "topPaths": {{{ chartconfig.topPaths }}},
+                     "requestsBySession": {{{ chartconfig.requestsBySession }}} };
 
         Object.keys(plots).forEach((cv) => {
                     if (plots[cv] == undefined) {
--- a/src/logsdb.rs	Fri Jul 15 08:19:13 2022 -0700
+++ b/src/logsdb.rs	Fri Jul 15 08:53:07 2022 -0700
@@ -169,7 +169,70 @@
 
         Ok(result
             .map(|r| r.map(|e| (e.get(0), e.get(1))))
-            .filter(|e| ready(e.is_ok()))
+            .take_while(|e| {
+                if e.is_ok() {
+                    ready(true)
+                } else {
+                    error!("SQL error: query_top_paths: {}", e.as_ref().unwrap_err());
+                    ready(false)
+                }
+            })
+            .map(|e| e.unwrap())
+            .collect()
+            .await)
+    }
+
+    pub async fn query_requests_per_session(
+        &mut self,
+        from: OffsetDateTime,
+        to: OffsetDateTime,
+        tz_offset: Option<i64>,
+        domainpattern: Option<&str>,
+    ) -> Result<Vec<(String, f32)>, Error> {
+        let tz_offset = tz_offset.unwrap_or(0);
+        let result = sqlx::query(
+            r#"
+SELECT d, CAST(nreq AS REAL)/nses
+FROM
+    (SELECT DATE(atime, 'unixepoch') AS d,
+            COUNT(*) AS nreq
+    FROM RequestLog
+    WHERE atime+? > ? AND atime+? < ? AND domain LIKE ?
+    GROUP BY d)
+JOIN
+    (SELECT DATE(start, 'unixepoch') AS d2,
+            COUNT(*) as nses
+     FROM Sessions
+     WHERE start+? > ? AND start+? < ? AND domain LIKE ?
+     GROUP BY d2)
+ON (d = d2)
+ORDER BY d ASC"#,
+        )
+        .bind(tz_offset)
+        .bind(from.unix_timestamp())
+        .bind(tz_offset)
+        .bind(to.unix_timestamp())
+        .bind(domainpattern.unwrap_or("%"))
+        .bind(tz_offset)
+        .bind(from.unix_timestamp())
+        .bind(tz_offset)
+        .bind(to.unix_timestamp())
+        .bind(domainpattern.unwrap_or("%"))
+        .fetch(&mut *self.0);
+
+        Ok(result
+            .map(|r| r.map(|e| (e.get(0), e.get(1))))
+            .take_while(|e| {
+                if e.is_ok() {
+                    ready(true)
+                } else {
+                    error!(
+                        "SQL error: query_requests_per_session: {}",
+                        e.as_ref().unwrap_err()
+                    );
+                    ready(false)
+                }
+            })
             .map(|e| e.unwrap())
             .collect()
             .await)
--- a/src/main.rs	Fri Jul 15 08:19:13 2022 -0700
+++ b/src/main.rs	Fri Jul 15 08:53:07 2022 -0700
@@ -266,6 +266,24 @@
         .to_string(),
         Err(e) => "undefined".to_string(),
     };
+    let reqbyses = match LogsDBSession(&mut conn)
+        .query_requests_per_session(begin, end, Some(tz_offset), domain)
+        .await
+    {
+        Ok(rs) => create_chart(
+            rs.iter().map(|(p, c)| p).collect(),
+            vec![(
+                "Requests per Session".into(),
+                rs.iter().map(|(p, c)| c).collect(),
+            )],
+            &ChartOptions {
+                typ: "line".into(),
+                ..ChartOptions::default()
+            },
+        )
+        .to_string(),
+        Err(e) => "undefined".to_string(),
+    };
     Template::render(
         "index",
         context![
@@ -273,7 +291,7 @@
         domain: domain,
         username: lig.0,
         flash: f,
-        chartconfig: context![ visitsAndSessions: vissess, topPaths: toppaths ]],
+        chartconfig: context![ visitsAndSessions: vissess, topPaths: toppaths, requestsBySession: reqbyses ]],
     )
 }