-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathprocess_archive.py
More file actions
289 lines (235 loc) · 8.72 KB
/
process_archive.py
File metadata and controls
289 lines (235 loc) · 8.72 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
#!/usr/bin/env python3
"""
GitHub Archive Processor
This script processes GitHub Archive files from archive/ directory,
extracts user-to-repo interactions, and saves them to cache/.
Usage:
python process_archive.py [--archive-dir DIR]
--archive-dir DIR: Directory containing archive files (default: archive)
"""
import os
import sys
import json
import gzip
import pandas as pd
import argparse
from datetime import datetime
from pathlib import Path
from tqdm import tqdm
from collections import defaultdict
import calendar
def extract_repo_from_url(repo_url):
"""Extract owner/repo from GitHub URL"""
if not repo_url:
return None
try:
# Handle different URL formats
if repo_url.startswith("https://github.com/"):
path = repo_url.replace("https://github.com/", "")
elif repo_url.startswith("https://api.github.com/repos/"):
path = repo_url.replace("https://api.github.com/repos/", "")
else:
return None
# Remove trailing slashes and extra path components
parts = path.rstrip("/").split("/")
if len(parts) >= 2:
return f"{parts[0]}/{parts[1]}"
except:
pass
return None
def process_event(event, interactions):
"""Process a single GitHub event and extract user-repo interactions"""
try:
event_type = event.get("type", "")
actor = event.get("actor", {})
repo = event.get("repo", {})
# Extract user info
user_login = actor.get("login")
if not user_login:
return
# Extract repo info
repo_name = repo.get("name")
if not repo_name:
repo_url = repo.get("url")
if repo_url:
repo_name = extract_repo_from_url(repo_url)
if not repo_name:
return
# Map GitHub event types to our interaction types
event_mapping = {
"PushEvent": "COMMIT_CODE",
"PullRequestEvent": "PULL_REQUEST_OPENED", # Will refine based on action
"IssuesEvent": "ISSUE_OPENED", # Will refine based on action
"WatchEvent": "STARRED",
"ForkEvent": "FORKED",
"CreateEvent": "CREATED",
"ReleaseEvent": "RELEASE_PUBLISHED",
"PullRequestReviewEvent": "PULL_REQUEST_REVIEWED",
"IssueCommentEvent": "ISSUE_COMMENTED",
"PullRequestReviewCommentEvent": "PULL_REQUEST_COMMENTED",
}
mapped_event = event_mapping.get(event_type)
# Handle special cases with payload inspection
if event_type == "PullRequestEvent":
payload = event.get("payload", {})
action = payload.get("action")
if action == "closed" and payload.get("pull_request", {}).get("merged"):
mapped_event = "PULL_REQUEST_MERGED"
elif action == "opened":
mapped_event = "PULL_REQUEST_OPENED"
elif action == "closed":
mapped_event = "PULL_REQUEST_CLOSED"
elif event_type == "IssuesEvent":
payload = event.get("payload", {})
action = payload.get("action")
if action == "opened":
mapped_event = "ISSUE_OPENED"
elif action == "closed":
mapped_event = "ISSUE_CLOSED"
if not mapped_event:
return
# Calculate event count (for PushEvent, use commit count)
event_count = 1
if event_type == "PushEvent":
payload = event.get("payload", {})
commits = payload.get("commits", [])
event_count = len(commits) if commits else 1
# Add to interactions
interaction_key = (user_login, repo_name, mapped_event)
interactions[interaction_key] += event_count
except Exception as e:
# Silently skip malformed events
pass
def process_archive_file(file_path, interactions):
"""Process a single archive file"""
events_processed = 0
try:
with gzip.open(file_path, "rt", encoding="utf-8") as f:
for line_num, line in enumerate(f, 1):
try:
event = json.loads(line.strip())
process_event(event, interactions)
events_processed += 1
except json.JSONDecodeError:
# Skip malformed JSON lines
continue
except Exception:
# Skip other errors silently
continue
except Exception as e:
print(f"Error processing {file_path}: {e}")
return events_processed
def get_month_year_from_files(archive_files):
"""Extract month and year from archive files and verify they're consistent"""
months_years = set()
days_found = set()
for file_path in archive_files:
filename = file_path.name
# Extract date from filename like "2015-01-01-0.json.gz"
try:
date_part = filename.split("-")
if len(date_part) >= 4: # year-month-day-hour.json.gz
year = date_part[0]
month = date_part[1]
day = date_part[2]
months_years.add((month, year))
days_found.add(int(day))
except:
continue
if len(months_years) == 0:
raise ValueError("No valid date found in archive files")
elif len(months_years) > 1:
raise ValueError(f"Multiple months/years found in archive: {months_years}")
month, year = months_years.pop()
# Check if all days in the month are present
year_int = int(year)
month_int = int(month)
days_in_month = calendar.monthrange(year_int, month_int)[1]
expected_days = set(range(1, days_in_month + 1))
missing_days = expected_days - days_found
if missing_days:
raise ValueError(
f"Missing days in month {month}/{year}: {sorted(missing_days)}"
)
return month, year
def main():
parser = argparse.ArgumentParser(
description="Process GitHub Archive files and extract interactions"
)
parser.add_argument(
"--archive-dir",
type=str,
default="archive",
help="Archive directory (default: archive)",
)
args = parser.parse_args()
print("GitHub Archive Processor")
print("========================")
print(f"Archive directory: {args.archive_dir}")
# Check archive directory
archive_dir = Path(args.archive_dir)
if not archive_dir.exists():
print(f"Error: Archive directory {archive_dir} does not exist")
return 1
# Get all .json.gz files
archive_files = list(archive_dir.glob("*.json.gz"))
if not archive_files:
print("No .json.gz files found in archive directory")
return 1
print(f"Found {len(archive_files)} archive files")
# Extract and verify month/year consistency
try:
month, year = get_month_year_from_files(archive_files)
print(f"Processing files for month {month}/{year}")
except ValueError as e:
print(f"Error: {e}")
return 1
# Process all files
print("Processing archive files...")
interactions = defaultdict(int)
total_events = 0
with tqdm(archive_files, desc="Processing files", unit="file") as pbar:
for file_path in pbar:
events_processed = process_archive_file(file_path, interactions)
total_events += events_processed
pbar.set_postfix(
{
"events": f"{total_events:,}",
"interactions": f"{len(interactions):,}",
}
)
print(f"✓ Processed {total_events:,} events")
print(f"✓ Found {len(interactions):,} unique interactions")
if not interactions:
print("No interactions found to save")
return 0
# Convert interactions to DataFrame
rows = []
for (user, repo, event_type), event_count in interactions.items():
rows.append(
{
"user": user,
"repo": repo,
"event_type": event_type,
"event_count": event_count,
}
)
df = pd.DataFrame(rows)
# Create cache directory
cache_dir = Path("cache")
cache_dir.mkdir(exist_ok=True)
# Save to CSV
output_file = cache_dir / f"interactions_{month}_{year}.csv"
df.to_csv(output_file, index=False)
print(f"✓ Saved {len(df):,} interactions to {output_file}")
# Show top interactions
print(f"\nTop 10 interactions:")
print("=" * 80)
top_interactions = df.nlargest(10, "event_count")
for _, row in top_interactions.iterrows():
print(
f"{row['user']:<20} {row['repo']:<30} {row['event_type']:<20} {row['event_count']:>6}"
)
return 0
if __name__ == "__main__":
sys.exit(main())