Mercurial > public > sg101
annotate gpp/core/management/commands/max_users.py @ 505:a5d11471d031
Refactor the logic in the rate limiter decorator. Check to see if the request was ajax, as the ajax view always returns 200. Have to decode the JSON response to see if an error occurred or not.
author | Brian Neal <bgneal@gmail.com> |
---|---|
date | Sat, 03 Dec 2011 19:13:38 +0000 |
parents | 3fe60148f75c |
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() |