From 53ae54fe98073f8fde7163f93f3d5db0ba126e76 Mon Sep 17 00:00:00 2001 From: shay Date: Sun, 29 Nov 2026 10:30:52 -0800 Subject: [PATCH] feat(launch): cap per-user language fetch at 40 repos, raise enrollment caps, fix cap message Co-Authored-By: Claude Opus 5.5 --- deploy/compose.yml | 5 +++++ fetcher/src/config.py | 4 ++++ fetcher/src/github.py | 10 ++++++++-- fetcher/tests/test_github.py | 27 +++++++++++++++++++++++++++ generator/frontend/src/App.tsx | 4 +++- generator/src/api.py | 3 ++- generator/src/placeholder.py | 2 +- 7 files changed, 50 insertions(+), 5 deletions(-) diff --git a/deploy/compose.yml b/deploy/compose.yml index 29bc2b5..a511617 100644 --- a/deploy/compose.yml +++ b/deploy/compose.yml @@ -41,6 +41,11 @@ services: BUILD_VERSION: ${IMAGE_TAG:-} DEV_DASHBOARD_USER: ${DEV_DASHBOARD_USER:?missing DEV_DASHBOARD_USER} DEV_DASHBOARD_PASSWORD: ${DEV_DASHBOARD_PASSWORD:?missing DEV_DASHBOARD_PASSWORD} + ENROLLMENT_HOURLY_CAP: "120" + ENROLLMENT_DAILY_CAP: "1500" + # Fallback while Caddy sends every visitor as 127.0.0.1 (one shared + # bucket). Remove once Caddy forwards Cf-Connecting-Ip as X-Forwarded-For. + RATE_LIMIT_READ_MAX: "30000" volumes: - ${HOME}/ghstats/data/generator:/app/data ports: diff --git a/fetcher/src/config.py b/fetcher/src/config.py index 93e8c48..1ead3bd 100644 --- a/fetcher/src/config.py +++ b/fetcher/src/config.py @@ -31,5 +31,9 @@ DEEP_COLLAB_THRESHOLD = int(os.getenv("DEEP_COLLAB_THRESHOLD", "25")) COMMIT_MAX_REPOS = int(os.getenv("COMMIT_MAX_REPOS", "10")) COMMIT_PER_REPO = int(os.getenv("COMMIT_PER_REPO", "30")) +# Per-repo language-bytes fetches: one REST call each, so cap them to the +# most recently pushed repos the user owns (forks excluded). +LANGUAGE_MAX_REPOS = int(os.getenv("LANGUAGE_MAX_REPOS", "40")) + ANALYTICS_FLUSH_SECONDS = int(os.getenv("ANALYTICS_FLUSH_SECONDS", "5")) ANALYTICS_QUEUE_MAX = int(os.getenv("ANALYTICS_QUEUE_MAX", "2000")) diff --git a/fetcher/src/github.py b/fetcher/src/github.py index e7f2cf3..0113be6 100644 --- a/fetcher/src/github.py +++ b/fetcher/src/github.py @@ -714,7 +714,7 @@ def fetch_github_data( avatar_b64 = "" avatar_url = user.get("avatar_url") if isinstance(user, dict) else None with ThreadPoolExecutor(max_workers=2, thread_name_prefix="fetch-stage2") as pool: - fut_lang = pool.submit(_enrich_repo_languages, repos, source) + fut_lang = pool.submit(_enrich_repo_languages, repos, source, username) fut_avatar = pool.submit(source.fetch_avatar, avatar_url) if avatar_url else None fut_lang.result() if fut_avatar is not None: @@ -733,13 +733,19 @@ def fetch_github_data( } -def _enrich_repo_languages(repos: list, source: GitHubDataSource) -> None: +def _enrich_repo_languages(repos: list, source: GitHubDataSource, username: str = "") -> None: """Mutates `repos` in-place, attaching `language_bytes: {lang: bytes}` to each repo. Uses a small thread pool — one REST call per repo is cheap individually, but sequentially N repos would block the fetch noticeably.""" if not repos: return names = [(i, r["full_name"]) for i, r in enumerate(repos) if r.get("full_name")] + if username: + owner = username.lower() + names = [(i, fn) for i, fn in names + if not repos[i].get("fork") and fn.split("/")[0].lower() == owner] + names.sort(key=lambda p: repos[p[0]].get("pushed_at") or "", reverse=True) + names = names[:config.LANGUAGE_MAX_REPOS] print(f" Fetching language byte breakdown for {len(names)} repos...") with ThreadPoolExecutor(max_workers=10) as pool: futures = {pool.submit(source.fetch_repo_languages, fn): i for i, fn in names} diff --git a/fetcher/tests/test_github.py b/fetcher/tests/test_github.py index 62be102..f4639cc 100644 --- a/fetcher/tests/test_github.py +++ b/fetcher/tests/test_github.py @@ -250,3 +250,30 @@ def test_rate_limited_repo_languages_aborts_whole_fetch(): repos = [{"full_name": "alice/proj"}] with pytest.raises(github.GitHubTransientError): github._enrich_repo_languages(repos, src) + + +def test_repo_languages_capped_to_recent_owned_repos(monkeypatch): + # One REST call per repo, so only the most recently pushed non-fork repos + # the user owns get a byte breakdown. + monkeypatch.setattr(github.config, "LANGUAGE_MAX_REPOS", 2) + + class _Src: + def __init__(self): + self.calls = [] + + def fetch_repo_languages(self, full_name): + self.calls.append(full_name) + return {"Python": 1} + + repos = [ + {"full_name": "Alice/old", "pushed_at": "2024-01-01T00:00:00Z"}, + {"full_name": "alice/new", "pushed_at": "2026-01-01T00:00:00Z"}, + {"full_name": "alice/mid", "pushed_at": "2025-01-01T00:00:00Z"}, + {"full_name": "alice/fork", "pushed_at": "2026-06-01T00:00:00Z", "fork": True}, + {"full_name": "org/other", "pushed_at": "2026-06-01T00:00:00Z"}, + ] + src = _Src() + github._enrich_repo_languages(repos, src, "alice") + assert sorted(src.calls) == ["alice/mid", "alice/new"] + assert repos[1]["language_bytes"] == {"Python": 1} + assert "language_bytes" not in repos[0] diff --git a/generator/frontend/src/App.tsx b/generator/frontend/src/App.tsx index 9e149c8..0e3e8d2 100644 --- a/generator/frontend/src/App.tsx +++ b/generator/frontend/src/App.tsx @@ -262,7 +262,9 @@ export default function App() { return } if (r.status === 429 || status === 'rate_limited') { - setFetchError('Enrollment limit reached — try again tomorrow') + setFetchError((body as { cap?: string }).cap === 'daily' + ? 'Enrollment limit reached — try again tomorrow' + : 'Enrollment limit reached — try again in an hour') setFetchDone(true) return } diff --git a/generator/src/api.py b/generator/src/api.py index 9b35fae..fa1ed81 100644 --- a/generator/src/api.py +++ b/generator/src/api.py @@ -827,7 +827,8 @@ def get_user_data(username: str): # Daily cap is the abuse backstop: a botnet typing random names # can't burn through the GitHub PAT budget past this number. if _enrollment_capped(): - return jsonify({"status": "rate_limited"}), 429 + cap = "daily" if db.enrollments_today() >= config.ENROLLMENT_DAILY_CAP else "hourly" + return jsonify({"status": "rate_limited", "cap": cap}), 429 defaults = { "theme": "dark", "enabled": config.ENABLED_WIDGETS, diff --git a/generator/src/placeholder.py b/generator/src/placeholder.py index 65fa92f..6d324c0 100644 --- a/generator/src/placeholder.py +++ b/generator/src/placeholder.py @@ -8,7 +8,7 @@ from .widgets.attribution import attribution_text _MESSAGES = { "building": ("Building @{u}'s widget\u2026", "This usually takes under a minute."), - "rate_limited": ("Too many new users today", "Try again tomorrow."), + "rate_limited": ("Too many new users right now", "Try again later."), "not_found": ("GitHub user @{u} doesn't exist", "Check the spelling of the username."), "unsubstituted": ("Replace YOUR_USERNAME in the URL", "Put your own GitHub username there, e.g. /api/octocat."), }