Add feature to recalculate visitor counts

This commit is contained in:
Noah 2014-12-06 22:45:20 +00:00
parent 83d878a6b6
commit ca241f2bcf
3 changed files with 38 additions and 1 deletions

View File

@ -113,6 +113,28 @@ def log_referrer(request, link):
return None 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(): def get_visitor_details():
"""Retrieve detailed visitor information for the frontend.""" """Retrieve detailed visitor information for the frontend."""
result = { result = {

View File

@ -5,6 +5,7 @@
from flask import g, Blueprint, request, redirect, url_for, session, flash from flask import g, Blueprint, request, redirect, url_for, session, flash
import rophako.model.user as User import rophako.model.user as User
import rophako.model.tracking as Tracking
from rophako.modules.account import validate_create_form from rophako.modules.account import validate_create_form
from rophako.utils import template, admin_required from rophako.utils import template, admin_required
@ -180,3 +181,11 @@ def unimpersonate():
flash("No longer impersonating.") flash("No longer impersonating.")
return redirect(url_for("index")) 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"))

View File

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