Added support for a popular datasets widget.
[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
@@ -3,15 +3,49 @@
 import ckan.lib.base as base
 import ckan.model as model
 
+from ckanext.ga_report.ga_model import GA_Url, GA_Publisher
+from ckanext.ga_report.controller import _get_publishers
 _log = logging.getLogger(__name__)
 
+def popular_datasets(count=10):
+    import random
+
+    publisher = None
+    publishers = _get_publishers(30)
+    total = len(publishers)
+    while not publisher or not datasets:
+        rand = random.randrange(0, total)
+        publisher = publishers[rand][0]
+        if not publisher.state == 'active':
+            publisher = None
+            continue
+        datasets = _datasets_for_publisher(publisher, 10)[:count]
+
+    ctx = {
+        'datasets': datasets,
+        'publisher': publisher
+    }
+    return base.render_snippet('ga_report/ga_popular_datasets.html', **ctx)
+
+
 def most_popular_datasets(publisher, count=20):
-    from ckanext.ga_report.ga_model import GA_Url
 
     if not publisher:
         _log.error("No valid publisher passed to 'most_popular_datasets'")
         return ""
 
+    results = _datasets_for_publisher(publisher, count)
+
+    ctx = {
+        'dataset_count': len(datasets),
+        'datasets': results,
+
+        'publisher': publisher
+    }
+
+    return base.render_snippet('ga_report/publisher/popular.html', **ctx)
+
+def _datasets_for_publisher(publisher, count):
     datasets = {}
     entries = model.Session.query(GA_Url).\
         filter(GA_Url.department_id==publisher.name).\
@@ -29,14 +63,5 @@
     for k, v in datasets.iteritems():
         results.append((k,v['views'],v['visits']))
 
-    results = sorted(results, key=operator.itemgetter(1), reverse=True)
+    return sorted(results, key=operator.itemgetter(1), reverse=True)
 
-    ctx = {
-        'dataset_count': len(datasets),
-        'datasets': results,
-
-        'publisher': publisher
-    }
-
-    return base.render_snippet('ga_report/publisher/popular.html', **ctx)
-