|
| 1 | +"""Pull floe-guard's public adoption metrics into a single snapshot. |
| 2 | +
|
| 3 | +No dependencies — stdlib only. Reads public APIs: |
| 4 | + - GitHub repo stats (stars, forks, watchers) — no auth needed |
| 5 | + - GitHub traffic (views, clones) — needs a token with push access |
| 6 | + - PyPI downloads (last day / week / month) — no auth needed (pypistats.org) |
| 7 | +
|
| 8 | +Usage: |
| 9 | + python scripts/metrics.py |
| 10 | + GITHUB_TOKEN=ghp_xxx python scripts/metrics.py # also fetch traffic (views/clones) |
| 11 | +
|
| 12 | +These are the runbook KPIs (star velocity, forks, install velocity). Run it on a |
| 13 | +schedule and diff snapshots to get velocity. It only reports real numbers — if a |
| 14 | +source is unreachable it says so rather than guessing. |
| 15 | +""" |
| 16 | +from __future__ import annotations |
| 17 | + |
| 18 | +import json |
| 19 | +import os |
| 20 | +import urllib.error |
| 21 | +import urllib.request |
| 22 | + |
| 23 | +REPO = "Floe-Labs/floe-guard" |
| 24 | +PACKAGE = "floe-guard" |
| 25 | + |
| 26 | + |
| 27 | +def _get( |
| 28 | + url: str, headers: dict[str, str] | None = None, timeout: float = 15.0 |
| 29 | +) -> dict[str, object]: |
| 30 | + req = urllib.request.Request(url, headers=headers or {}) |
| 31 | + with urllib.request.urlopen(req, timeout=timeout) as resp: # noqa: S310 (trusted hosts) |
| 32 | + return json.loads(resp.read().decode()) |
| 33 | + |
| 34 | + |
| 35 | +def github_repo() -> dict[str, object]: |
| 36 | + try: |
| 37 | + d = _get( |
| 38 | + f"https://api.github.com/repos/{REPO}", |
| 39 | + headers={"Accept": "application/vnd.github+json", "User-Agent": "floe-guard-metrics"}, |
| 40 | + ) |
| 41 | + return { |
| 42 | + "stars": d.get("stargazers_count"), |
| 43 | + "forks": d.get("forks_count"), |
| 44 | + "watchers": d.get("subscribers_count"), |
| 45 | + "open_issues": d.get("open_issues_count"), |
| 46 | + } |
| 47 | + except (urllib.error.URLError, ValueError) as e: |
| 48 | + return {"error": f"github repo stats unavailable: {e}"} |
| 49 | + |
| 50 | + |
| 51 | +def github_traffic() -> dict[str, object]: |
| 52 | + token = os.environ.get("GITHUB_TOKEN", "").strip() |
| 53 | + if not token: |
| 54 | + return {"note": "set GITHUB_TOKEN (push access) to fetch views/clones"} |
| 55 | + hdr = { |
| 56 | + "Authorization": f"Bearer {token}", |
| 57 | + "Accept": "application/vnd.github+json", |
| 58 | + "User-Agent": "floe-guard-metrics", |
| 59 | + } |
| 60 | + out: dict[str, object] = {} |
| 61 | + for kind in ("views", "clones"): |
| 62 | + try: |
| 63 | + d = _get(f"https://api.github.com/repos/{REPO}/traffic/{kind}", headers=hdr) |
| 64 | + out[kind] = {"count": d.get("count"), "uniques": d.get("uniques")} |
| 65 | + except (urllib.error.URLError, ValueError) as e: |
| 66 | + out[kind] = {"error": str(e)} |
| 67 | + return out |
| 68 | + |
| 69 | + |
| 70 | +def pypi_downloads() -> dict[str, object]: |
| 71 | + try: |
| 72 | + d = _get( |
| 73 | + f"https://pypistats.org/api/packages/{PACKAGE}/recent", |
| 74 | + headers={"User-Agent": "floe-guard-metrics"}, |
| 75 | + ) |
| 76 | + return d.get("data", {"error": "no data field"}) |
| 77 | + except (urllib.error.URLError, ValueError) as e: |
| 78 | + return {"error": f"pypistats unavailable (new packages take ~1 day to appear): {e}"} |
| 79 | + |
| 80 | + |
| 81 | +def main() -> None: |
| 82 | + snapshot = { |
| 83 | + "repo": REPO, |
| 84 | + "package": PACKAGE, |
| 85 | + "github": github_repo(), |
| 86 | + "traffic": github_traffic(), |
| 87 | + "pypi_downloads_recent": pypi_downloads(), |
| 88 | + } |
| 89 | + print(json.dumps(snapshot, indent=2)) |
| 90 | + |
| 91 | + |
| 92 | +if __name__ == "__main__": |
| 93 | + main() |
0 commit comments