From e34cb32f73a3aaf1b1633119b5fafde4f2c39bae Mon Sep 17 00:00:00 2001
From: David Trattnig <david@subsquare.at>
Date: Mon, 25 Mar 2024 21:20:44 +0100
Subject: [PATCH] refactor: improved timetable merge

---
 src/aura_engine/scheduling/timetable.py | 23 +++++++++--------------
 1 file changed, 9 insertions(+), 14 deletions(-)

diff --git a/src/aura_engine/scheduling/timetable.py b/src/aura_engine/scheduling/timetable.py
index 18382eff..9d7e3e88 100644
--- a/src/aura_engine/scheduling/timetable.py
+++ b/src/aura_engine/scheduling/timetable.py
@@ -397,10 +397,14 @@ class TimetableMerger:
             remote: Timeslot = ts_relation.get("remote")
 
             if (float(timestamp) - scheduling_window_start) < now:
-                # It's past the scheduling window, so keep the local one as is
+                # It's past the scheduling window start, so keep the local one as is
                 if local:
-                    merged_ts.append(local)
-                    resolution = "skip"
+                    # Only keep timeslots which have not yet ended.
+                    if local.end > now:
+                        merged_ts.append(local)
+                        resolution = "add | currently playing"
+                    else:
+                        resolution = "skip | out of scheduling window"
                 else:
                     # No local timeslot, so add it in any case.
                     resolution = "add"
@@ -416,22 +420,13 @@ class TimetableMerger:
                 elif local and remote:
                     # Timeslot existing locally, was updated or did not change remotely
                     # Update the local timeslot with possibly changed data
-                    local.show = remote.show
-                    local.episode = remote.episode
-                    local.id = remote.id
-                    local.repetition_id = remote.repetition_id
-                    if local.playlists.timeslot:
-                        local.playlists.timeslot.update_playlist(remote.playlists.timeslot)
-                    if local.playlists.schedule:
-                        local.playlists.schedule.update_playlist(remote.playlists.schedule)
-                    if local.playlists.show:
-                        local.playlists.show.update_playlist(remote.playlists.show)
+                    local.update(remote)
                     merged_ts.append(local)
                     resolution = "update"
                 else:
                     # Relations w/o local and remote timeslots should not happen
                     self.logger.critical(SU.red("Skipping invalid merge case!"))
-                    resolution = "skip"
+                    resolution = "skip | invalid merge case"
 
             local = "local:" + str(local).ljust(25)
             remote = "remote: " + str(remote).ljust(25)
-- 
GitLab