feat(processor): refactor byte tally logic for authored repositories only.

This commit is contained in:
shay
2026-04-20 14:19:26 -04:00
parent fdd6b61172
commit 599c1ed672
2 changed files with 47 additions and 13 deletions
+46 -12
View File
@@ -520,18 +520,45 @@ def compute_languages(github_data: dict, hidden_languages: list[str] = None) ->
hidden_set = {h for h in hidden_languages} hidden_set = {h for h in hidden_languages}
repos = github_data["repos"] repos = github_data["repos"]
byte_totals: dict[str, int] = defaultdict(int) login = (github_data.get("user") or {}).get("login") or ""
have_bytes = False login_lc = login.lower()
for r in repos:
lb = r.get("language_bytes") or {}
if not isinstance(lb, dict) or not lb:
continue
have_bytes = True
for lang, n in lb.items():
if lang and lang not in hidden_set:
byte_totals[lang] += int(n or 0)
if have_bytes and byte_totals: def _is_authored(r: dict) -> bool:
# Repos the user mostly didn't write shouldn't dominate their
# language mix. "Authored" = not a fork AND owned by the user
# (or by an org where we can't easily tell, so stay conservative
# and require personal ownership). This drops the big-fork /
# random-org-contribution case that was skewing the widget.
if r.get("fork"):
return False
owner_login = ((r.get("owner") or {}).get("login") or "").lower()
return bool(owner_login) and owner_login == login_lc
def _byte_tally(predicate) -> dict[str, int]:
totals: dict[str, int] = defaultdict(int)
for r in repos:
if not predicate(r):
continue
lb = r.get("language_bytes") or {}
if not isinstance(lb, dict):
continue
for lang, n in lb.items():
if lang and lang not in hidden_set:
totals[lang] += int(n or 0)
return totals
# Preferred: bytes across authored repos only.
byte_totals = _byte_tally(_is_authored)
# Fallback ladder so the widget is never empty: if the user has no
# authored repos with byte data (rare — new accounts, or everything
# is a fork), expand to all non-fork repos, and finally to everything.
if not byte_totals:
byte_totals = _byte_tally(lambda r: not r.get("fork"))
if not byte_totals:
byte_totals = _byte_tally(lambda r: True)
if byte_totals:
total = sum(byte_totals.values()) or 1 total = sum(byte_totals.values()) or 1
return [ return [
LanguageData( LanguageData(
@@ -542,12 +569,19 @@ def compute_languages(github_data: dict, hidden_languages: list[str] = None) ->
for lang, size in sorted(byte_totals.items(), key=lambda x: -x[1]) for lang, size in sorted(byte_totals.items(), key=lambda x: -x[1])
] ]
# Legacy fallback: repo-count by primary language. # Legacy fallback: repo-count by primary language, same ownership filter.
lang_counts: dict[str, int] = defaultdict(int) lang_counts: dict[str, int] = defaultdict(int)
for r in repos: for r in repos:
if not _is_authored(r):
continue
lang = r.get("language") lang = r.get("language")
if lang and lang not in hidden_set: if lang and lang not in hidden_set:
lang_counts[lang] += 1 lang_counts[lang] += 1
if not lang_counts:
for r in repos:
lang = r.get("language")
if lang and lang not in hidden_set:
lang_counts[lang] += 1
total = sum(lang_counts.values()) or 1 total = sum(lang_counts.values()) or 1
return [ return [
+1 -1
View File
@@ -138,7 +138,7 @@ def render_grade_widget(data: GradeData, theme_name: str = "dark", settings: dic
stroke-linecap="round" transform="rotate(-90)"> stroke-linecap="round" transform="rotate(-90)">
<animate attributeName="stroke-dashoffset" from="{circumference}" to="{offset}" dur="1s" fill="freeze"/> <animate attributeName="stroke-dashoffset" from="{circumference}" to="{offset}" dur="1s" fill="freeze"/>
</circle> </circle>
<text x="0" y="2" text-anchor="middle" dominant-baseline="middle" <text x="0" y="{grade_font * 0.35:.2f}" text-anchor="middle"
font-family="-apple-system,BlinkMacSystemFont,Segoe UI,Helvetica,Arial,sans-serif" font-family="-apple-system,BlinkMacSystemFont,Segoe UI,Helvetica,Arial,sans-serif"
font-size="{grade_font}" font-weight="800" fill="{color}">{escape(data.grade)}</text> font-size="{grade_font}" font-weight="800" fill="{color}">{escape(data.grade)}</text>
</g> </g>