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

TMSS-610: Fix logic and refactoring

parent 14cc4364
No related branches found
No related tags found
3 merge requests!634WIP: COBALT commissioning delta,!492Resolve TMSS-610,!481Draft: SW-971 SW-973 SW-975: Various fixes to build LOFAR correctly.
...@@ -5,6 +5,7 @@ from lofar.sas.tmss.tmss.tmssapp import serializers ...@@ -5,6 +5,7 @@ from lofar.sas.tmss.tmss.tmssapp import serializers
from rest_framework.request import Request from rest_framework.request import Request
from datetime import timedelta from datetime import timedelta
# Cycle Report
def create_cycle_report(request: Request, cycle: models.Cycle) -> {}: def create_cycle_report(request: Request, cycle: models.Cycle) -> {}:
""" """
...@@ -26,32 +27,35 @@ def create_cycle_report(request: Request, cycle: models.Cycle) -> {}: ...@@ -26,32 +27,35 @@ def create_cycle_report(request: Request, cycle: models.Cycle) -> {}:
def _get_telescope_time_distribution(cycle: models.Cycle): def _get_telescope_time_distribution(cycle: models.Cycle):
# TODO: Check if these are the states we want to include. """
categories = models.ProjectCategory.Choices Help function to retrieve telescope time distribution info.
"""
result = {} result = {}
# ProjectCategories # ProjectCategories
for c in categories: for c in models.ProjectCategory.Choices:
total, succeeded, failed = 0, 0, 0
projects = models.Project.objects.filter(cycles=cycle, project_category=c.value) projects = models.Project.objects.filter(cycles=cycle, project_category=c.value)
succeeded, failed = 0, 0
for p in projects: for p in projects:
subs = models.SchedulingUnitBlueprint.objects.filter(draft__scheduling_set__project__pk=p.pk) # Get durations for single project and aggregate to get the totals
for sub in subs: _, durations = _get_subs_and_durations_from_project(p)
succeeded += sub.status if sub.status == 'finished' else 0 total += durations['total']
failed += sub.status if sub.status == 'cancelled' else 0 succeeded += durations['total_succeeded']
# TODO: Calculate idle. failed += durations['total_failed']
result[c.name] = {'durations': {'succeeded': succeeded, 'failed': failed, 'idle': 0}} idle = total - succeeded - failed
result[c.name] = {'durations': {'total': total, 'succeeded': succeeded, 'failed': failed, 'idle': idle}}
# Filler # Filler
succeeded, failed = 0, 0 total, succeeded, failed = 0, 0, 0
projects = models.Project.objects.filter(cycles=cycle, filler=True) projects = models.Project.objects.filter(cycles=cycle, filler=True)
for p in projects: for p in projects:
subs = models.SchedulingUnitBlueprint.objects.filter(draft__scheduling_set__project__pk=p.pk) # Get durations for single project and aggregate to get the totals
for sub in subs: _, durations = _get_subs_and_durations_from_project(p)
succeeded += sub.status if sub.status == 'finished' else 0 total += durations['total']
failed += sub.status if sub.status == 'cancelled' else 0 succeeded += durations['total_succeeded']
# TODO: Calculate idle. failed += durations['total_failed']
result['FILLER'] = {'durations': {'succeeded': succeeded, 'failed': failed, 'idle': 0}} idle = total - succeeded - failed
result['FILLER'] = {'durations': {'total': total, 'succeeded': succeeded, 'failed': failed, 'idle': idle}}
return result return result
...@@ -65,32 +69,26 @@ def _get_completion_level(cycle: models.Cycle): ...@@ -65,32 +69,26 @@ def _get_completion_level(cycle: models.Cycle):
def _get_observation_hours_per_category(cycle: models.Cycle): def _get_observation_hours_per_category(cycle: models.Cycle):
"""
Help function to retrieve observation hours per category info.
"""
# TODO: Better define system unavailability and idle values. # TODO: Better define system unavailability and idle values.
result = {} result = {'total_duration_failed': 0, 'total_duration_idle': 0}
result['total_duration_A'], result['total_duration_B'], result['total_duration_failed'], result['total_duration_idle'] \
= timedelta(), timedelta(), timedelta(), timedelta() # Filter durations for each prio. basing on SUBs states
for prio in models.PriorityQueueType.Choices:
# Get SUBs within prio A result[f'total_duration_{prio.name}'] = 0
subs_A = models.SchedulingUnitBlueprint.objects.filter(draft__scheduling_set__project__cycles=cycle.pk).filter(priority_queue='A') subs = models.SchedulingUnitBlueprint.objects.filter(draft__scheduling_set__project__cycles=cycle.pk).filter(priority_queue=prio.value)
for sub in subs_A: for sub in subs:
if sub.status == 'finished': result['total_duration_idle'] += sub.duration.total_seconds()
result['total_duration_A'] += sub.duration if sub.status == 'finished':
if sub.status == 'cancelled': result[f'total_duration_{prio.name}'] += sub.duration.total_seconds()
result['total_duration_failed'] += sub.duration if sub.status == 'cancelled':
result['total_duration_idle'] += sub.duration # TODO: Need to subtract other states. result['total_duration_failed'] += sub.duration.total_seconds()
# Get SUBs within prio B # Subtract prio states from total to get partial idle
subs_B = models.SchedulingUnitBlueprint.objects.filter(draft__scheduling_set__project__cycles=cycle.pk).filter(priority_queue='B') result['total_duration_idle'] -= result[f'total_duration_{prio.name}']
for sub in subs_B: # Subtract total failed to get total idle eventually
if sub.status == 'finished': result['total_duration_idle'] -= result['total_duration_failed']
result['total_duration_B'] += sub.duration
if sub.status == 'cancelled':
result['total_duration_failed'] += sub.duration
result['total_duration_idle'] += sub.duration # TODO: Need to subtract other states.
result['total_duration_A'] = result['total_duration_A'].total_seconds()
result['total_duration_B'] = result['total_duration_B'].total_seconds()
result['total_duration_failed'] = result['total_duration_failed'].total_seconds()
result['total_duration_idle'] = result['total_duration_idle'].total_seconds()
return result return result
...@@ -104,9 +102,9 @@ def _get_data_ingested_per_site_and_category(cycle: models.Cycle): ...@@ -104,9 +102,9 @@ def _get_data_ingested_per_site_and_category(cycle: models.Cycle):
def _get_projects_summary(request: Request, cycle: models.Cycle): def _get_projects_summary(request: Request, cycle: models.Cycle):
''' """
Help function to retrieve projects info. Help function to retrieve projects info.
''' """
projects_summary = [] projects_summary = []
# Get Projects related to the Cycle # Get Projects related to the Cycle
projects = models.Project.objects.filter(cycles=cycle) projects = models.Project.objects.filter(cycles=cycle)
...@@ -125,6 +123,8 @@ def _get_failures(cycle: models.Cycle): ...@@ -125,6 +123,8 @@ def _get_failures(cycle: models.Cycle):
return 0 return 0
#Project Report
def create_project_report(request: Request, project: models.Project) -> {}: def create_project_report(request: Request, project: models.Project) -> {}:
""" """
Create a project report as a JSON object. Create a project report as a JSON object.
......
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