]> git.ipfire.org Git - ipfire.org.git/blobdiff - src/backend/analytics.py
analytics: Show total page views/page views in the last 24h for blog/docs
[ipfire.org.git] / src / backend / analytics.py
index cfecbd78420a34fa4ffeed353ef70936dfea416c..9172adade1ce7d71c5bd3caf29be2b2327477f4f 100644 (file)
@@ -88,3 +88,40 @@ class Analytics(misc.Object):
                        user_agent, q, bot, source or "", medium or "", campaign or "", content or "",
                        term or "",
                )
+
+       def get_total_page_views(self, host, uri, since=None):
+               # Make since an absolute timestamp
+               if since and isinstance(since, datetime.timedelta):
+                       since = datetime.datetime.utcnow() - since
+
+               if since:
+                       res = self.db.get("""
+                               SELECT
+                                       COUNT(*) AS c
+                               FROM
+                                       analytics_unique_visits
+                               WHERE
+                                       host = %s
+                               AND
+                                       uri = %s
+                               AND
+                                       created_at >= %s
+                               """, host, uri, since,
+                       )
+               else:
+                       res = self.db.get("""
+                               SELECT
+                                       COUNT(*) AS c
+                               FROM
+                                       analytics_unique_visits
+                               WHERE
+                                       host = %s
+                               AND
+                                       uri = %s
+                               """, host, uri,
+                       )
+
+               if res and res.c:
+                       return res.c
+
+               return 0