Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
60 changes: 48 additions & 12 deletions src/asr/observability/console.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,11 +8,26 @@
from typing import TextIO

from rich.console import Console
from rich.progress import BarColumn, Progress, TextColumn
from rich.progress import Progress, ProgressColumn, Task, TextColumn
from rich.text import Text

from asr.observability.events import ObservabilityEvent

_PROGRESS_BAR_WIDTH = 16
_PROGRESS_BAR_WIDTH = 20


class _ThinProgressBarColumn(ProgressColumn):
"""Render a full-length thin progress track."""

def render(self, task: Task) -> Text:
total = max(1.0, float(task.total or 1.0))
completed = max(0.0, min(float(task.completed), total))
filled = int((completed / total) * _PROGRESS_BAR_WIDTH + 0.5)
empty = _PROGRESS_BAR_WIDTH - filled
bar = Text()
bar.append("━" * filled, style="cyan")
bar.append("─" * empty, style="dim white")
return bar


@dataclass(slots=True)
Expand Down Expand Up @@ -101,7 +116,11 @@ def _record_window_progress(self, event: ObservabilityEvent) -> None:
def _window_progress_line(self, perf_counter: float) -> str:
count = self._current_window_count
index = min(self._current_window_index, count) if count else self._current_window_index
return f"{self._progress_bar(index, count)} | {index}/{count} | {self._elapsed(perf_counter)}"
return (
f"{self._progress_bar(index, count)} | "
f"{self._progress_percent(index, count)} | "
f"{self._progress_elapsed(perf_counter)}"
)

def _write_window_progress(self, perf_counter: float) -> None:
if not self.is_tty:
Expand All @@ -112,20 +131,23 @@ def _write_window_progress(self, perf_counter: float) -> None:
count = self._current_window_count
total = max(1, count)
completed = min(self._current_window_index, total)
elapsed = self._elapsed(perf_counter)
elapsed = self._progress_elapsed(perf_counter)
percent = self._progress_percent(self._current_window_index, count)
if self._progress_task_id is None:
self._progress_task_id = progress.add_task(
self._progress_description(),
total=total,
completed=completed,
elapsed=elapsed,
percent=percent,
)
else:
progress.update(
self._progress_task_id,
total=total,
completed=completed,
elapsed=elapsed,
percent=percent,
)
progress.refresh()

Expand All @@ -138,12 +160,11 @@ def _ensure_progress(self) -> Progress:
self._console = Console(
file=self.stream,
force_terminal=True,
no_color=True,
)
self._progress = Progress(
TextColumn("{task.description}"),
BarColumn(bar_width=_PROGRESS_BAR_WIDTH),
TextColumn("{task.completed:.0f}/{task.total:.0f}"),
_ThinProgressBarColumn(),
TextColumn("{task.fields[percent]}"),
TextColumn("{task.fields[elapsed]}"),
console=self._console,
auto_refresh=False,
Expand All @@ -160,8 +181,6 @@ def _stop_progress(self) -> None:
return
self._progress.stop()
if self.is_tty:
self.stream.write("\n")
self.stream.flush()
self._last_width = 0
self._progress = None
self._progress_task_id = None
Expand All @@ -171,9 +190,26 @@ def _progress_description(self) -> str:

def _progress_bar(self, index: int, count: int) -> str:
if count <= 0:
return "░" * _PROGRESS_BAR_WIDTH
filled = round((max(0, min(index, count)) / count) * _PROGRESS_BAR_WIDTH)
return ("█" * filled) + ("░" * (_PROGRESS_BAR_WIDTH - filled))
return "─" * _PROGRESS_BAR_WIDTH
filled = int((max(0, min(index, count)) / count) * _PROGRESS_BAR_WIDTH + 0.5)
return ("━" * filled) + ("─" * (_PROGRESS_BAR_WIDTH - filled))

def _progress_percent(self, index: int, count: int) -> str:
if count <= 0:
return "0%"
bounded = max(0, min(index, count))
percent = int((bounded / count) * 100 + 0.5)
return f"{percent}%"

def _progress_elapsed(self, perf_counter: float) -> str:
if self._file_start_perf is None:
return "00:00"
elapsed = int(max(0.0, perf_counter - self._file_start_perf))
minutes, seconds = divmod(elapsed, 60)
hours, minutes = divmod(minutes, 60)
if hours:
return f"{hours:d}:{minutes:02d}:{seconds:02d}"
return f"{minutes:02d}:{seconds:02d}"

def _elapsed(self, perf_counter: float) -> str:
if self._file_start_perf is None:
Expand Down
31 changes: 18 additions & 13 deletions tests/test_observability.py
Original file line number Diff line number Diff line change
Expand Up @@ -136,7 +136,7 @@ def test_non_tty_falls_back_to_plain_lines(self) -> None:
self.assertIn("prepare", output)
self.assertIn("0.5s", output)

def test_provider_window_step_renders_bar_count_and_elapsed_only(self) -> None:
def test_provider_window_step_renders_thin_bar_percent_and_elapsed(self) -> None:
stream = io.StringIO()
observer = ConsoleProgressObserver(stream=stream, is_tty=False)
observer.on_event(
Expand All @@ -156,16 +156,18 @@ def test_provider_window_step_renders_bar_count_and_elapsed_only(self) -> None:
file_id="1",
source_path="demo.wav",
step="provider_window",
meta={"window_index": 2, "window_count": 8},
perf_counter=12.5,
meta={"window_index": 3, "window_count": 8},
perf_counter=52.0,
)
)

output = stream.getvalue()
self.assertIn("2/8", output)
self.assertIn("2.5s", output)
self.assertIn("█", output)
self.assertIn("░", output)
self.assertIn("[1/1] demo.wav", output)
self.assertIn("38%", output)
self.assertIn("00:42", output)
self.assertIn("━", output)
self.assertIn("─", output)
self.assertNotIn("3/8", output)
self.assertNotIn("transcribe", output)
self.assertNotIn("window", output)

Expand All @@ -190,7 +192,7 @@ def test_tty_provider_window_uses_rich_progress(self) -> None:
source_path="demo.wav",
step="provider_window",
meta={"window_index": 3, "window_count": 8},
perf_counter=18.0,
perf_counter=52.0,
)
)

Expand All @@ -199,8 +201,11 @@ def test_tty_provider_window_uses_rich_progress(self) -> None:
output = stream.getvalue()
self.assertGreaterEqual(output.count("[1/1] demo.wav"), 2)
self.assertGreaterEqual(output.count("demo.wav"), 2)
self.assertIn("3/8", output)
self.assertIn("8.0s", output)
self.assertIn("38%", output)
self.assertIn("00:42", output)
self.assertIn("━", output)
self.assertIn("─", output)
self.assertNotIn("3/8", output)
self.assertNotIn("transcribe", output)
self.assertNotIn("window", output)

Expand Down Expand Up @@ -240,9 +245,9 @@ def test_file_end_finalizes_window_progress_bar(self) -> None:
)

output = stream.getvalue()
self.assertTrue(output.endswith("\n"))
self.assertIn("8/8", output)
self.assertIn("15.5s", output)
self.assertIn("100%", output)
self.assertIn("00:15", output)
self.assertNotIn("8/8", output)
self.assertEqual(observer._last_width, 0)

def test_vad_missing_dependency_warning_is_rendered_once(self) -> None:
Expand Down