From c7b1ade6b7e454c2c9ea2a37bcbcda33acde930d Mon Sep 17 00:00:00 2001 From: Nathaniel Kofalt Date: Thu, 26 Oct 2017 11:04:41 -0500 Subject: [PATCH 1/3] Fix some null checks, and add a unique Queue.search filter --- api/dao/containerutil.py | 8 +++++--- api/handlers/containerhandler.py | 15 +++++++++++++-- 2 files changed, 18 insertions(+), 5 deletions(-) diff --git a/api/dao/containerutil.py b/api/dao/containerutil.py index 619caac13..03169d9f3 100644 --- a/api/dao/containerutil.py +++ b/api/dao/containerutil.py @@ -8,12 +8,14 @@ CONT_TYPES = ['acquisition', 'analysis', 'collection', 'group', 'project', 'session'] SINGULAR_TO_PLURAL = { + 'acquisition': 'acquisitions', + 'analysis': 'analyses', + 'collection': 'collections', + 'device': 'device', 'group': 'groups', + 'job': 'job', 'project': 'projects', 'session': 'sessions', - 'acquisition': 'acquisitions', - 'collection': 'collections', - 'analysis': 'analyses', } PLURAL_TO_SINGULAR = {p: s for s, p in SINGULAR_TO_PLURAL.iteritems()} diff --git a/api/handlers/containerhandler.py b/api/handlers/containerhandler.py index 2689533ab..3439ae1cc 100644 --- a/api/handlers/containerhandler.py +++ b/api/handlers/containerhandler.py @@ -162,10 +162,11 @@ def handle_origin(self, result): # Join from database if we haven't for this origin before if j_type != 'unknown' and result['join-origin'][j_type].get(j_id, None) is None: # Initial join - join_doc = config.db[j_type + 's'].find_one({'_id': j_id_b}) + j_types = containerutil.pluralize(j_type) + join_doc = config.db[j_types].find_one({'_id': j_id_b}) # Join in gear name on the job doc if requested - if join_gear_name and j_type == 'job': + if join_doc is not None and join_gear_name and j_type == 'job': gear_id = join_doc['gear_id'] gear_name = None @@ -267,6 +268,16 @@ def get_jobs(self, cid): cont_array = [containerutil.ContainerReference('analysis', cid) for cid in id_array] results += Queue.search(cont_array, states=states, tags=tags) + # Stateful closure to remove search duplicates + # Eventually, this code should not call Queue.search and should instead do its own work. + def match_ids(x): + should_add = str(x['_id']) in match_ids.unique_job_ids + match_ids.unique_job_ids.add(str(x['_id'])) + return should_add + + match_ids.unique_job_ids = set() + results = filter(lambda x: match_ids(x), results) + # Ensure job uniqueness seen_jobs = [] seen_gears = [] From ae78c4ac81d2302cd1fa4281fff358005fed08dd Mon Sep 17 00:00:00 2001 From: Nathaniel Kofalt Date: Thu, 26 Oct 2017 11:58:05 -0500 Subject: [PATCH 2/3] Pylint is a curious beast --- api/handlers/containerhandler.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/api/handlers/containerhandler.py b/api/handlers/containerhandler.py index 3439ae1cc..32d4a6e5a 100644 --- a/api/handlers/containerhandler.py +++ b/api/handlers/containerhandler.py @@ -276,7 +276,7 @@ def match_ids(x): return should_add match_ids.unique_job_ids = set() - results = filter(lambda x: match_ids(x), results) + results = filter(match_ids, results) # Ensure job uniqueness seen_jobs = [] From cadcbf1f58767ffd6d342204297137446f89d52c Mon Sep 17 00:00:00 2001 From: Nathaniel Kofalt Date: Thu, 26 Oct 2017 16:45:32 -0500 Subject: [PATCH 3/3] Correct plural map --- api/dao/containerutil.py | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/api/dao/containerutil.py b/api/dao/containerutil.py index 03169d9f3..6cebbd0f9 100644 --- a/api/dao/containerutil.py +++ b/api/dao/containerutil.py @@ -11,11 +11,12 @@ 'acquisition': 'acquisitions', 'analysis': 'analyses', 'collection': 'collections', - 'device': 'device', + 'device': 'devices', 'group': 'groups', - 'job': 'job', + 'job': 'jobs', 'project': 'projects', 'session': 'sessions', + 'user': 'users', } PLURAL_TO_SINGULAR = {p: s for s, p in SINGULAR_TO_PLURAL.iteritems()}