add resources by organization
add resources by organization

--- 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.res_by_org = stats.res_by_org()
         c.top_active_orgs = stats.top_active_orgs()
         c.user_access_list = stats.user_access_list()
         c.recent_datasets = stats.recent_datasets()

--- a/ckanext/dga_stats/stats.py
+++ b/ckanext/dga_stats/stats.py
@@ -6,6 +6,8 @@
 
 import ckan.plugins as p
 import ckan.model as model
+
+import re
 
 cache_enabled = p.toolkit.asbool(config.get('ckanext.stats.cache_enabled', 'True'))
 
@@ -76,6 +78,30 @@
 		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 res_by_org(cls, limit=10):
+        connection = model.Session.connection()
+        reses = connection.execute("select owner_org,format,count(*) from \
+		resource inner join resource_group on resource.resource_group_id = resource_group.id \
+		inner join package on resource_group.package_id = package.id group by owner_org,format order by count desc;").fetchall();
+	group_ids = []
+	group_tab = {}
+	group_spatial = {}
+	group_other = {}
+        for group_id,format,count in reses:
+		if group_id not in group_ids:
+			group_ids.append(group_id) 
+			group_tab[group_id] = 0
+			group_spatial[group_id] = 0 
+			group_other[group_id] = 0
+		if re.search('xls|csv|ms-excel|spreadsheetml.sheet|zip|netcdf',format, re.IGNORECASE):
+			group_tab[group_id] = group_tab[group_id] + count
+		elif re.search('wms|wfs|wcs|shp|kml|kmz',format, re.IGNORECASE):
+			group_spatial[group_id] = group_spatial[group_id] + count
+		else:
+			group_other[group_id] = group_other[group_id] + count
+	return [(model.Session.query(model.Group).get(unicode(group_id)), group_tab[group_id],group_spatial[group_id],group_other[group_id], group_tab[group_id]+group_spatial[group_id]+group_other[group_id]) for group_id in group_ids]
 
     @classmethod
     def top_active_orgs(cls, limit=10):

--- a/ckanext/dga_stats/templates/ckanext/stats/index.html
+++ b/ckanext/dga_stats/templates/ckanext/stats/index.html
@@ -203,6 +203,35 @@
                 <td class="metric">{{ num_packages }}</td>
               </tr>
 		{% endif %}
+            {% endfor %}
+          </tbody>
+        </table>
+      {% else %}
+        <p class="empty">{{ _('No groups') }}</p>
+      {% endif %}
+    </section>
+    <section id="stats-res-by-org" class="module-content tab-content">
+      <h2>{{ _('Resources by Organization') }}</h2>
+      {% if c.res_by_org %}
+        <table class="table table-chunky table-bordered table-striped">
+          <thead>
+            <tr>
+              <th>{{ _('Organisation') }}</th>
+              <th>{{ _('Tabular') }}</th>
+              <th>{{ _('Spatial') }}</th>
+              <th>{{ _('Other') }}</th>
+              <th class="metric">{{ _('Total') }}</th>
+            </tr>
+          </thead>
+          <tbody>
+            {% for group,t,s,o,tot in c.res_by_org %}
+              <tr>
+                <td>{{ h.link_to(group.title or group.name, h.url_for(controller='organization', action='read', id=group.name)) }}</td>
+                <td>{{ t }}</td>
+                <td>{{ s }}</td>
+                <td>{{ o }}</td>
+                <td class="metric">{{ tot }}</td>
+              </tr>
             {% endfor %}
           </tbody>
         </table>
@@ -274,6 +303,7 @@
         <li class="nav-item"><a href="#stats-dataset-revisions" data-toggle="tab">{{ _('Dataset Revisions per Week') }}</a></li>
         <li class="nav-item"><a href="#stats-most-edited" data-toggle="tab">{{ _('Most Edited Datasets') }}</a></li>
         <li class="nav-item"><a href="#stats-by-org" data-toggle="tab">{{ _('Datasets by Organization') }}</a></li> 
+        <li class="nav-item"><a href="#stats-res-by-org" data-toggle="tab">{{ _('Resources by Organization') }}</a></li> 
         <li class="nav-item active"><a href="#stats-summary" data-toggle="tab">{{ _('Summary') }}</a></li> 
       </ul>
     </nav>