Error check for potentially missing data
Error check for potentially missing data

--- a/ckanext/ga_report/controller.py
+++ b/ckanext/ga_report/controller.py
@@ -216,12 +216,17 @@
         q = q.filter(GA_Stat.period_name==c.month) if c.month else q
         q = q.order_by("ga_stat.value::int desc")
 
+        data = collections.defaultdict(int)
         for entry in q.all():
             r = model.Session.query(model.Resource).filter(model.Resource.url==entry.key).first()
-            if r:
-                c.downloads.append((r,entry.value))
-            else:
-                log.info("Failed to find resource for %s" % entry.key)
+            if not r:
+                continue
+            data[r] += int(entry.value)
+
+        for k,v in data.iteritems():
+            c.downloads.append((k,v))
+
+        c.downloads = sorted(c.downloads, key=operator.itemgetter(1), reverse=True)
 
         return render('ga_report/site/downloads.html')
 

--- a/ckanext/ga_report/download_analytics.py
+++ b/ckanext/ga_report/download_analytics.py
@@ -306,6 +306,8 @@
         result_data = results.get('rows')
         # [[url, count], [url],count]
         data = {}
+        if not result_data:
+            log.error(results)
         for result in result_data:
             data[result[0]] = data.get(result[0], 0) + int(result[1])
         self._filter_out_long_tail(data, MIN_DOWNLOADS)