diff --git a/SAS/TMSS/backend/src/tmss/tmssapp/adapters/reports.py b/SAS/TMSS/backend/src/tmss/tmssapp/adapters/reports.py index 322aefabaf107c28cf31f5ed4ac6cc6eeafafd60..35901e233b6db13bd177c4a4927440b7ee76f58e 100644 --- a/SAS/TMSS/backend/src/tmss/tmssapp/adapters/reports.py +++ b/SAS/TMSS/backend/src/tmss/tmssapp/adapters/reports.py @@ -43,7 +43,7 @@ def _get_telescope_time_distribution(cycle: models.Cycle): # 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. + # # Note: We can filter observations by considering observed_duration in the SUB, for now. See TMSS-610. # _, durations = _get_subs_and_durations_from_project(p) # total += durations['total'] # succeeded += durations['total_succeeded'] @@ -216,7 +216,7 @@ def _get_subs_and_durations_from_project(project_pk: int) -> ({}, {}): # 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. + # We can filter observations by considering observed_duration 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()}) diff --git a/SAS/TMSS/backend/src/tmss/tmssapp/models/specification.py b/SAS/TMSS/backend/src/tmss/tmssapp/models/specification.py index b927f609a143033d3169b34b1d4a30bcd7bb3360..ecc2a6a69138ffd92d02a4528eff22ad738c80c3 100644 --- a/SAS/TMSS/backend/src/tmss/tmssapp/models/specification.py +++ b/SAS/TMSS/backend/src/tmss/tmssapp/models/specification.py @@ -557,6 +557,21 @@ class SchedulingUnitBlueprint(RefreshFromDbInvalidatesCachedPropertiesMixin, Tem else: return None + @property + def observed_start_time(self) -> datetime or None: + """ + return the earliest start time of all (observation) tasks of this scheduling unit with the status observed/finished + """ + observed_tasks = [] + for task in self.task_blueprints.all(): + if task.specifications_template.type.value == TaskType.Choices.OBSERVATION.value and \ + (task.status == "observed" or task.status == "finished") and task.start_time is not None: + observed_tasks.append(task) + if observed_tasks: + return min(observed_tasks, key=lambda x: x.start_time).start_time + else: + return None + @property def observed_end_time(self) -> datetime or None: """ @@ -572,6 +587,16 @@ class SchedulingUnitBlueprint(RefreshFromDbInvalidatesCachedPropertiesMixin, Tem else: return None + @property + def observed_duration(self) -> datetime or None: + """ + return the overall observed duration of all (observation) tasks of this scheduling unit + """ + if self.observed_start_time and self.observed_end_time: + return self.observed_end_time - self.observed_start_time + else: + return None + @property def status(self): """ diff --git a/SAS/TMSS/backend/test/t_scheduling.py b/SAS/TMSS/backend/test/t_scheduling.py index 2772564e229298bcbc88791c7f9c23ed0acf9e23..5d9a8bae0f59a8213d156ea36d428b977431285b 100755 --- a/SAS/TMSS/backend/test/t_scheduling.py +++ b/SAS/TMSS/backend/test/t_scheduling.py @@ -887,6 +887,7 @@ class TestWithUC1Specifications(unittest.TestCase): set_subtask_state_following_allowed_transitions(subtask, "finished") # Check times + self.assertEqual("2020-11-01 08:00:00", self.scheduling_unit_blueprint.observed_start_time.strftime("%Y-%m-%d %H:%M:%S")) self.assertEqual("2020-11-01 19:20:00", self.scheduling_unit_blueprint.observed_end_time.strftime("%Y-%m-%d %H:%M:%S")) self.assertEqual(timedelta(0), self.scheduling_unit_blueprint.relative_start_time) self.assertEqual(timedelta(hours=8, minutes=22), self.scheduling_unit_blueprint.relative_stop_time)