Skip to content
Snippets Groups Projects
Commit eca51fa2 authored by Megan Henning's avatar Megan Henning
Browse files

Add non-compliant session count to stats

parent 6e0e398a
No related branches found
No related tags found
No related merge requests found
......@@ -174,7 +174,7 @@ routes = [
webapp2.Route(r'/api/projects/groups', containerhandler.ContainerHandler, handler_method='get_groups_with_project', methods=['GET']),
webapp2.Route(r'/api/projects/recalc', containerhandler.ContainerHandler, handler_method='calculate_project_compliance', methods=['POST']),
webapp2.Route(_format(r'/api/projects/<cid:{cid_re}>/template'), containerhandler.ContainerHandler, handler_method='set_project_template', methods=['POST']),
webapp2.Route(_format(r'/api/projects/<cid:{cid_re}>/template/recalc'), containerhandler.ContainerHandler, handler_method='calculate_project_compliance', methods=['POST']),
webapp2.Route(_format(r'/api/projects/<cid:{cid_re}>/recalc'), containerhandler.ContainerHandler, handler_method='calculate_project_compliance', methods=['POST']),
webapp2.Route(_format(r'/api/<par_cont_name:groups>/<par_id:{group_id_re}>/<cont_name:projects>'), containerhandler.ContainerHandler, name='cont_sublist_groups', handler_method='get_all', methods=['GET']),
webapp2.Route(_format(r'/api/<par_cont_name:{cont_name_re}>/<par_id:{cid_re}>/<cont_name:{cont_name_re}>'), containerhandler.ContainerHandler, name='cont_sublist', handler_method='get_all', methods=['GET']),
......
......@@ -194,7 +194,7 @@ class ProjectStorage(ContainerStorage):
# Recalc all projects
projects = self.get_all_el({'template': {'$exists': True}}, None, None)
else:
project = self.get_container(project_id, get_children=True)
project = self.get_container(project_id)
if project:
projects = [project]
else:
......@@ -204,10 +204,11 @@ class ProjectStorage(ContainerStorage):
for project in projects:
template = project.get('template',{})
if not template:
return
continue
else:
session_storage = SessionStorage()
for s in project.get('sessions', []):
sessions = session_storage.get_all_el({'project': project['_id']}, None, None)
for s in sessions:
changed = session_storage.recalc_session_compliance(s['_id'], session=s, template=template)
if changed:
changed_sessions.append(s['_id'])
......
......@@ -39,22 +39,46 @@ def add_id_to_subject(subject, pid):
def get_stats(cont, cont_type):
"""
Add a session and attachment count to a project or collection
Add a session, subject, non-compliant session and attachment count to a project or collection
"""
if cont_type not in ['projects', 'collections']:
return cont
session_ids = []
# Get attachment count from file array length
cont['attachment_count'] = len(cont.get('files', []))
# Get session and non-compliant session count
match_q = {}
if cont_type == 'projects':
result = list(config.db.sessions.find({'project': cont['_id']}, {'_id': 1}))
session_ids = [s['_id'] for s in result]
match_q = {'project': cont['_id']}
elif cont_type == 'collections':
result = config.db.acquisitions.find({'collections': cont['_id']}, {'session': 1})
session_ids = list(set([s['session'] for s in result]))
match_q = {'_id': {'$in': session_ids}}
pipeline = [
{'$match': match_q},
{'$project': {'_id': 1, 'non_compliant': {'$cond': [{'$eq': ['$satisfies_template', False]}, 1, 0]}}},
{'$group': {'_id': 1, 'noncompliant_count': {'$sum': '$non_compliant'}, 'total': {'$sum': 1}}}
]
result = config.db.command('aggregate', 'sessions', pipeline=pipeline).get('result', [])
if len(result) > 0:
cont['session_count'] = result[0].get('total', 0)
cont['noncompliant_session_count'] = result[0].get('noncompliant_count', 0)
else:
# If there are no sessions, return zero'd out stats
cont['session_count'] = 0
cont['noncompliant_session_count'] = 0
cont['subject_count'] = 0
return cont
# Get subject count
match_q['subject._id'] = {'$ne': None}
pipeline = [
{'$match': {'_id': {'$in': session_ids}, 'subject._id': {'$ne': None}}},
{'$match': match_q},
{'$group': {'_id': '$subject._id'}},
{'$group': {'_id': 1, 'count': { '$sum': 1 }}}
]
......@@ -66,9 +90,6 @@ def get_stats(cont, cont_type):
else:
cont['subject_count'] = 0
cont['attachment_count'] = len(cont.get('files', []))
cont['session_count'] = len(session_ids)
return cont
......
......@@ -507,6 +507,7 @@ class ContainerHandler(base.RequestHandler):
def calculate_project_compliance(self, **kwargs):
project_id = kwargs.pop('cid', None)
log.debug("project_id is {}".format(project_id))
self.config = self.container_handler_configurations['projects']
self.storage = self.config['storage']
return {'sessions_changed': self.storage.recalc_sessions_compliance(project_id=project_id)}
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment