diff --git a/api/dao/containerutil.py b/api/dao/containerutil.py index 619caac13..6cebbd0f9 100644 --- a/api/dao/containerutil.py +++ b/api/dao/containerutil.py @@ -8,12 +8,15 @@ CONT_TYPES = ['acquisition', 'analysis', 'collection', 'group', 'project', 'session'] SINGULAR_TO_PLURAL = { + 'acquisition': 'acquisitions', + 'analysis': 'analyses', + 'collection': 'collections', + 'device': 'devices', 'group': 'groups', + 'job': 'jobs', 'project': 'projects', 'session': 'sessions', - 'acquisition': 'acquisitions', - 'collection': 'collections', - 'analysis': 'analyses', + 'user': 'users', } 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..32d4a6e5a 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(match_ids, results) + # Ensure job uniqueness seen_jobs = [] seen_gears = []