From: Michael Tremer Date: Sun, 7 Jan 2024 15:11:05 +0000 (+0000) Subject: analytics: Show total page views/page views in the last 24h for blog/docs X-Git-Url: http://git.ipfire.org/?a=commitdiff_plain;h=672be31640df86fd18869c7f01b579ab854fe034;p=ipfire.org.git analytics: Show total page views/page views in the last 24h for blog/docs Signed-off-by: Michael Tremer --- diff --git a/Makefile.am b/Makefile.am index b59bbfa4..1ec48d59 100644 --- a/Makefile.am +++ b/Makefile.am @@ -84,6 +84,7 @@ backenddir = $(pythondir)/ipfire web_PYTHON = \ src/web/__init__.py \ + src/web/analytics.py \ src/web/auth.py \ src/web/base.py \ src/web/blog.py \ @@ -114,6 +115,13 @@ templates_DATA = \ templatesdir = $(datadir)/templates +templates_analyticsdir = $(templatesdir)/analytics + +templates_analytics_modules_DATA = \ + src/templates/analytics/modules/summary.html + +templates_analytics_modulesdir = $(templates_analyticsdir)/modules + templates_auth_DATA = \ src/templates/auth/activate.html \ src/templates/auth/activated.html \ diff --git a/src/backend/analytics.py b/src/backend/analytics.py index cfecbd78..9172adad 100644 --- a/src/backend/analytics.py +++ b/src/backend/analytics.py @@ -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 diff --git a/src/templates/analytics/modules/summary.html b/src/templates/analytics/modules/summary.html new file mode 100644 index 00000000..8bda7027 --- /dev/null +++ b/src/templates/analytics/modules/summary.html @@ -0,0 +1,15 @@ +
+
+
+

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

+

{{ total_page_views }}

+
+
+ +
+
+

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

+

{{ total_page_views_24h }}

+
+
+
diff --git a/src/templates/blog/post.html b/src/templates/blog/post.html index fd143eb6..28015f28 100644 --- a/src/templates/blog/post.html +++ b/src/templates/blog/post.html @@ -173,4 +173,19 @@ + + {# Analytics #} + {% if post.is_published() %} + {% if current_user and current_user.is_admin() %} +
+
+
+

{{ _("Analytics") }}

+ + {% module AnalyticsSummary() %} +
+
+
+ {% end %} + {% end %} {% end block %} diff --git a/src/templates/docs/page.html b/src/templates/docs/page.html index bd8209e0..914776e0 100644 --- a/src/templates/docs/page.html +++ b/src/templates/docs/page.html @@ -80,4 +80,17 @@ + + {# Analytics #} + {% if current_user and current_user.is_admin() %} +
+
+
+

{{ _("Analytics") }}

+ + {% module AnalyticsSummary() %} +
+
+
+ {% end %} {% end block %} diff --git a/src/web/__init__.py b/src/web/__init__.py index cdb534c1..7ca4087f 100644 --- a/src/web/__init__.py +++ b/src/web/__init__.py @@ -14,6 +14,7 @@ from .. import util from .handlers import * +from . import analytics from . import auth from . import blog from . import boot @@ -60,6 +61,9 @@ class Application(tornado.web.Application): # UI Modules "ui_modules" : { + # Analytics + "AnalyticsSummary" : analytics.SummaryModule, + # Auth "Password" : auth.PasswordModule, diff --git a/src/web/analytics.py b/src/web/analytics.py new file mode 100644 index 00000000..66c694a6 --- /dev/null +++ b/src/web/analytics.py @@ -0,0 +1,23 @@ +#!/usr/bin/python3 + +import datetime + +from . import ui_modules + +class SummaryModule(ui_modules.UIModule): + def render(self, host=None, uri=None): + if host is None: + host = self.request.host + + if uri is None: + uri = self.request.path + + # Fetch the total number of page views + total_page_views = self.backend.analytics.get_total_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, + 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)