Spaces:
Sleeping
Sleeping
Create tasks.py
Browse files- core/tasks.py +109 -0
core/tasks.py
ADDED
|
@@ -0,0 +1,109 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import random
|
| 2 |
+
from dataclasses import dataclass, field, asdict
|
| 3 |
+
from typing import List, Dict, Optional, Any
|
| 4 |
+
|
| 5 |
+
PRIORITIES = ["P0", "P1", "P2", "P3"]
|
| 6 |
+
|
| 7 |
+
@dataclass
|
| 8 |
+
class Task:
|
| 9 |
+
task_id: str
|
| 10 |
+
title: str
|
| 11 |
+
task_type: str
|
| 12 |
+
priority: str
|
| 13 |
+
created_t: int
|
| 14 |
+
sla_due_t: int
|
| 15 |
+
est_effort_min: int
|
| 16 |
+
value_usd: float
|
| 17 |
+
|
| 18 |
+
dependencies: List[str] = field(default_factory=list)
|
| 19 |
+
|
| 20 |
+
status: str = "QUEUED" # QUEUED | IN_PROGRESS | BLOCKED | DONE | REWORK
|
| 21 |
+
owner: Optional[str] = None
|
| 22 |
+
started_t: Optional[int] = None
|
| 23 |
+
completed_t: Optional[int] = None
|
| 24 |
+
attempts: int = 0
|
| 25 |
+
notes: Dict[str, Any] = field(default_factory=dict)
|
| 26 |
+
|
| 27 |
+
class TaskSystem:
|
| 28 |
+
def __init__(self, seed: int):
|
| 29 |
+
self.rng = random.Random(seed)
|
| 30 |
+
self.tasks: Dict[str, Task] = {}
|
| 31 |
+
self.counter = 0
|
| 32 |
+
|
| 33 |
+
def _next_id(self) -> str:
|
| 34 |
+
self.counter += 1
|
| 35 |
+
return f"T{self.counter:05d}"
|
| 36 |
+
|
| 37 |
+
def create_task(
|
| 38 |
+
self,
|
| 39 |
+
t_sim: int,
|
| 40 |
+
title: str,
|
| 41 |
+
task_type: str,
|
| 42 |
+
priority: str,
|
| 43 |
+
sla_ticks: int,
|
| 44 |
+
est_effort_min: int,
|
| 45 |
+
value_usd: float,
|
| 46 |
+
dependencies: Optional[List[str]] = None,
|
| 47 |
+
urgent: bool = False,
|
| 48 |
+
) -> Task:
|
| 49 |
+
tid = self._next_id()
|
| 50 |
+
sla_due = t_sim + max(1, int(sla_ticks))
|
| 51 |
+
if urgent:
|
| 52 |
+
priority = "P0"
|
| 53 |
+
task = Task(
|
| 54 |
+
task_id=tid,
|
| 55 |
+
title=title,
|
| 56 |
+
task_type=task_type,
|
| 57 |
+
priority=priority,
|
| 58 |
+
created_t=t_sim,
|
| 59 |
+
sla_due_t=sla_due,
|
| 60 |
+
est_effort_min=est_effort_min,
|
| 61 |
+
value_usd=value_usd,
|
| 62 |
+
dependencies=dependencies or [],
|
| 63 |
+
)
|
| 64 |
+
self.tasks[tid] = task
|
| 65 |
+
return task
|
| 66 |
+
|
| 67 |
+
def queued_tasks(self) -> List[Task]:
|
| 68 |
+
return [t for t in self.tasks.values() if t.status in ("QUEUED", "REWORK")]
|
| 69 |
+
|
| 70 |
+
def active_tasks(self) -> List[Task]:
|
| 71 |
+
return [t for t in self.tasks.values() if t.status in ("IN_PROGRESS", "BLOCKED")]
|
| 72 |
+
|
| 73 |
+
def done_tasks(self) -> List[Task]:
|
| 74 |
+
return [t for t in self.tasks.values() if t.status == "DONE"]
|
| 75 |
+
|
| 76 |
+
def overdue_tasks(self, t_sim: int) -> List[Task]:
|
| 77 |
+
return [t for t in self.tasks.values() if t.status != "DONE" and t_sim > t.sla_due_t]
|
| 78 |
+
|
| 79 |
+
def can_start(self, task: Task) -> bool:
|
| 80 |
+
# Dependencies must be DONE
|
| 81 |
+
for dep in task.dependencies:
|
| 82 |
+
if dep in self.tasks and self.tasks[dep].status != "DONE":
|
| 83 |
+
return False
|
| 84 |
+
return True
|
| 85 |
+
|
| 86 |
+
def pick_next_task(self, t_sim: int) -> Optional[Task]:
|
| 87 |
+
# Priority then due date
|
| 88 |
+
q = [t for t in self.queued_tasks() if self.can_start(t)]
|
| 89 |
+
if not q:
|
| 90 |
+
return None
|
| 91 |
+
pr_order = {p: i for i, p in enumerate(PRIORITIES)}
|
| 92 |
+
q.sort(key=lambda t: (pr_order.get(t.priority, 9), t.sla_due_t, t.created_t))
|
| 93 |
+
return q[0]
|
| 94 |
+
|
| 95 |
+
def to_compact_table(self, t_sim: int, limit: int = 14) -> str:
|
| 96 |
+
# Simple readable view for UI
|
| 97 |
+
items = sorted(self.tasks.values(), key=lambda t: (t.status != "DONE", t.sla_due_t))
|
| 98 |
+
items = items[:limit]
|
| 99 |
+
lines = ["id | pri | status | due(t) | owner | title"]
|
| 100 |
+
lines.append("---|-----|--------|--------|-------|------")
|
| 101 |
+
for t in items:
|
| 102 |
+
due = t.sla_due_t
|
| 103 |
+
lines.append(f"{t.task_id} | {t.priority} | {t.status} | {due} | {t.owner or '-'} | {t.title[:42]}")
|
| 104 |
+
overdue = len(self.overdue_tasks(t_sim))
|
| 105 |
+
lines.append(f"\nOverdue: {overdue} | Total: {len(self.tasks)} | Done: {len(self.done_tasks())}")
|
| 106 |
+
return "\n".join(lines)
|
| 107 |
+
|
| 108 |
+
def as_dict(self) -> Dict[str, Any]:
|
| 109 |
+
return {k: asdict(v) for k, v in self.tasks.items()}
|