Done integrating graphs onto site_usage/publishers and site_usage/datasets. Including some interesting queries.
Done integrating graphs onto site_usage/publishers and site_usage/datasets. Including some interesting queries.

--- a/ckanext/ga_report/controller.py
+++ b/ckanext/ga_report/controller.py
@@ -253,7 +253,9 @@
         writer = csv.writer(response)
         writer.writerow(["Publisher Title", "Publisher Name", "Views", "Visits", "Period Name"])
 
-        for publisher,view,visit in _get_top_publishers(None):
+        top_publishers, top_publishers_graph = _get_top_publishers(None)
+
+        for publisher,view,visit in top_publishers:
             writer.writerow([publisher.title.encode('utf-8'),
                              publisher.name.encode('utf-8'),
                              view,
@@ -302,7 +304,9 @@
         if c.month:
             c.month_desc = ''.join([m[1] for m in c.months if m[0]==c.month])
 
-        c.top_publishers = _get_top_publishers()
+        c.top_publishers, graph_data = _get_top_publishers()
+        c.top_publishers_graph = json.dumps( _to_rickshaw(graph_data.values()) )
+
         return render('ga_report/publisher/index.html')
 
     def _get_packages(self, publisher=None, count=-1):
@@ -405,9 +409,22 @@
                 'x':_get_unix_epoch(entry.period_name),
                 'y':int(entry.pageviews),
                 })
-        c.graph_data = json.dumps(graph_data.values())
+
+        c.graph_data = json.dumps( _to_rickshaw(graph_data.values()) )
 
         return render('ga_report/publisher/read.html')
+
+def _to_rickshaw(data):
+    num_points = []    
+    for package in data:
+        package['data'] = sorted( package['data'], key=lambda x:x['x'] )
+        num_points.append( len(package['data']) )
+    if len(set(num_points))>1:
+        example = num_points[ num_points.index(max(num_points)) ]
+        for package in data:
+            while len(package['data'])<example:
+                package['data'].insert(0, package['data'][0])
+    return data
 
 def _get_top_publishers(limit=20):
     '''
@@ -430,11 +447,35 @@
 
     top_publishers = []
     res = connection.execute(q, month)
+    department_ids = []
     for row in res:
         g = model.Group.get(row[0])
         if g:
+            department_ids.append(row[0])
             top_publishers.append((g, row[1], row[2]))
-    return top_publishers
+
+    graph = {}
+    if limit is not None:
+        # Query for a history graph of these publishers
+        q = model.Session.query(
+                GA_Url.department_id, 
+                GA_Url.period_name, 
+                func.sum(cast(GA_Url.pageviews,sqlalchemy.types.INT)))\
+            .filter( GA_Url.department_id.in_(department_ids) )\
+            .filter( GA_Url.period_name!='All' )\
+            .filter( GA_Url.url.like('/dataset/%') )\
+            .filter( GA_Url.package_id!='' )\
+            .group_by( GA_Url.department_id, GA_Url.period_name )
+        for dept_id,period_name,views in q:
+            graph[dept_id] = graph.get( dept_id, {
+                'name' : model.Group.get(dept_id).title,
+                'data' : []
+                })
+            graph[dept_id]['data'].append({
+                'x': _get_unix_epoch(period_name),
+                'y': views
+                })
+    return top_publishers, graph
 
 
 def _get_publishers():

--- a/ckanext/ga_report/public/css/ga_report.css
+++ b/ckanext/ga_report/public/css/ga_report.css
@@ -5,7 +5,7 @@
 }
 .rickshaw_chart_container {
   position: relative;
-  height: 300px;
+  height: 350px;
   margin: 0 auto 20px auto;
 }
 .rickshaw_chart {
@@ -20,9 +20,12 @@
   right: 0;
   top: 0;
   margin-left: 15px;
+  padding: 0 5px;
   background: transparent;
   max-width: 150px;
   overflow: hidden;
+  background: rgba(0,0,0,0.05);
+  border-radius:5px;
 }
 .rickshaw_y_axis {
   position: absolute;
@@ -30,4 +33,9 @@
   bottom: 0;
   width: 40px;
 }
+.rickshaw_legend .label {
+  background: transparent !important;
+  color: #000000 !important;
+  font-weight: normal !important;
+}
 

--- a/ckanext/ga_report/templates/ga_report/ga_util.html
+++ b/ckanext/ga_report/templates/ga_report/ga_util.html
@@ -37,16 +37,16 @@
     <div id="legend_$id" class="rickshaw_legend"></div>
     <script type="text/javascript">
       $(function() {
-          var series = ${items_json};
-          Rickshaw.Series.zeroFill(series);
-          var palette = new Rickshaw.Color.Palette( { scheme: 'spectrum2001' } );
-          $.each(series, function(i, object) {
+          var all_series = ${items_json};
+          var palette = new Rickshaw.Color.Palette( { scheme: 'munin' } );
+          $.each(all_series, function(i, object) {
               object['color'] = palette.color();
           });
           var graph = new Rickshaw.Graph( {
               element: document.querySelector("#chart_$id"),
               renderer: 'line',
-              series: series 
+              series: all_series ,
+              height: 328
           });
           var x_axis = new Rickshaw.Graph.Axis.Time( { graph: graph } );
           var y_axis = new Rickshaw.Graph.Axis.Y( {

--- 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')}
+       ${rickshaw_graph(c.top_publishers_graph,'publishers')}
        <table class="table table-condensed table-bordered table-striped">
 	 <tr>
 	   <th>Publisher</th>