Refactor graph code. Querying to build a Top Packages graph.
--- a/ckanext/ga_report/controller.py
+++ b/ckanext/ga_report/controller.py
@@ -273,7 +273,7 @@
if not c.publisher:
abort(404, 'A publisher with that name could not be found')
- packages, graph_data = self._get_packages(c.publisher)
+ packages = self._get_packages(c.publisher)
response.headers['Content-Type'] = "text/csv; charset=utf-8"
response.headers['Content-Disposition'] = \
str('attachment; filename=datasets_%s_%s.csv' % (c.publisher_name, month,))
@@ -319,7 +319,6 @@
q = q.filter(GA_Url.department_id==publisher.name)
q = q.filter(GA_Url.period_name==month)
q = q.order_by('ga_url.pageviews::int desc')
- graph_data = []
top_packages = []
if count == -1:
entries = q.all()
@@ -328,7 +327,6 @@
for entry,package in entries:
if package:
- graph = []
# Downloads ....
if have_download_data:
dls = model.Session.query(GA_Stat).\
@@ -338,16 +336,14 @@
dls = dls.filter(GA_Stat.period_name==month)
downloads = 0
for x in dls:
- graph.append({ 'x': _get_unix_epoch(d.period_name), 'y': int(d.value)})
- downloads += int(d.value)
+ downloads += int(x.value)
else:
downloads = 'No data'
- top_packages.append((package, entry.pageviews, entry.visits, downloads, graph_data))
- graph_data.append({'name':package.title, 'data':graph})
+ top_packages.append((package, entry.pageviews, entry.visits, downloads))
else:
log.warning('Could not find package associated package')
- return top_packages,graph_data
+ return top_packages
def read(self):
'''
@@ -389,8 +385,27 @@
entry = q.filter(GA_Url.period_name==c.month).first()
c.publisher_page_views = entry.pageviews if entry else 0
- c.top_packages, graph_data = self._get_packages(c.publisher, 20)
- c.graph_data = json.dumps(graph_data)
+ c.top_packages = self._get_packages(c.publisher, 20)
+
+ # Graph query
+ top_package_names = [ x[0].name for x in c.top_packages ]
+ graph_query = model.Session.query(GA_Url,model.Package)\
+ .filter(model.Package.name==GA_Url.package_id)\
+ .filter(GA_Url.url.like('/dataset/%'))\
+ .filter(GA_Url.package_id.in_(top_package_names))
+ graph_data = {}
+ for entry,package in graph_query:
+ if not package: continue
+ if entry.period_name=='All': continue
+ graph_data[package.id] = graph_data.get(package.id,{
+ 'name':package.title,
+ 'data':[]
+ })
+ graph_data[package.id]['data'].append({
+ 'x':_get_unix_epoch(entry.period_name),
+ 'y':int(entry.pageviews),
+ })
+ c.graph_data = json.dumps(graph_data.values())
return render('ga_report/publisher/read.html')
--- a/ckanext/ga_report/templates/ga_report/ga_util.html
+++ b/ckanext/ga_report/templates/ga_report/ga_util.html
@@ -30,7 +30,7 @@
</table>
-<div py:def="rickshaw_graph(items_json,id,debug=False)">
+<div py:def="rickshaw_graph(items_json,id)">
<div id="chart_container_$id" class="rickshaw_chart_container">
<div id="y_axis_$id" class="rickshaw_y_axis"></div>
<div id="chart_$id" class="rickshaw_chart"></div>
@@ -38,7 +38,7 @@
<script type="text/javascript">
$(function() {
var series = ${items_json};
- <py:if test="debug">console.log(series);</py:if>
+ Rickshaw.Series.zeroFill(series);
var palette = new Rickshaw.Color.Palette( { scheme: 'spectrum2001' } );
$.each(series, function(i, object) {
object['color'] = palette.color();
--- a/ckanext/ga_report/templates/ga_report/publisher/index.html
+++ b/ckanext/ga_report/templates/ga_report/publisher/index.html
@@ -42,7 +42,7 @@
</div>
</form>
- ${rickshaw_graph('[{name:"test series",data:[{x:1,y:200},{x:3,y:300},{x:5,y:100}]}]','dataset-downloads',debug=True)}
+ ${rickshaw_graph('[{name:"test series",data:[{x:1,y:200},{x:3,y:300},{x:5,y:100}]}]','dataset-downloads')}
<table class="table table-condensed table-bordered table-striped">
<tr>
<th>Publisher</th>