From 6b0799b643af1a6294b78be28c328ef233b7cf78 Mon Sep 17 00:00:00 2001
From: Jorrit Schaap <schaap@astron.nl>
Date: Mon, 27 Nov 2023 13:21:02 +0100
Subject: [PATCH] TMSS-2847: call evaluate_daily_constraints from
 get_earliest_possible_start_time_for_daily_constraints with non-gridded
 timestamp, in order to yield the correct non-gridded earliest_possible_start.
 Fixed blinking test_triggered_scheduling_unit_unschedules_regular_observation

---
 SAS/TMSS/backend/services/scheduling/lib/constraints.py  | 6 +++---
 .../services/scheduling/test/t_dynamic_scheduling.py     | 9 +++------
 2 files changed, 6 insertions(+), 9 deletions(-)

diff --git a/SAS/TMSS/backend/services/scheduling/lib/constraints.py b/SAS/TMSS/backend/services/scheduling/lib/constraints.py
index eafb17d2fbf..1c6d6e9785e 100644
--- a/SAS/TMSS/backend/services/scheduling/lib/constraints.py
+++ b/SAS/TMSS/backend/services/scheduling/lib/constraints.py
@@ -1669,8 +1669,7 @@ def get_earliest_possible_start_time_for_daily_constraints(scheduling_unit: mode
     # (daily constrains are (almost) cyclic over 24 hours)
     if gridder is None:
         gridder = Gridder()
-    gridded_lower_bound = gridder.grid_time(lower_bound)
-    possible_start_time = gridded_lower_bound
+    possible_start_time = lower_bound
     while possible_start_time < lower_bound+timedelta(hours=24):
         raise_if_interruped()
 
@@ -2097,7 +2096,8 @@ def compute_scheduling_unit_start_time(scheduling_unit: models.SchedulingUnitBlu
 
     unit_gridder = fine_enough_gridder(scheduling_unit, gridder)
 
-    earliest_possible_start_time = get_earliest_possible_start_time_taking_used_stations_into_account(scheduling_unit, lower_bound, min(upper_bound, lower_bound+timedelta(hours=24)), gridder=unit_gridder)
+    get_earliest_possible_start_time_method = get_earliest_possible_start_time if scheduling_unit.status.value in models.SchedulingUnitStatus.ACTIVE_OR_FINISHED_STATUS_VALUES else get_earliest_possible_start_time_taking_used_stations_into_account
+    earliest_possible_start_time = get_earliest_possible_start_time_method(scheduling_unit, lower_bound, min(upper_bound, lower_bound+timedelta(hours=24)), gridder=unit_gridder)
 
     if earliest_possible_start_time is not None and can_run_at(scheduling_unit, earliest_possible_start_time, unit_gridder):
         assert earliest_possible_start_time >= lower_bound, "SUB id=%s earliest_possible_start_time='%s' should be >= lower_bound='%s'" % (scheduling_unit.id, earliest_possible_start_time, lower_bound)
diff --git a/SAS/TMSS/backend/services/scheduling/test/t_dynamic_scheduling.py b/SAS/TMSS/backend/services/scheduling/test/t_dynamic_scheduling.py
index 4b7c029776e..8f4c95e4a3d 100755
--- a/SAS/TMSS/backend/services/scheduling/test/t_dynamic_scheduling.py
+++ b/SAS/TMSS/backend/services/scheduling/test/t_dynamic_scheduling.py
@@ -4867,16 +4867,13 @@ class TestTriggers(BaseDynamicSchedulingTestCase):
         triggered_scheduling_unit_blueprint = create_scheduling_unit_blueprint_and_tasks_and_subtasks_from_scheduling_unit_draft(scheduling_unit_draft)
 
         scheduled_scheduling_units = self.scheduler.do_dynamic_schedule()
-        scheduled_scheduling_unit = scheduled_scheduling_units[0]
-
-        # Assert now the new triggered scheduling_unit has been scheduled, and the regular one has been unscheduled
-        self.assertIsNotNone(scheduled_scheduling_unit)
-        self.assertEqual(scheduled_scheduling_unit.id, triggered_scheduling_unit_blueprint.id)
-
+        self.assertEqual(2, len(scheduled_scheduling_units))
+        # Assert now the new triggered scheduling_unit has been scheduled, and the regular one has been rescheduled
         regular_scheduling_unit_blueprint.refresh_from_db()
         triggered_scheduling_unit_blueprint.refresh_from_db()
         self.assertEqual(regular_scheduling_unit_blueprint.status.value, 'scheduled')
         self.assertEqual(triggered_scheduling_unit_blueprint.status.value, 'scheduled')
+        self.assertTrue(triggered_scheduling_unit_blueprint.scheduled_start_time < regular_scheduling_unit_blueprint.scheduled_start_time)
 
     @unittest.skip('the behavior under test is debatable')
     def test_triggered_fixed_time_scheduling_unit_unschedules_dynamically_scheduled_regular_observations(self):
-- 
GitLab