From a3c35eb53dd6c8ed1d83d11dfc550a02904b40b5 Mon Sep 17 00:00:00 2001
From: Mario Raciti <mario.raciti@inaf.it>
Date: Thu, 20 May 2021 17:08:14 +0200
Subject: [PATCH] TMSS-610: Add comments waiting for the workflow status design

---
 .../src/tmss/tmssapp/adapters/reports.py      | 47 ++++++++++++-------
 1 file changed, 29 insertions(+), 18 deletions(-)

diff --git a/SAS/TMSS/backend/src/tmss/tmssapp/adapters/reports.py b/SAS/TMSS/backend/src/tmss/tmssapp/adapters/reports.py
index 6e52292e741..322aefabaf1 100644
--- a/SAS/TMSS/backend/src/tmss/tmssapp/adapters/reports.py
+++ b/SAS/TMSS/backend/src/tmss/tmssapp/adapters/reports.py
@@ -39,13 +39,15 @@ def _get_telescope_time_distribution(cycle: models.Cycle):
         projects = models.Project.objects.filter(cycles=cycle, project_category=c.value) if (c != 'UNASSIGNED' and c != 'FILLER') \
             else models.Project.objects.filter(cycles=cycle, filler=True) if c == 'FILLER' \
             else models.Project.objects.filter(cycles=cycle, project_category__isnull=True)
-        for p in projects:
-            # Get durations for single project and aggregate to get the totals
-            # TODO: Filter observations only.
-            _, durations = _get_subs_and_durations_from_project(p)
-            total += durations['total']
-            succeeded += durations['total_succeeded']
-            failed += durations['total_failed']
+        # TODO: Use QA workflow flag to get successful or failed SUBs, instead of SUBs' states.
+        #       At the moment just return some 0 placeholder values.
+        # for p in projects:
+        #     # Get durations for single project and aggregate to get the totals
+        #     # Note: We can filter observations by considering observed_end_time in the SUB, for now. See TMSS-610.
+        #     _, durations = _get_subs_and_durations_from_project(p)
+        #     total += durations['total']
+        #     succeeded += durations['total_succeeded']
+        #     failed += durations['total_failed']
         idle = total - succeeded - failed
         result[c if c == 'UNASSIGNED' or c == 'FILLER' else c.name] = {'durations': {'total': total, 'succeeded': succeeded,
                                                                        'failed': failed, 'idle': idle}}
@@ -84,7 +86,9 @@ def _get_average_efficiency(cycle: models.Cycle):
         total_succeeded_per_day = 0
         for s in subtasks:  # TODO: Handle the multiple days situations.
             total_per_day += s.duration.total_seconds() if s.start_time.date() in days and s.stop_time.date() in days else 0
-            total_succeeded_per_day += s.duration.total_seconds() if s.start_time.date() in days and s.stop_time.date() in days and s.state == 'finished' else 0
+            # TODO: Use QA workflow flag to get successful or failed SUBs, instead of SUBs' states.
+            #       At the moment just return some 0 placeholder values.
+            # total_succeeded_per_day += s.duration.total_seconds() if s.start_time.date() in days and s.stop_time.date() in days and s.state == 'finished' else 0
         result['days'].append({str(d): {'total': total_per_day, 'succeeded': total_succeeded_per_day}})
 
     return result
@@ -108,7 +112,9 @@ def _get_completion_level(cycle: models.Cycle):
     total_succeeded = 0
     for s in subtasks:
         total += s.duration.total_seconds()
-        total_succeeded += s.duration.total_seconds() if s.state == 'finished' else 0
+        # TODO: Use QA workflow flag to get successful SUBs, instead of SUBs' states.
+        #       At the moment just return some 0 placeholder values.
+        # total_succeeded += s.duration.total_seconds() if s.state == 'finished' else 0
     result['total'], result['succeeded'] = total, total_succeeded
 
     return result
@@ -126,10 +132,12 @@ def _get_observation_hours_per_category(cycle: models.Cycle):
         subs = models.SchedulingUnitBlueprint.objects.filter(draft__scheduling_set__project__cycles=cycle.pk).filter(priority_queue=prio.value)
         for sub in subs:
             result['total_duration_idle'] += sub.duration.total_seconds()
-            if sub.status == 'finished':    # TODO: Use QA workflow flag instead of the finished status
-                result[f'total_duration_{prio.name}'] += sub.duration.total_seconds()
-            if sub.status == 'cancelled':
-                result['total_duration_failed'] += sub.duration.total_seconds()
+            # TODO: Use QA workflow flag to get successful or failed SUBs, instead of SUBs' states.
+            #       At the moment just return some 0 placeholder values.
+            # if sub.status == 'finished':
+            #     result[f'total_duration_{prio.name}'] += sub.duration.total_seconds()
+            # if sub.status == 'error':
+            #     result['total_duration_failed'] += sub.duration.total_seconds()
         # Subtract prio states from total to get partial idle
         result['total_duration_idle'] -= result[f'total_duration_{prio.name}']
     # Subtract total failed to get total idle eventually
@@ -199,20 +207,23 @@ def _get_subs_and_durations_from_project(project_pk: int) -> ({}, {}):
     """
     # 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.
+    # TODO: Split into total, prio A, prio B. See TMSS-592.
     total_duration, total_succeeded_duration, total_failed_duration = timedelta(), timedelta(), timedelta()
     subs_succeeded, subs_failed = [], []
 
     # NOTE: This might be optimised later with the use of Django's ORM as done for LTA dataproducts.
     for sub in scheduling_unit_blueprints:  # Distinguish between succeeded and failed observations
-        # TODO: Use QA workflow flag instead of the finished status? See TMSS-592.
-        if sub.status == 'finished':        # Succeeded observations
+        # TODO: Use QA workflow flag to get successful or failed SUBs, instead of SUBs' states.
+        #       Cancelled SUBs are not failed SUBs. We need to adjust this once the QA workflow flag will be defined.
+        #       Also clarify if this info should be related only to obs or all SUBs in general. The latter are considered for now.
+        #       We can filter observations by considering observed_end_time in the SUB, for now. See TMSS-610 comments.
+        if sub.status == 'finished':        # Succeeded SUBs
             total_succeeded_duration += sub.duration
             subs_succeeded.append({'id': sub.pk, 'name': sub.name, 'duration': sub.duration.total_seconds()})
-        elif sub.status == 'cancelled':     # Failed observations
+        elif sub.status == 'cancelled':     # Failed SUBs
             total_failed_duration += sub.duration
             subs_failed.append({'id': sub.pk, 'name': sub.name, 'duration': sub.duration.total_seconds()})
-        total_duration += sub.duration      # Total duration without considering the status of the obs.
+        total_duration += sub.duration      # Total duration without considering the status of the SUBs.
 
     total_not_cancelled = total_duration - total_failed_duration  # Calculate not_cancelled duration
     durations = {'total': total_duration.total_seconds(), 'total_succeeded': total_succeeded_duration.total_seconds(),
-- 
GitLab