Graphing dataset downloads.
Graphing dataset downloads.

--- 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 = self._get_packages(c.publisher)
+        packages, graph_data = 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,6 +319,7 @@
             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()
@@ -327,6 +328,7 @@
 
         for entry,package in entries:
             if package:
+                graph = []
                 # Downloads ....
                 if have_download_data:
                     dls = model.Session.query(GA_Stat).\
@@ -334,15 +336,18 @@
                         filter(GA_Stat.key==package.name)
                     if month != 'All':  # Fetch everything unless the month is specific
                         dls = dls.filter(GA_Stat.period_name==month)
-
-                    downloads = sum(int(d.value) for d in dls.all())
+                    downloads = 0
+                    for x in dls:
+                        graph.append({ 'x': _get_unix_epoch(d.period_name), 'y': int(d.value)})
+                        downloads += int(d.value)
                 else:
                     downloads = 'No data'
-                top_packages.append((package, entry.pageviews, entry.visits, downloads))
+                top_packages.append((package, entry.pageviews, entry.visits, downloads, graph_data))
+                graph_data.append({'name':package.title, 'data':graph})
             else:
                 log.warning('Could not find package associated package')
 
-        return top_packages
+        return top_packages,graph_data
 
     def read(self):
         '''
@@ -384,7 +389,8 @@
         entry = q.filter(GA_Url.period_name==c.month).first()
         c.publisher_page_views = entry.pageviews if entry else 0
 
-        c.top_packages = self._get_packages(c.publisher, 20)
+        c.top_packages, graph_data = self._get_packages(c.publisher, 20)
+        c.graph_data = json.dumps(graph_data)
 
         return render('ga_report/publisher/read.html')
 

--- a/ckanext/ga_report/templates/ga_report/publisher/read.html
+++ b/ckanext/ga_report/templates/ga_report/publisher/read.html
@@ -52,7 +52,7 @@
      <p py:if="not c.top_packages">No page views in this period</p>
 
      <py:if test="c.top_packages">
-       ${rickshaw_graph('[{name:"test series",data:[{x:1,y:200},{x:3,y:300},{x:5,y:100}]}]','dataset-downloads',debug=True)}
+       ${rickshaw_graph(c.graph_data,'dataset-downloads',debug=True)}
        <table class="table table-condensed table-bordered table-striped">
          <tr>
            <th>Dataset</th>