|
| 1 | +from typing import ( |
| 2 | + Dict, |
| 3 | + Optional, |
| 4 | +) |
| 5 | + |
| 6 | +from rich.console import Console |
| 7 | +from rich.progress import ( |
| 8 | + BarColumn, |
| 9 | + Progress, |
| 10 | + TextColumn, |
| 11 | + TaskProgressColumn, |
| 12 | +) |
| 13 | +from rich.theme import Theme |
| 14 | + |
| 15 | + |
| 16 | +class WorkflowProgress(Progress): |
| 17 | + invocation_state: str = "new" |
| 18 | + step_count: Optional[int] = None |
| 19 | + job_count: int = 0 |
| 20 | + steps_color: str = "cyan" |
| 21 | + jobs_color: str = "cyan" |
| 22 | + step_states: Dict = {} |
| 23 | + |
| 24 | + def __init__(self): |
| 25 | + custom_theme = Theme({ |
| 26 | + "bar.complete": "green", |
| 27 | + }) |
| 28 | + console = Console(theme=custom_theme) |
| 29 | + super().__init__( |
| 30 | + TextColumn("[progress.description]{task.description}"), |
| 31 | + BarColumn(), |
| 32 | + TaskProgressColumn(), |
| 33 | + TextColumn(text_format="{task.fields[status]}"), |
| 34 | + console=console, |
| 35 | + ) |
| 36 | + |
| 37 | + def handle_invocation(self, invocation: Dict, job_state_summary: Dict): |
| 38 | + self.invocation_state = invocation.get("state") or "new" |
| 39 | + self.step_count = len(invocation.get("steps") or []) or None |
| 40 | + self.step_states = step_states(invocation) |
| 41 | + |
| 42 | + steps_completed = None |
| 43 | + |
| 44 | + steps_status = "" |
| 45 | + if self.step_count is None: |
| 46 | + steps_status = "Loading steps." |
| 47 | + self.steps_color = "cyan" |
| 48 | + elif self.invocation_state == "cancelled": |
| 49 | + steps_status = "Invocation cancelled" |
| 50 | + self.steps_color = "red" |
| 51 | + elif self.invocation_state == "failed": |
| 52 | + steps_status = "Invocation failed" |
| 53 | + self.steps_color = "red" |
| 54 | + else: |
| 55 | + num_scheduled = self.step_states.get("scheduled") or 0 |
| 56 | + if num_scheduled > 0: |
| 57 | + self.steps_color = "green" |
| 58 | + else: |
| 59 | + self.steps_color = "cyan" |
| 60 | + steps_completed = num_scheduled |
| 61 | + steps_status = f"{num_scheduled}/{self.step_count} scheduled" |
| 62 | + |
| 63 | + jobs_status = "" |
| 64 | + self.job_count = job_count(job_state_summary) |
| 65 | + num_errors = error_count(job_state_summary) |
| 66 | + num_ok = ok_count(job_state_summary) |
| 67 | + jobs_completed = num_ok + num_errors |
| 68 | + jobs_total = self.job_count |
| 69 | + if num_errors > 0: |
| 70 | + self.jobs_color = "red" |
| 71 | + elif self.job_count > 0: |
| 72 | + self.jobs_color = "green" |
| 73 | + else: |
| 74 | + self.jobs_color = "cyan" |
| 75 | + jobs_completed = None |
| 76 | + jobs_total = None |
| 77 | + if self.job_count > 0: |
| 78 | + jobs_status = f"{jobs_completed}/{self.job_count} terminal" |
| 79 | + self.update(self._steps_task, total=self.step_count, completed=steps_completed, description=f"[{self.steps_color}]Steps...", status=steps_status) |
| 80 | + self.update(self._jobs_task, total=jobs_total, completed=jobs_completed, description=f"[{self.jobs_color}]Jobs...", status=jobs_status) |
| 81 | + |
| 82 | + def add_bars(self): |
| 83 | + self._steps_task = self.add_task("[green]Steps...", status="") |
| 84 | + self._jobs_task = self.add_task("[green]Jobs...", status="") |
| 85 | + |
| 86 | + |
| 87 | +# converted from Galaxy TypeScript (see util.ts next to WorkflowInvocationState.vue) |
| 88 | +def count_states(job_summary: Optional[Dict], query_states: list[str]) -> int: |
| 89 | + count = 0 |
| 90 | + states = job_summary.get("states") if job_summary else None |
| 91 | + if states: |
| 92 | + for state in query_states: |
| 93 | + count += states.get(state, 0) |
| 94 | + return count |
| 95 | + |
| 96 | + |
| 97 | +def job_count(job_summary: Optional[Dict]) -> int: |
| 98 | + states = job_summary.get("states") if job_summary else None |
| 99 | + count = 0 |
| 100 | + if states: |
| 101 | + for state_count in states.values(): |
| 102 | + if state_count: |
| 103 | + count += state_count |
| 104 | + return count |
| 105 | + |
| 106 | + |
| 107 | +def step_states(invocation: Dict): |
| 108 | + step_states = {} |
| 109 | + steps = invocation.get("steps") or [] |
| 110 | + for step in steps: |
| 111 | + if not step: |
| 112 | + continue |
| 113 | + step_state = step.get("state") or "unknown" |
| 114 | + if step_state not in step_states: |
| 115 | + step_states[step_state] = 0 |
| 116 | + step_states[step_state] += 1 |
| 117 | + |
| 118 | + return step_states |
| 119 | + |
| 120 | + |
| 121 | +def ok_count(job_summary: Dict) -> int: |
| 122 | + return count_states(job_summary, ["ok", "skipped"]) |
| 123 | + |
| 124 | + |
| 125 | +ERROR_STATES = ["error", "deleted"] |
| 126 | + |
| 127 | + |
| 128 | +def error_count(job_summary: Dict) -> int: |
| 129 | + return count_states(job_summary, ERROR_STATES) |
| 130 | + |
| 131 | + |
| 132 | +def running_count(job_summary: Dict) -> int: |
| 133 | + return count_states(job_summary, ["running"]) |
0 commit comments