-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathanalyse_transit.py
More file actions
373 lines (310 loc) · 14.3 KB
/
Copy pathanalyse_transit.py
File metadata and controls
373 lines (310 loc) · 14.3 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
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
# analyse_transit.py
import streamlit as st
import pandas as pd
import os
import datetime
from generate_digest import generate_monthly_digest
from datetime import timezone
from collections import Counter
from wordcloud import WordCloud
import matplotlib.pyplot as plt
from github import Github
# --- Step 1: Data Loading ---
BASE_PATH = "github_export"
FILES_TO_LOAD = {
"issues": "issues.json",
"issues_comments": "issues_comments.json",
"pulls": "pulls.json",
"pr_comments": "pr_comments.json",
}
@st.cache_data
def load_all_data():
dataframes = {}
for key, file_name in FILES_TO_LOAD.items():
path = os.path.join(BASE_PATH, file_name)
try:
dataframes[key] = pd.read_json(path)
except FileNotFoundError:
st.error(f"File not found: {path}")
return None
except Exception as e:
st.error(f"Error loading {file_name}: {e}")
return None
return dataframes
all_data = load_all_data()
if all_data and all(k in all_data for k in ["issues", "issues_comments", "pr_comments", "pulls"]):
issues_df = all_data["issues"]
issues_comments_df = all_data["issues_comments"]
pr_comments_df = all_data["pr_comments"]
pulls_df = all_data["pulls"]
# --- Data Cleaning ---
for col in ['created_at', 'updated_at', 'closed_at']:
if col in issues_df.columns:
issues_df[col] = pd.to_datetime(issues_df[col], errors='coerce', utc=True)
if 'user' in issues_df.columns:
issues_df['username'] = issues_df['user'].apply(lambda u: u.get('login') if isinstance(u, dict) else None)
issues_comments_df['body'] = issues_comments_df['body'].fillna('')
pr_comments_df['body'] = pr_comments_df['body'].fillna('')
# --- Streamlit UI ---
st.title("🚆 Google Transit - Contribution Analyzer")
st.info("Use the sidebar to generate a monthly digest or filter issues/PRs.")
# --- Sidebar for Digest ---
st.sidebar.header("🗓️ Monthly Digest")
year = st.sidebar.selectbox("Year", [2025, 2024, 2023, 2022, 2021, 2020, 2019, 2018, 2017, 2016, 2015], index=0)
month = st.sidebar.selectbox("Month", list(range(1, 13)), index=datetime.date.today().month - 1)
# -------------------------------
# 1. GTFS DIGEST
# -------------------------------
# --- Sélection du mois et de l'année dans la page principale ---
st.header("🗓️ Generate Monthly Digest")
col1, col2 = st.columns(2)
with col1:
year = st.selectbox("Year", [2025, 2024, 2023, 2022, 2021, 2020, 2019, 2018, 2017, 2016, 2015], index=0)
with col2:
month = st.selectbox("Month", list(range(1, 13)), index=datetime.date.today().month - 1)
# --- Bouton Generate Digest ---
if st.button(f"✨Generate {month}-{year} Digest"):
with st.spinner("Fetching data from GitHub..."):
issues, pulls, issue_comments, pr_comments = generate_monthly_digest(
st.secrets["github"]["token"],
year,
month
)
start_date = datetime.datetime(year, month, 1, tzinfo=timezone.utc)
end_date = (start_date + pd.offsets.MonthEnd(1)).to_pydatetime().replace(tzinfo=timezone.utc)
def created_within(item):
dt = pd.to_datetime(item.get('created_at'), utc=True)
return start_date <= dt <= end_date
# --- 1a. Top Contributors ---
contrib_counter = Counter()
for i in issues:
if created_within(i):
login = i.get('user', {}).get('login')
if login: contrib_counter[login] += 1
for p in pulls:
if created_within(p):
login = p.get('user', {}).get('login')
if login: contrib_counter[login] += 1
for c in issue_comments + pr_comments:
created = pd.to_datetime(c.get('created_at'), utc=True)
if start_date <= created <= end_date:
login = c.get('user', {}).get('login')
if login: contrib_counter[login] += 1
contrib_df = pd.DataFrame.from_dict(contrib_counter, orient='index', columns=['count']).sort_values('count', ascending=False)
st.subheader("1️⃣ Top Contributors (comments, issues, PRs)")
st.bar_chart(contrib_df.head(10))
# --- 1b. Most Commented ---
st.subheader("2️⃣ Most Commented Issues & PRs")
comment_counts = {}
for c in issue_comments + pr_comments:
created = pd.to_datetime(c.get('created_at'), utc=True)
if start_date <= created <= end_date:
num = c.get('issue_url') or c.get('pull_request_url')
if num:
num = num.split('/')[-1]
comment_counts[num] = comment_counts.get(num, 0) + 1
top_comments = sorted(comment_counts.items(), key=lambda x: x[1], reverse=True)[:10]
for num, count in top_comments:
match = next((i for i in issues if str(i.get('number')) == str(num)), None)
if not match:
match = next((p for p in pulls if str(p.get('number')) == str(num)), None)
if match:
st.markdown(f"- [{match.get('title')}]({match.get('html_url')}) - {count} comments")
# --- 1c. Word Cloud ---
st.subheader("3️⃣ Word Cloud (issues, PRs, comments)")
text_corpus = []
for i in issues + pulls:
if created_within(i):
text_corpus.append(i.get('title',''))
for c in issue_comments + pr_comments:
created = pd.to_datetime(c.get('created_at'), utc=True)
if start_date <= created <= end_date:
text_corpus.append(c.get('body',''))
stopwords = {
"the","with","that","this","what","would","could","should","have","from",
"your","their","there","were","when","them","then","also","these","they",
"been","about","than","into","like","over","some","such","only","more",
"most","many","make","just","will","who","whom","each","other","after",
"before","while","where","here","how","why","which","and","but","for","are",
"not","you","all","our","its","was","had","can","may","get","use","using",
"does","did","done","very", "it's", "that's"
}
words = [w.lower() for txt in text_corpus for w in txt.split()
if len(w) >= 4 and w.lower() not in stopwords]
word_freq = dict(Counter(words).most_common(10))
if word_freq:
wc = WordCloud(width=800, height=400, background_color='white').generate_from_frequencies(word_freq)
fig, ax = plt.subplots(figsize=(10,5))
ax.imshow(wc, interpolation='bilinear')
ax.axis('off')
st.pyplot(fig)
else:
st.info("No words to generate word cloud.")
# --- 1d. New Contributors ---
st.subheader("🆕 New Contributors This Month")
def extract_logins_df(df):
"""Extrait les logins depuis une colonne 'user' ou 'username' d'un DataFrame"""
if 'username' in df.columns:
return set(df['username'].dropna())
elif 'user' in df.columns:
return set(df['user'].apply(lambda u: u.get('login') if isinstance(u, dict) else None).dropna())
return set()
# --- Contributeurs du mois sélectionné ---
month_issues = issues_df[
(issues_df['created_at'] >= pd.Timestamp(year, month, 1, tz='UTC')) &
(issues_df['created_at'] <= pd.Timestamp(year, month, 1, tz='UTC') + pd.offsets.MonthEnd(1))
]
month_pulls = pulls_df[
(pulls_df['created_at'] >= pd.Timestamp(year, month, 1, tz='UTC')) &
(pulls_df['created_at'] <= pd.Timestamp(year, month, 1, tz='UTC') + pd.offsets.MonthEnd(1))
]
month_issue_comments = issues_comments_df[
(issues_comments_df['created_at'] >= pd.Timestamp(year, month, 1, tz='UTC')) &
(issues_comments_df['created_at'] <= pd.Timestamp(year, month, 1, tz='UTC') + pd.offsets.MonthEnd(1))
]
month_pr_comments = pr_comments_df[
(pr_comments_df['created_at'] >= pd.Timestamp(year, month, 1, tz='UTC')) &
(pr_comments_df['created_at'] <= pd.Timestamp(year, month, 1, tz='UTC') + pd.offsets.MonthEnd(1))
]
current_month_contribs = (
extract_logins_df(month_issues) |
extract_logins_df(month_pulls) |
extract_logins_df(month_issue_comments) |
extract_logins_df(month_pr_comments)
)
# --- Contributeurs avant le mois sélectionné ---
past_issues = issues_df[issues_df['created_at'] < pd.Timestamp(year, month, 1, tz='UTC')]
past_pulls = pulls_df[pulls_df['created_at'] < pd.Timestamp(year, month, 1, tz='UTC')]
past_issue_comments = issues_comments_df[issues_comments_df['created_at'] < pd.Timestamp(year, month, 1, tz='UTC')]
past_pr_comments = pr_comments_df[pr_comments_df['created_at'] < pd.Timestamp(year, month, 1, tz='UTC')]
past_contribs = (
extract_logins_df(past_issues) |
extract_logins_df(past_pulls) |
extract_logins_df(past_issue_comments) |
extract_logins_df(past_pr_comments)
)
# --- Nouveaux contributeurs ---
new_contribs = current_month_contribs - past_contribs
if new_contribs:
for login in sorted(new_contribs):
profile_url = f"https://github.com/{login}"
st.markdown(f"- [{login}]({profile_url})")
else:
st.info("No new contributors this month.")
# -------------------------------
# 2. State of PR (active PRs)
# -------------------------------
@st.cache_data(ttl=3600) #Cache for 1 hour
def fetch_live_prs():
token = st.secrets["github"]["token"]
gh = Github(token)
repo = gh.get_repo("google/transit")
pulls = repo.get_pulls(state="open")
prs = []
for pr in pulls:
prs.append({
"number": pr.number,
"title": pr.title,
"html_url": pr.html_url,
"labels": [ {"name": lbl.name} for lbl in pr.get_labels() ]
})
return prs
st.header("🚨 State of Active PRs (Live)")
live_prs = fetch_live_prs()
label_to_prs = {
"Discussion Period": [p for p in live_prs if any(l["name"] == "Discussion Period" for l in p["labels"])],
"Vote to Adopt": [p for p in live_prs if any(l["name"] == "Vote to Adopt" for l in p["labels"])],
"Vote to Test": [p for p in live_prs if any(l["name"] == "Vote to Test" for l in p["labels"])]
}
if not any(label_to_prs.values()):
st.info("No active PRs found at the moment.")
else:
for label, prs in label_to_prs.items():
if prs:
st.markdown(f"**{label}**")
for p in prs:
st.markdown(f"- [{p['title']}]({p['html_url']})")
# -------------------------------
# 3. Searchable Stats (Issues/PRs + Comments)
# -------------------------------
st.header("🔍 Searchable Stats")
st.sidebar.header("Filters for Search")
# --- Sidebar Filters ---
users = sorted(issues_df['username'].dropna().unique()) if 'username' in issues_df.columns else []
selected_users = st.sidebar.multiselect("Filter by user", users)
min_date = min(issues_df['created_at'].min(), pulls_df['created_at'].min()).date()
max_data_date = max(
issues_df['created_at'].max(),
pulls_df['created_at'].max(),
issues_comments_df['created_at'].max(),
pr_comments_df['created_at'].max()
).date()
today = datetime.date.today()
max_date = max(max_data_date, today)
selected_dates = st.sidebar.date_input(
"Filter by creation date",
value=(min_date, max_date),
min_value=min_date,
max_value=max_date
)
search_term = st.sidebar.text_input("Search keyword in title or comments")
# --- Filter Issues/PRs ---
filtered_issues = issues_df.copy()
filtered_pulls = pulls_df.copy()
if selected_users:
filtered_issues = filtered_issues[filtered_issues['username'].isin(selected_users)]
filtered_pulls = filtered_pulls[filtered_pulls['user'].apply(lambda u: u.get('login') if isinstance(u, dict) else None).isin(selected_users)]
if len(selected_dates) == 2:
start_date, end_date = pd.to_datetime(selected_dates[0]), pd.to_datetime(selected_dates[1])
filtered_issues = filtered_issues[
(filtered_issues['created_at'].dt.date >= start_date.date()) &
(filtered_issues['created_at'].dt.date <= end_date.date())
]
filtered_pulls = filtered_pulls[
(filtered_pulls['created_at'].dt.date >= start_date.date()) &
(filtered_pulls['created_at'].dt.date <= end_date.date())
]
if search_term:
# Filter titles
issue_title_mask = filtered_issues['title'].str.contains(search_term, case=False, na=False)
pull_title_mask = filtered_pulls['title'].str.contains(search_term, case=False, na=False)
filtered_issues = filtered_issues[issue_title_mask]
filtered_pulls = filtered_pulls[pull_title_mask]
# --- Display Issues & PRs ---
st.subheader(f"Issues found: {len(filtered_issues)}")
st.dataframe(filtered_issues[['number', 'title', 'username', 'state', 'created_at']])
st.subheader(f"Pull Requests found: {len(filtered_pulls)}")
st.dataframe(filtered_pulls[['number', 'title', 'user', 'state', 'created_at']])
# --- Filter Comments ---
filtered_issue_comments = issues_comments_df.copy()
filtered_pr_comments = pr_comments_df.copy()
# Filter by date
if len(selected_dates) == 2:
filtered_issue_comments = filtered_issue_comments[
(pd.to_datetime(filtered_issue_comments['created_at']).dt.date >= start_date.date()) &
(pd.to_datetime(filtered_issue_comments['created_at']).dt.date <= end_date.date())
]
filtered_pr_comments = filtered_pr_comments[
(pd.to_datetime(filtered_pr_comments['created_at']).dt.date >= start_date.date()) &
(pd.to_datetime(filtered_pr_comments['created_at']).dt.date <= end_date.date())
]
# Filter by search term
if search_term:
filtered_issue_comments = filtered_issue_comments[filtered_issue_comments['body'].str.contains(search_term, case=False, na=False)]
filtered_pr_comments = filtered_pr_comments[filtered_pr_comments['body'].str.contains(search_term, case=False, na=False)]
# --- Extract pseudo/login ---
filtered_issue_comments['login'] = filtered_issue_comments['user'].apply(lambda u: u.get('login') if isinstance(u, dict) else None)
filtered_pr_comments['login'] = filtered_pr_comments['user'].apply(lambda u: u.get('login') if isinstance(u, dict) else None)
# Add type column
filtered_issue_comments['type'] = 'Issue Comment'
filtered_pr_comments['type'] = 'PR Comment'
# Combine comments
filtered_comments = pd.concat([filtered_issue_comments, filtered_pr_comments], ignore_index=True)
# --- Top Contributors (comments) ---
top_contributors = filtered_comments['login'].value_counts().head(9).reset_index()
top_contributors.columns = ['Contributor', 'Comments Count']
st.subheader("🏆 Top Contributors (comments)")
st.dataframe(top_contributors)
# --- Display filtered comments ---
st.subheader(f"Comments found: {len(filtered_comments)}")
st.dataframe(filtered_comments[['login','type','body','created_at']].rename(columns={'login':'Contributor'}))