Added a helper for generating a list of all time popular datasets
--- /dev/null
+++ b/ckanext/ga_report/helpers.py
@@ -1,1 +1,40 @@
+import logging
+import operator
+import ckan.lib.base as base
+import ckan.model as model
+_log = logging.getLogger(__name__)
+
+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 ""
+
+ datasets = {}
+ entries = model.Session.query(GA_Url).\
+ filter(GA_Url.department_id==publisher.name).\
+ filter(GA_Url.url.like('/dataset/%')).\
+ order_by('ga_url.pageviews::int desc')[:count]
+ for entry in entries:
+ p = model.Package.get(entry.url[len('/dataset/'):])
+ if not p in datasets:
+ datasets[p] = {'views':0, 'visits': 0}
+ datasets[p]['views'] = datasets[p]['views'] + int(entry.pageviews)
+ datasets[p]['visits'] = datasets[p]['visits'] + int(entry.visitors)
+
+ results = []
+ for k, v in datasets.iteritems():
+ results.append((k,v['views'],v['visits']))
+
+ results = 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)
+
--- a/ckanext/ga_report/plugin.py
+++ b/ckanext/ga_report/plugin.py
@@ -2,19 +2,27 @@
import ckan.lib.helpers as h
import ckan.plugins as p
from ckan.plugins import implements, toolkit
-#import gasnippet
-#import commands
-#import dbutil
log = logging.getLogger('ckanext.ga-report')
class GAReportPlugin(p.SingletonPlugin):
implements(p.IConfigurer, inherit=True)
implements(p.IRoutes, inherit=True)
+ implements(p.ITemplateHelpers, inherit=True)
def update_config(self, config):
toolkit.add_template_directory(config, 'templates')
toolkit.add_public_directory(config, 'public')
+
+ def get_helpers(self):
+ """
+ A dictionary of extra helpers that will be available to provide
+ ga report info to templates.
+ """
+ from ckanext.ga_report.helpers import most_popular_datasets
+ return {
+ 'most_popular_datasets': most_popular_datasets
+ }
def after_map(self, map):
map.connect(
--- /dev/null
+++ b/ckanext/ga_report/templates/ga_report/publisher/popular.html
@@ -1,1 +1,22 @@
+<html xmlns:py="http://genshi.edgewall.org/"
+ xmlns:i18n="http://genshi.edgewall.org/i18n"
+ xmlns:xi="http://www.w3.org/2001/XInclude"
+ py:strip="">
+ <py:if test="dataset_count > 0">
+ <div class="popular_datasets">
+ <ul class="popular_dataset_list">
+ <py:for each="dataset,views,visits in datasets">
+ <li>
+ ${h.link_to(dataset.title, h.url_for(controller='package', action='read', id=dataset.name))}
+ </li>
+ </py:for>
+ </ul>
+ <p>${h.link_to("More analytics for " + publisher.title, h.url_for(controller='ckanext.ga_report.controller:GaPublisherReport',action='read',id=publisher.name))}</p>
+ </div>
+ </py:if>
+</html>
+
+
+
+
--- a/ckanext/ga_report/templates/ga_report/publisher/read.html
+++ b/ckanext/ga_report/templates/ga_report/publisher/read.html
@@ -6,6 +6,7 @@
<py:def function="page_title">Analytics for ${g.site_title}</py:def>
<py:match path="primarysidebar">
+
<li class="widget-container boxed widget_text">
<h4>${c.publisher.title}</h4>
<p>