Mercurial > public > sg101
annotate gpp/forums/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 | e10fa0d8e7ad |
children |
rev | line source |
---|---|
bgneal@395 | 1 """ |
bgneal@395 | 2 This module is responsible for managing various forum statistics. |
bgneal@395 | 3 |
bgneal@395 | 4 """ |
bgneal@395 | 5 from django.core.cache import cache |
bgneal@395 | 6 |
bgneal@395 | 7 from forums.models import Post |
bgneal@395 | 8 |
bgneal@395 | 9 |
bgneal@520 | 10 CACHE_KEY = 'forums-stats-2' |
bgneal@395 | 11 CACHE_TIMEOUT = 4 * 60 * 60 # seconds |
bgneal@395 | 12 |
bgneal@395 | 13 |
bgneal@395 | 14 def calc_stats(): |
bgneal@395 | 15 """ |
bgneal@395 | 16 This function is responsible for computing the forum statistics. |
bgneal@520 | 17 The forums post count is returned. |
bgneal@395 | 18 |
bgneal@395 | 19 """ |
bgneal@395 | 20 post_count = Post.objects.all().count() |
bgneal@520 | 21 return post_count |
bgneal@395 | 22 |
bgneal@395 | 23 |
bgneal@395 | 24 def update_stats(): |
bgneal@395 | 25 """ |
bgneal@395 | 26 This function is responsible for computing the forum statistics and |
bgneal@395 | 27 inserting them into the cache. The stats are returned. |
bgneal@395 | 28 |
bgneal@395 | 29 This function should be run periodically, preferably outside of the |
bgneal@395 | 30 request/response cycle. On MySQL under InnoDb it is expensive to retrieve |
bgneal@395 | 31 the total post count. |
bgneal@395 | 32 |
bgneal@395 | 33 """ |
bgneal@395 | 34 stats = calc_stats() |
bgneal@395 | 35 cache.set(CACHE_KEY, stats, CACHE_TIMEOUT) |
bgneal@395 | 36 return stats |
bgneal@395 | 37 |
bgneal@395 | 38 |
bgneal@395 | 39 def retrieve_stats(): |
bgneal@395 | 40 """ |
bgneal@395 | 41 This function retrieves the forum stats from the cache if they are |
bgneal@395 | 42 available. If there is a cache-miss, the stats are calcuated, the cache is |
bgneal@395 | 43 updated, and the stats returned. |
bgneal@395 | 44 |
bgneal@395 | 45 """ |
bgneal@395 | 46 stats = cache.get(CACHE_KEY) |
bgneal@395 | 47 if stats is None: |
bgneal@395 | 48 stats = update_stats() |
bgneal@395 | 49 |
bgneal@395 | 50 return stats |