Skip to content
Snippets Groups Projects
Commit ba2f2cfb authored by Mario Raciti's avatar Mario Raciti
Browse files

TMSS-692: Fix logic for durations; refactoring

parent e2cd3490
No related branches found
No related tags found
1 merge request!410Resolve TMSS-692
...@@ -8,6 +8,32 @@ import logging ...@@ -8,6 +8,32 @@ import logging
logger = logging.getLogger(__name__) 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) -> {}: def create_project_report(request: Request, project: models.Project) -> {}:
# TODO: Retrieve the information needed, all in one go. # TODO: Retrieve the information needed, all in one go.
project_pk = project.pk project_pk = project.pk
...@@ -19,28 +45,6 @@ def create_project_report(request: Request, project: models.Project) -> {}: ...@@ -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')}, ] result['quota'] = [{k: project_quota_data[k] for k in ('id', 'resource_type_id', 'value')}, ]
# Add durations to result # Add durations to result
# TODO: Refactoring, choose better structure for durations result. result['durations'] = _get_subs_and_durations_from_project(project_pk)
# 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}
return result return result
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