Mercurial > public > sg101
changeset 239:dcc929973bba
Fix the max users online statistic as per ticket #90.
author | Brian Neal <bgneal@gmail.com> |
---|---|
date | Sun, 12 Sep 2010 18:30:23 +0000 |
parents | a3b47d0f4df1 |
children | 1246a4f1ab4f |
files | gpp/core/management/__init__.py gpp/core/management/commands/__init__.py gpp/core/management/commands/max_users.py gpp/core/models.py gpp/forums/management/__init__.py gpp/forums/management/commands/__init__.py gpp/forums/management/commands/forum_stats.py gpp/forums/models.py gpp/forums/templatetags/forum_tags.py |
diffstat | 5 files changed, 65 insertions(+), 50 deletions(-) [+] |
line wrap: on
line diff
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/gpp/core/management/commands/max_users.py Sun Sep 12 18:30:23 2010 +0000 @@ -0,0 +1,44 @@ +""" +max_users is a custom manage.py command. +It is intended to be called from a cron job to calculate the maximum +number of users online statistic. +""" +import datetime + +from django.core.management.base import NoArgsCommand + +from core.models import UserLastVisit, AnonLastVisit, Statistic + + +class Command(NoArgsCommand): + help = "Run periodically to compute the max users online statistic." + + def handle_noargs(self, **options): + + now = datetime.datetime.now() + cut_off = now - datetime.timedelta(minutes=15) + + users = UserLastVisit.objects.filter(last_visit__gte=cut_off).count() + guests = AnonLastVisit.objects.filter(last_visit__gte=cut_off).count() + + updated = False + try: + stat = Statistic.objects.get(pk=1) + except Statistic.DoesNotExist: + stat = Statistic(max_users=users, + max_users_date=now, + max_anon_users=guests, + max_anon_users_date=now) + updated=True + else: + if users > stat.max_users: + stat.max_users = users + stat.max_users_date = now + updated=True + if guests > stat.max_anon_users: + stat.max_anon_users = guests + stat.max_anon_users_date = now + updated=True + + if updated: + stat.save()
--- a/gpp/core/models.py Sun Sep 12 04:04:42 2010 +0000 +++ b/gpp/core/models.py Sun Sep 12 18:30:23 2010 +0000 @@ -1,8 +1,10 @@ """ This file contains the core Models used in gpp """ +import datetime + from django.db import models -from django.contrib import auth +from django.contrib.auth.models import User class UserLastVisit(models.Model): @@ -10,7 +12,7 @@ This model represents timestamps indicating a user's last visit to the site. """ - user = models.ForeignKey(auth.models.User, unique=True) + user = models.ForeignKey(User, unique=True) last_visit = models.DateTimeField(db_index=True) @@ -22,3 +24,19 @@ ip = models.CharField(max_length=16, db_index=True, unique=True) last_visit = models.DateTimeField(db_index=True) + +class Statistic(models.Model): + """ + This model keeps track of site statistics. Currently, the only statistic + is the maximum number of users online. This stat is computed by a mgmt. + command that is run on a cron job to peek at the previous two models. + """ + max_users = models.IntegerField() + max_users_date = models.DateTimeField() + max_anon_users = models.IntegerField() + max_anon_users_date = models.DateTimeField() + + def __unicode__(self): + return u'%d users on %s' % (self.max_users, + self.max_users_date.strftime('%Y-%m-%d %H:%M:%S')) +
--- a/gpp/forums/management/commands/forum_stats.py Sun Sep 12 04:04:42 2010 +0000 +++ /dev/null Thu Jan 01 00:00:00 1970 +0000 @@ -1,29 +0,0 @@ -""" -forum_stats is a custom manage.py command for the forums application. -It is intended to be called from a cron job to calculate various forum -statistics. -""" -from django.core.management.base import NoArgsCommand -from django.core.cache import cache - -from forums.models import Statistic -import forums.middleware - - -class Command(NoArgsCommand): - help = "Run periodically to gather forum statistics." - - def handle_noargs(self, **options): - # update maximum users online statistic - users_online = cache.get(forums.middleware.USERS_ONLINE_KEY) - if users_online: - try: - stats = Statistic.objects.get(pk=1) - except Statistic.DoesNotExist: - stats = Statistic(max_users=0) - - curr_users = len(users_online) - - if curr_users > stats.max_users: - stats.max_users = curr_users - stats.save()
--- a/gpp/forums/models.py Sun Sep 12 04:04:42 2010 +0000 +++ b/gpp/forums/models.py Sun Sep 12 18:30:23 2010 +0000 @@ -361,21 +361,3 @@ def touch(self): self.last_visit = datetime.datetime.now() - -class Statistic(models.Model): - """ - This model keeps track of forum statistics. Currently, the only statistic - is the maximum number of users online. This stat is computed by a mgmt. - command that is run on a cron job to peek at the "users_online" dict - that is maintained in cache by the forums middleware. - """ - max_users = models.IntegerField() - max_users_date = models.DateTimeField() - - def __unicode__(self): - return u'%d users on %s' % (self.max_users, - self.max_users_date.strftime('%Y-%m-%d %H:%M:%S')) - - def save(self, *args, **kwargs): - self.max_users_date = datetime.datetime.now() - super(Statistic, self).save(*args, **kwargs)
--- a/gpp/forums/templatetags/forum_tags.py Sun Sep 12 04:04:42 2010 +0000 +++ b/gpp/forums/templatetags/forum_tags.py Sun Sep 12 18:30:23 2010 +0000 @@ -11,7 +11,7 @@ from forums.models import Topic from forums.models import Post -from forums.models import Statistic +from core.models import Statistic register = template.Library()