mirror of
https://github.com/ShayManor/github-readme-stats.git
synced 2026-12-02 11:53:48 +05:30
feat(launch): cap per-user language fetch at 40 repos, raise enrollment caps, fix cap message
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
@@ -41,6 +41,11 @@ services:
|
|||||||
BUILD_VERSION: ${IMAGE_TAG:-}
|
BUILD_VERSION: ${IMAGE_TAG:-}
|
||||||
DEV_DASHBOARD_USER: ${DEV_DASHBOARD_USER:?missing DEV_DASHBOARD_USER}
|
DEV_DASHBOARD_USER: ${DEV_DASHBOARD_USER:?missing DEV_DASHBOARD_USER}
|
||||||
DEV_DASHBOARD_PASSWORD: ${DEV_DASHBOARD_PASSWORD:?missing DEV_DASHBOARD_PASSWORD}
|
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:
|
volumes:
|
||||||
- ${HOME}/ghstats/data/generator:/app/data
|
- ${HOME}/ghstats/data/generator:/app/data
|
||||||
ports:
|
ports:
|
||||||
|
|||||||
@@ -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_MAX_REPOS = int(os.getenv("COMMIT_MAX_REPOS", "10"))
|
||||||
COMMIT_PER_REPO = int(os.getenv("COMMIT_PER_REPO", "30"))
|
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_FLUSH_SECONDS = int(os.getenv("ANALYTICS_FLUSH_SECONDS", "5"))
|
||||||
ANALYTICS_QUEUE_MAX = int(os.getenv("ANALYTICS_QUEUE_MAX", "2000"))
|
ANALYTICS_QUEUE_MAX = int(os.getenv("ANALYTICS_QUEUE_MAX", "2000"))
|
||||||
|
|||||||
@@ -714,7 +714,7 @@ def fetch_github_data(
|
|||||||
avatar_b64 = ""
|
avatar_b64 = ""
|
||||||
avatar_url = user.get("avatar_url") if isinstance(user, dict) else None
|
avatar_url = user.get("avatar_url") if isinstance(user, dict) else None
|
||||||
with ThreadPoolExecutor(max_workers=2, thread_name_prefix="fetch-stage2") as pool:
|
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_avatar = pool.submit(source.fetch_avatar, avatar_url) if avatar_url else None
|
||||||
fut_lang.result()
|
fut_lang.result()
|
||||||
if fut_avatar is not None:
|
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
|
"""Mutates `repos` in-place, attaching `language_bytes: {lang: bytes}` to
|
||||||
each repo. Uses a small thread pool — one REST call per repo is cheap
|
each repo. Uses a small thread pool — one REST call per repo is cheap
|
||||||
individually, but sequentially N repos would block the fetch noticeably."""
|
individually, but sequentially N repos would block the fetch noticeably."""
|
||||||
if not repos:
|
if not repos:
|
||||||
return
|
return
|
||||||
names = [(i, r["full_name"]) for i, r in enumerate(repos) if r.get("full_name")]
|
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...")
|
print(f" Fetching language byte breakdown for {len(names)} repos...")
|
||||||
with ThreadPoolExecutor(max_workers=10) as pool:
|
with ThreadPoolExecutor(max_workers=10) as pool:
|
||||||
futures = {pool.submit(source.fetch_repo_languages, fn): i for i, fn in names}
|
futures = {pool.submit(source.fetch_repo_languages, fn): i for i, fn in names}
|
||||||
|
|||||||
@@ -250,3 +250,30 @@ def test_rate_limited_repo_languages_aborts_whole_fetch():
|
|||||||
repos = [{"full_name": "alice/proj"}]
|
repos = [{"full_name": "alice/proj"}]
|
||||||
with pytest.raises(github.GitHubTransientError):
|
with pytest.raises(github.GitHubTransientError):
|
||||||
github._enrich_repo_languages(repos, src)
|
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]
|
||||||
|
|||||||
@@ -262,7 +262,9 @@ export default function App() {
|
|||||||
return
|
return
|
||||||
}
|
}
|
||||||
if (r.status === 429 || status === 'rate_limited') {
|
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)
|
setFetchDone(true)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -827,7 +827,8 @@ def get_user_data(username: str):
|
|||||||
# Daily cap is the abuse backstop: a botnet typing random names
|
# Daily cap is the abuse backstop: a botnet typing random names
|
||||||
# can't burn through the GitHub PAT budget past this number.
|
# can't burn through the GitHub PAT budget past this number.
|
||||||
if _enrollment_capped():
|
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 = {
|
defaults = {
|
||||||
"theme": "dark",
|
"theme": "dark",
|
||||||
"enabled": config.ENABLED_WIDGETS,
|
"enabled": config.ENABLED_WIDGETS,
|
||||||
|
|||||||
@@ -8,7 +8,7 @@ from .widgets.attribution import attribution_text
|
|||||||
|
|
||||||
_MESSAGES = {
|
_MESSAGES = {
|
||||||
"building": ("Building @{u}'s widget\u2026", "This usually takes under a minute."),
|
"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."),
|
"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."),
|
"unsubstituted": ("Replace YOUR_USERNAME in the URL", "Put your own GitHub username there, e.g. /api/octocat."),
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user