From a832fa2ba8f841f5162e6a712d3e6d3aa9a45d03 Mon Sep 17 00:00:00 2001
From: Jorrit Schaap <schaap@astron.nl>
Date: Fri, 8 Oct 2021 15:30:01 +0200
Subject: [PATCH] TMSS-1066: fix for scheduling the combined observation. We'll
 need a better and single way of getting the duration of a generic task,
 normal observation task or combined observation task. Let's fo that in
 TMSS-671 (dynamic scheduling)

---
 .../constraints/template_constraints_v1.py    | 21 ++++++++++---------
 .../src/tmss/tmssapp/models/specification.py  |  4 ++--
 2 files changed, 13 insertions(+), 12 deletions(-)

diff --git a/SAS/TMSS/backend/services/scheduling/lib/constraints/template_constraints_v1.py b/SAS/TMSS/backend/services/scheduling/lib/constraints/template_constraints_v1.py
index ea361a875c1..29104798404 100644
--- a/SAS/TMSS/backend/services/scheduling/lib/constraints/template_constraints_v1.py
+++ b/SAS/TMSS/backend/services/scheduling/lib/constraints/template_constraints_v1.py
@@ -242,16 +242,17 @@ def can_run_within_timewindow_with_sky_constraints(scheduling_unit: models.Sched
     Checks whether it is possible to run the scheduling unit /somewhere/ in the given time window, considering the duration of the involved observation.
     :return: True if there is at least one possibility to place the scheduling unit in a way that all sky constraints are met over the runtime of the observation, else False.
     """
-    for task in scheduling_unit.specifications_doc['tasks'].values():
-        if 'specifications_doc' in task:
-            if 'duration' in task['specifications_doc']:
-                duration = timedelta(seconds=task['specifications_doc']['duration'])
-                window_lower_bound = lower_bound
-                while window_lower_bound + duration <= upper_bound:
-                    window_upper_bound = window_lower_bound + duration
-                    if can_run_anywhere_within_timewindow_with_sky_constraints(scheduling_unit, window_lower_bound, window_upper_bound):
-                        return True
-                    window_lower_bound += min(timedelta(hours=1), upper_bound - window_lower_bound)
+    main_observation_task_name = get_longest_observation_task_name_from_specifications_doc(scheduling_unit)
+    main_obs_task_spec = scheduling_unit.specifications_doc['tasks'][main_observation_task_name]['specifications_doc']
+    duration = timedelta(seconds=main_obs_task_spec.get('duration', main_obs_task_spec.get('target',{}).get('duration',0)))
+
+    window_lower_bound = lower_bound
+    while window_lower_bound + duration <= upper_bound:
+        window_upper_bound = window_lower_bound + duration
+        if can_run_anywhere_within_timewindow_with_sky_constraints(scheduling_unit, window_lower_bound, window_upper_bound):
+            return True
+        window_lower_bound += min(timedelta(hours=1), upper_bound - window_lower_bound)
+
     return False
 
 
diff --git a/SAS/TMSS/backend/src/tmss/tmssapp/models/specification.py b/SAS/TMSS/backend/src/tmss/tmssapp/models/specification.py
index e32d0f3195f..ff90f461d83 100644
--- a/SAS/TMSS/backend/src/tmss/tmssapp/models/specification.py
+++ b/SAS/TMSS/backend/src/tmss/tmssapp/models/specification.py
@@ -1212,8 +1212,8 @@ class TaskBlueprint(ProjectPropertyMixin, TemplateSchemaMixin, NamedCommon):
         '''
         # todo: when it was added, check if subtask.specifications_template.type.value == TaskType.Choices.OBSERVATION.value:
         try:
-            duration = self.specifications_doc["duration"]
-            return self.relative_start_time + datetime.timedelta(seconds=duration)
+            duration = datetime.timedelta(seconds=self.specifications_doc.get('duration', self.specifications_doc.get('target', {}).get('duration', 0)))
+            return self.relative_start_time + duration
         except:
             pass
         return self.relative_start_time
-- 
GitLab