@@ -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
0 commit comments