diff --git a/SAS/TMSS/backend/src/tmss/tmssapp/adapters/reports.py b/SAS/TMSS/backend/src/tmss/tmssapp/adapters/reports.py index d542b102d92075e5798654d0cff473810f62093b..6c4e8f3bb4dfdf0d59fb16af27d75e9d8ad7e990 100644 --- a/SAS/TMSS/backend/src/tmss/tmssapp/adapters/reports.py +++ b/SAS/TMSS/backend/src/tmss/tmssapp/adapters/reports.py @@ -8,6 +8,32 @@ import logging logger = logging.getLogger(__name__) +def _get_subs_and_durations_from_project(project_pk: int) -> {}: + """ + Help function to retrieve durations and scheduling_units distinguished by success/fail. + """ + # Get SUBs related to the project + scheduling_unit_blueprints = models.SchedulingUnitBlueprint.objects.filter(draft__scheduling_set__project__pk=project_pk) + # TODO: Split into total, prio A, prio B? See TMSS-592. + total_duration, total_succeeded_duration, total_not_cancelled, total_failed_duration = timedelta(), timedelta(), timedelta(), timedelta() + subs_succeeded, subs_failed = [], [] + + for sub in scheduling_unit_blueprints: # Distinguish between succeeded and failed observations + if sub.status == 'finished': # Succeeded observations + total_succeeded_duration += sub.duration + subs_succeeded.append({'id': sub.pk, 'name': sub.name, 'duration': sub.duration}) + elif sub.status == 'cancelled': # Failed observations + total_failed_duration += sub.duration + subs_failed.append({'id': sub.pk, 'name': sub.name, 'duration': sub.duration}) + total_duration += sub.duration # Total duration without considering the status of the obs. + + total_not_cancelled = total_duration-total_failed_duration # Calculate not_cancelled duration + durations = {'total': total_duration, 'total_succeeded': total_succeeded_duration, 'total_not_cancelled': total_not_cancelled, + 'total_failed': total_failed_duration, 'scheduling_unit_blueprints_finished': subs_succeeded, + 'scheduling_unit_blueprints_failed': subs_failed} + return durations + + def create_project_report(request: Request, project: models.Project) -> {}: # TODO: Retrieve the information needed, all in one go. project_pk = project.pk @@ -19,28 +45,6 @@ def create_project_report(request: Request, project: models.Project) -> {}: result['quota'] = [{k: project_quota_data[k] for k in ('id', 'resource_type_id', 'value')}, ] # Add durations to result - # TODO: Refactoring, choose better structure for durations result. - # Get SUBs related to this project - scheduling_unit_blueprints = models.SchedulingUnitBlueprint.objects.filter(draft__scheduling_set__project__pk=project_pk) - subs_succeeded = [] - subs_failed = [] - total_duration = timedelta() # TODO: Split into total, prio A, prio B? See TMSS-592. - total_succeeded_duration = timedelta() - total_failed_duration = timedelta() - for sub in scheduling_unit_blueprints: # Distinguish between succeeded and failed observations - if sub.status != 'cancelled': # Observations that have ran or will still run - if sub.status == 'finished': # Succeeded observations - total_succeeded_duration += sub.duration - subs_succeeded.append({'scheduling_unit_blueprint': sub.name, 'duration': sub.duration}) - else: - total_duration += sub.duration - elif sub.status == 'cancelled': # Failed observations - total_failed_duration += sub.duration - subs_succeeded.append({'scheduling_unit_blueprint': sub.name, 'duration': sub.duration}) - # Calculate total duration and build results - total_duration = (total_duration + total_succeeded_duration + total_failed_duration) - result['durations'] = {'total': total_duration, - 'total_succeeded': total_succeeded_duration, 'total_failed': total_failed_duration, - 'subs_finished': subs_succeeded, 'subs_failed': subs_failed} + result['durations'] = _get_subs_and_durations_from_project(project_pk) return result