Minor tweaks
--- a/ckanext/ga_report/controller.py
+++ b/ckanext/ga_report/controller.py
@@ -216,12 +216,15 @@
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)
+
+ c.downloads = [(k,v,) for k,v in data.iteritems()]
+ 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
@@ -304,6 +304,12 @@
max_results=10000,
end_date=end_date).execute()
result_data = results.get('rows')
+ if not result_data:
+ # We may not have data for this time period, so we need to bail
+ # early.
+ log.info("There is no download data for this time period")
+ return
+
# [[url, count], [url],count]
data = {}
for result in result_data:
--- a/ckanext/ga_report/templates/ga_report/ga_util.html
+++ b/ckanext/ga_report/templates/ga_report/ga_util.html
@@ -46,7 +46,7 @@
<table py:def="downloads_table(items)" class="table table-condensed table-bordered table-striped">
<tr>
- <th>Name</th>
+ <th>Dataset and resource</th>
<th>Downloads</th>
</tr>
<py:for each="resource, value in items">