bgneal@239: """ bgneal@239: max_users is a custom manage.py command. bgneal@239: It is intended to be called from a cron job to calculate the maximum bgneal@239: number of users online statistic. bgneal@239: """ bgneal@239: import datetime bgneal@239: bgneal@239: from django.core.management.base import NoArgsCommand bgneal@239: bgneal@239: from core.models import UserLastVisit, AnonLastVisit, Statistic bgneal@239: bgneal@239: bgneal@239: class Command(NoArgsCommand): bgneal@239: help = "Run periodically to compute the max users online statistic." bgneal@239: bgneal@239: def handle_noargs(self, **options): bgneal@239: bgneal@239: now = datetime.datetime.now() bgneal@239: cut_off = now - datetime.timedelta(minutes=15) bgneal@239: bgneal@239: users = UserLastVisit.objects.filter(last_visit__gte=cut_off).count() bgneal@239: guests = AnonLastVisit.objects.filter(last_visit__gte=cut_off).count() bgneal@239: bgneal@239: updated = False bgneal@239: try: bgneal@239: stat = Statistic.objects.get(pk=1) bgneal@239: except Statistic.DoesNotExist: bgneal@239: stat = Statistic(max_users=users, bgneal@239: max_users_date=now, bgneal@239: max_anon_users=guests, bgneal@239: max_anon_users_date=now) bgneal@239: updated=True bgneal@239: else: bgneal@239: if users > stat.max_users: bgneal@239: stat.max_users = users bgneal@239: stat.max_users_date = now bgneal@239: updated=True bgneal@239: if guests > stat.max_anon_users: bgneal@239: stat.max_anon_users = guests bgneal@239: stat.max_anon_users_date = now bgneal@239: updated=True bgneal@239: bgneal@239: if updated: bgneal@239: stat.save()