Skip to content

Instantly share code, notes, and snippets.

@Pikachuxxxx
Created December 29, 2025 05:24
Show Gist options
  • Select an option

  • Save Pikachuxxxx/7ca35ad4ae18c4d783614c528ad0946d to your computer and use it in GitHub Desktop.

Select an option

Save Pikachuxxxx/7ca35ad4ae18c4d783614c528ad0946d to your computer and use it in GitHub Desktop.
import argparse
import requests
from collections import defaultdict
from datetime import datetime
BASE_URL = "https://swarm.internal.company"
session = requests.Session()
session.verify = True
# ==================== API HELPERS ====================
def api_get(path, params=None):
r = session.get(f"{BASE_URL}{path}", params=params)
r.raise_for_status()
return r.json()
def get_user_reviews(user):
return api_get("/api/v9/reviews", {"author": user}).get("reviews", [])
def get_review_changes(review_id):
return api_get(f"/api/v9/reviews/{review_id}/changes").get("changes", [])
def get_change_diffs(change_id):
return api_get(f"/api/v9/changes/{change_id}/diffs").get("diffs", [])
# ==================== STATS HELPERS ====================
def parse_diff_stats(diff_text):
added = removed = 0
for line in diff_text.splitlines():
if line.startswith("+") and not line.startswith("+++"):
added += 1
elif line.startswith("-") and not line.startswith("---"):
removed += 1
return added, removed
def month_key(ts):
return datetime.fromtimestamp(ts).strftime("%Y-%m")
# ==================== PRETTY FORMATTING ====================
def big_banner(title):
return (
"\n"
"++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++\n"
f"++++ {title.center(52)} ++++\n"
"++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++"
)
def section(title):
return f"\n{title}\n" + "-" * len(title)
def kv(key, value, width=24):
return f" {key.ljust(width)} : {value}"
def footer():
return (
"\n"
"------------------------------------------------------------\n"
"+++ End of report +++\n"
"------------------------------------------------------------"
)
# ==================== REPORT CORE ====================
def generate_report(username):
total_added = 0
total_removed = 0
commits_per_month = defaultdict(int)
commit_sizes = []
reviews = get_user_reviews(username)
for review in reviews:
for change in get_review_changes(review["id"]):
commits_per_month[month_key(change["time"])] += 1
change_added = 0
change_removed = 0
for d in get_change_diffs(change["id"]):
if "diff" not in d:
continue
a, r = parse_diff_stats(d["diff"])
change_added += a
change_removed += r
total_added += change_added
total_removed += change_removed
commit_sizes.append(change_added + change_removed)
total_commits = sum(commits_per_month.values())
active_months = len(commits_per_month)
avg_commits_per_month = (
total_commits / active_months if active_months else 0
)
lines = []
lines.append(big_banner(f"SWARM REPORT — {username}"))
lines.append(section("User summary"))
lines.append(kv("User", username))
lines.append(kv("Total commits", total_commits))
lines.append(kv("Active months", active_months))
lines.append(kv("Avg commits / month", f"{avg_commits_per_month:.2f}"))
lines.append(section("Code churn"))
lines.append(kv("LOC added", total_added))
lines.append(kv("LOC removed", total_removed))
lines.append(kv("Net LOC", total_added - total_removed))
lines.append(section("Commit size"))
lines.append(kv(
"Average LOC",
sum(commit_sizes) // len(commit_sizes) if commit_sizes else 0
))
lines.append(kv("Largest commit", max(commit_sizes) if commit_sizes else 0))
lines.append(kv("Smallest commit", min(commit_sizes) if commit_sizes else 0))
lines.append(footer())
return "\n".join(lines)
# ==================== CLI ====================
def main():
parser = argparse.ArgumentParser(
description="Swarm contribution report (multi-user, readable formatting)"
)
parser.add_argument(
"users",
nargs="+",
help="Swarm usernames"
)
args = parser.parse_args()
for user in args.users:
report = generate_report(user)
print(report)
with open(f"{user}_swarm_report.txt", "w") as f:
f.write(report)
if __name__ == "__main__":
main()

Comments are disabled for this gist.