more refactoring

This commit is contained in:
Seb Bacon 2011-04-05 10:38:25 +01:00
parent b69d89bf18
commit c91ab410c0
6 changed files with 107 additions and 49 deletions

View File

@ -9,15 +9,6 @@ log = logging.getLogger('ckanext.googleanalytics')
class GAController(BaseController):
def view(self):
# get package objects corresponding to popular GA content
self.parse_ga_data()
c.top_packages = self.get_top_packages()
return render('index.html')
def __str__(self):
# XXX hack to provide consistent cache key; what's the
# canonical way of doing caching like this in CKAN right now?
return "analyticscontroller"
def get_top_packages(self):
items = dbutil.get_top_packages()
return items
c.top_packages = dbutil.get_top_packages(limit=10)
c.top_resources = dbutil.get_top_resources(limit=10)
return render('summary.html')

View File

@ -77,12 +77,29 @@ def get_top_packages(limit=20):
q = authorizer.authorized_query(PSEUDO_USER__VISITOR,
model.Package)
connection = model.Session.connection()
res = connection.execute("""SELECT package_id, visits_recently
res = connection.execute("""SELECT package_id, visits_recently,
visits_ever
FROM package_stats
ORDER BY visits_recently DESC;""").fetchmany(limit)
for package_id, visits in res:
for package_id, recent, ever in res:
item = q.filter("package.id = '%s'" % package_id)
if not item.count():
continue
items.append((item.first(), visits))
items.append((item.first(), recent, ever))
return items
def get_top_resources(limit=20):
items = []
connection = model.Session.connection()
res = connection.execute("""SELECT resource_id, visits_recently,
visits_ever
FROM resource_stats
ORDER BY visits_recently DESC;""").fetchmany(limit)
for resource_id, recent, ever in res:
item = model.Session.query(model.Resource)\
.filter("resource.id = '%s'" % resource_id)
if not item.count():
continue
items.append((item.first(), recent, ever))
return items

View File

@ -3,8 +3,8 @@ import urllib
import os
from genshi.filters import Transformer
from genshi import HTML
from genshi.core import START, TEXT
from genshi.filters.transform import INSIDE
from genshi.core import START, TEXT, END
from genshi.filters.transform import INSIDE, EXIT
from pylons import config
from ckan.plugins import implements, SingletonPlugin
from ckan.plugins import IGenshiStreamFilter, IConfigurable, IRoutes
@ -51,18 +51,20 @@ class GoogleAnalyticsPlugin(SingletonPlugin):
# add some stats
def download_adder(stream):
download_html = ' <span="downloads-count">(%s downloads)</span>'
download_html = ''' <span class="downloads-count">
(%s downloads)</span>'''
count = None
for mark, (kind, data, pos) in stream:
if mark and kind == START:
href = data[1].get('href')
count = dbutil.get_resource_visits_for_url(href)
if count and kind == TEXT and mark == INSIDE:
yield mark, (kind,
data + download_html % count,
pos)
else:
yield mark, (kind, data, pos)
if href:
count = dbutil.get_resource_visits_for_url(href)
if count and mark is EXIT:
# emit count
yield INSIDE, (TEXT,
HTML(download_html % count),
pos)
yield mark, (kind, data, pos)
# perform the stream transform
stream = stream | Transformer(

View File

@ -1,24 +0,0 @@
<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:def function="page_title">Google analytics for CKAN</py:def>
<div py:match="content">
<h3>Most visited packages in last 14 days:</h3>
<ul>
<py:for each="package, count in c.top_packages">
<li>${count}: ${h.link_to(package.title or package.name, h.url_for(controller='package', action='read', id=package.name))}
</li>
</py:for>
</ul>
</div>
<xi:include href="layout.html" />
</html>

View File

@ -0,0 +1,19 @@
<html xmlns="http://www.w3.org/1999/xhtml"
xmlns:py="http://genshi.edgewall.org/"
xmlns:xi="http://www.w3.org/2001/XInclude"
xmlns:doap="http://usefulinc.com/ns/doap"
xmlns:foaf="http://xmlns.com/foaf/0.1/"
py:strip=""
>
<py:match path="//link[@rel='stylesheet']">
${select('.*')}
<style type="text/css">
span.downloads-count {
font-size: 75%;
}
</style>
</py:match>
<xi:include href="layout_base.html" />
</html>

View File

@ -0,0 +1,53 @@
<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:def function="page_title">Google analytics for CKAN</py:def>
<div py:match="content">
<h2>Most viewed packages</h2>
<p><em>Note: this data does not include API calls</em></p>
<table>
<tr>
<th>Package</th>
<th>Unique views (last 14 days)</th>
<th>Unique views (since recording started)</th>
</tr>
<py:for each="package, recent, ever in c.top_packages">
<tr>
<td>${h.link_to(package.title or package.name, h.url_for(controller='package', action='read', id=package.name))}
</td>
<td>${recent}</td>
<td>${ever}</td>
</tr>
</py:for>
</table>
<h2>Most downloaded resources</h2>
<table>
<tr>
<th>Resource</th>
<th>Unique views (last 14 days)</th>
<th>Unique views (since recording started)</th>
</tr>
<py:for each="resource, recent, ever in c.top_resources">
<tr>
<td>${h.link_to(resource.description or resource.format, resource.url)}<br />
<em>in ${h.link_to(resource.resource_group.package.title or resource.resource_group.package.name, h.url_for(controller='package', action='read', id=resource.resource_group.package.name))}</em>
</td>
<td>${recent}</td>
<td>${ever}</td>
</tr>
</py:for>
</table>
</div>
<xi:include href="layout.html" />
</html>