Skip to content
Snippets Groups Projects
Commit 4b2488ef authored by David Trattnig's avatar David Trattnig
Browse files

refactor: extend merge strategy

parent eb5f7c7b
No related branches found
No related tags found
1 merge request!35ORM-less scheduling
...@@ -37,6 +37,7 @@ import jsonpickle ...@@ -37,6 +37,7 @@ import jsonpickle
import aura_engine.engine as engine import aura_engine.engine as engine
import aura_engine.scheduling.api as api import aura_engine.scheduling.api as api
from aura_engine.base.config import AuraConfig from aura_engine.base.config import AuraConfig
from aura_engine.base.lang import synchronized
from aura_engine.base.utils import SimpleUtil as SU from aura_engine.base.utils import SimpleUtil as SU
from aura_engine.scheduling.domain import Playlist, PlaylistItem, Timeslot from aura_engine.scheduling.domain import Playlist, PlaylistItem, Timeslot
...@@ -76,6 +77,7 @@ class TimetableService: ...@@ -76,6 +77,7 @@ class TimetableService:
self.timetable_file = self.cache_location + "/timetable.json" self.timetable_file = self.cache_location + "/timetable.json"
self.load_timetable() self.load_timetable()
@synchronized
def refresh(self): def refresh(self):
""" """
Update the timetable. Update the timetable.
...@@ -208,7 +210,7 @@ class TimetableService: ...@@ -208,7 +210,7 @@ class TimetableService:
return current_timeslot return current_timeslot
def get_next_timeslots(self, max_count: int = 0, window_aware=False) -> [Timeslot]: def get_next_timeslots(self, max_count: int = 0, window_aware=False) -> list[Timeslot]:
""" """
Retrieve the timeslots to be played after the current one. Retrieve the timeslots to be played after the current one.
...@@ -356,6 +358,7 @@ class TimetableMerger: ...@@ -356,6 +358,7 @@ class TimetableMerger:
idx["local"] = None idx["local"] = None
return timeslot_map return timeslot_map
@synchronized
def merge( def merge(
self, local_timeslots: list[Timeslot], remote_timeslots: list[Timeslot] self, local_timeslots: list[Timeslot], remote_timeslots: list[Timeslot]
) -> list[Timeslot]: ) -> list[Timeslot]:
...@@ -380,8 +383,8 @@ class TimetableMerger: ...@@ -380,8 +383,8 @@ class TimetableMerger:
merge_info = SU.cyan("\nMap for timetable merge:") merge_info = SU.cyan("\nMap for timetable merge:")
for timestamp, ts_relation in timeslot_map.items(): for timestamp, ts_relation in timeslot_map.items():
local = ts_relation.get("local") local: Timeslot = ts_relation.get("local")
remote = ts_relation.get("remote") remote: Timeslot = ts_relation.get("remote")
if (float(timestamp) - scheduling_window_start) < now: 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, so keep the local one as is
...@@ -396,21 +399,29 @@ class TimetableMerger: ...@@ -396,21 +399,29 @@ class TimetableMerger:
if local and not remote: if local and not remote:
# Timeslot was deleted remotely, remove any local one # Timeslot was deleted remotely, remove any local one
resolution = "remove" resolution = "remove"
continue
elif not local and remote: elif not local and remote:
# Timeslot was added remotely # Timeslot was added remotely
resolution = "add" resolution = "add"
merged_ts.append(remote) merged_ts.append(remote)
elif not local and not remote: 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.playlist.timeslot:
local.playlist.timeslot.update_playlist(remote.playlist.timeslot)
if local.playlist.schedule:
local.playlist.schedule.update_playlist(remote.playlist.schedule)
if local.playlist.show:
local.playlist.show.update_playlist(remote.playlist.show)
merged_ts.append(local)
resolution = "update"
else:
# Relations w/o local and remote timeslots should not happen # Relations w/o local and remote timeslots should not happen
self.logger(SU.red("Skipping invalid merge case!")) self.logger.critical(SU.red("Skipping invalid merge case!"))
resolution = "skip" resolution = "skip"
merged_ts.append(remote)
else:
# Timeslot was updated or did not change
# Use the potentially newer, remote version
resolution = "update"
merged_ts.append(remote)
local = "local:" + str(local).ljust(25) local = "local:" + str(local).ljust(25)
remote = "remote: " + str(remote).ljust(25) remote = "remote: " + str(remote).ljust(25)
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment