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

refactor: improve typing, debug and null handling

parent 33eb46b5
No related branches found
No related tags found
1 merge request!35ORM-less scheduling
......@@ -200,6 +200,7 @@ class TimetableService:
# Iterate over all timeslots and find the one to be played right now
if self.timetable:
timeslot: Timeslot
for timeslot in self.timetable:
if timeslot.get_start() <= now and now < timeslot.get_end():
current_timeslot = timeslot
......@@ -320,7 +321,7 @@ class TimetableMerger:
self.config = AuraConfig.instance.config
self.logger = logging.getLogger("engine")
def build_map(self, local_timeslots: [Timeslot], remote_timeslots: [Timeslot]) -> dict:
def build_map(self, local_timeslots: list[Timeslot], remote_timeslots: list[Timeslot]) -> dict:
"""
Build a map of local and remote timeslots relations.
......@@ -334,7 +335,7 @@ class TimetableMerger:
Returns:
({str: {}}): Map with timestamp as key and a dictionary referencing timeslots.
"""
timeslot_map: {str: {}} = {}
timeslot_map: dict[str:dict] = {}
if local_timeslots:
for ts in local_timeslots:
if not timeslot_map.get(str(ts.get_start())):
......@@ -342,7 +343,7 @@ class TimetableMerger:
idx = timeslot_map[str(ts.get_start())]
idx["local"] = ts
idx["remote"] = ""
idx["remote"] = None
if remote_timeslots:
for ts in remote_timeslots:
......@@ -352,10 +353,12 @@ class TimetableMerger:
idx = timeslot_map[str(ts.get_start())]
idx["remote"] = ts
if not idx.get("local"):
idx["local"] = ""
idx["local"] = None
return timeslot_map
def merge(self, local_timeslots: [Timeslot], remote_timeslots: [Timeslot]) -> [Timeslot]:
def merge(
self, local_timeslots: list[Timeslot], remote_timeslots: list[Timeslot]
) -> list[Timeslot]:
"""
Merge strategy for local and remote timeslots.
......@@ -374,7 +377,7 @@ class TimetableMerger:
scheduling_window_start = self.config.scheduler.scheduling_window_start
now: float = SU.timestamp()
resolution: str = ""
self.logger.debug("\nMap for timetable merge:")
merge_info = SU.cyan("\nMap for timetable merge:")
for timestamp, ts_relation in timeslot_map.items():
local = ts_relation.get("local")
......@@ -382,7 +385,8 @@ class TimetableMerger:
if (float(timestamp) - scheduling_window_start) < now:
# It's past the scheduling window, so keep the local one as is
merged_ts.append(local)
if local:
merged_ts.append(local)
resolution = "skip"
else:
if local and not remote:
......@@ -393,6 +397,11 @@ class TimetableMerger:
# Timeslot was added remotely
resolution = "add"
merged_ts.append(remote)
elif not local and not remote:
# Relations w/o local and remote timeslots should not happen
self.logger(SU.red("Skipping invalid merge case!"))
resolution = "skip"
merged_ts.append(remote)
else:
# Timeslot was updated or did not change
# Use the potentially newer, remote version
......@@ -401,6 +410,7 @@ class TimetableMerger:
local = "local:" + str(local).ljust(25)
remote = "remote: " + str(remote).ljust(25)
self.logger.debug(f"\tTIME:{timestamp} - {local} | {remote} ↦ ({resolution})")
merge_info += SU.cyan(f"\n\tTIME:{timestamp} - {local} | {remote} ↦ ({resolution})")
self.logger.debug(merge_info + "\n")
return merged_ts
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment