Mercurial > public > sg101
annotate gpp/core/management/commands/max_users.py @ 308:4d086d2210b9
Fixing #141; cache badge ownership to reduce database hits when browsing forum topics.
author | Brian Neal <bgneal@gmail.com> |
---|---|
date | Sun, 16 Jan 2011 21:06:40 +0000 |
parents | dcc929973bba |
children | 3fe60148f75c |
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@239 | 10 from core.models import UserLastVisit, AnonLastVisit, Statistic |
bgneal@239 | 11 |
bgneal@239 | 12 |
bgneal@239 | 13 class Command(NoArgsCommand): |
bgneal@239 | 14 help = "Run periodically to compute the max users online statistic." |
bgneal@239 | 15 |
bgneal@239 | 16 def handle_noargs(self, **options): |
bgneal@239 | 17 |
bgneal@239 | 18 now = datetime.datetime.now() |
bgneal@239 | 19 cut_off = now - datetime.timedelta(minutes=15) |
bgneal@239 | 20 |
bgneal@239 | 21 users = UserLastVisit.objects.filter(last_visit__gte=cut_off).count() |
bgneal@239 | 22 guests = AnonLastVisit.objects.filter(last_visit__gte=cut_off).count() |
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() |