comparison gpp/core/management/commands/max_users.py @ 239:dcc929973bba

Fix the max users online statistic as per ticket #90.
author Brian Neal <bgneal@gmail.com>
date Sun, 12 Sep 2010 18:30:23 +0000
parents
children 3fe60148f75c
comparison
equal deleted inserted replaced
238:a3b47d0f4df1 239:dcc929973bba
1 """
2 max_users is a custom manage.py command.
3 It is intended to be called from a cron job to calculate the maximum
4 number of users online statistic.
5 """
6 import datetime
7
8 from django.core.management.base import NoArgsCommand
9
10 from core.models import UserLastVisit, AnonLastVisit, Statistic
11
12
13 class Command(NoArgsCommand):
14 help = "Run periodically to compute the max users online statistic."
15
16 def handle_noargs(self, **options):
17
18 now = datetime.datetime.now()
19 cut_off = now - datetime.timedelta(minutes=15)
20
21 users = UserLastVisit.objects.filter(last_visit__gte=cut_off).count()
22 guests = AnonLastVisit.objects.filter(last_visit__gte=cut_off).count()
23
24 updated = False
25 try:
26 stat = Statistic.objects.get(pk=1)
27 except Statistic.DoesNotExist:
28 stat = Statistic(max_users=users,
29 max_users_date=now,
30 max_anon_users=guests,
31 max_anon_users_date=now)
32 updated=True
33 else:
34 if users > stat.max_users:
35 stat.max_users = users
36 stat.max_users_date = now
37 updated=True
38 if guests > stat.max_anon_users:
39 stat.max_anon_users = guests
40 stat.max_anon_users_date = now
41 updated=True
42
43 if updated:
44 stat.save()