From 01927b2173aec73840fd0f28141aec1ade5d76e1 Mon Sep 17 00:00:00 2001 From: shay Date: Mon, 11 May 2026 23:34:57 -0400 Subject: [PATCH] feat(analytics): include render-only users in activity count query --- generator/src/analytics.py | 13 ++++++++++--- generator/tests/test_analytics.py | 18 ++++++++++++++++++ 2 files changed, 28 insertions(+), 3 deletions(-) diff --git a/generator/src/analytics.py b/generator/src/analytics.py index 77b5a54..77b2d27 100644 --- a/generator/src/analytics.py +++ b/generator/src/analytics.py @@ -222,15 +222,22 @@ def query_users(q: str = "", sort: str = "requests", limit: int = 200) -> list[d with db.analytics_conn() as c: c.execute("ATTACH DATABASE ? AS settings", (db.SETTINGS_DB_PATH,)) try: + # Count any event kind (request OR render) as activity so the + # table matches `active_users_7d` in query_summary — without + # this, render-only users (e.g. cron-driven worker refreshes + # before any edge traffic) appear in the count but not the + # table, which looks like a bug. rows = c.execute( """SELECT e.username AS username, - COUNT(*) AS requests_7d, + SUM(CASE WHEN e.kind='request' THEN 1 ELSE 0 END) AS requests_7d, MAX(e.ts) AS last_seen, - AVG(e.latency_ms) AS avg_latency_ms, + COALESCE(AVG(CASE WHEN e.kind='request' + THEN e.latency_ms END), 0) AS avg_latency_ms, (SELECT u.github_avatar_url FROM settings.users u WHERE u.username = e.username) AS github_avatar_url FROM events e - WHERE e.kind='request' AND e.ts >= ? AND e.username IS NOT NULL + WHERE e.kind IN ('request', 'render') + AND e.ts >= ? AND e.username IS NOT NULL GROUP BY e.username""", (week_ago,), ).fetchall() diff --git a/generator/tests/test_analytics.py b/generator/tests/test_analytics.py index 27e0406..4562fec 100644 --- a/generator/tests/test_analytics.py +++ b/generator/tests/test_analytics.py @@ -166,3 +166,21 @@ def test_queue_drops_when_full_and_counts(fresh, monkeypatch): for i in range(10): a.record_render("u", "composite", i) assert a._events_dropped == 7 + + +def test_query_users_includes_render_only_users(fresh): + """Render-only users (worker fired but no edge traffic yet) must still + appear in the table — without this, `active_users_7d` says N while the + table looks empty, which reads as a bug.""" + now = int(time.time()) + a.ingest_batch([ + {"ts": now - 60, "service": "generator", "kind": "render", + "username": "alice", "widget": "composite", "latency_ms": 200}, + ]) + users = a.query_users() + assert len(users) == 1 + u = users[0] + assert u["username"] == "alice" + assert u["requests_7d"] == 0 # no request events yet + assert u["last_seen"] == now - 60 + assert u["avg_latency_ms"] == 0 # latency only averages request events