"""Workload heatmap view — renders a Plotly heatmap of team workload over weeks.""" import re import plotly.graph_objects as go import pandas as pd from core.planner import ProjectPlan def _natural_sort_key(s: str) -> list: """Sort key that orders W1, W2, ..., W10, W11 correctly (not W1, W10, W11, W2).""" return [int(c) if c.isdigit() else c.lower() for c in re.split(r"(\d+)", s)] COLORS = { "bg": "#0a0a0a", "surface": "#111111", "surface_alt": "#181818", "green": "#4ade80", "green_accent": "#22c55e", "silver": "#e8e8e8", "silver_muted": "#a0a0a0", "border": "#2a2a2a", } def _build_matrix(plan: ProjectPlan) -> tuple[list[str], list[str], list[list[float]]]: """Return (team_names, week_labels, z_matrix) for the workload heatmap. Model: plan.workload is list[TeamWorkload]; each has team_name and weeks: list[WorkloadWeek]. """ if not plan.workload: return [], [], [] # Collect all unique week labels across all teams weeks_set: list[str] = [] for entry in plan.workload: for w in entry.weeks: if w.week_label not in weeks_set: weeks_set.append(w.week_label) weeks_sorted = sorted(weeks_set, key=_natural_sort_key) teams = [entry.team_name for entry in plan.workload] # Build load lookup: (team_name, week_label) -> load_score load_map: dict[tuple[str, str], float] = {} for entry in plan.workload: for w in entry.weeks: load_map[(entry.team_name, w.week_label)] = w.load_score z_matrix = [ [load_map.get((team, week), 0.0) for week in weeks_sorted] for team in teams ] return teams, weeks_sorted, z_matrix def render(plan: ProjectPlan) -> go.Figure: """Render a workload heatmap for the given ProjectPlan.""" fig = go.Figure() teams, weeks, z_matrix = _build_matrix(plan) if not teams: fig.add_annotation( text="No workload data", xref="paper", yref="paper", x=0.5, y=0.5, showarrow=False, font=dict(color=COLORS["silver_muted"], size=18), ) fig.update_layout(paper_bgcolor="rgba(0,0,0,0)", plot_bgcolor="rgba(0,0,0,0)") return fig # VIBGYOR: Violet (min/low load) → Red (max/high load) colorscale = [ [0.00, "#4B0082"], # Violet [0.17, "#0000FF"], # Blue [0.33, "#00BFFF"], # Cyan-Blue [0.50, "#00CC44"], # Green [0.67, "#FFD700"], # Yellow [0.83, "#FF7F00"], # Orange [1.00, "#CC0000"], # Deep Red ] fig.add_trace(go.Heatmap( z=z_matrix, x=weeks, y=teams, colorscale=colorscale, zmin=0, zmax=10, hovertemplate="Team: %{y}
Week: %{x}
Load: %{z}/10", colorbar=dict( title=dict(text="Load (0-10)", font=dict(color=COLORS["silver"])), tickfont=dict(color=COLORS["silver"]), bgcolor="rgba(0,0,0,0)", bordercolor="rgba(128,128,128,0.4)", ), )) fig.update_layout( template="plotly_dark", paper_bgcolor="rgba(0,0,0,0)", plot_bgcolor="rgba(0,0,0,0)", font=dict(color=COLORS["silver"]), xaxis=dict( showgrid=False, zeroline=False, color=COLORS["silver"], tickfont=dict(color=COLORS["silver"], size=13), ), yaxis=dict( showgrid=False, zeroline=False, color=COLORS["silver"], tickfont=dict(color=COLORS["silver"], size=13), ), margin=dict(l=20, r=20, t=40, b=20), ) return fig