From adc38bac5eb25f453d125b75ab128475744505e3 Mon Sep 17 00:00:00 2001
From: Jorrit Schaap <schaap@astron.nl>
Date: Fri, 13 Nov 2020 12:24:27 +0100
Subject: [PATCH] TMSS-244: crude first implementation of the before and after
 time constraint

---
 .../lib/constraints/template_constraints_v1.py | 18 +++++++++++++++++-
 1 file changed, 17 insertions(+), 1 deletion(-)

diff --git a/SAS/TMSS/services/scheduling/lib/constraints/template_constraints_v1.py b/SAS/TMSS/services/scheduling/lib/constraints/template_constraints_v1.py
index 06b343ec06d..a7f2a979a6c 100644
--- a/SAS/TMSS/services/scheduling/lib/constraints/template_constraints_v1.py
+++ b/SAS/TMSS/services/scheduling/lib/constraints/template_constraints_v1.py
@@ -28,6 +28,7 @@
 import logging
 logger = logging.getLogger(__name__)
 from datetime import datetime, timedelta
+from lofar.common.datetimeutils import parseDatetime
 
 from lofar.sas.tmss.tmss.tmssapp import models
 from lofar.sas.tmss.tmss.tmssapp.conversions import create_astroplan_observer_for_station, Time, timestamps_and_stations_to_sun_rise_and_set
@@ -87,6 +88,18 @@ def can_run_within_timewindow_with_time_constraints(scheduling_unit: models.Sche
     '''evaluate the time contraint(s)'''
     constraints = scheduling_unit.draft.scheduling_constraints_doc
     # TODO: TMSS-244 (and more?), evaluate the constraints in constraints['time']
+    if 'before' in constraints['time']:
+        before = parseDatetime(constraints['time']['before'].replace('T', ' ').replace('Z', ''))
+        return upper_bound <= before
+
+    if 'after' in constraints['time']:
+        after = parseDatetime(constraints['time']['after'].replace('T', ' ').replace('Z', ''))
+        return lower_bound >= after
+
+    # if 'between' in constraints['time']:
+    #     betweens = [ dateutil.parser.parse(constraints['time']['between'])
+    #     return lower_bound >= after
+
     return True # for now, ignore time contraints.
 
 
@@ -102,6 +115,9 @@ def get_earliest_possible_start_time(scheduling_unit: models.SchedulingUnitBluep
     constraints = scheduling_unit.draft.scheduling_constraints_doc
 
     try:
+        if 'after' in constraints['time']:
+            return parseDatetime(constraints['time']['after'].replace('T', ' ').replace('Z', ''))
+
         if constraints['daily']['require_day'] or constraints['daily']['require_night']:
 
             # TODO: TMSS-254 and TMSS-255
@@ -162,5 +178,5 @@ def compute_scores(scheduling_unit: models.SchedulingUnitBlueprint, lower_bound:
     return ScoredSchedulingUnit(scheduling_unit=scheduling_unit,
                                 scores=scores,
                                 weighted_score=weighted_score,
-                                start_time=lower_bound)
+                                start_time=get_earliest_possible_start_time(scheduling_unit, lower_bound))
 
-- 
GitLab