web_PYTHON = \
src/web/__init__.py \
+ src/web/analytics.py \
src/web/auth.py \
src/web/base.py \
src/web/blog.py \
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 \
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
--- /dev/null
+<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>
</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 %}
</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 %}
from .handlers import *
+from . import analytics
from . import auth
from . import blog
from . import boot
# UI Modules
"ui_modules" : {
+ # Analytics
+ "AnalyticsSummary" : analytics.SummaryModule,
+
# Auth
"Password" : auth.PasswordModule,
--- /dev/null
+#!/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)