Add organisation public/private dataset count page
[ckanext-dga-stats.git] / ckanext / dga_stats / stats.py
blob:a/ckanext/dga_stats/stats.py -> blob:b/ckanext/dga_stats/stats.py
--- a/ckanext/dga_stats/stats.py
+++ b/ckanext/dga_stats/stats.py
@@ -2,6 +2,7 @@
 
 from pylons import config
 from sqlalchemy import Table, select, func, and_
+from sqlalchemy.sql.expression import text
 
 import ckan.plugins as p
 import ckan.model as model
@@ -122,7 +123,6 @@
     @classmethod
     def summary_stats(cls):
        connection = model.Session.connection()
-#				select name,role from user_object_role inner join \"user\" on user_object_role.user_id = \"user\".id where name not in ('logged_in','visitor') group by name,role"
 
        res = connection.execute("SELECT 'Total Organisations', count(*) from \"group\" where type = 'organization' and state = 'active' union \
 				select 'Total Datasets', count(*) from package where state='active' or state='draft' or state='draft-complete' union \
@@ -135,6 +135,22 @@
        connection = model.Session.connection()
        res = connection.execute("select to_char(timestamp, 'YYYY-MM') as month,activity_type, count(*) from activity group by month, activity_type order by month;").fetchall();
        return res
+
+    @classmethod
+    def user_access_list(cls):
+       connection = model.Session.connection()
+       res = connection.execute("select name,sysadmin,role from user_object_role right outer join \"user\" on user_object_role.user_id = \"user\".id where name not in ('logged_in','visitor') group by name,sysadmin,role order by sysadmin desc, role asc;").fetchall();
+       return res
+
+    @classmethod
+    def recent_datasets(cls):
+        activity = table('activity')
+        package = table('package')
+        s = select([func.max(activity.c.timestamp),package.c.id, activity.c.activity_type], from_obj=[activity.join(package,activity.c.object_id == package.c.id)]).where(package.c.private == 'f').\
+            where(activity.c.timestamp > func.now() - text("interval '60 day'")).group_by(package.c.id,activity.c.activity_type).order_by(func.max(activity.c.timestamp))
+        result = model.Session.execute(s).fetchall()
+	return [(datetime2date(timestamp), model.Session.query(model.Package).get(unicode(package_id)), activity_type) for timestamp,package_id,activity_type in result]
+
 
 
 class RevisionStats(object):
@@ -205,6 +221,7 @@
             # be 'for all time' else you get first revision in the time period.
             package_revision = table('package_revision')
             revision = table('revision')
+            package = table('package')
             s = select([package_revision.c.id, func.min(revision.c.timestamp)], from_obj=[package_revision.join(revision)]).\
 	      where(package.c.private == 'f').\
 	      group_by(package_revision.c.id).order_by(func.min(revision.c.timestamp))