-
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmulti_year_summary.py
More file actions
310 lines (251 loc) · 11.1 KB
/
multi_year_summary.py
File metadata and controls
310 lines (251 loc) · 11.1 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
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
#!/usr/bin/env python3
"""
Multi-Year Summary Generator for Plone GitHub Statistics
Combines organisation statistics across multiple years into comprehensive
summary reports showing total activity across the specified period.
Supports any combination of years and generates both CSV and TXT outputs.
"""
import pandas as pd
import argparse
import sys
import subprocess
from pathlib import Path
def load_organisation_file(year):
"""Load organisation statistics file for a specific year."""
filename = f"{year}-plone-organisation-contributors.csv"
filepath = Path(filename)
if not filepath.exists():
print(f"❌ Missing file: {filename}")
print(f" Run: make run-organisation-stats-{year}")
return None
try:
df = pd.read_csv(filepath)
df['year'] = year
return df
except Exception as e:
print(f"❌ Error reading {filename}: {e}")
return None
def combine_multi_year_stats(years):
"""Combine organisation statistics across multiple years."""
year_count = len(years)
year_label = "Multi-Year" if year_count != 3 else "Three-Year"
print(f"{year_label} Plone Organisation Summary Generator")
print("=" * 50)
# Load data for each year
dataframes = []
for year in years:
print(f"Loading {year} organisation statistics...")
df = load_organisation_file(year)
if df is None:
return None
dataframes.append(df)
print(f" ✓ Loaded {len(df)} organisations for {year}")
# Combine all years
print("\nCombining data across years...")
all_data = pd.concat(dataframes, ignore_index=True)
# Aggregate by organisation across all years
print("Aggregating statistics by organisation...")
aggregated = all_data.groupby('organisation').agg({
'total_commits': 'sum',
'total_pull_requests': 'sum',
'contributors_count': lambda x: len(set(str(contributors) for contributors in x if pd.notna(contributors))),
'repositories_count': lambda x: len(set(str(repos) for repos in x if pd.notna(repos))),
'year': lambda x: sorted(x.unique())
}).reset_index()
# Create comprehensive contributor and repository lists
contributor_lists = []
repository_lists = []
years_active = []
first_contributions = []
last_contributions = []
for org in aggregated['organisation']:
org_data = all_data[all_data['organisation'] == org]
# Combine contributors across years
all_contributors = []
for contributors_str in org_data['contributors']:
if pd.notna(contributors_str):
contributors = [c.strip() for c in str(contributors_str).split(',')]
all_contributors.extend(contributors)
unique_contributors = sorted(list(set(all_contributors)))
contributor_lists.append(', '.join(unique_contributors))
# Combine repositories across years
all_repositories = []
for repos_str in org_data['repositories']:
if pd.notna(repos_str):
repositories = [r.strip() for r in str(repos_str).split(',')]
all_repositories.extend(repositories)
unique_repositories = sorted(list(set(all_repositories)))
repository_lists.append(', '.join(unique_repositories))
# Years active
years_active.append(', '.join(map(str, sorted(org_data['year'].unique()))))
# First and last contribution dates across all years
first_dates = pd.to_datetime(org_data['first_contribution'], errors='coerce')
last_dates = pd.to_datetime(org_data['last_contribution'], errors='coerce')
first_contributions.append(first_dates.min().strftime('%Y-%m-%d') if not first_dates.isna().all() else '')
last_contributions.append(last_dates.max().strftime('%Y-%m-%d') if not last_dates.isna().all() else '')
# Add computed columns
aggregated['contributors'] = contributor_lists
aggregated['repositories'] = repository_lists
aggregated['years_active'] = years_active
aggregated['first_contribution'] = first_contributions
aggregated['last_contribution'] = last_contributions
aggregated['repositories_count'] = [len(repos.split(', ')) if repos else 0 for repos in repository_lists]
aggregated['contributors_count'] = [len(contribs.split(', ')) if contribs else 0 for contribs in contributor_lists]
# Reorder columns for better readability
column_order = [
'organisation',
'total_commits',
'total_pull_requests',
'contributors_count',
'contributors',
'repositories_count',
'repositories',
'years_active',
'first_contribution',
'last_contribution'
]
result = aggregated[column_order]
# Sort by total pull requests descending
result = result.sort_values('total_pull_requests', ascending=False).reset_index(drop=True)
return result
def save_summary(df, years):
"""Save the multi-year summary to CSV and TXT."""
year_range = f"{min(years)}-{max(years)}"
year_count = len(years)
# Generate filenames based on year count
if year_count == 3:
csv_filename = f"summary-past-three-years-{year_range}.csv"
txt_filename = f"STATISTICS-PAST-THREE-YEARS.txt"
summary_type = "Three-year"
elif year_count == 5:
csv_filename = f"summary-past-five-years-{year_range}.csv"
txt_filename = f"STATISTICS-PAST-FIVE-YEARS.txt"
summary_type = "Five-year"
elif year_count == 10:
csv_filename = f"summary-past-ten-years-{year_range}.csv"
txt_filename = f"STATISTICS-PAST-TEN-YEARS.txt"
summary_type = "Ten-year"
else:
csv_filename = f"summary-past-{year_count}-years-{year_range}.csv"
txt_filename = f"STATISTICS-PAST-{year_count}-YEARS.txt"
summary_type = f"{year_count}-year"
# Save CSV file
df.to_csv(csv_filename, index=False)
# Prepare text content for both console output and file
if year_count == 10:
period_label = "the decade"
elif year_count == 5:
period_label = "the past 5 years"
else:
period_label = f"{year_count} years"
txt_content = []
txt_content.append(f"# {summary_type.title()} Summary Results ({year_range})")
txt_content.append("")
txt_content.append(f"- {len(df)} total organisations tracked across {period_label}")
txt_content.append(f"- {df['total_commits'].sum():,} total commits and {df['total_pull_requests'].sum():,} pull requests")
txt_content.append(f"- {df['contributors_count'].sum()} unique contributors across all organisations")
txt_content.append(f"- File generated: {csv_filename}")
# Add top individual contributors dynamically
top_contributors_lines = get_top_contributors(years, year_range)
if top_contributors_lines:
txt_content.append("")
txt_content.append(f"## Top 10 contributors by pull requests ({year_range})")
txt_content.append("")
txt_content.extend(top_contributors_lines)
# Add top organisations
txt_content.append("")
txt_content.append(f"## Top 10 organisations by pull requests ({year_range})")
txt_content.append("")
top_orgs = df.head(10)
for i, row in top_orgs.iterrows():
txt_content.append(f"{i+1}. {row['organisation']}: {row['total_pull_requests']:,} PRs ({row['total_commits']:,} commits)")
# Write to TXT file
with open(txt_filename, 'w', encoding='utf-8') as f:
f.write('\n'.join(txt_content))
# Print to console
print(f"\n✅ {summary_type} summary saved to: {csv_filename}")
print(f"✅ {summary_type} statistics saved to: {txt_filename}")
for line in txt_content:
print(line)
return csv_filename, txt_filename
def get_top_contributors(years, year_range):
"""Get top contributors dynamically by running the analysis."""
try:
# Try to run the top contributors analysis dynamically
import tempfile
import os
# Create a temporary script to run the top contributors analysis
temp_script = f"""
import sys
import os
sys.path.append('.')
# Suppress the verbose output from analyze_top_contributors
original_stdout = sys.stdout
sys.stdout = open(os.devnull, 'w')
from top_contributors_summary import analyze_top_contributors
years = {list(years)}
result_df = analyze_top_contributors(years, 10, 'total_pull_requests')
# Restore stdout and print only our desired format
sys.stdout.close()
sys.stdout = original_stdout
if result_df is not None:
for i in range(min(10, len(result_df))):
row = result_df.iloc[i]
print(f"{{i+1}}. {{row['username']}} - {{row['total_pull_requests']:,}} PRs ({{row['total_commits']:,}} commits)")
"""
with tempfile.NamedTemporaryFile(mode='w', suffix='.py', delete=False) as f:
f.write(temp_script)
temp_file = f.name
try:
result = subprocess.run([sys.executable, temp_file],
capture_output=True, text=True, timeout=30)
if result.returncode == 0:
# Parse the captured output (which contains the formatted list)
lines = result.stdout.strip().split('\n')
contributor_lines = []
printed_count = 0
for line in lines:
if line.strip() and (line.strip().startswith(tuple(f"{i}." for i in range(1, 11)))) and printed_count < 10:
contributor_lines.append(line.strip())
printed_count += 1
return contributor_lines
else:
return ["Individual contributor analysis not available"]
finally:
os.unlink(temp_file)
except Exception:
return ["Individual contributor analysis not available"]
def parse_arguments():
"""Parse command line arguments."""
parser = argparse.ArgumentParser(
description='Generate multi-year summary of Plone organisation statistics'
)
parser.add_argument(
'--years',
nargs='+',
type=int,
default=[2022, 2023, 2024],
help='Years to combine (default: 2022 2023 2024)'
)
return parser.parse_args()
def main():
"""Main execution function."""
args = parse_arguments()
years = sorted(args.years)
year_count = len(years)
summary_label = f"{year_count}-year" if year_count != 3 else "three-year"
print(f"Generating {summary_label} summary for: {', '.join(map(str, years))}")
print()
# Combine statistics
summary_df = combine_multi_year_stats(years)
if summary_df is None:
print("❌ Failed to generate summary. Please ensure all required files exist.")
return 1
# Save results
csv_filename, txt_filename = save_summary(summary_df, years)
print(f"\n🎉 {summary_label.title()} summary generation completed!")
print(f" Generated: {csv_filename}")
print(f" Generated: {txt_filename}")
return 0
if __name__ == "__main__":
sys.exit(main())