From: Michael Tremer Date: Sun, 7 Jan 2024 18:25:12 +0000 (+0000) Subject: analytics: Show total page views/last 24 hours X-Git-Url: http://git.ipfire.org/?a=commitdiff_plain;h=ee3fe069861157ef7b653dfcca72997faf4a24bf;p=ipfire.org.git analytics: Show total page views/last 24 hours Signed-off-by: Michael Tremer --- diff --git a/src/backend/analytics.py b/src/backend/analytics.py index 9172adad..ac350130 100644 --- a/src/backend/analytics.py +++ b/src/backend/analytics.py @@ -89,7 +89,40 @@ class Analytics(misc.Object): term or "", ) - def get_total_page_views(self, host, uri, since=None): + def get_total_page_views(self, host, 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 + created_at >= %s + """, host, since, + ) + else: + res = self.db.get(""" + SELECT + COUNT(*) AS c + FROM + analytics_unique_visits + WHERE + host = %s + """, host, + ) + + if res and res.c: + return res.c + + return 0 + + def get_page_views(self, host, uri, since=None): # Make since an absolute timestamp if since and isinstance(since, datetime.timedelta): since = datetime.datetime.utcnow() - since diff --git a/src/templates/analytics/index.html b/src/templates/analytics/index.html index 73b54377..8cd60afb 100644 --- a/src/templates/analytics/index.html +++ b/src/templates/analytics/index.html @@ -3,6 +3,8 @@ {% block title %}{{ _("Analytics") }}{% end block %} {% block container %} + {% import datetime %} +
@@ -21,4 +23,33 @@
+ + {# Fetch some data #} + {% set total_page_views = backend.analytics.get_total_page_views(request.host) %} + {% set total_page_views_24h = backend.analytics.get_total_page_views(request.host, + since=datetime.timedelta(hours=24)) %} + +
+
+
+
+
+

{{ _("Total Page Views") }}

+

+ {{ total_page_views }} +

+
+
+ +
+
+

{{ _("Total Page Views (Last 24h)") }}

+

+ {{ total_page_views_24h }} +

+
+
+
+
+
{% end block %} diff --git a/src/web/analytics.py b/src/web/analytics.py index a5f13704..ab8179e5 100644 --- a/src/web/analytics.py +++ b/src/web/analytics.py @@ -21,11 +21,11 @@ class SummaryModule(ui_modules.UIModule): uri = self.request.path # Fetch the total number of page views - total_page_views = self.backend.analytics.get_total_page_views(host, uri) + total_page_views = self.backend.analytics.get_page_views(host, uri) # Fetch the total number of page views in the last 24h - total_page_views_24h = self.backend.analytics.get_total_page_views(host, uri, + total_page_views_24h = self.backend.analytics.get_page_views(host, uri, since=datetime.timedelta(hours=24)) - return self.render_string("analytics/modules/summary.html", - host=host, uri=uri, total_page_views=total_page_views, total_page_views_24h=total_page_views_24h) + return self.render_string("analytics/modules/summary.html", host=host, uri=uri, + total_page_views=total_page_views, total_page_views_24h=total_page_views_24h)