Skip to content

Commit

Permalink
Add non-compliant session count to stats
Browse files Browse the repository at this point in the history
  • Loading branch information
nagem committed Oct 12, 2016
1 parent 6e0e398 commit eca51fa
Show file tree
Hide file tree
Showing 4 changed files with 35 additions and 12 deletions.
2 changes: 1 addition & 1 deletion api/api.py
Original file line number Diff line number Diff line change
Expand Up @@ -174,7 +174,7 @@ def _format(route):
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']),
Expand Down
7 changes: 4 additions & 3 deletions api/dao/containerstorage.py
Original file line number Diff line number Diff line change
Expand Up @@ -194,7 +194,7 @@ def recalc_sessions_compliance(self, project_id=None):
# 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:
Expand All @@ -204,10 +204,11 @@ def recalc_sessions_compliance(self, project_id=None):
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'])
Expand Down
37 changes: 29 additions & 8 deletions api/dao/containerutil.py
Original file line number Diff line number Diff line change
Expand Up @@ -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 }}}
]
Expand All @@ -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


Expand Down
1 change: 1 addition & 0 deletions api/handlers/containerhandler.py
Original file line number Diff line number Diff line change
Expand Up @@ -507,6 +507,7 @@ def set_project_template(self, **kwargs):

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)}
Expand Down

0 comments on commit eca51fa

Please sign in to comment.