Mercurial > public > sg101
comparison gpp/accounts/stats.py @ 520:e94570675664
Created stats for users (number of users and list of newest users).
Better separated the forums, who's online, and user stats.
Use a Celery task to execute the new user stats processing.
This addresses #194 and #238.
author | Brian Neal <bgneal@gmail.com> |
---|---|
date | Sat, 17 Dec 2011 23:19:15 +0000 |
parents | |
children |
comparison
equal
deleted
inserted
replaced
519:f72ace06658a | 520:e94570675664 |
---|---|
1 """ | |
2 This module performs user account related statistics. | |
3 | |
4 """ | |
5 import logging | |
6 | |
7 from django.db.models.signals import post_save | |
8 from django.contrib.auth.models import User | |
9 | |
10 from core.services import get_redis_connection | |
11 | |
12 | |
13 # Redis key names | |
14 USER_COUNT_KEY = "accounts:user_count" | |
15 NEW_USERS_KEY = "accounts:new_users" | |
16 | |
17 | |
18 logger = logging.getLogger(__name__) | |
19 | |
20 | |
21 def on_user_save(sender, **kwargs): | |
22 """ | |
23 This function is our signal handler for when a User object is saved. | |
24 | |
25 """ | |
26 from accounts.tasks import user_stats_task | |
27 | |
28 if kwargs['created']: | |
29 user = kwargs['instance'] | |
30 | |
31 # kick off a task to update user stats | |
32 | |
33 user_stats_task.delay(user.id) | |
34 | |
35 | |
36 def update_user_stats(user_id): | |
37 """ | |
38 This function is given a new user id and is responsible for updating various | |
39 user account statistics. | |
40 | |
41 """ | |
42 try: | |
43 user = User.objects.get(pk=user_id) | |
44 except User.DoesNotExist: | |
45 logger.warning("update_user_stats: user id %d doesn't exist", user_id) | |
46 return | |
47 | |
48 redis = get_redis_connection() | |
49 | |
50 # update the count of registered users | |
51 | |
52 count = redis.incr(USER_COUNT_KEY) | |
53 if count == 1: | |
54 # it is likely redis got wiped out; update it now | |
55 count = User.objects.all().count() | |
56 redis.set(USER_COUNT_KEY, count) | |
57 | |
58 # update the list of new users | |
59 | |
60 pipeline = redis.pipeline() | |
61 pipeline.lpush(NEW_USERS_KEY, user.username) | |
62 pipeline.ltrim(NEW_USERS_KEY, 0, 9) | |
63 pipeline.execute() | |
64 | |
65 | |
66 def get_user_count(redis=None): | |
67 """ | |
68 This function returns the current count of users. | |
69 | |
70 """ | |
71 if redis is None: | |
72 redis = get_redis_connection() | |
73 return redis.get(USER_COUNT_KEY) | |
74 | |
75 | |
76 def get_new_users(redis=None): | |
77 """ | |
78 This function returns a list of new usernames. | |
79 | |
80 """ | |
81 if redis is None: | |
82 redis = get_redis_connection() | |
83 return redis.lrange(NEW_USERS_KEY, 0, -1) | |
84 | |
85 | |
86 def get_user_stats(redis=None): | |
87 """ | |
88 This function returns a tuple of the user stats. Element 0 is the user count | |
89 and element 1 is the list of new users. | |
90 | |
91 """ | |
92 if redis is None: | |
93 redis = get_redis_connection() | |
94 return get_user_count(redis), get_new_users(redis) | |
95 | |
96 | |
97 post_save.connect(on_user_save, sender=User, dispatch_uid='accounts.stats') |