Add most active orgs ranking
Add most active orgs ranking

--- a/ckanext/dga_stats/controller.py
+++ b/ckanext/dga_stats/controller.py
@@ -17,6 +17,7 @@
         c.summary_stats = stats.summary_stats()
         c.activity_counts = stats.activity_counts()
         c.by_org = stats.by_org()
+        c.top_active_orgs = stats.top_active_orgs()
         c.user_access_list = stats.user_access_list()
         c.recent_datasets = stats.recent_datasets()
         c.new_packages_by_week = rev_stats.get_by_week('new_packages')

--- a/ckanext/dga_stats/stats.py
+++ b/ckanext/dga_stats/stats.py
@@ -75,6 +75,20 @@
 		group by package.owner_org,\"group\".name, package.private \
 		order by \"group\".name, package.private;").fetchall();
         res_groups = [(model.Session.query(model.Group).get(unicode(group_id)), private, val) for group_id, private, val in res]
+        return res_groups
+
+    @classmethod
+    def top_active_orgs(cls, limit=10):
+        connection = model.Session.connection()
+        res = connection.execute("select package.owner_org, count(*) from package \
+		inner join \"group\" on package.owner_org = \"group\".id \
+                inner join (select distinct object_id from activity where activity.timestamp > (now() - interval '60 day')) \
+                latestactivities on latestactivities.object_id = package.id \
+                where package.state='active' \
+                and package.private = 'f' \
+                group by package.owner_org \
+                order by count(*) desc;").fetchall();
+        res_groups = [(model.Session.query(model.Group).get(unicode(group_id)), val) for group_id, val in res]
         return res_groups
 
     @classmethod

--- a/ckanext/dga_stats/templates/ckanext/stats/index.html
+++ b/ckanext/dga_stats/templates/ckanext/stats/index.html
@@ -185,7 +185,7 @@
         <table class="table table-chunky table-bordered table-striped">
           <thead>
             <tr>
-              <th>{{ _('Group') }}</th>
+              <th>{{ _('Organisation') }}</th>
               <th>{{ _('Public/Archived') }}</th>
               <th class="metric">{{ _('Number of datasets') }}</th>
             </tr>
@@ -203,6 +203,29 @@
                 <td class="metric">{{ num_packages }}</td>
               </tr>
 		{% endif %}
+            {% endfor %}
+          </tbody>
+        </table>
+      {% else %}
+        <p class="empty">{{ _('No groups') }}</p>
+      {% endif %}
+    </section>
+    <section id="stats-activity-org" class="module-content tab-content">
+      <h2>{{ _('Most Active Organisations') }}</h2>
+      {% if c.top_active_orgs %}
+        <table class="table table-chunky table-bordered table-striped">
+          <thead>
+            <tr>
+              <th>{{ _('Organisation') }}</th>
+              <th class="metric">{{ _('Number of datasets updated recently') }}</th>
+            </tr>
+          </thead>
+          <tbody>
+            {% for group, num_packages in c.top_active_orgs %}
+              <tr>
+                <td>{{ h.link_to(group.title or group.name, h.url_for(controller='organization', action='read', id=group.name)) }}</td>
+                <td class="metric">{{ num_packages }}</td>
+              </tr>
             {% endfor %}
           </tbody>
         </table>