Commits

Donald Stufft committed 161990e

Add the rolling download counts into the API

Comments (0)

Files changed (4)

     info['release_url'] = 'http://pypi.python.org/pypi/%s/%s' % (package_name,
         version)
     info['docs_url'] = store.docs_url(package_name)
+    info['downloads'] = store.download_counts(package_name)
     return info
 package_data = release_data     # "deprecated"
 
         XXX update schema info ...
             Packages are unique by (name, version).
     '''
-    def __init__(self, config, queue=None):
+    def __init__(self, config, queue=None, redis=None):
         self.config = config
         self.username = None
         self.userip = None
             self.can_lock = True
 
         self.queue = queue
+        self.redis = redis
 
         self._changed_packages = set()
 
         else:
             self.queue.enqueue(func, *args, **kwargs)
 
+    def download_counts(self, name):
+        # 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 = {
+                "last_day": last_1,
+                "last_week": last_7,
+                "last_month": last_30,
+            }
+        return download_counts
+
     def last_id(self, tablename):
         ''' Return an SQL expression that returns the last inserted row,
         where the row is in the given table.

templates/display.pt

 <ul class="nodot" tal:condition="data/download_counts | nothing">
   <li><strong>Downloads (All Versions):</strong></li>
   <li>
-    <span tal:content="data/download_counts/day" /> downloads in the last day
+    <span tal:content="data/download_counts/last_day" /> downloads in the last day
   </li>
   <li>
-    <span tal:content="data/download_counts/week" /> downloads in the last week
+    <span tal:content="data/download_counts/last_week" /> downloads in the last week
   </li>
   <li>
-    <span tal:content="data/download_counts/month" /> downloads in the last month
+    <span tal:content="data/download_counts/last_month" /> downloads in the last month
   </li>
 </ul>
 
             # failed during initialization
             self.fail(self.failed)
             return
-        self.store = store.Store(self.config, queue=self.queue)
+        self.store = store.Store(
+            self.config,
+            queue=self.queue,
+            redis=self.redis,
+        )
         try:
             try:
                 self.store.get_cursor() # make sure we can connect
         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,
-            }
+        download_counts = self.store.download_counts(name)
 
         self.write_template('display.pt',
                             name=name, version=version, release=release,
Tip: Filter by directory path e.g. /media app.js to search for public/media/app.js.
Tip: Use camelCasing e.g. ProjME to search for ProjectModifiedEvent.java.
Tip: Filter by extension type e.g. /repo .js to search for all .js files in the /repo directory.
Tip: Separate your search with spaces e.g. /ssh pom.xml to search for src/ssh/pom.xml.
Tip: Use ↑ and ↓ arrow keys to navigate and return to view the file.
Tip: You can also navigate files with Ctrl+j (next) and Ctrl+k (previous) and view the file with Ctrl+o.
Tip: You can also navigate files with Alt+j (next) and Alt+k (previous) and view the file with Alt+o.