File size: 4,145 Bytes
0e7cb64
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
from pathlib import Path

import gradio as gr
import pandas as pd
import plotly.express as px
import plotly.graph_objects as go


CSV_PATH = Path("metrics.csv")


def load_data() -> pd.DataFrame:
    df = pd.read_csv(CSV_PATH)
    # Normalize/parse columns
    if "date" not in df.columns:
        raise ValueError("Expected a 'date' column in metrics.csv")
    df["date"] = pd.to_datetime(df["date"], errors="coerce")
    df = df.sort_values("date").reset_index(drop=True)
    # Ensure numeric columns are numeric
    for c in df.columns:
        if c == "date":
            continue
        df[c] = pd.to_numeric(df[c], errors="coerce")
    return df


def line_figure(df: pd.DataFrame, cols: list[str], title: str, yaxis_title: str = "") -> go.Figure:
    if not cols:
        # Empty placeholder so the UI doesn't error
        fig = go.Figure()
        fig.update_layout(title=f"{title} (no series selected)")
        return fig
    fig = px.line(
        df,
        x="date",
        y=cols,
        markers=True,
        title=title,
    )
    # Improve layout for time series
    fig.update_layout(
        legend_title_text="Series",
        xaxis_title="Date",
        yaxis_title=yaxis_title,
        hovermode="x unified",
        margin={"l": 50, "r": 20, "t": 50, "b": 40},
    )
    return fig


TAB_SPEC = {
    "Docstrings": [
        "docstring coverage",
        "docstring missing",
    ],
    "Size (Lines/Statements/Expressions/Parameters)": [
        "lines mean",
        "lines max",
        "lines 90th-percentile",
        "statements mean",
        "statements max",
        "statements 90th-percentile",
        "expressions mean",
        "expressions max",
        "expressions 90th-percentile",
        "parameters mean",
        "parameters max",
        "parameters 90th-percentile",
    ],
    "Complexity": [
        "cyclomatic_complexity mean",
        "cyclomatic_complexity max",
        "cyclomatic_complexity 90th-percentile",
    ],
    "Typing": [
        "type_coverage mean",
        "type_coverage min",
        "type_coverage 50th-percentile",
    ],
    "Duplication": [
        "duplication.score mean",
        "duplication.score max",
        "duplication.score 90th-percentile",
        "duplication.score 50th-percentile",
        "duplication.duplicated-lines total",
    ],
    "TODOs": [
        "todo_comments total",
    ],
    "CLOC (Repository scope)": [
        "files",
        "lines blank",
        "lines comment",
        "lines code",
    ],
}

Y_LABELS = {
    "Docstrings": "value",
    "Size (Lines/Statements/Expressions/Parameters)": "count",
    "Complexity": "complexity",
    "Typing": "fraction / coverage",
    "Duplication": "score / lines",
    "TODOs": "count",
    "CLOC (Repository scope)": "lines / files",
}

DF = load_data()

with gr.Blocks(title="Code Metrics – Time Series", fill_height=True) as demo:
    gr.Markdown(
        "## Diffusers Code Metrics Over Time\n"
        f"Loaded **{CSV_PATH}** with {len(DF)} rows spanning "
        f"{DF['date'].min().date()} β†’ {DF['date'].max().date()}.\n\n"
        "Use each tab to pick the series you want to plot."
    )

    with gr.Tabs():
        tab_controls = []
        tab_plots = []

        for tab_name, series in TAB_SPEC.items():
            available = [s for s in series if s in DF.columns]  # guard against missing cols
            with gr.Tab(tab_name):
                with gr.Row():
                    sel = gr.CheckboxGroup(
                        choices=available,
                        value=available,
                        label="Series",
                    )
                plot = gr.Plot(
                    value=line_figure(DF, available, tab_name, Y_LABELS.get(tab_name, "")),
                    show_label=False,
                )

                sel.change(
                    fn=lambda cols, t=tab_name: line_figure(DF, cols, t, Y_LABELS.get(t, "")),
                    inputs=sel,
                    outputs=plot,
                )

                tab_controls.append(sel)
                tab_plots.append(plot)


if __name__ == "__main__":
    demo.launch()