Пример #1
0
def xkcd_statistics():
    cached_reference_count = cache.get(CACHE_KEY_REFERENCE_COUNT)
    current_reference_count = db.get_xkcd_event_count()

    # If nothing changed since the last xkcd reference, load cached json
    if cached_reference_count and int(cached_reference_count) == current_reference_count:
        cached_stats_context = cache.get_json(CACHE_KEY_STATS_CONTEXT)
        if cached_stats_context:
            return render_template('stats.html', track=settings.ENABLE_TRACKING, **cached_stats_context)

    # Get general statistics
    unique_comics_referenced, unique_subreddit_referencers, unique_user_referencers, total_reference_count, \
        mean_references, variance = db.get_xkcd_global_stats()
    std_dev = math.sqrt(variance)

    # Build top user references list
    top_referencers = []
    user_rank = 1
    for user, count in db.get_top_user_referencers(5):
        top_referencers.append([user_rank, user, count])
        user_rank += 1

    # Get list of unique subreddits
    unique_subreddits_list = [s[0] for s in db.get_unique_subreddits()]
    unique_subreddits_list = list(sorted(unique_subreddits_list))

    # Get comic title info
    comic_titles = []
    for comic_id, title in db.get_xkcd_comic_titles():
        comic_titles.append('%d: %s' % (comic_id, title))

    # Get comic rankings
    comic_rankings = []
    for rank, comic_id, title, comic_count, comic_percentage, num_std_dev in get_rankings():
        comic_rankings.append([rank, [comic_id, title], comic_count, comic_percentage, num_std_dev])

    context = {
        'unique_comics_count': unique_comics_referenced,
        'unique_subreddits_count': unique_subreddit_referencers,
        'unique_referencers': unique_user_referencers,
        'total_count': total_reference_count,
        'mean_value': mean_references,
        'std_deviation': std_dev,
        'top_users': top_referencers,
        'unique_subreddits': unique_subreddits_list,
        'comic_titles': simplejson.dumps(comic_titles),
        'comic_rankings': comic_rankings,
    }

    # Cache the new values
    cache.set(CACHE_KEY_REFERENCE_COUNT, current_reference_count)
    cache.set_json(CACHE_KEY_STATS_CONTEXT, context)

    return render_template('stats.html', track=settings.ENABLE_TRACKING, **context)
Пример #2
0
def xkcd_backreferences():
    # Get comic title info
    comic_titles = []
    for comic_id, title in db.get_xkcd_comic_titles():
        comic_titles.append({
            'value': comic_id,
            'label': str(comic_id) + ': ' + title
        })

    context = {
        'comic_titles': simplejson.dumps(comic_titles),
    }

    return render_template('backrefs.html', track=settings.ENABLE_TRACKING, **context)