mirror of
https://github.com/ShayManor/github-readme-stats.git
synced 2026-12-02 11:53:48 +05:30
feat(admin dashboard): add admin dashboard
This commit is contained in:
+19
-1
@@ -3,16 +3,19 @@ import hmac
|
||||
import logging
|
||||
import re
|
||||
import threading
|
||||
import time
|
||||
import requests
|
||||
from concurrent.futures import ThreadPoolExecutor
|
||||
from flask import Flask, jsonify, request, Response
|
||||
from functools import wraps
|
||||
|
||||
from . import config, db, github
|
||||
from . import analytics, config, db, github
|
||||
|
||||
app = Flask(__name__)
|
||||
log = logging.getLogger("fetcher.api")
|
||||
|
||||
analytics.start_flush_thread()
|
||||
|
||||
# Background pool for /fetch-async. The fetch routinely takes 10-20s of
|
||||
# wall clock even after parallelization, which is too long to hold an HTTP
|
||||
# request open from the generator (it would block the worker on its hot
|
||||
@@ -63,13 +66,18 @@ def get_data(username: str):
|
||||
row = db.get_user(username)
|
||||
if row is None:
|
||||
# Auto-fetch path
|
||||
t0 = time.monotonic()
|
||||
try:
|
||||
data = github.fetch_github_data(username, token=config.GITHUB_PAT)
|
||||
except Exception:
|
||||
analytics.record_fetch(username, 502, int((time.monotonic() - t0) * 1000))
|
||||
log.exception("github fetch failed for %s", username)
|
||||
return jsonify({"error": "fetch_failed"}), 502
|
||||
if _is_github_not_found(data):
|
||||
data = {"error": "not_found"}
|
||||
analytics.record_fetch(username, 404, int((time.monotonic() - t0) * 1000))
|
||||
else:
|
||||
analytics.record_fetch(username, 200, int((time.monotonic() - t0) * 1000))
|
||||
h = db.upsert_user(username, data)
|
||||
return jsonify({"data": data, "payload_hash": h, "fetched": True})
|
||||
return jsonify({
|
||||
@@ -87,15 +95,20 @@ def force_fetch():
|
||||
username = body.get("username")
|
||||
if not _valid_username(username):
|
||||
return jsonify({"error": "invalid_username"}), 400
|
||||
t0 = time.monotonic()
|
||||
try:
|
||||
data = github.fetch_github_data(username, token=config.GITHUB_PAT)
|
||||
except Exception:
|
||||
# Previously returned f"fetch failed: {e}" which leaks internal
|
||||
# exception text (URLs, stack fragments) across the trust boundary.
|
||||
analytics.record_fetch(username, 502, int((time.monotonic() - t0) * 1000))
|
||||
log.exception("github fetch failed for %s", username)
|
||||
return jsonify({"error": "fetch_failed"}), 502
|
||||
if _is_github_not_found(data):
|
||||
data = {"error": "not_found"}
|
||||
analytics.record_fetch(username, 404, int((time.monotonic() - t0) * 1000))
|
||||
else:
|
||||
analytics.record_fetch(username, 200, int((time.monotonic() - t0) * 1000))
|
||||
old = db.get_user(username)
|
||||
old_hash = old["payload_hash"] if old else None
|
||||
new_hash = db.upsert_user(username, data)
|
||||
@@ -113,13 +126,18 @@ def _bg_fetch(username: str) -> None:
|
||||
time out on long fetches.
|
||||
"""
|
||||
try:
|
||||
t0 = time.monotonic()
|
||||
try:
|
||||
data = github.fetch_github_data(username, token=config.GITHUB_PAT)
|
||||
if _is_github_not_found(data):
|
||||
data = {"error": "not_found"}
|
||||
analytics.record_fetch(username, 404, int((time.monotonic() - t0) * 1000))
|
||||
else:
|
||||
analytics.record_fetch(username, 200, int((time.monotonic() - t0) * 1000))
|
||||
payload_hash = db.upsert_user(username, data)
|
||||
ok = True
|
||||
except Exception:
|
||||
analytics.record_fetch(username, 502, int((time.monotonic() - t0) * 1000))
|
||||
log.exception("background fetch failed for %s", username)
|
||||
payload_hash = ""
|
||||
ok = False
|
||||
|
||||
@@ -30,3 +30,6 @@ DEEP_COLLAB_THRESHOLD = int(os.getenv("DEEP_COLLAB_THRESHOLD", "25"))
|
||||
# Commit-fetching caps
|
||||
COMMIT_MAX_REPOS = int(os.getenv("COMMIT_MAX_REPOS", "10"))
|
||||
COMMIT_PER_REPO = int(os.getenv("COMMIT_PER_REPO", "30"))
|
||||
|
||||
ANALYTICS_FLUSH_SECONDS = int(os.getenv("ANALYTICS_FLUSH_SECONDS", "5"))
|
||||
ANALYTICS_QUEUE_MAX = int(os.getenv("ANALYTICS_QUEUE_MAX", "2000"))
|
||||
|
||||
Reference in New Issue
Block a user