]> git.ipfire.org Git - ipfire.org.git/commitdiff
analytics: Show total page views/page views in the last 24h for blog/docs
authorMichael Tremer <michael.tremer@ipfire.org>
Sun, 7 Jan 2024 15:11:05 +0000 (15:11 +0000)
committerMichael Tremer <michael.tremer@ipfire.org>
Sun, 7 Jan 2024 15:11:05 +0000 (15:11 +0000)
Signed-off-by: Michael Tremer <michael.tremer@ipfire.org>
Makefile.am
src/backend/analytics.py
src/templates/analytics/modules/summary.html [new file with mode: 0644]
src/templates/blog/post.html
src/templates/docs/page.html
src/web/__init__.py
src/web/analytics.py [new file with mode: 0644]

index b59bbfa40b0a45425da32ae6c88322307bd8bd84..1ec48d59d1b64052701d861f83e913a26a204eec 100644 (file)
@@ -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 \
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
diff --git a/src/templates/analytics/modules/summary.html b/src/templates/analytics/modules/summary.html
new file mode 100644 (file)
index 0000000..8bda702
--- /dev/null
@@ -0,0 +1,15 @@
+<div class="level">
+       <div class="level-item has-text-centered">
+               <div>
+                       <p class="heading">{{ _("Total Page Views") }}</p>
+                       <p class="title">{{ total_page_views }}</p>
+               </div>
+       </div>
+
+       <div class="level-item has-text-centered">
+               <div>
+                       <p class="heading">{{ _("Total Page Views (Last 24h)") }}</p>
+                       <p class="title">{{ total_page_views_24h }}</p>
+               </div>
+       </div>
+</div>
index fd143eb63994aca02d226f80c6863680246c52c0..28015f288915f0115fbdfec5dff568258f4cae3f 100644 (file)
                        </div>
                </div>
        </section>
+
+       {# Analytics #}
+       {% if post.is_published() %}
+               {% if current_user and current_user.is_admin() %}
+                       <section class="hero is-dark">
+                               <div class="hero-body">
+                                       <div class="container">
+                                               <h4 class="title is-4">{{ _("Analytics") }}</h4>
+
+                                               {% module AnalyticsSummary() %}
+                                       </div>
+                               </div>
+                       </section>
+               {% end %}
+       {% end %}
 {% end block %}
index bd8209e06232a0321e64e7b7fe4152a389fccf4f..914776e05178588ef2856718cd091b7dbe3d8c65 100644 (file)
                        </div>
                </div>
        </section>
+
+       {# Analytics #}
+       {% if current_user and current_user.is_admin() %}
+               <section class="hero is-dark">
+                       <div class="hero-body">
+                               <div class="container">
+                                       <h4 class="title is-4">{{ _("Analytics") }}</h4>
+
+                                       {% module AnalyticsSummary() %}
+                               </div>
+                       </div>
+               </section>
+       {% end %}
 {% end block %}
index cdb534c1b5f0e6c31406a19093c88898984dd89c..7ca4087f3a76885de18281ed77dce11d989871f6 100644 (file)
@@ -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 (file)
index 0000000..66c694a
--- /dev/null
@@ -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)