From 4a05ed7e0205c7849fc36e2610b1d2bd1fcaac43 Mon Sep 17 00:00:00 2001 From: Megan Henning <meganhenning@flywheel.io> Date: Mon, 10 Oct 2016 17:49:34 -0500 Subject: [PATCH] Add subject count --- api/dao/containerutil.py | 40 ++++++++++++++++++++++++---------------- 1 file changed, 24 insertions(+), 16 deletions(-) diff --git a/api/dao/containerutil.py b/api/dao/containerutil.py index 390b0fed..c79fc0f6 100644 --- a/api/dao/containerutil.py +++ b/api/dao/containerutil.py @@ -42,25 +42,33 @@ def get_stats(cont, cont_type): Add a session and attachment count to a project or collection """ + if cont_type not in ['projects', 'collections']: + return cont + + session_ids = [] if cont_type == 'projects': - cont['session_count'] = config.db.sessions.count({'project': cont['_id']}) - elif cont_type is 'collections': - pipeline = [ - {'$match': {'collections': cont['_id']}}, - {'$group': {'_id': '$session'}}, - {'$group': {'_id': 1, 'count': { '$sum': 1 }}} - ] - result = config.db.command('aggregate', 'acquisitions', pipeline=pipeline).get('result', []) - if len(result) > 0: - cont['session_count'] = result[0].get('count', 0) - else: - cont['session_count'] = 0 - - # session_ids = config.db.acquisitions.find({'collection': bson.ObjectId(cont['_id'])}. {'session': 1}) - # cont['session_count'] = len(set([s['session'] for s in session_ids])) + result = list(config.db.sessions.find({'project': cont['_id']}, {'_id': 1})) + session_ids = [s['_id'] for s in result] + elif cont_type == 'collections': + result = config.db.acquisitions.find({'collections': cont['_id']}, {'session': 1}) + session_ids = list(set([s['session'] for s in result])) + + pipeline = [ + {'$match': {'_id': {'$in': session_ids}, 'subject._id': {'$ne': None}}}, + {'$group': {'_id': '$subject._id'}}, + {'$group': {'_id': 1, 'count': { '$sum': 1 }}} + ] + + result = config.db.command('aggregate', 'sessions', pipeline=pipeline).get('result', []) + + if len(result) > 0: + cont['subject_count'] = result[0].get('count', 0) else: - return cont + cont['subject_count'] = 0 + cont['attachment_count'] = len(cont.get('files', [])) + cont['session_count'] = len(session_ids) + return cont -- GitLab