|
|
from typing import Literal, TypedDict, List |
|
|
|
|
|
Priority = Literal["low", "normal", "high", "critical"] |
|
|
|
|
|
class Task(TypedDict): |
|
|
task_id: str |
|
|
task_type: str |
|
|
priority: Priority |
|
|
source: str |
|
|
|
|
|
|
|
|
class ScheduledTask(TypedDict): |
|
|
task_id: str |
|
|
order: int |
|
|
reason: str |
|
|
|
|
|
|
|
|
def schedule_tasks(tasks: List[Task]) -> List[ScheduledTask]: |
|
|
"""Order tasks using simple priority and source rules. |
|
|
|
|
|
- critical > high > normal > low |
|
|
- user > sensor > schedule > system (within same priority) |
|
|
""" |
|
|
pr_order = {"critical": 0, "high": 1, "normal": 2, "low": 3} |
|
|
src_order = {"user": 0, "sensor": 1, "schedule": 2, "system": 3} |
|
|
|
|
|
sorted_tasks = sorted( |
|
|
tasks, |
|
|
key=lambda t: ( |
|
|
pr_order.get(t["priority"], 2), |
|
|
src_order.get(t["source"], 3), |
|
|
), |
|
|
) |
|
|
|
|
|
result: List[ScheduledTask] = [] |
|
|
for idx, t in enumerate(sorted_tasks, start=1): |
|
|
reason = f"priority={t['priority']}, source={t['source']} -> position {idx}." |
|
|
result.append({"task_id": t["task_id"], "order": idx, "reason": reason}) |
|
|
|
|
|
return result |
|
|
|
|
|
|
|
|
if __name__ == "__main__": |
|
|
sample: List[Task] = [ |
|
|
{"task_id": "T_CLEAN_KITCHEN", "task_type": "clean_room", "priority": "high", "source": "schedule"}, |
|
|
{"task_id": "T_REMIND_TRASH", "task_type": "reminder", "priority": "high", "source": "sensor"}, |
|
|
{"task_id": "T_PATROL_NIGHT", "task_type": "patrol", "priority": "normal", "source": "schedule"}, |
|
|
{"task_id": "T_STATUS", "task_type": "status_report", "priority": "low", "source": "system"}, |
|
|
{"task_id": "T_CLEAN_LIVING", "task_type": "clean_room", "priority": "critical", "source": "user"}, |
|
|
] |
|
|
for st in schedule_tasks(sample): |
|
|
print(st) |
|
|
|