Coverage for collectra/pipelines/run_plan.py: 100.00%

44 statements  

« prev     ^ index     » next       coverage.py v7.16.2, created at 2026-11-13 04:26 +0000

1"""Plan which tasks a run of one page will run, re-run or skip.""" 

2 

3from __future__ import annotations 

4 

5from collections import deque 

6from dataclasses import dataclass 

7from typing import Literal 

8 

9 

10@dataclass(frozen=True) 

11class TaskPlan: 

12 """What a run does with one task, and why.""" 

13 

14 name: str 

15 action: Literal["run", "rerun", "skip"] 

16 reason: str 

17 

18 

19@dataclass(frozen=True) 

20class RunPlan: 

21 """The plan for every task of a pipeline, in pipeline order. 

22 

23 ``warnings`` says how the page's results differ from the pipeline, such as 

24 another version having made them; they do not stop the run. 

25 """ 

26 

27 tasks: list[TaskPlan] 

28 warnings: tuple[str, ...] = () 

29 

30 @property 

31 def to_run(self) -> list[str]: 

32 """Names of the tasks that will run or re-run.""" 

33 return [task.name for task in self.tasks if task.action != "skip"] 

34 

35 

36@dataclass(frozen=True) 

37class TaskOutcome: 

38 """What happened to one task in a run, and why.""" 

39 

40 name: str 

41 status: Literal["ran", "re-ran", "skipped", "failed", "cancelled"] 

42 detail: str 

43 

44 

45@dataclass(frozen=True) 

46class RunReport: 

47 """What happened to every task of a page run, in pipeline order.""" 

48 

49 outcomes: list[TaskOutcome] 

50 

51 @property 

52 def failed(self) -> list[TaskOutcome]: 

53 """The tasks that failed.""" 

54 return [outcome for outcome in self.outcomes if outcome.status == "failed"] 

55 

56 

57def plan_tasks( 

58 missing: set[str], children: dict[str, set[str]], order: list[str] 

59) -> RunPlan: 

60 """Plan a run given the tasks that have no output yet. 

61 

62 A missing task runs. A task downstream of a running task, however far, 

63 re-runs because its input changes; its reason names the nearest running 

64 task upstream. Every other task is skipped. 

65 """ 

66 upstream: dict[str, str] = {} 

67 queue = deque((name, name) for name in order if name in missing) 

68 while queue: 

69 name, source = queue.popleft() 

70 for child in sorted(children.get(name, ())): 

71 if child not in missing and child not in upstream: 

72 upstream[child] = source 

73 queue.append((child, child)) 

74 plans = [] 

75 for name in order: 

76 if name in missing: 

77 plans.append(TaskPlan(name, "run", "no output yet")) 

78 elif name in upstream: 

79 plans.append(TaskPlan(name, "rerun", f"upstream {upstream[name]} runs")) 

80 else: 

81 plans.append(TaskPlan(name, "skip", "already has output")) 

82 return RunPlan(plans)