feat(perf): Expand instrumentation with context manager and extended metrics
This commit is contained in:
+45
-42
@@ -28,6 +28,7 @@ See Also:
|
||||
"""
|
||||
from typing import List
|
||||
from src.models import Ticket
|
||||
from src.performance_monitor import get_monitor
|
||||
|
||||
class TrackDAG:
|
||||
"""
|
||||
@@ -87,29 +88,30 @@ class TrackDAG:
|
||||
Returns:
|
||||
True if a cycle is detected, False otherwise.
|
||||
"""
|
||||
visited = set()
|
||||
rec_stack = set()
|
||||
with get_monitor().scope("dag_has_cycle"):
|
||||
visited = set()
|
||||
rec_stack = set()
|
||||
|
||||
def is_cyclic(ticket_id: str) -> bool:
|
||||
"""Internal recursive helper for cycle detection."""
|
||||
if ticket_id in rec_stack:
|
||||
return True
|
||||
if ticket_id in visited:
|
||||
return False
|
||||
visited.add(ticket_id)
|
||||
rec_stack.add(ticket_id)
|
||||
ticket = self.ticket_map.get(ticket_id)
|
||||
if ticket:
|
||||
for neighbor in ticket.depends_on:
|
||||
if is_cyclic(neighbor):
|
||||
return True
|
||||
rec_stack.remove(ticket_id)
|
||||
return False
|
||||
for ticket in self.tickets:
|
||||
if ticket.id not in visited:
|
||||
if is_cyclic(ticket.id):
|
||||
def is_cyclic(ticket_id: str) -> bool:
|
||||
"""Internal recursive helper for cycle detection."""
|
||||
if ticket_id in rec_stack:
|
||||
return True
|
||||
return False
|
||||
if ticket_id in visited:
|
||||
return False
|
||||
visited.add(ticket_id)
|
||||
rec_stack.add(ticket_id)
|
||||
ticket = self.ticket_map.get(ticket_id)
|
||||
if ticket:
|
||||
for neighbor in ticket.depends_on:
|
||||
if is_cyclic(neighbor):
|
||||
return True
|
||||
rec_stack.remove(ticket_id)
|
||||
return False
|
||||
for ticket in self.tickets:
|
||||
if ticket.id not in visited:
|
||||
if is_cyclic(ticket.id):
|
||||
return True
|
||||
return False
|
||||
|
||||
def topological_sort(self) -> List[str]:
|
||||
"""
|
||||
@@ -119,24 +121,25 @@ class TrackDAG:
|
||||
Raises:
|
||||
ValueError: If a dependency cycle is detected.
|
||||
"""
|
||||
if self.has_cycle():
|
||||
raise ValueError("Dependency cycle detected")
|
||||
visited = set()
|
||||
stack = []
|
||||
with get_monitor().scope("dag_topological_sort"):
|
||||
if self.has_cycle():
|
||||
raise ValueError("Dependency cycle detected")
|
||||
visited = set()
|
||||
stack = []
|
||||
|
||||
def visit(ticket_id: str) -> None:
|
||||
"""Internal recursive helper for topological sorting."""
|
||||
if ticket_id in visited:
|
||||
return
|
||||
visited.add(ticket_id)
|
||||
ticket = self.ticket_map.get(ticket_id)
|
||||
if ticket:
|
||||
for dep_id in ticket.depends_on:
|
||||
visit(dep_id)
|
||||
stack.append(ticket_id)
|
||||
for ticket in self.tickets:
|
||||
visit(ticket.id)
|
||||
return stack
|
||||
def visit(ticket_id: str) -> None:
|
||||
"""Internal recursive helper for topological sorting."""
|
||||
if ticket_id in visited:
|
||||
return
|
||||
visited.add(ticket_id)
|
||||
ticket = self.ticket_map.get(ticket_id)
|
||||
if ticket:
|
||||
for dep_id in ticket.depends_on:
|
||||
visit(dep_id)
|
||||
stack.append(ticket_id)
|
||||
for ticket in self.tickets:
|
||||
visit(ticket.id)
|
||||
return stack
|
||||
|
||||
class ExecutionEngine:
|
||||
"""
|
||||
@@ -161,9 +164,10 @@ class ExecutionEngine:
|
||||
Returns:
|
||||
A list of ready Ticket objects.
|
||||
"""
|
||||
self.dag.cascade_blocks()
|
||||
ready = self.dag.get_ready_tasks()
|
||||
return ready
|
||||
with get_monitor().scope("dag_tick"):
|
||||
self.dag.cascade_blocks()
|
||||
ready = self.dag.get_ready_tasks()
|
||||
return ready
|
||||
|
||||
def approve_task(self, task_id: str) -> None:
|
||||
"""
|
||||
@@ -185,4 +189,3 @@ class ExecutionEngine:
|
||||
ticket = self.dag.ticket_map.get(task_id)
|
||||
if ticket:
|
||||
ticket.status = status
|
||||
|
||||
|
||||
Reference in New Issue
Block a user