Repository navigation
Expand file tree
/
Copy pathgithub_commit_stats.py
More file actions
134 lines (106 loc) · 3.05 KB
/
Copy pathgithub_commit_stats.py
File metadata and controls
134 lines (106 loc) · 3.05 KB
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
"""
GitHub Commit Stats
Fetches your commit activity from GitHub and prints a pretty stats table.
Dependencies:
uv add requests rich python-dotenv
Setup:
Create a .env file with:
GITHUB_TOKEN=ghp_your_token
Usage:
uv run git-average-commit.py
Config:
Edit the time window:
timedelta(days=60)
Notes:
- Includes 0-commit days
- Median = typical day
- High std dev = bursty workflow
Security:
If you leak your token → revoke it immediately.
"""
import requests
from datetime import datetime, timedelta, UTC
import statistics
import os
from rich.console import Console
from rich.table import Table
from rich.panel import Panel
from dotenv import load_dotenv
console = Console()
load_dotenv()
# Token from env
GITHUB_TOKEN = os.getenv("GITHUB_TOKEN")
if not GITHUB_TOKEN:
console.print("[red]Error: Set GITHUB_TOKEN env variable[/red]")
exit()
headers = {"Authorization": f"Bearer {GITHUB_TOKEN}"}
# Time range
to_date = datetime.now(UTC)
from_date = to_date - timedelta(days=60)
query = f"""
{{
viewer {{
contributionsCollection(from: "{from_date.isoformat()}", to: "{to_date.isoformat()}") {{
contributionCalendar {{
weeks {{
contributionDays {{
contributionCount
date
}}
}}
}}
}}
}}
}}
"""
response = requests.post(
"https://api.github.com/graphql",
json={"query": query},
headers=headers
)
data = response.json()
days = [
day["contributionCount"]
for week in data["data"]["viewer"]["contributionsCollection"]["contributionCalendar"]["weeks"]
for day in week["contributionDays"]
]
# Stats
avg = sum(days) / len(days)
median = statistics.median(days)
std_dev = statistics.stdev(days)
std_dev_pop = statistics.pstdev(days)
active_days = [d for d in days if d > 0]
activity_ratio = len(active_days) / len(days)
mad = statistics.median([abs(x - median) for x in days])
p90 = sorted(days)[int(0.9 * len(days))]
max_day = max(days)
# Table
table = Table(title="GitHub Commit Stats", show_lines=True)
table.add_column("Metric", style="cyan", no_wrap=True)
table.add_column("Value", justify="right", style="bold")
table.add_row("From", str(from_date.date()))
table.add_row("To", str(to_date.date()))
table.add_row("Average commits/day", f"{avg:.2f}")
table.add_row("Median commits/day", str(median))
table.add_row("Std dev (sample)", f"{std_dev:.2f}")
table.add_row("Std dev (population)", f"{std_dev_pop:.2f}")
table.add_row("Active day ratio", f"{activity_ratio:.2%}")
table.add_row("Max commits/day", str(max_day))
table.add_row("MAD", f"{mad:.2f}")
table.add_row("90th percentile", str(p90))
console.print(table)
# Simple interpretation panel
if avg > median * 2:
pattern = "[yellow]Bursty workflow (spikes dominate)[/yellow]"
else:
pattern = "[green]Consistent workflow[/green]"
panel = Panel(
f"""
Median baseline: [bold]{median}[/bold] commits/day
High-intensity days (~p90): [bold]{p90}[/bold]
Activity ratio: [bold]{activity_ratio:.2%}[/bold]
Pattern: {pattern}
""",
title="Interpretation",
)
console.print(panel)