New single entry random dataset helper
New single entry random dataset helper

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
   
from ckanext.ga_report.ga_model import GA_Url, GA_Publisher from ckanext.ga_report.ga_model import GA_Url, GA_Publisher
from ckanext.ga_report.controller import _get_publishers from ckanext.ga_report.controller import _get_publishers
_log = logging.getLogger(__name__) _log = logging.getLogger(__name__)
   
def popular_datasets(count=10): def popular_datasets(count=10):
import random import random
   
publisher = None publisher = None
publishers = _get_publishers(30) publishers = _get_publishers(30)
total = len(publishers) total = len(publishers)
while not publisher or not datasets: while not publisher or not datasets:
rand = random.randrange(0, total) rand = random.randrange(0, total)
publisher = publishers[rand][0] publisher = publishers[rand][0]
if not publisher.state == 'active': if not publisher.state == 'active':
publisher = None publisher = None
continue continue
datasets = _datasets_for_publisher(publisher, 10)[:count] datasets = _datasets_for_publisher(publisher, 10)[:count]
   
ctx = { ctx = {
'datasets': datasets, 'datasets': datasets,
'publisher': publisher 'publisher': publisher
} }
return base.render_snippet('ga_report/ga_popular_datasets.html', **ctx) return base.render_snippet('ga_report/ga_popular_datasets.html', **ctx)
   
  def single_popular_dataset(top=20):
  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]
   
   
  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 = {
  'dataset': dataset,
  'publisher': publisher
  }
  return base.render_snippet('ga_report/ga_popular_single.html', **ctx)
   
   
def most_popular_datasets(publisher, count=20): def most_popular_datasets(publisher, count=20):
   
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 ""
   
results = _datasets_for_publisher(publisher, count) results = _datasets_for_publisher(publisher, count)
   
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)
   
def _datasets_for_publisher(publisher, count): def _datasets_for_publisher(publisher, count):
datasets = {} datasets = {}
entries = model.Session.query(GA_Url).\ entries = model.Session.query(GA_Url).\
filter(GA_Url.department_id==publisher.name).\ filter(GA_Url.department_id==publisher.name).\
filter(GA_Url.url.like('/dataset/%')).\ filter(GA_Url.url.like('/dataset/%')).\
order_by('ga_url.pageviews::int desc').all() order_by('ga_url.pageviews::int desc').all()
for entry in entries: for entry in entries:
if len(datasets) < count: if len(datasets) < count:
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']))
   
return sorted(results, key=operator.itemgetter(1), reverse=True) return sorted(results, key=operator.itemgetter(1), reverse=True)
   
import logging import logging
import ckan.lib.helpers as h import ckan.lib.helpers as h
import ckan.plugins as p import ckan.plugins as p
from ckan.plugins import implements, toolkit from ckan.plugins import implements, toolkit
   
from ckanext.ga_report.helpers import (most_popular_datasets, from ckanext.ga_report.helpers import (most_popular_datasets,
popular_datasets) popular_datasets,
  single_popular_dataset)
   
log = logging.getLogger('ckanext.ga-report') log = logging.getLogger('ckanext.ga-report')
   
class GAReportPlugin(p.SingletonPlugin): class GAReportPlugin(p.SingletonPlugin):
implements(p.IConfigurer, inherit=True) implements(p.IConfigurer, inherit=True)
implements(p.IRoutes, inherit=True) implements(p.IRoutes, inherit=True)
implements(p.ITemplateHelpers, inherit=True) implements(p.ITemplateHelpers, inherit=True)
   
def update_config(self, config): def update_config(self, config):
toolkit.add_template_directory(config, 'templates') toolkit.add_template_directory(config, 'templates')
toolkit.add_public_directory(config, 'public') toolkit.add_public_directory(config, 'public')
   
def get_helpers(self): def get_helpers(self):
""" """
A dictionary of extra helpers that will be available to provide A dictionary of extra helpers that will be available to provide
ga report info to templates. ga report info to templates.
""" """
return { return {
'ga_report_installed': lambda: True, 'ga_report_installed': lambda: True,
'popular_datasets': popular_datasets, 'popular_datasets': popular_datasets,
'most_popular_datasets': most_popular_datasets, 'most_popular_datasets': most_popular_datasets,
  'single_popular_dataset': single_popular_dataset
} }
   
def after_map(self, map): def after_map(self, map):
map.connect( map.connect(
'/data/site-usage/publisher', '/data/site-usage/publisher',
controller='ckanext.ga_report.controller:GaPublisherReport', controller='ckanext.ga_report.controller:GaPublisherReport',
action='index' action='index'
) )
map.connect( map.connect(
'/data/site-usage/publisher_{month}.csv', '/data/site-usage/publisher_{month}.csv',
controller='ckanext.ga_report.controller:GaPublisherReport', controller='ckanext.ga_report.controller:GaPublisherReport',
action='csv' action='csv'
) )
map.connect( map.connect(
'/data/site-usage/publisher/{id}_{month}.csv', '/data/site-usage/publisher/{id}_{month}.csv',
controller='ckanext.ga_report.controller:GaPublisherReport', controller='ckanext.ga_report.controller:GaPublisherReport',
action='publisher_csv' action='publisher_csv'
) )
map.connect( map.connect(
'/data/site-usage/publisher/{id}', '/data/site-usage/publisher/{id}',
controller='ckanext.ga_report.controller:GaPublisherReport', controller='ckanext.ga_report.controller:GaPublisherReport',
action='read' action='read'
) )
map.connect( map.connect(
'/data/site-usage', '/data/site-usage',
controller='ckanext.ga_report.controller:GaReport', controller='ckanext.ga_report.controller:GaReport',
action='index' action='index'
) )
map.connect( map.connect(
'/data/site-usage/data_{month}.csv', '/data/site-usage/data_{month}.csv',
controller='ckanext.ga_report.controller:GaReport', controller='ckanext.ga_report.controller:GaReport',
action='csv' action='csv'
) )
return map return map
   
   
  <html xmlns:py="http://genshi.edgewall.org/"
  xmlns:i18n="http://genshi.edgewall.org/i18n"
  xmlns:xi="http://www.w3.org/2001/XInclude"
  py:strip="">
 
 
  <div class="popular_datasets">
  <h2>Featured dataset</h2>
 
  <h3>${h.link_to(dataset.title, h.url_for(controller='package', action='read', id=dataset.name))}</h3> <div>${h.truncate(dataset.notes, length=200, whole_word=True)}</div>
  <p></p>
  <div>
  <a href="${h.url_for(controller='ckanext.ga_report.controller:GaPublisherReport',action='index')}" class="btn btn-primary">More popular datasets</a>
  <a href="${h.url_for(controller='ckanext.ga_report.controller:GaReport',action='index')}" class="btn btn-primary">All usage data</a>
  </div>
  </div>
 
 
  </html>