|
| 1 | +from rich.console import Console |
| 2 | +from rich.panel import Panel |
| 3 | +from rich.box import ROUNDED |
| 4 | +from datetime import datetime, timezone |
| 5 | +import statistics |
| 6 | + |
| 7 | +console = Console() |
| 8 | + |
| 9 | +def format_time(hours: float) -> str: |
| 10 | + """Convert hours to a human-readable format""" |
| 11 | + if hours < 1: |
| 12 | + return f"{int(hours * 60)} minutes" |
| 13 | + elif hours < 24: |
| 14 | + return f"{round(hours, 1)} hours" |
| 15 | + else: |
| 16 | + days = hours / 24 |
| 17 | + return f"{round(days, 1)} days" |
| 18 | + |
| 19 | +def display_linear_metrics(org_metrics): |
| 20 | + """Display Linear metrics with a modern UI using Rich components.""" |
| 21 | + # Header with organization info and time range |
| 22 | + now = datetime.now(timezone.utc) |
| 23 | + console.print(Panel( |
| 24 | + "[bold cyan]Linear Engineering Analytics[/]\n" + |
| 25 | + f"[dim]Organization: {org_metrics.name}[/]\n" + |
| 26 | + f"[dim]Report Generated: {now.strftime('%Y-%m-%d %H:%M')} UTC[/]", |
| 27 | + box=ROUNDED, |
| 28 | + style="cyan" |
| 29 | + )) |
| 30 | + |
| 31 | + # 1. Core Issue Metrics with health indicators |
| 32 | + total_issues = org_metrics.issues.total_created |
| 33 | + completed_issues = org_metrics.issues.total_completed |
| 34 | + completion_rate = (completed_issues / total_issues * 100) if total_issues > 0 else 0 |
| 35 | + |
| 36 | + health_indicator = "🟢" if completion_rate > 80 else "🟡" if completion_rate > 60 else "🔴" |
| 37 | + |
| 38 | + console.print(Panel( |
| 39 | + f"{health_indicator} [bold green]Issues Created:[/] {total_issues}\n" + |
| 40 | + f"[bold yellow]Issues Completed:[/] {completed_issues} ({completion_rate:.1f}% completion rate)\n" + |
| 41 | + f"[bold red]Bugs Created:[/] {org_metrics.issues.bugs_created}\n" + |
| 42 | + f"[bold blue]Features Created:[/] {org_metrics.issues.features_created}", |
| 43 | + title="[bold]Issue Flow", |
| 44 | + box=ROUNDED |
| 45 | + )) |
| 46 | + |
| 47 | + # 2. Time Metrics with visual indicators |
| 48 | + cycle = org_metrics.cycle_time |
| 49 | + avg_cycle_time = statistics.mean(cycle.cycle_times) if cycle.cycle_times else 0 |
| 50 | + cycle_health = "🟢" if avg_cycle_time < 24 else "🟡" if avg_cycle_time < 72 else "🔴" |
| 51 | + |
| 52 | + console.print(Panel( |
| 53 | + f"{cycle_health} [bold]Cycle Time:[/] {format_time(avg_cycle_time)}\n" + |
| 54 | + f"[bold]Time to Start:[/] {format_time(statistics.mean(cycle.time_to_start) if cycle.time_to_start else 0)}\n" + |
| 55 | + f"[bold]Time in Progress:[/] {format_time(statistics.mean(cycle.time_in_progress) if cycle.time_in_progress else 0)}", |
| 56 | + title="[bold blue]Time Metrics", |
| 57 | + box=ROUNDED |
| 58 | + )) |
| 59 | + |
| 60 | + # 3. Estimation Accuracy |
| 61 | + est = org_metrics.estimation |
| 62 | + if est.total_estimated > 0: |
| 63 | + accuracy_rate = (est.accurate_estimates / est.total_estimated * 100) |
| 64 | + accuracy_health = "🟢" if accuracy_rate > 80 else "🟡" if accuracy_rate > 60 else "🔴" |
| 65 | + |
| 66 | + console.print(Panel( |
| 67 | + f"{accuracy_health} [bold]Estimation Accuracy:[/] {accuracy_rate:.1f}%\n" + |
| 68 | + f"[bold green]Accurate Estimates:[/] {est.accurate_estimates}\n" + |
| 69 | + f"[bold red]Underestimates:[/] {est.underestimates}\n" + |
| 70 | + f"[bold yellow]Overestimates:[/] {est.overestimates}\n" + |
| 71 | + f"[bold]Average Variance:[/] {statistics.mean(est.estimation_variance) if est.estimation_variance else 0:.1f} hours", |
| 72 | + title="[bold yellow]Estimation Health", |
| 73 | + box=ROUNDED |
| 74 | + )) |
| 75 | + |
| 76 | + # 4. Team Performance |
| 77 | + if org_metrics.teams: |
| 78 | + team_panels = [] |
| 79 | + for team_name, team in org_metrics.teams.items(): |
| 80 | + completion_rate = (team.issues_completed / team.issues_created * 100) if team.issues_created > 0 else 0 |
| 81 | + team_health = "🟢" if completion_rate > 80 else "🟡" if completion_rate > 60 else "🔴" |
| 82 | + |
| 83 | + team_panels.append( |
| 84 | + f"{team_health} [bold cyan]{team_name}[/]\n" + |
| 85 | + f"Issues: {team.issues_created} created, {team.issues_completed} completed ({completion_rate:.1f}%)\n" + |
| 86 | + f"Cycle Time: {format_time(team.avg_cycle_time)}\n" + |
| 87 | + f"Estimation Accuracy: {team.estimation_accuracy:.1f}%" |
| 88 | + ) |
| 89 | + |
| 90 | + console.print(Panel( |
| 91 | + "\n\n".join(team_panels), |
| 92 | + title="[bold green]Team Performance", |
| 93 | + box=ROUNDED |
| 94 | + )) |
| 95 | + |
| 96 | + # 5. Project Health |
| 97 | + if org_metrics.projects: |
| 98 | + project_panels = [] |
| 99 | + for project_key, project in org_metrics.projects.items(): |
| 100 | + progress_indicator = "🟢" if project.progress >= 80 else "🟡" if project.progress >= 50 else "🔴" |
| 101 | + |
| 102 | + # Calculate project-specific metrics |
| 103 | + completion_rate = (project.completed_issues / project.total_issues * 100) if project.total_issues > 0 else 0 |
| 104 | + |
| 105 | + project_panels.append( |
| 106 | + f"{progress_indicator} [bold cyan]{project.name}[/]\n" + |
| 107 | + f"Progress: {project.progress:.1f}%\n" + |
| 108 | + f"Issues: {project.total_issues} total, {project.completed_issues} completed ({completion_rate:.1f}%)\n" + |
| 109 | + f"Bugs: {project.bugs_count} | Features: {project.features_count}\n" + |
| 110 | + f"Teams Involved: {len(project.teams_involved)}" |
| 111 | + ) |
| 112 | + |
| 113 | + console.print(Panel( |
| 114 | + "\n\n".join(project_panels), |
| 115 | + title="[bold magenta]Project Health", |
| 116 | + box=ROUNDED |
| 117 | + )) |
| 118 | + |
| 119 | + # 6. Label Distribution |
| 120 | + display_label_summary(org_metrics.label_counts) |
| 121 | + |
| 122 | +def display_label_summary(label_counts): |
| 123 | + """Display a visual summary of issue labels.""" |
| 124 | + if not label_counts: |
| 125 | + return |
| 126 | + |
| 127 | + # Sort labels by count in descending order |
| 128 | + sorted_labels = sorted(label_counts.items(), key=lambda x: x[1], reverse=True) |
| 129 | + |
| 130 | + # Calculate the maximum count for scaling |
| 131 | + max_count = max(count for _, count in sorted_labels) |
| 132 | + max_bar_length = 40 # Maximum length of the bar in characters |
| 133 | + |
| 134 | + # Create the label summary |
| 135 | + label_lines = [] |
| 136 | + for label, count in sorted_labels: |
| 137 | + # Calculate bar length proportional to count |
| 138 | + bar_length = int((count / max_count) * max_bar_length) |
| 139 | + bar = "█" * bar_length |
| 140 | + |
| 141 | + # Choose color based on label name (you can customize this) |
| 142 | + color = "green" if "feature" in label.lower() else \ |
| 143 | + "red" if "bug" in label.lower() else \ |
| 144 | + "yellow" if "improvement" in label.lower() else \ |
| 145 | + "blue" |
| 146 | + |
| 147 | + label_lines.append( |
| 148 | + f"[{color}]{label:<20}[/] {bar} ({count})" |
| 149 | + ) |
| 150 | + |
| 151 | + console.print(Panel( |
| 152 | + "\n".join(label_lines), |
| 153 | + title="[bold cyan]Label Distribution", |
| 154 | + box=ROUNDED |
| 155 | + )) |
0 commit comments