Changed to provide a lambda to check if the extension is installed
[ckanext-ga-report.git] / ckanext / ga_report /
blob:a/ckanext/ga_report/ -> blob:b/ckanext/ga_report/
import logging import logging
import operator import operator
import ckan.lib.base as base import ckan.lib.base as base
import ckan.model as model import ckan.model as model
_log = logging.getLogger(__name__) _log = logging.getLogger(__name__)
def most_popular_datasets(publisher, count=20): def most_popular_datasets(publisher, count=20):
from ckanext.ga_report.ga_model import GA_Url from ckanext.ga_report.ga_model import GA_Url
if not publisher: if not publisher:
_log.error("No valid publisher passed to 'most_popular_datasets'") _log.error("No valid publisher passed to 'most_popular_datasets'")
return "" return ""
datasets = {} datasets = {}
entries = model.Session.query(GA_Url).\ entries = model.Session.query(GA_Url).\
filter(\ filter(\
filter('/dataset/%')).\ filter('/dataset/%')).\
order_by('ga_url.pageviews::int desc')[:count] order_by('ga_url.pageviews::int desc')[:count]
for entry in entries: for entry in entries:
p = model.Package.get(entry.url[len('/dataset/'):]) p = model.Package.get(entry.url[len('/dataset/'):])
if not p in datasets: if not p in datasets:
datasets[p] = {'views':0, 'visits': 0} datasets[p] = {'views':0, 'visits': 0}
datasets[p]['views'] = datasets[p]['views'] + int(entry.pageviews) datasets[p]['views'] = datasets[p]['views'] + int(entry.pageviews)
datasets[p]['visits'] = datasets[p]['visits'] + int(entry.visitors) datasets[p]['visits'] = datasets[p]['visits'] + int(entry.visitors)
results = [] results = []
for k, v in datasets.iteritems(): for k, v in datasets.iteritems():
results.append((k,v['views'],v['visits'])) results.append((k,v['views'],v['visits']))
results = sorted(results, key=operator.itemgetter(1), reverse=True) results = sorted(results, key=operator.itemgetter(1), reverse=True)
ctx = { ctx = {
'dataset_count': len(datasets), 'dataset_count': len(datasets),
'datasets': results, 'datasets': results,
'publisher': publisher 'publisher': publisher
} }
return base.render_snippet('ga_report/publisher/popular.html', **ctx) return base.render_snippet('ga_report/publisher/popular.html', **ctx)