-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathfetch.py
More file actions
496 lines (412 loc) · 16.9 KB
/
fetch.py
File metadata and controls
496 lines (412 loc) · 16.9 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
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
#!/usr/bin/env python3
"""
Fetch closed bugs from various ML framework GitHub repositories.
Supports: PyTorch, TensorRT, TensorFlow, JAX, and Triton.
"""
import requests
import json
from datetime import datetime, timedelta
from typing import List, Dict, Optional
import time
import argparse
import os
# Framework configurations with their GitHub repos and filters
FRAMEWORK_CONFIG = {
"pytorch": {
"repo": "pytorch/pytorch",
"filter": "is:issue is:closed label:triaged"
},
"tensorrt": {
"repo": "NVIDIA/TensorRT",
"filter": "is:issue is:closed label:triaged"
},
"tensorflow": {
"repo": "tensorflow/tensorflow",
"filter": "is:issue is:closed label:type:bug"
},
"jax": {
"repo": "jax-ml/jax",
"filter": "is:issue is:closed label:bug"
},
"triton": {
"repo": "triton-lang/triton",
"filter": "is:issue is:closed label:bug"
}
}
def fetch_bugs_for_date_range(
framework: str,
start_date: str,
end_date: str,
per_page: int = 100,
max_pages: Optional[int] = None,
custom_filter: Optional[str] = None
) -> List[Dict]:
"""
Fetch closed bugs from a specific ML framework repository for a date range.
Args:
framework: Name of the ML framework
start_date: Start date in YYYY-MM-DD format
end_date: End date in YYYY-MM-DD format
per_page: Number of results per page (max 100)
max_pages: Maximum number of pages to fetch (None for all)
custom_filter: Optional custom filter to override the default filter
Returns:
List of bug issues
"""
if framework not in FRAMEWORK_CONFIG:
raise ValueError(f"Unknown framework: {framework}. Supported: {list(FRAMEWORK_CONFIG.keys())}")
config = FRAMEWORK_CONFIG[framework]
repo = config["repo"]
# Build query with date range
if custom_filter:
query = f"repo:{repo} {custom_filter} created:{start_date}..{end_date}"
else:
query = f"repo:{repo} {config['filter']} created:{start_date}..{end_date}"
all_bugs = []
page = 1
headers = {
"Accept": "application/vnd.github.v3+json",
}
# Add GitHub token if available (from environment variable)
github_token = os.environ.get('GITHUB_TOKEN')
if github_token:
headers["Authorization"] = f"token {github_token}"
print(f"✓ Using GitHub token for authentication (token starts with: {github_token[:20]}...)")
print(" Rate limits: 30 requests/minute, 5000 requests/hour")
else:
print("✗ No GitHub token found. Consider setting GITHUB_TOKEN environment variable.")
print(" Rate limits: 10 requests/minute, 60 requests/hour")
print(f"Fetching {framework.upper()} bugs for {start_date} to {end_date}...")
print(f"Query: {query}")
base_url = "https://api.github.com/search/issues"
while True:
params = {
"q": query,
"per_page": per_page,
"page": page,
"sort": "created",
"order": "desc"
}
response = None
try:
response = requests.get(base_url, params=params, headers=headers)
response.raise_for_status()
# Debug: Show rate limit info on first request
if page == 1:
rate_limit = response.headers.get('X-RateLimit-Limit', 'N/A')
rate_remaining = response.headers.get('X-RateLimit-Remaining', 'N/A')
print(f" Rate limit: {rate_remaining}/{rate_limit} requests remaining")
data = response.json()
if "items" not in data:
print(f"Unexpected response format: {data}")
break
bugs = data["items"]
all_bugs.extend(bugs)
# Print progress
print(f"Page {page}: fetched {len(bugs)} bugs (total: {len(all_bugs)})")
# Check if we've fetched all results
if len(bugs) < per_page:
print("No more results.")
break
# Check if we've reached the maximum number of pages
if max_pages and page >= max_pages:
print(f"Reached maximum page limit ({max_pages}).")
break
# GitHub search API has a limit of 1000 results
if len(all_bugs) >= 1000:
print("\nReached GitHub's 1000 result limit for search API.")
print("To get more results, use a more specific filter or date range.")
break
# GitHub API rate limiting - be nice
time.sleep(1)
page += 1
except requests.exceptions.HTTPError as e:
if e.response.status_code == 403:
# Rate limit hit - check headers for reset time
reset_time = e.response.headers.get('X-RateLimit-Reset')
remaining = e.response.headers.get('X-RateLimit-Remaining', '0')
if reset_time:
reset_datetime = datetime.fromtimestamp(int(reset_time))
wait_time = (reset_datetime - datetime.now()).total_seconds()
wait_time = max(wait_time, 60) # Wait at least 60 seconds
else:
wait_time = 300 # Default 5 minutes if no reset time
print(f"\nRate limit exceeded (remaining: {remaining})")
print(f"Waiting {int(wait_time)} seconds until reset...")
print("Consider adding a GitHub token for higher rate limits")
# Show progress while waiting
for i in range(int(wait_time), 0, -10):
print(f" Resuming in {i} seconds...", end='\r')
time.sleep(min(i, 10))
print("\nResuming...")
continue # Retry the same page
elif e.response.status_code == 422:
# GitHub returns 422 when trying to access beyond 1000 results
print(f"\nReached GitHub's 1000 result limit for this date range.")
break
else:
print(f"Error fetching page {page}: {e}")
break
except requests.exceptions.RequestException as e:
print(f"Error fetching page {page}: {e}")
break
print(f"Fetched {len(all_bugs)} bugs for this date range")
return all_bugs
def fetch_framework_bugs(
framework: str,
since_date: str,
per_page: int = 100,
max_pages: Optional[int] = None,
output_file: Optional[str] = None,
custom_filter: Optional[str] = None,
chunk_days: int = 30
) -> List[Dict]:
"""
Fetch closed bugs from a specific ML framework repository, automatically
splitting date ranges to handle GitHub's 1000 result limit.
Args:
framework: Name of the ML framework
since_date: Date in YYYY-MM-DD format to fetch bugs from
per_page: Number of results per page (max 100)
max_pages: Maximum number of pages to fetch (None for all)
output_file: Optional file to save results to
custom_filter: Optional custom filter to override the default filter
chunk_days: Number of days per chunk (default 30)
Returns:
List of bug issues
"""
print(f"Fetching closed {framework.upper()} bugs created since {since_date}...")
# Parse dates
start_date = datetime.strptime(since_date, "%Y-%m-%d")
end_date = datetime.now()
all_bugs = []
current_start = start_date
# Process in chunks
while current_start < end_date:
# Calculate chunk end date
current_end = min(current_start + timedelta(days=chunk_days), end_date)
# Fetch bugs for this chunk
chunk_bugs = fetch_bugs_for_date_range(
framework=framework,
start_date=current_start.strftime("%Y-%m-%d"),
end_date=current_end.strftime("%Y-%m-%d"),
per_page=per_page,
max_pages=max_pages,
custom_filter=custom_filter
)
all_bugs.extend(chunk_bugs)
# If we got close to 1000 results, use smaller chunks
if len(chunk_bugs) >= 900:
chunk_days = max(7, chunk_days // 2)
print(f"Reducing chunk size to {chunk_days} days due to high result count")
# Move to next chunk
current_start = current_end + timedelta(days=1)
print(f"\nTotal bugs fetched across all date ranges: {len(all_bugs)}")
# Remove duplicates (in case of date boundary issues)
unique_bugs = []
seen_ids = set()
for bug in all_bugs:
if bug['id'] not in seen_ids:
unique_bugs.append(bug)
seen_ids.add(bug['id'])
if len(unique_bugs) < len(all_bugs):
print(f"Removed {len(all_bugs) - len(unique_bugs)} duplicate bugs")
# Filter out issues marked as duplicates and bot-created issues
filtered_bugs = []
duplicate_count = 0
bot_count = 0
for bug in unique_bugs:
# Check if any label indicates this is a duplicate
is_duplicate = any(
'duplicate' in label['name'].lower()
for label in bug.get('labels', [])
)
# Check if created by a bot
is_bot_created = False
if bug.get('user', {}).get('login', '').endswith('[bot]'):
is_bot_created = True
# Also check for common bot patterns in usernames
bot_patterns = ['bot]', 'automation', 'ci-', 'test-bot', 'github-actions']
username = bug.get('user', {}).get('login', '').lower()
if any(pattern in username for pattern in bot_patterns):
is_bot_created = True
# Check for automated test failure patterns in titles
if 'DISABLED test_' in bug.get('title', '') or 'CI failure' in bug.get('title', ''):
is_bot_created = True
if not is_duplicate and not is_bot_created:
filtered_bugs.append(bug)
elif is_duplicate:
duplicate_count += 1
elif is_bot_created:
bot_count += 1
if duplicate_count > 0:
print(f"Filtered out {duplicate_count} issues marked as duplicates")
if bot_count > 0:
print(f"Filtered out {bot_count} bot-created issues")
# Save to file if requested
if output_file:
with open(output_file, 'w') as f:
json.dump(filtered_bugs, f, indent=2)
print(f"Results saved to: {output_file}")
return filtered_bugs
def analyze_bugs(bugs: List[Dict], framework: str) -> None:
"""Analyze and print summary statistics about the bugs."""
if not bugs:
print("No bugs to analyze.")
return
print(f"\n=== {framework.upper()} Bug Analysis ===")
print(f"Total closed bugs: {len(bugs)}")
# Count by close reason
close_reasons = {}
labels_count = {}
for bug in bugs:
# Count labels
for label in bug.get("labels", []):
label_name = label["name"]
labels_count[label_name] = labels_count.get(label_name, 0) + 1
# Analyze state reason if available
state_reason = bug.get("state_reason", "unknown")
close_reasons[state_reason] = close_reasons.get(state_reason, 0) + 1
print("\nClose reasons:")
for reason, count in sorted(close_reasons.items(), key=lambda x: x[1], reverse=True):
print(f" {reason}: {count}")
print("\nTop 15 labels:")
for label, count in sorted(labels_count.items(), key=lambda x: x[1], reverse=True)[:15]:
print(f" {label}: {count}")
# Show some example bugs
print("\nExample bugs (first 5):")
for i, bug in enumerate(bugs[:5]):
print(f"\n{i+1}. #{bug['number']}: {bug['title']}")
print(f" Created: {bug['created_at']}")
print(f" Closed: {bug['closed_at']}")
print(f" Labels: {', '.join([l['name'] for l in bug['labels']])}")
print(f" URL: {bug['html_url']}")
def list_frameworks():
"""List all supported frameworks and their configurations."""
print("Supported ML Frameworks:")
print("=" * 60)
for name, config in FRAMEWORK_CONFIG.items():
print(f"\n{name.upper()}")
print(f" Repository: {config['repo']}")
print(f" Default filter: {config['filter']}")
if __name__ == "__main__":
parser = argparse.ArgumentParser(
description="Fetch closed bugs from ML framework GitHub repositories",
formatter_class=argparse.RawDescriptionHelpFormatter,
epilog="""
Examples:
# Fetch PyTorch bugs from last 1400 days
python fetch_ml_framework_bugs.py --framework pytorch --days 1400
# Fetch TensorFlow bugs with custom filter
python fetch_ml_framework_bugs.py --framework tensorflow --custom-filter "is:issue is:closed label:comp:gpu"
# List all supported frameworks
python fetch_ml_framework_bugs.py --list-frameworks
"""
)
parser.add_argument(
"--framework",
type=str,
choices=list(FRAMEWORK_CONFIG.keys()) + ['all'],
help="ML framework to fetch bugs from (use 'all' for all frameworks)"
)
parser.add_argument(
"--days",
type=int,
default=1825, # 5 years = 5 * 365 days
help="Number of days to look back (default: 1825 = 5 years)"
)
parser.add_argument(
"--max-pages",
type=int,
default=None,
help="Maximum number of pages to fetch (default: all)"
)
parser.add_argument(
"--output",
type=str,
help="Output file for results (default: {framework}_issues.json)"
)
parser.add_argument(
"--custom-filter",
type=str,
help="Custom filter to override the default (e.g., 'is:issue is:closed label:bug')"
)
parser.add_argument(
"--no-analysis",
action="store_true",
help="Skip the analysis output"
)
parser.add_argument(
"--list-frameworks",
action="store_true",
help="List all supported frameworks and their configurations"
)
parser.add_argument(
"--chunk-days",
type=int,
default=30,
help="Days per chunk when splitting date ranges (default: 30)"
)
args = parser.parse_args()
# Handle list frameworks
if args.list_frameworks:
list_frameworks()
exit(0)
# Check if framework is provided
if not args.framework:
parser.error("--framework is required unless using --list-frameworks")
# Set default output file if not provided
if not args.output:
args.output = f"{args.framework}_issues.json"
# Calculate the date to search from
since_date = (datetime.now() - timedelta(days=args.days)).strftime("%Y-%m-%d")
# Handle "all" framework option
if args.framework == 'all':
all_bugs = []
for fw_name in FRAMEWORK_CONFIG.keys():
print(f"\n{'='*60}")
print(f"Fetching bugs for {fw_name.upper()}")
print(f"{'='*60}")
# Always save individual files for each framework
fw_output = f"{fw_name}_issues.json"
fw_bugs = fetch_framework_bugs(
framework=fw_name,
since_date=since_date,
max_pages=args.max_pages,
output_file=fw_output,
custom_filter=args.custom_filter,
chunk_days=args.chunk_days
)
# Add framework field to each bug
for bug in fw_bugs:
bug['framework'] = fw_name
all_bugs.extend(fw_bugs)
# Save combined results
if args.output:
with open(args.output, 'w') as f:
json.dump(all_bugs, f, indent=2)
print(f"\nCombined results saved to: {args.output}")
# Analyze combined results
if not args.no_analysis:
print(f"\n{'='*60}")
print("COMBINED ANALYSIS FOR ALL FRAMEWORKS")
print(f"{'='*60}")
analyze_bugs(all_bugs, "ALL FRAMEWORKS")
# Per-framework breakdown
print("\nPer-framework breakdown:")
for fw_name in FRAMEWORK_CONFIG.keys():
fw_bugs = [b for b in all_bugs if b.get('framework') == fw_name]
print(f" {fw_name}: {len(fw_bugs)} bugs")
else:
# Single framework
bugs = fetch_framework_bugs(
framework=args.framework,
since_date=since_date,
max_pages=args.max_pages,
output_file=args.output,
custom_filter=args.custom_filter,
chunk_days=args.chunk_days
)
# Analyze the results
if not args.no_analysis:
analyze_bugs(bugs, args.framework)