Browse Source

Add feature to recalculate visitor counts

pull/2/head
Noah Petherbridge 7 years ago
parent
commit
ca241f2bcf
  1. 22
      rophako/model/tracking.py
  2. 9
      rophako/modules/admin/__init__.py
  3. 8
      rophako/modules/admin/templates/admin/index.html

22
rophako/model/tracking.py

@ -113,6 +113,28 @@ def log_referrer(request, link):
return None
def rebuild_visitor_stats():
"""Recalculate the total unique/hits based on daily info."""
total_unique = {}
total_hits = 0
# Tally them all up!
for date in JsonDB.list_docs("traffic/unique"):
if date == "total":
continue
db = JsonDB.get("traffic/unique/{}".format(date), cache=False)
total_unique.update(db)
for date in JsonDB.list_docs("traffic/hits"):
if date == "total":
continue
db = JsonDB.get("traffic/hits/{}".format(date), cache=False)
total_hits += db.get("hits", 0)
# Write the outputs.
JsonDB.commit("traffic/unique/total", total_unique)
JsonDB.commit("traffic/hits/total", dict(hits=total_hits))
def get_visitor_details():
"""Retrieve detailed visitor information for the frontend."""
result = {

9
rophako/modules/admin/__init__.py

@ -5,6 +5,7 @@
from flask import g, Blueprint, request, redirect, url_for, session, flash
import rophako.model.user as User
import rophako.model.tracking as Tracking
from rophako.modules.account import validate_create_form
from rophako.utils import template, admin_required
@ -180,3 +181,11 @@ def unimpersonate():
flash("No longer impersonating.")
return redirect(url_for("index"))
@mod.route("/maint/rebuild_visitor_counts")
@admin_required
def rebuild_visitor_counts():
"""Rebuild visitor counts."""
Tracking.rebuild_visitor_stats()
flash("Visitor counts recalculated.")
return redirect(url_for(".index"))

8
rophako/modules/admin/templates/admin/index.html

@ -9,4 +9,10 @@
<li><a href="{{ url_for('admin.users') }}">View and Manage Users</a></li>
</ul>
{% endblock %}
<h1>Maintenance Tasks</h1>
<ul>
<li><a href="{{ url_for('admin.rebuild_visitor_counts') }}">Rebuild Visitor Counts</a></li>
</ul>
{% endblock %}

Loading…
Cancel
Save