|
| 1 | +import json |
| 2 | +from datetime import datetime, timedelta |
| 3 | +from collections import defaultdict |
| 4 | +from typing import Dict, List, Optional |
| 5 | +import argparse |
| 6 | +from copy import deepcopy |
| 7 | + |
| 8 | +def parse_timestamp(ts: str) -> datetime: |
| 9 | + """Parse GitHub timestamp format to datetime""" |
| 10 | + return datetime.strptime(ts, "%Y-%m-%dT%H:%M:%SZ") |
| 11 | + |
| 12 | +def get_activity_period(timestamp: str, period: str = "daily") -> str: |
| 13 | + """Convert timestamp to period key (daily/weekly/monthly)""" |
| 14 | + dt = parse_timestamp(timestamp) |
| 15 | + if period == "daily": |
| 16 | + return dt.strftime("%Y-%m-%d") |
| 17 | + elif period == "weekly": |
| 18 | + # Get start of week (Monday) |
| 19 | + start = dt - timedelta(days=dt.weekday()) |
| 20 | + return start.strftime("%Y-%m-%d") |
| 21 | + else: # monthly |
| 22 | + return dt.strftime("%Y-%m") |
| 23 | + |
| 24 | +def aggregate_contributor_data(data: Dict, period: str) -> Dict[str, List]: |
| 25 | + """Aggregate contributor data by time period""" |
| 26 | + period_data = defaultdict(lambda: defaultdict(lambda: { |
| 27 | + "contributor": "", |
| 28 | + "score": 0, |
| 29 | + "summary": "", |
| 30 | + "avatar_url": "", |
| 31 | + "activity": { |
| 32 | + "code": { |
| 33 | + "total_commits": 0, |
| 34 | + "total_prs": 0, |
| 35 | + "commits": [], |
| 36 | + "pull_requests": [] |
| 37 | + }, |
| 38 | + "issues": { |
| 39 | + "total_opened": 0, |
| 40 | + "opened": [] |
| 41 | + }, |
| 42 | + "engagement": { |
| 43 | + "total_comments": 0, |
| 44 | + "total_reviews": 0, |
| 45 | + "comments": [], |
| 46 | + "reviews": [] |
| 47 | + } |
| 48 | + } |
| 49 | + })) |
| 50 | + |
| 51 | + # Process each contributor |
| 52 | + for contrib in data: |
| 53 | + username = contrib["contributor"] |
| 54 | + |
| 55 | + # Process commits |
| 56 | + for commit in contrib["activity"]["code"]["commits"]: |
| 57 | + period_key = get_activity_period(commit["created_at"], period) |
| 58 | + period_data[period_key][username]["contributor"] = username |
| 59 | + period_data[period_key][username]["avatar_url"] = contrib["avatar_url"] |
| 60 | + period_data[period_key][username]["activity"]["code"]["commits"].append(commit) |
| 61 | + period_data[period_key][username]["activity"]["code"]["total_commits"] += 1 |
| 62 | + |
| 63 | + # Process PRs |
| 64 | + for pr in contrib["activity"]["code"]["pull_requests"]: |
| 65 | + period_key = get_activity_period(pr["created_at"], period) |
| 66 | + period_data[period_key][username]["contributor"] = username |
| 67 | + period_data[period_key][username]["avatar_url"] = contrib["avatar_url"] |
| 68 | + period_data[period_key][username]["activity"]["code"]["pull_requests"].append(pr) |
| 69 | + period_data[period_key][username]["activity"]["code"]["total_prs"] += 1 |
| 70 | + |
| 71 | + # Process issues |
| 72 | + for issue in contrib["activity"]["issues"]["opened"]: |
| 73 | + period_key = get_activity_period(issue["created_at"], period) |
| 74 | + period_data[period_key][username]["contributor"] = username |
| 75 | + period_data[period_key][username]["avatar_url"] = contrib["avatar_url"] |
| 76 | + period_data[period_key][username]["activity"]["issues"]["opened"].append(issue) |
| 77 | + period_data[period_key][username]["activity"]["issues"]["total_opened"] += 1 |
| 78 | + |
| 79 | + # Convert defaultdict to regular dict and list structure |
| 80 | + result = {} |
| 81 | + for period_key, contributors in period_data.items(): |
| 82 | + result[period_key] = list(contributors.values()) |
| 83 | + |
| 84 | + return result |
| 85 | + |
| 86 | +def save_period_data(data: Dict[str, List], output_dir: str, period: str): |
| 87 | + """Save aggregated data to appropriate directories""" |
| 88 | + import os |
| 89 | + from pathlib import Path |
| 90 | + |
| 91 | + # Create directory structure |
| 92 | + base_dir = Path(output_dir) |
| 93 | + period_dir = base_dir / period |
| 94 | + history_dir = period_dir / "history" |
| 95 | + |
| 96 | + os.makedirs(period_dir, exist_ok=True) |
| 97 | + os.makedirs(history_dir, exist_ok=True) |
| 98 | + |
| 99 | + # Save each period's data |
| 100 | + for date_key, contributors in data.items(): |
| 101 | + if not contributors: # Skip empty periods |
| 102 | + continue |
| 103 | + |
| 104 | + # Save current data |
| 105 | + current_file = period_dir / "scored.json" |
| 106 | + with open(current_file, 'w') as f: |
| 107 | + json.dump(contributors, f, indent=2) |
| 108 | + |
| 109 | + # Save historical copy |
| 110 | + history_file = history_dir / f"scored_{date_key}.json" |
| 111 | + with open(history_file, 'w') as f: |
| 112 | + json.dump(contributors, f, indent=2) |
| 113 | + |
| 114 | +def main(): |
| 115 | + parser = argparse.ArgumentParser(description="Aggregate GitHub activity data by time period") |
| 116 | + parser.add_argument("input_file", help="Input contributors JSON file") |
| 117 | + parser.add_argument("output_dir", help="Output directory for aggregated data") |
| 118 | + parser.add_argument("--periods", nargs="+", choices=["daily", "weekly", "monthly"], |
| 119 | + default=["daily", "weekly", "monthly"], |
| 120 | + help="Time periods to generate") |
| 121 | + args = parser.parse_args() |
| 122 | + |
| 123 | + # Load data |
| 124 | + print(f"\nLoading data from {args.input_file}...") |
| 125 | + with open(args.input_file) as f: |
| 126 | + data = json.load(f) |
| 127 | + |
| 128 | + # Process each time period |
| 129 | + for period in args.periods: |
| 130 | + print(f"\nProcessing {period} aggregation...") |
| 131 | + aggregated = aggregate_contributor_data(data, period) |
| 132 | + |
| 133 | + print(f"Saving {period} data...") |
| 134 | + save_period_data(aggregated, args.output_dir, period) |
| 135 | + |
| 136 | + # Print some stats |
| 137 | + total_periods = len(aggregated) |
| 138 | + total_contributions = sum(len(contributors) for contributors in aggregated.values()) |
| 139 | + print(f"Generated {total_periods} {period} periods with {total_contributions} total contributions") |
| 140 | + |
| 141 | + print("\nProcessing complete!") |
| 142 | + |
| 143 | +if __name__ == "__main__": |
| 144 | + main() |
0 commit comments