From a6f19105957b8caeb0c3ca50c97d86d1e3e25bde Mon Sep 17 00:00:00 2001 From: Fred Wenzel Date: Wed, 1 Dec 2010 15:48:53 -0800 Subject: [PATCH] Show tweet statistics in the Customer Care sidebar. Bugs 612952, 612966, 612960, 612964. --- apps/customercare/cron.py | 58 +++++++++++- .../templates/customercare/landing.html | 59 +++++++++++- apps/customercare/views.py | 35 +++++++ media/css/sidebar.css | 86 +++++++++++++++++- media/img/customercare/bubble.png | Bin 0 -> 3135 bytes media/js/customercare.js | 20 ++++ settings.py | 15 ++- 7 files changed, 260 insertions(+), 13 deletions(-) create mode 100644 media/img/customercare/bubble.png diff --git a/apps/customercare/cron.py b/apps/customercare/cron.py index 5ee8a0e71..e1261614a 100644 --- a/apps/customercare/cron.py +++ b/apps/customercare/cron.py @@ -5,14 +5,17 @@ import logging import re import rfc822 import urllib +import urllib2 from django.conf import settings +from django.core.cache import cache from django.db.utils import IntegrityError from django.utils.encoding import smart_str import cronjobs +import tweepy -from .models import Tweet +from customercare.models import Tweet SEARCH_URL = 'http://search.twitter.com/search.json' @@ -118,3 +121,56 @@ def _filter_tweet(item): return None return item + + +@cronjobs.register +def get_customercare_stats(): + """ + Fetch Customer Care stats from Mozilla Metrics. + + Example Activity Stats data: + {"resultset": [["Yesterday",1234,123,0.0154], + ["Last Week",12345,1234,0.0240], ...] + "metadata": [...]} + + Example Top Contributor data: + {"resultset": [[1,"Overall","John Doe","johndoe",840], + [2,"Overall","Jane Doe","janedoe",435], ...], + "metadata": [...]} + """ + + stats_sources = { + settings.CC_TWEET_ACTIVITY_URL: settings.CC_TWEET_ACTIVITY_CACHE_KEY, + settings.CC_TOP_CONTRIB_URL: settings.CC_TOP_CONTRIB_CACHE_KEY, + } + for url, cache_key in stats_sources.items(): + log.debug('Updating %s from %s' % (cache_key, url)) + try: + json_data = json.load(urllib2.urlopen(url)) + json_data['resultset'] = '' + if not json_data['resultset']: + raise KeyError('Result set was empty.') + except Exception, e: + log.error('Error updating %s: %s' % (cache_key, e)) + continue + + # Grab top contributors' avatar URLs from the public twitter API. + if cache_key == settings.CC_TOP_CONTRIB_CACHE_KEY: + twitter = tweepy.API() + avatars = {} + for contrib in json_data['resultset']: + username = contrib[3] + + if avatars.get(username): + continue + + try: + user = twitter.get_user(username) + except tweepy.TweepError, e: + log.warning('Error grabbing avatar of user %s: %s' % ( + username, e)) + else: + avatars[username] = user.profile_image_url + json_data['avatars'] = avatars + + cache.set(cache_key, json_data, settings.CC_STATS_CACHE_TIMEOUT) diff --git a/apps/customercare/templates/customercare/landing.html b/apps/customercare/templates/customercare/landing.html index 3fda69e1b..8ea3cb7b8 100644 --- a/apps/customercare/templates/customercare/landing.html +++ b/apps/customercare/templates/customercare/landing.html @@ -18,7 +18,7 @@
  • Respond to the tweet!
  • - +

    Choose a tweet to help

    @@ -31,7 +31,7 @@ {% endif %}
    - +