56 lines
1.9 KiB
Python
56 lines
1.9 KiB
Python
from debuggable import Debuggable
|
|
from schedule import Schedule
|
|
|
|
|
|
def check_schedules(schedules):
|
|
for schedule in schedules:
|
|
if isinstance(schedule, Schedule):
|
|
continue
|
|
raise Exception("Invalid argument provided: {}".format(schedule))
|
|
|
|
|
|
class Planning(Debuggable):
|
|
|
|
schedules: list = []
|
|
|
|
def __init__(self):
|
|
super(Planning, self).__init__()
|
|
|
|
def add(self, schedules):
|
|
try:
|
|
# check if all are valid schedules
|
|
check_schedules(schedules)
|
|
# clean the schedules for elapsed
|
|
self.clean_schedules()
|
|
# add the schedules
|
|
self.debug("planning_add_schedules", "Adding {} schedules".format(len(schedules)))
|
|
self.schedules.extend(schedules)
|
|
self.debug("planning_add_schedules", "Got {} schedules after".format(len(self.schedules)))
|
|
# sort
|
|
self.schedules.sort(key=lambda x: x.start_at)
|
|
|
|
except Exception as exc:
|
|
print(exc)
|
|
|
|
def clean_schedules(self):
|
|
removed = 0
|
|
self.debug("planning_clean_schedules","Got {} schedules before cleaning".format(len(self.schedules)))
|
|
schedule_length = len(self.schedules)
|
|
for j in range(0,schedule_length):
|
|
i = schedule_length - j - 1
|
|
schedule = self.schedules[i]
|
|
if schedule.elapsed():
|
|
del(self.schedules[i])
|
|
self.debug("planning_clean_schedules_indexes", "Removed index {}/{}".format(i,schedule_length))
|
|
removed += 1
|
|
self.debug("planning_clean_schedules","Removed {} schedules".format(removed))
|
|
|
|
def get_last_schedule(self):
|
|
end_at_ordered = self.schedules.copy()
|
|
if not end_at_ordered:
|
|
return None
|
|
end_at_ordered.sort(key=lambda x: x.end_at)
|
|
return end_at_ordered.pop() if len(self.schedules) else None
|
|
|
|
# EOF
|