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@423: from core.models import Statistic bgneal@423: from core.whos_online import get_users_online, get_visitors_online, tick 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: bgneal@423: users = len(get_users_online()) bgneal@423: guests = len(get_visitors_online()) 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() bgneal@423: bgneal@423: # "tick" the who's online data collector bgneal@423: tick()