Show top datasets cross-publisher. Drop-down for the publisher. Browser version numbers filtered on download, so you get this version in the CSV too - for privacy. single_popular_dataset now copes when not much data, and can return the figures so DGU can reskin it in its own repo. Notes about usage stats centralised to notes.html.
[ckanext-ga-report.git] / ckanext / ga_report / helpers.py
blob:a/ckanext/ga_report/helpers.py -> blob:b/ckanext/ga_report/helpers.py
--- a/ckanext/ga_report/helpers.py
+++ b/ckanext/ga_report/helpers.py
@@ -28,28 +28,37 @@
     return base.render_snippet('ga_report/ga_popular_datasets.html', **ctx)
 
 def single_popular_dataset(top=20):
+    '''Returns a random dataset from the most popular ones.
+
+    :param top: the number of top datasets to select from
+    '''
     import random
 
-    datasets = {}
-    rand = random.randrange(0, top)
-    entry = model.Session.query(GA_Url).\
-        filter(GA_Url.url.like('/dataset/%')).\
-        order_by('ga_url.pageviews::int desc')[rand]
+    top_datasets = model.Session.query(GA_Url).\
+                   filter(GA_Url.url.like('/dataset/%')).\
+                   order_by('ga_url.pageviews::int desc')
+    num_top_datasets = top_datasets.count()
 
-
-    dataset = None
-    while not dataset:
-        dataset = model.Package.get(entry.url[len('/dataset/'):])
-        if dataset and not dataset.state == 'active':
-            dataset = None
-        else:
-            publisher = model.Group.get(entry.department_id)
-
-    ctx = {
+    if num_top_datasets:
+        dataset = None
+        while not dataset:
+            rand = random.randrange(0, min(top, num_top_datasets))
+            ga_url = top_datasets[rand]
+            dataset = model.Package.get(ga_url.url[len('/dataset/'):])
+            if dataset and not dataset.state == 'active':
+                dataset = None
+    else:
+        dataset = model.Session.query(model.Package)\
+                  .filter_by(state='active').first()
+    publisher = dataset.get_groups('publisher')[0]
+    return {
         'dataset': dataset,
         'publisher': publisher
     }
-    return base.render_snippet('ga_report/ga_popular_single.html', **ctx)
+
+def single_popular_dataset_html(top=20):
+    context = single_popular_dataset(top)
+    return base.render_snippet('ga_report/ga_popular_single.html', **context)
 
 
 def most_popular_datasets(publisher, count=20):
@@ -61,7 +70,7 @@
     results = _datasets_for_publisher(publisher, count)
 
     ctx = {
-        'dataset_count': len(datasets),
+        'dataset_count': len(results),
         'datasets': results,
 
         'publisher': publisher