feat(admin dashboard): add admin dashboard

This commit is contained in:
shay
2026-05-11 18:26:43 -04:00
parent 00bf46968a
commit ef48d2581b
14 changed files with 1225 additions and 0 deletions
+168
View File
@@ -0,0 +1,168 @@
import os, tempfile, time
import pytest
from src import db as dbmod
from src import analytics as a
@pytest.fixture
def fresh(monkeypatch):
with tempfile.TemporaryDirectory() as d:
monkeypatch.setattr(dbmod, "SETTINGS_DB_PATH", os.path.join(d, "s.db"))
monkeypatch.setattr(dbmod, "WIDGETS_DB_PATH", os.path.join(d, "w.db"))
monkeypatch.setattr(dbmod, "ANALYTICS_DB_PATH", os.path.join(d, "a.db"))
dbmod.init_dbs()
a._reset_for_tests()
yield
def test_ingest_batch_writes_rows(fresh):
n = a.ingest_batch([
{"ts": 1000, "service": "edge", "kind": "request",
"username": "alice", "endpoint": "/<u>", "widget": "composite",
"status": 200, "latency_ms": 12, "cache_hit": 1},
{"ts": 1001, "service": "edge", "kind": "request",
"username": "bob", "endpoint": "/<u>", "widget": "composite",
"status": 200, "latency_ms": 8, "cache_hit": 0},
])
assert n == 2
with dbmod.analytics_conn() as c:
rows = c.execute("SELECT username, latency_ms FROM events ORDER BY ts").fetchall()
assert [(r["username"], r["latency_ms"]) for r in rows] == [("alice", 12), ("bob", 8)]
def test_ingest_drops_bad_rows(fresh):
n = a.ingest_batch([
{"ts": 1, "service": "edge", "kind": "request", "latency_ms": 1},
{"missing": "everything"},
{"ts": 2, "service": "edge", "kind": "request", "latency_ms": "not-int"},
])
assert n == 1
def test_record_render_flushes_to_db(fresh, monkeypatch):
monkeypatch.setattr(a, "_FLUSH_SECONDS", 0.05)
a.start_flush_thread()
a.record_render("alice", "composite", 142)
deadline = time.time() + 3
row = None
while time.time() < deadline:
with dbmod.analytics_conn() as c:
row = c.execute("SELECT * FROM events WHERE kind='render'").fetchone()
if row:
break
time.sleep(0.05)
assert row is not None
assert row["username"] == "alice"
assert row["widget"] == "composite"
assert row["latency_ms"] == 142
def test_summary_aggregates_last_7d(fresh):
now = int(time.time())
rows = [
{"ts": now - 60, "service": "edge", "kind": "request",
"username": "alice", "endpoint": "/<u>", "widget": "composite",
"status": 200, "latency_ms": 10, "cache_hit": 1},
{"ts": now - 120, "service": "edge", "kind": "request",
"username": "bob", "endpoint": "/<u>", "widget": "composite",
"status": 200, "latency_ms": 50, "cache_hit": 0},
{"ts": now - 10 * 86400, "service": "edge", "kind": "request",
"username": "old", "endpoint": "/<u>", "widget": "composite",
"status": 200, "latency_ms": 999, "cache_hit": 0},
{"ts": now - 30, "service": "generator", "kind": "render",
"username": "alice", "widget": "composite", "latency_ms": 200},
]
a.ingest_batch(rows)
s = a.query_summary()
assert s["requests_7d"] == 2 # 'old' falls outside the 7d window
assert s["active_users_7d"] == 2 # alice (edge+render dedup) + bob
assert s["renders_7d"] == 1
assert s["avg_render_ms"] == 200
assert s["p50_ms"] in (10, 50)
assert s["p95_ms"] == 50
assert len(s["daily_requests"]) == 7
def test_query_users_returns_per_user_rows(fresh):
now = int(time.time())
a.ingest_batch([
{"ts": now - 60, "service": "edge", "kind": "request",
"username": "alice", "endpoint": "/<u>", "widget": "composite",
"status": 200, "latency_ms": 10, "cache_hit": 1},
{"ts": now - 30, "service": "edge", "kind": "request",
"username": "alice", "endpoint": "/<u>/grade.svg", "widget": "grade",
"status": 200, "latency_ms": 20, "cache_hit": 1},
{"ts": now - 10, "service": "edge", "kind": "request",
"username": "alice", "endpoint": "/<u>", "widget": "composite",
"status": 200, "latency_ms": 30, "cache_hit": 1},
])
users = a.query_users()
assert len(users) == 1
u = users[0]
assert u["username"] == "alice"
assert u["requests_7d"] == 3
assert u["top_endpoint"] == "/<u>"
assert u["avg_latency_ms"] == 20
def test_query_latency_returns_percentiles(fresh):
now = int(time.time())
a.ingest_batch([
{"ts": now - 1, "service": "edge", "kind": "request",
"username": "u", "endpoint": "/<u>", "widget": "composite",
"status": 200, "latency_ms": v, "cache_hit": 1}
for v in range(1, 101)
])
rows = a.query_latency()
by_ep = {r["endpoint"]: r for r in rows}
assert by_ep["/<u>"]["count"] == 100
assert by_ep["/<u>"]["p50"] == 50
assert by_ep["/<u>"]["p95"] == 95
assert by_ep["/<u>"]["p99"] == 99
def test_query_health(fresh):
now = int(time.time())
a.ingest_batch([
{"ts": now - 1, "service": "edge", "kind": "request",
"username": "u", "endpoint": "/<u>", "widget": "composite",
"status": 200, "latency_ms": 1, "cache_hit": 1},
{"ts": now - 2, "service": "edge", "kind": "request",
"username": "u", "endpoint": "/<u>", "widget": "composite",
"status": 200, "latency_ms": 1, "cache_hit": 0},
{"ts": now - 3, "service": "fetcher", "kind": "fetch",
"username": "u", "endpoint": "github/users/<u>",
"status": 500, "latency_ms": 100},
{"ts": now - 4, "service": "fetcher", "kind": "fetch",
"username": "u", "endpoint": "github/users/<u>",
"status": 200, "latency_ms": 100},
])
h = a.query_health()
assert abs(h["edge_cache_hit_rate"] - 0.5) < 1e-6
assert abs(h["fetcher_error_rate"] - 0.5) < 1e-6
assert h["events_dropped_24h"] == 0
def test_prune_drops_old_events(fresh):
now = int(time.time())
a.ingest_batch([
{"ts": now - 20 * 86400, "service": "edge", "kind": "request",
"username": "u", "endpoint": "/<u>", "widget": "composite",
"status": 200, "latency_ms": 1, "cache_hit": 1},
{"ts": now, "service": "edge", "kind": "request",
"username": "u", "endpoint": "/<u>", "widget": "composite",
"status": 200, "latency_ms": 1, "cache_hit": 1},
])
deleted = a.prune_old(retention_days=14)
assert deleted == 1
with dbmod.analytics_conn() as c:
n = c.execute("SELECT COUNT(*) AS n FROM events").fetchone()["n"]
assert n == 1
def test_queue_drops_when_full_and_counts(fresh, monkeypatch):
monkeypatch.setattr(a, "_QUEUE_MAX", 3)
a._reset_for_tests()
for i in range(10):
a.record_render("u", "composite", i)
assert a._events_dropped == 7
+108
View File
@@ -0,0 +1,108 @@
"""Per-widget attribution behaves correctly in both contexts.
Standalone widget SVGs (served at /api/<u>/<widget>.svg) must show the
"Generated with gh-stats" footer; the composite (served at /api/<u>)
must strip the per-widget footer and only show its own bottom one.
"""
from src.models import GradeData, LanguageData, TagData
from src.widgets import (
render_grade_widget,
render_languages_widget,
compose_widget,
)
def _grade_fixture() -> GradeData:
return GradeData(
grade="A", score=80,
stats={"commits": 100, "prs": 10, "stars": 5, "repos": 3, "followers": 1},
tags=[TagData(tag="builder", source="earned")],
breakdown={"commits": 0.8, "consistency": 0.6, "stars": 0.4},
)
def _languages_fixture() -> list[LanguageData]:
return [
LanguageData(language="Python", percentage=60.0, loc=0),
LanguageData(language="TypeScript", percentage=40.0, loc=0),
]
def test_standalone_grade_widget_has_attribution():
svg = render_grade_widget(_grade_fixture(), theme_name="dark")
assert "Generated with gh-stats" in svg
assert 'data-gh-attribution="1"' in svg
def test_standalone_languages_widget_has_attribution():
svg = render_languages_widget(_languages_fixture(), theme_name="dark")
assert "Generated with gh-stats" in svg
assert 'data-gh-attribution="1"' in svg
def test_composite_strips_per_widget_attribution_from_grade():
"""Embedded inside the composite, the grade widget's per-widget
attribution must be removed — the composite has its own footer at
the bottom, showing both would be visually redundant."""
grade_svg = render_grade_widget(_grade_fixture(), theme_name="dark")
composite = compose_widget(
widgets={"grade": grade_svg},
enabled=["grade"],
theme_name="dark",
username="alice",
avatar_b64="",
show_name=False,
)
# The composite has exactly ONE attribution (its own). The widget's
# footer attribution should be gone.
assert composite.count("Generated with gh-stats") == 1
# No leftover attribution markers leaked through.
assert "data-gh-attribution" not in composite
def test_composite_strips_per_widget_attribution_from_languages():
langs_svg = render_languages_widget(_languages_fixture(), theme_name="dark")
composite = compose_widget(
widgets={"languages": langs_svg},
enabled=["languages"],
theme_name="dark",
username="alice",
avatar_b64="",
show_name=False,
)
assert composite.count("Generated with gh-stats") == 1
assert "data-gh-attribution" not in composite
def test_composite_strips_attribution_from_multiple_widgets():
"""When several attribution-bearing widgets are inlined together,
the composite still shows only its own single footer line."""
grade_svg = render_grade_widget(_grade_fixture(), theme_name="dark")
langs_svg = render_languages_widget(_languages_fixture(), theme_name="dark")
composite = compose_widget(
widgets={"grade": grade_svg, "languages": langs_svg},
enabled=["grade", "languages"],
theme_name="dark",
username="alice",
avatar_b64="",
show_name=False,
)
# Each widget contributed an attribution; the composite contributes
# one of its own — total visible attributions must be exactly 1.
assert composite.count("Generated with gh-stats") == 1
def test_composite_reclaims_attribution_height():
"""Stripping the attribution from a per-widget SVG must also reduce
the slot height the composite reserves — otherwise we'd leave 16px
of empty space at the bottom of every embedded widget."""
from src.widgets import composite as composite_mod
grade_svg = render_grade_widget(_grade_fixture(), theme_name="dark")
inner_with_attr, h_with_attr = composite_mod._extract_inner(grade_svg, "grade")
# The standalone grade SVG declares its full height including the
# 16px footer; the embed height should be 16 less.
import re
full_h = int(re.search(r'height="(\d+)"', grade_svg).group(1))
assert h_with_attr == full_h - 16
assert "data-gh-attribution" not in inner_with_attr