Commits

Donald Stufft committed 19d2b9a

Enable (hidden) download counts

  • Participants
  • Parent commits 5c33a31

Comments (0)

Files changed (5)

config.ini.template

 files_dir = /MacDev/svn.python.org/pypi-pep345/files
 docs_dir = /MacDev/svn.python.org/pypi-pep345/docs
 package_docs_url = http://pythonhosted.org/
+redis_url = redis://localhost:6379/0
 
 [webui]
 mailhost = mail.python.org
         self.fromaddr = c.get('logging', 'fromaddr')
         self.toaddrs = c.get('logging', 'toaddrs').split(',')
 
+        self.redis_url = c.get('database', 'redis_url')
+
         self.sentry_dsn = c.get('sentry', 'dsn')
 
         self.passlib = CryptContext(
 python-openid==2.2.5
 pytz==2013b
 raven==3.3.7
+redis==2.7.6
 requests==1.2.3
 six==1.3.0
 transaction==1.4.1

templates/display.pt

 <tr><td id="last" colspan="6"></td></tr>
   </table>
 
+<ul style="display: none;" class="nodot" tal:condition="data/download_counts | nothing">
+  <li><strong>Downloads:</strong></li>
+  <li>
+    <span tal:content="data/download_counts/day" /> downloads in the last day
+  </li>
+  <!-- will be displayed when we have enough data -->
+  <li style="display: none;">
+    <span tal:content="data/download_counts/day" /> downloads in the last week
+  </li>
+  <li style="display: none;">
+    <span tal:content="data/download_counts/day" /> downloads in the last month
+  </li>
+</ul>
+
 <ul class="nodot">
  <li tal:condition="data/release/author | nothing">
   <strong>Author:</strong>
 import sys, os, urllib, cStringIO, traceback, cgi, binascii, gzip
 import time, random, smtplib, base64, email, types, urlparse
 import re, zipfile, logging, shutil, Cookie, subprocess, hashlib
+import datetime
 from zope.pagetemplate.pagetemplatefile import PageTemplateFile
 from distutils.util import rfc822_escape
 from distutils2.metadata import Metadata
 from xml.etree import cElementTree
 import itsdangerous
+import redis
 
 try:
     import json
 def enumerate(sequence):
     return [(i, sequence[i]) for i in range(len(sequence))]
 
+PRECISIONS = [
+    ("hour", "%y-%m-%d-%H"),
+    ("daily", "%y-%m-%d"),
+]
+def make_key(precision, datetime, key):
+    return "downloads:%s:%s:%s:*" % (
+        precision[0], datetime.strftime(precision[1]), key)
+
+
 # Requires:
 #   - ASCII letters
 #   - ASCII digits
         self.sentry_client = None
         if self.config.sentry_dsn:
             self.sentry_client = raven.Client(self.config.sentry_dsn)
+        if self.config.redis_url:
+            self.redis = redis.Redis.from_url(self.config.redis_url)
+        else:
+            self.redis = None
         self.env = env
         self.nav_current = None
         self.privkey = None
         docs = self.store.docs_url(name)
         files = self.store.list_files(name, version)
 
+        # Download Counts from redis
+        download_counts = {}
+        if self.redis is not None:
+            # Get the current utc time
+            current = datetime.datetime.utcnow()
+
+            # Get the download count for the last 24 hours (roughly)
+            keys = [make_key(PRECISIONS[0], current - datetime.timedelta(hours=x), name) for x in xrange(25)]
+            last_1 = sum([int(x) for x in self.redis.mget(*keys) if x is not None])
+
+            # Get the download count for the last 7 days (roughly)
+            keys = [make_key(PRECISIONS[1], current - datetime.timedelta(days=x), name) for x in xrange(8)]
+            last_7 = sum([int(x) for x in self.redis.mget(*keys) if x is not None])
+
+            # Get the download count for the last month (roughly)
+            keys = [make_key(PRECISIONS[1], current - datetime.timedelta(days=x), name) for x in xrange(31)]
+            last_30 = sum([int(x) for x in self.redis.mget(*keys) if x is not None])
+
+            download_counts = {
+                "day": last_1,
+                "week": last_7,
+                "month": last_30,
+            }
+
         self.write_template('display.pt',
                             name=name, version=version, release=release,
                             description=release.get('summary') or name,
                             obsoletes_dist=obsoletes_dist,
                             requires_external=requires_external,
                             project_url=project_url,
-			    bugtrack_url = bugtrack_url,
+                            download_counts=download_counts,
+                            bugtrack_url=bugtrack_url,
                             requires_python=release.get('requires_python', ''))
 
     def index(self, nav_current='index', releases=None):