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:
@@ -0,0 +1,93 @@
|
||||
"""Edge analytics — record + flush + push to generator.
|
||||
|
||||
The hot path enqueues; the background thread does the network I/O. If the
|
||||
generator is unreachable we drop the batch (logging a warning) and let the
|
||||
queue catch the next tick.
|
||||
"""
|
||||
import logging
|
||||
import threading
|
||||
import time
|
||||
from collections import deque
|
||||
import requests
|
||||
|
||||
from . import config
|
||||
|
||||
log = logging.getLogger("edge.analytics")
|
||||
|
||||
_QUEUE: deque = deque(maxlen=config.ANALYTICS_QUEUE_MAX)
|
||||
_QUEUE_LOCK = threading.Lock()
|
||||
_THREAD_LOCK = threading.Lock()
|
||||
_FLUSH_THREAD: threading.Thread | None = None
|
||||
_events_dropped: int = 0
|
||||
_drop_lock = threading.Lock()
|
||||
|
||||
|
||||
def _reset_for_tests() -> None:
|
||||
global _events_dropped, _FLUSH_THREAD, _QUEUE
|
||||
with _QUEUE_LOCK:
|
||||
_QUEUE = deque(maxlen=config.ANALYTICS_QUEUE_MAX)
|
||||
with _drop_lock:
|
||||
_events_dropped = 0
|
||||
_FLUSH_THREAD = None
|
||||
|
||||
|
||||
def record_request(endpoint: str, username: str | None, widget: str | None,
|
||||
status: int, latency_ms: int, cache_hit: int | None = None) -> None:
|
||||
ev = {
|
||||
"ts": int(time.time()),
|
||||
"service": "edge",
|
||||
"kind": "request",
|
||||
"username": username,
|
||||
"endpoint": endpoint,
|
||||
"widget": widget,
|
||||
"status": int(status),
|
||||
"latency_ms": int(latency_ms),
|
||||
"cache_hit": cache_hit,
|
||||
}
|
||||
global _events_dropped
|
||||
with _QUEUE_LOCK:
|
||||
if len(_QUEUE) >= _QUEUE.maxlen:
|
||||
with _drop_lock:
|
||||
_events_dropped += 1
|
||||
return
|
||||
_QUEUE.append(ev)
|
||||
|
||||
|
||||
def flush_now() -> int:
|
||||
with _QUEUE_LOCK:
|
||||
batch = list(_QUEUE)
|
||||
_QUEUE.clear()
|
||||
if not batch:
|
||||
return 0
|
||||
if not config.ANALYTICS_GENERATOR_URL or not config.ANALYTICS_INTERNAL_TOKEN:
|
||||
return 0
|
||||
try:
|
||||
requests.post(
|
||||
f"{config.ANALYTICS_GENERATOR_URL}/internal/analytics/events",
|
||||
headers={"X-Internal-Token": config.ANALYTICS_INTERNAL_TOKEN},
|
||||
json={"events": batch},
|
||||
timeout=2,
|
||||
)
|
||||
except Exception:
|
||||
log.warning("analytics flush failed (events lost)", exc_info=True)
|
||||
return 0
|
||||
return len(batch)
|
||||
|
||||
|
||||
def _flush_loop():
|
||||
while True:
|
||||
time.sleep(config.ANALYTICS_FLUSH_SECONDS)
|
||||
try:
|
||||
flush_now()
|
||||
except Exception:
|
||||
log.exception("flush loop error")
|
||||
|
||||
|
||||
def start_flush_thread():
|
||||
global _FLUSH_THREAD
|
||||
with _THREAD_LOCK:
|
||||
if _FLUSH_THREAD is not None and _FLUSH_THREAD.is_alive():
|
||||
return
|
||||
t = threading.Thread(target=_flush_loop, name="edge-analytics-flush", daemon=True)
|
||||
t.start()
|
||||
_FLUSH_THREAD = t
|
||||
@@ -0,0 +1,46 @@
|
||||
import responses
|
||||
from src import analytics as a
|
||||
from src import config as cfg
|
||||
|
||||
|
||||
def test_record_request_enqueues():
|
||||
a._reset_for_tests()
|
||||
a.record_request("/<u>", "alice", "composite", 200, 12, cache_hit=1)
|
||||
assert len(a._QUEUE) == 1
|
||||
e = a._QUEUE[0]
|
||||
assert e["service"] == "edge"
|
||||
assert e["username"] == "alice"
|
||||
assert e["latency_ms"] == 12
|
||||
|
||||
|
||||
@responses.activate
|
||||
def test_flush_posts_batch(monkeypatch):
|
||||
monkeypatch.setattr(cfg, "ANALYTICS_GENERATOR_URL", "http://gen:5002")
|
||||
monkeypatch.setattr(cfg, "ANALYTICS_INTERNAL_TOKEN", "secret")
|
||||
a._reset_for_tests()
|
||||
responses.add(responses.POST, "http://gen:5002/internal/analytics/events",
|
||||
json={"ingested": 2}, status=200)
|
||||
a.record_request("/<u>", "alice", "composite", 200, 12, cache_hit=1)
|
||||
a.record_request("/<u>", "bob", "composite", 200, 8, cache_hit=0)
|
||||
a.flush_now()
|
||||
assert len(responses.calls) == 1
|
||||
import json
|
||||
body = json.loads(responses.calls[0].request.body)
|
||||
assert len(body["events"]) == 2
|
||||
|
||||
|
||||
def test_instrument_wraps_serve_and_records(monkeypatch):
|
||||
monkeypatch.setattr(cfg, "ANALYTICS_INTERNAL_TOKEN", "secret")
|
||||
a._reset_for_tests()
|
||||
from src import api as apimod
|
||||
with apimod.app.test_client() as c:
|
||||
class FakeResp:
|
||||
status_code = 200
|
||||
content = b"<svg/>"
|
||||
headers = {"X-Widget-Status": "ready", "Content-Type": "image/svg+xml"}
|
||||
monkeypatch.setattr(apimod, "_fetch_origin", lambda *a, **k: FakeResp())
|
||||
apimod.cache_ext.clear()
|
||||
c.get("/alice")
|
||||
assert len(a._QUEUE) == 1
|
||||
assert a._QUEUE[0]["username"] == "alice"
|
||||
assert a._QUEUE[0]["endpoint"] == "/<u>"
|
||||
Reference in New Issue
Block a user