Tracks duration of lint stages and generates summary table. Matches the exact output format of the bash script.
| 14 | |
| 15 | |
| 16 | class DurationTracker: |
| 17 | """ |
| 18 | Tracks duration of lint stages and generates summary table. |
| 19 | |
| 20 | Matches the exact output format of the bash script. |
| 21 | """ |
| 22 | |
| 23 | def __init__(self) -> None: |
| 24 | self.results: list[StageResult] = [] |
| 25 | self.start_times: dict[str, float] = {} |
| 26 | |
| 27 | def start_stage(self, name: str) -> None: |
| 28 | """Record the start time of a stage.""" |
| 29 | self.start_times[name] = time.time() |
| 30 | |
| 31 | def record_stage(self, name: str, duration: float, skipped: bool = False) -> None: |
| 32 | """Record a completed stage.""" |
| 33 | self.results.append(StageResult(name, duration, skipped)) |
| 34 | |
| 35 | def end_stage(self, name: str, skipped: bool = False) -> None: |
| 36 | """Calculate and record the duration for a stage.""" |
| 37 | if name in self.start_times: |
| 38 | duration = time.time() - self.start_times[name] |
| 39 | self.record_stage(name, duration, skipped) |
| 40 | else: |
| 41 | # If start time not found, record with 0 duration |
| 42 | self.record_stage(name, 0.0, skipped) |
| 43 | |
| 44 | def generate_summary(self) -> str: |
| 45 | """ |
| 46 | Generate the summary table matching bash script format. |
| 47 | |
| 48 | Returns: |
| 49 | Formatted summary table as a string |
| 50 | """ |
| 51 | if not self.results: |
| 52 | return "" |
| 53 | |
| 54 | # Calculate column widths |
| 55 | max_name_width = max(len(r.name) for r in self.results) |
| 56 | header_width = len("Linter Name") |
| 57 | max_name_width = max(max_name_width, header_width) |
| 58 | |
| 59 | # Build separator line |
| 60 | separator = "-" * max_name_width + "-+-" + "-" * 24 |
| 61 | |
| 62 | # Build table |
| 63 | lines = [ |
| 64 | "", |
| 65 | "Linting Execution Summary:", |
| 66 | separator, |
| 67 | f"{'Linter Name':<{max_name_width}} | {'Duration':>24}", |
| 68 | separator, |
| 69 | ] |
| 70 | |
| 71 | # Add each result |
| 72 | for result in self.results: |
| 73 | if result.skipped: |