Skip to content

Commit 226f425

Browse files
authored
Merge pull request #930 from scitran/usage-500
Usage report updated for analyses as collections
2 parents 1532a58 + 1e63c37 commit 226f425

File tree

2 files changed

+23
-27
lines changed

2 files changed

+23
-27
lines changed

api/handlers/reporthandler.py

Lines changed: 9 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -872,25 +872,25 @@ def _build_project_report(self, base_query):
872872
report_obj = self._create_default(project=p)
873873

874874
# Grab sessions and their ids
875-
sessions = config.db.sessions.find({'project': p['_id']}, {'_id': 1, 'analyses':1})
875+
sessions = config.db.sessions.find({'project': p['_id']}, {'_id': 1})
876876
session_ids = [s['_id'] for s in sessions]
877877

878878
# Grab acquisitions and their ids
879-
acquisitions = config.db.acquisitions.find({'session': {'$in': session_ids}}, {'_id': 1, 'analyses':1})
879+
acquisitions = config.db.acquisitions.find({'session': {'$in': session_ids}}, {'_id': 1})
880880
acquisition_ids = [a['_id'] for a in acquisitions]
881881

882882
# For the project and each session and acquisition, create a list of analysis ids
883-
analysis_ids = [an['_id'] for an in p.get('analyses', [])]
884-
analysis_ids.extend([an['_id'] for an in s.get('analyses', []) for s in sessions])
885-
analysis_ids.extend([an['_id'] for an in a.get('analyses', []) for a in acquisitions])
886-
883+
parent_ids = session_ids + acquisition_ids + [p['_id']]
884+
analysis_ids = [an['_id'] for an in config.db.analyses.find({'parent.id': {'$in': parent_ids}})]
885+
887886
report_obj['session_count'] = len(session_ids)
888887

889888
# for each type of container below it will have a slightly modified match query
890889
cont_query = {
891890
'projects': {'_id': {'project': p['_id']}},
892891
'sessions': {'project': p['_id']},
893-
'acquisitions': {'session': {'$in': session_ids}}
892+
'acquisitions': {'session': {'$in': session_ids}},
893+
'analyses': {'parent.id' : {'$in':parent_ids}}
894894
}
895895

896896
# Create queries for files and analyses based on created date if a range was provided
@@ -901,32 +901,14 @@ def _build_project_report(self, base_query):
901901
file_q['files.created'] = base_query['created']
902902
analysis_q['analyses.created'] = base_query['created']
903903

904-
for cont_name in ['projects', 'sessions', 'acquisitions']:
904+
for cont_name in ['projects', 'sessions', 'acquisitions', 'analyses']:
905905

906906
# Aggregate file size in megabytes
907907
pipeline = [
908908
{'$match': cont_query[cont_name]},
909909
{'$unwind': '$files'},
910910
{'$match': file_q},
911-
{'$project': {'mbs': {'$divide': ['$files.size', BYTES_IN_MEGABYTE]}}},
912-
{'$group': {'_id': 1, 'mb_total': {'$sum':'$mbs'}}}
913-
]
914-
915-
try:
916-
result = self._get_result(config.db.command('aggregate', cont_name, pipeline=pipeline))
917-
except APIReportException:
918-
result = None
919-
920-
if result:
921-
report_obj['file_mbs'] += result['mb_total']
922-
923-
# Aggregate analysis file size in megabytes
924-
pipeline = [
925-
{'$match': cont_query[cont_name]},
926-
{'$unwind': '$analyses'},
927-
{'$unwind': '$analyses.files'},
928-
{'$match': analysis_q},
929-
{'$project': {'mbs': {'$divide': ['$analyses.files.size', BYTES_IN_MEGABYTE]}}},
911+
{'$project': {'mbs': {'$divide': [{'$cond': ['$files.input', 0, '$files.size']}, BYTES_IN_MEGABYTE]}}},
930912
{'$group': {'_id': 1, 'mb_total': {'$sum':'$mbs'}}}
931913
]
932914

test/integration_tests/python/test_reports.py

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -284,3 +284,17 @@ def test_usage_report(data_builder, file_form, as_user, as_admin):
284284
assert usage[0]['session_count'] == 1
285285
assert usage[0]['file_mbs'] > 0
286286
assert usage[0]['gear_execution_count'] == 1
287+
288+
# Test if empty project breaks Usage report
289+
group = data_builder.create_group()
290+
r = as_admin.post('/projects', params={'inherit': 'false'}, json={'label': 'project2', 'group': group})
291+
assert r.ok
292+
project = r.json()['_id']
293+
294+
# get project-aggregated usage report
295+
r = as_admin.get('/report/usage', params={'type': 'project'})
296+
assert r.ok
297+
298+
# delete project
299+
r= as_admin.delete('/projects/' + project)
300+
assert r.ok

0 commit comments

Comments
 (0)