annotate gpp/core/management/commands/max_users.py @ 423:3fe60148f75c

Fixing #203; use Redis for who's online function.
author Brian Neal <bgneal@gmail.com>
date Sat, 23 Apr 2011 19:19:38 +0000
parents dcc929973bba
children f72ace06658a
rev   line source
bgneal@239 1 """
bgneal@239 2 max_users is a custom manage.py command.
bgneal@239 3 It is intended to be called from a cron job to calculate the maximum
bgneal@239 4 number of users online statistic.
bgneal@239 5 """
bgneal@239 6 import datetime
bgneal@239 7
bgneal@239 8 from django.core.management.base import NoArgsCommand
bgneal@239 9
bgneal@423 10 from core.models import Statistic
bgneal@423 11 from core.whos_online import get_users_online, get_visitors_online, tick
bgneal@239 12
bgneal@239 13
bgneal@239 14 class Command(NoArgsCommand):
bgneal@239 15 help = "Run periodically to compute the max users online statistic."
bgneal@239 16
bgneal@239 17 def handle_noargs(self, **options):
bgneal@239 18
bgneal@239 19 now = datetime.datetime.now()
bgneal@239 20
bgneal@423 21 users = len(get_users_online())
bgneal@423 22 guests = len(get_visitors_online())
bgneal@239 23
bgneal@239 24 updated = False
bgneal@239 25 try:
bgneal@239 26 stat = Statistic.objects.get(pk=1)
bgneal@239 27 except Statistic.DoesNotExist:
bgneal@239 28 stat = Statistic(max_users=users,
bgneal@239 29 max_users_date=now,
bgneal@239 30 max_anon_users=guests,
bgneal@239 31 max_anon_users_date=now)
bgneal@239 32 updated=True
bgneal@239 33 else:
bgneal@239 34 if users > stat.max_users:
bgneal@239 35 stat.max_users = users
bgneal@239 36 stat.max_users_date = now
bgneal@239 37 updated=True
bgneal@239 38 if guests > stat.max_anon_users:
bgneal@239 39 stat.max_anon_users = guests
bgneal@239 40 stat.max_anon_users_date = now
bgneal@239 41 updated=True
bgneal@239 42
bgneal@239 43 if updated:
bgneal@239 44 stat.save()
bgneal@423 45
bgneal@423 46 # "tick" the who's online data collector
bgneal@423 47 tick()