allura-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From john...@apache.org
Subject [1/3] git commit: [#5465] ticket:310 Ticket's labels autocompletion
Date Wed, 10 Apr 2013 22:27:30 GMT
Updated Branches:
  refs/heads/master 06a70ab32 -> e62d190be


[#5465] ticket:310 Ticket's labels autocompletion

Added query to retrieve labels for tracker sorted by usage frequency
and filtered by user input using mongo aggregation framework.

Needs further performance testing, second '$match' clause won't touch the index
(because it uses case insensitive regex match and takes place after '$unwind'),
but it's best I can do with such data layout.

Also simplified the test, because mim doesn't support aggregation queries.


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

Branch: refs/heads/master
Commit: e62d190beca2cdfa213a4edd283fcbf016953b34
Parents: 828b29f
Author: Igor Bondarenko <jetmind2@gmail.com>
Authored: Wed Apr 10 16:06:19 2013 +0000
Committer: Cory Johns <cjohns@slashdotmedia.com>
Committed: Wed Apr 10 22:00:17 2013 +0000

----------------------------------------------------------------------
 .../forgetracker/tests/functional/test_root.py     |    9 +----
 ForgeTracker/forgetracker/tracker_main.py          |   28 ++++++++++++--
 2 files changed, 26 insertions(+), 11 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/incubator-allura/blob/e62d190b/ForgeTracker/forgetracker/tests/functional/test_root.py
----------------------------------------------------------------------
diff --git a/ForgeTracker/forgetracker/tests/functional/test_root.py b/ForgeTracker/forgetracker/tests/functional/test_root.py
index 917b064..ed03410 100644
--- a/ForgeTracker/forgetracker/tests/functional/test_root.py
+++ b/ForgeTracker/forgetracker/tests/functional/test_root.py
@@ -1441,13 +1441,8 @@ class TestFunctionalController(TrackerTestController):
         self.new_ticket(summary='a', labels='tag1,tag2')
         self.new_ticket(summary='b', labels='tag2')
         self.new_ticket(summary='c', labels='42cc,test')
-        r = self.app.get('/p/test/bugs/tags?term=t')
-        assert_equal(json.loads(r.body), ['tag2', 'tag1', 'test'])
-        r = self.app.get('/p/test/bugs/tags?term=ta')
-        assert_equal(json.loads(r.body), ['tag2', 'tag1'])
-        r = self.app.get('/p/test/bugs/tags?term=te')
-        assert_equal(json.loads(r.body), ['test'])
-        r = self.app.get('/p/test/bugs/tags?term=nope')
+        # Testing only empty 'term', because mim doesn't support aggregation calls
+        r = self.app.get('/p/test/bugs/tags')
         assert_equal(json.loads(r.body), [])
         r = self.app.get('/p/test/bugs/tags?term=')
         assert_equal(json.loads(r.body), [])

http://git-wip-us.apache.org/repos/asf/incubator-allura/blob/e62d190b/ForgeTracker/forgetracker/tracker_main.py
----------------------------------------------------------------------
diff --git a/ForgeTracker/forgetracker/tracker_main.py b/ForgeTracker/forgetracker/tracker_main.py
index e615b17..f62ae03 100644
--- a/ForgeTracker/forgetracker/tracker_main.py
+++ b/ForgeTracker/forgetracker/tracker_main.py
@@ -6,7 +6,7 @@ from urllib import urlencode, unquote
 from urllib2 import urlopen
 from webob import exc
 import json
-from itertools import ifilter
+from itertools import ifilter, imap
 
 # Non-stdlib imports
 import pkg_resources
@@ -17,6 +17,7 @@ from pylons import tmpl_context as c, app_globals as g
 from pylons import request, response
 from formencode import validators
 from bson import ObjectId
+from bson.son import SON
 from bson.errors import InvalidId
 from webhelpers import feedgenerator as FG
 
@@ -482,9 +483,28 @@ class RootController(BaseController):
         return {'milestone_counts': milestone_counts}
 
     @expose('json:')
-    def tags(self, term, **kw):
-        tags = ['sad', 'happy']
-        return json.dumps(tags)
+    def tags(self, term=None, **kw):
+        if not term:
+            return json.dumps([])
+        db = M.session.project_doc_session.db
+        tickets = db[TM.Ticket.__mongometa__.name]
+        tags = tickets.aggregate([
+            {
+                '$match': {
+                    'app_config_id': c.app.config._id,
+                    'labels': {
+                        '$exists': True,
+                        '$ne': [],
+                    }
+                }
+            },
+            {'$project': {'labels': 1}},
+            {'$unwind': '$labels'},
+            {'$match': {'labels': {'$regex': '^%s' % term, '$options': 'i'}}},
+            {'$group': { '_id': '$labels', 'count': {'$sum': 1}}},
+            {'$sort': SON([('count', -1), ('_id', 1)])}
+        ])
+        return json.dumps([tag['_id'] for tag in tags.get('result', [])])
 
     @with_trailing_slash
     @h.vardec


Mime
View raw message