allura-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From brond...@apache.org
Subject [2/2] git commit: [#7644] ticket:658 Pre-populate roles cache to reduce # of mongo queries
Date Mon, 22 Sep 2014 18:14:29 GMT
[#7644] ticket:658 Pre-populate roles cache to reduce # of mongo queries

Also fix users_with_named_role so that it would actually use cache.

Template for `/nf/new_projects/` displays admins for each project (and
there can be a lot of projects) and it generates a lot of mongo queries to
lookup user roles. Pre-populating the cache once helps to reduce query
count more than twice, and improves overall page rendering time.


Project: http://git-wip-us.apache.org/repos/asf/allura/repo
Commit: http://git-wip-us.apache.org/repos/asf/allura/commit/e3316fda
Tree: http://git-wip-us.apache.org/repos/asf/allura/tree/e3316fda
Diff: http://git-wip-us.apache.org/repos/asf/allura/diff/e3316fda

Branch: refs/heads/master
Commit: e3316fda268f5d1b6a049249ac67f45f88df1f3b
Parents: 8c803cd
Author: Igor Bondarenko <jetmind2@gmail.com>
Authored: Fri Sep 19 12:26:33 2014 +0300
Committer: Dave Brondsema <dbrondsema@slashdotmedia.com>
Committed: Mon Sep 22 18:14:15 2014 +0000

----------------------------------------------------------------------
 Allura/allura/controllers/site_admin.py | 7 +++++--
 Allura/allura/lib/security.py           | 2 +-
 2 files changed, 6 insertions(+), 3 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/allura/blob/e3316fda/Allura/allura/controllers/site_admin.py
----------------------------------------------------------------------
diff --git a/Allura/allura/controllers/site_admin.py b/Allura/allura/controllers/site_admin.py
index 755c405..8c56f9f 100644
--- a/Allura/allura/controllers/site_admin.py
+++ b/Allura/allura/controllers/site_admin.py
@@ -36,7 +36,7 @@ from allura.lib import validators as v
 from allura.lib.decorators import require_post
 from allura.lib.plugin import SiteAdminExtension, ProjectRegistrationProvider, AuthenticationProvider
 from allura.lib import search
-from allura.lib.security import require_access
+from allura.lib.security import require_access, Credentials
 from allura.lib.widgets import form_fields as ffw
 from allura.ext.admin.widgets import AuditLog
 from allura.lib.widgets import forms
@@ -184,7 +184,10 @@ class SiteAdminController(object):
             'neighborhood_id': {'$ne': nb._id},
             'deleted': False,
             '_id': {'$lt': start, '$gt': end},
-        }).sort('_id', -1))
+        }).sort('_id', -1)).all()
+        # pre-populate roles cache, so we won't query mongo for roles for every project
+        # when getting admins with p.admins() in a template
+        Credentials.get().load_project_roles(*[p._id for p in projects])
         step = start_dt - end_dt
         params = request.params.copy()
         params['start-dt'] = (start_dt + step).strftime('%Y/%m/%d %H:%M:%S')

http://git-wip-us.apache.org/repos/asf/allura/blob/e3316fda/Allura/allura/lib/security.py
----------------------------------------------------------------------
diff --git a/Allura/allura/lib/security.py b/Allura/allura/lib/security.py
index c966e34..ad7fd2a 100644
--- a/Allura/allura/lib/security.py
+++ b/Allura/allura/lib/security.py
@@ -147,7 +147,7 @@ class Credentials(object):
 
     def users_with_named_role(self, project_id, name):
         """ returns in sorted order """
-        role = RoleCache(self, self.project_role.find({'project_id': project_id, 'name':
name}))
+        role = RoleCache(self, [r for r in self.project_roles(project_id) if r['name'] ==
name])
         return sorted(role.users_that_reach, key=lambda u: u.username)
 
     def userids_with_named_role(self, project_id, name):


Mime
View raw message