diff --git a/generator/src/db.py b/generator/src/db.py index 5150b34..a6bd627 100644 --- a/generator/src/db.py +++ b/generator/src/db.py @@ -44,6 +44,16 @@ CREATE TABLE IF NOT EXISTS jobs ( updated_at TEXT NOT NULL ); CREATE INDEX IF NOT EXISTS idx_jobs_pending ON jobs(status, created_at); +CREATE TABLE IF NOT EXISTS user_streaks ( + username TEXT PRIMARY KEY, + current_streak INTEGER NOT NULL DEFAULT 0, + current_start TEXT NOT NULL DEFAULT '', + last_active_date TEXT NOT NULL DEFAULT '', + max_streak INTEGER NOT NULL DEFAULT 0, + max_start TEXT NOT NULL DEFAULT '', + max_end TEXT NOT NULL DEFAULT '', + updated_at TEXT NOT NULL +); """ _WIDGETS_SCHEMA = """ @@ -253,6 +263,56 @@ def set_last_fetcher_hash(username: str, h: str) -> None: c.commit() +def get_user_streak(username: str) -> Optional[dict]: + """Return the persisted streak row for a user, or None if never written.""" + with _settings_conn() as c: + row = c.execute( + "SELECT * FROM user_streaks WHERE username=?", (username,) + ).fetchone() + if row is None: + return None + return { + "current_streak": row["current_streak"], + "current_start": row["current_start"], + "last_active_date": row["last_active_date"], + "max_streak": row["max_streak"], + "max_start": row["max_start"], + "max_end": row["max_end"], + "updated_at": row["updated_at"], + } + + +def put_user_streak(username: str, streak: dict) -> None: + """UPSERT the streak row for a user. Caller supplies the merged values.""" + now = _now() + with _settings_conn() as c: + c.execute( + """INSERT INTO user_streaks( + username, current_streak, current_start, last_active_date, + max_streak, max_start, max_end, updated_at + ) VALUES (?, ?, ?, ?, ?, ?, ?, ?) + ON CONFLICT(username) DO UPDATE SET + current_streak=excluded.current_streak, + current_start=excluded.current_start, + last_active_date=excluded.last_active_date, + max_streak=excluded.max_streak, + max_start=excluded.max_start, + max_end=excluded.max_end, + updated_at=excluded.updated_at""", + ( + username, + int(streak.get("current_streak", 0)), + streak.get("current_start", ""), + streak.get("last_active_date", ""), + int(streak.get("max_streak", 0)), + streak.get("max_start", ""), + streak.get("max_end", ""), + now, + ), + ) + c.commit() + + def set_github_profile(username: str, github_id: int, github_avatar_url: str) -> None: with _settings_conn() as c: c.execute( diff --git a/generator/tests/test_db.py b/generator/tests/test_db.py index 6063899..ea12999 100644 --- a/generator/tests/test_db.py +++ b/generator/tests/test_db.py @@ -110,3 +110,43 @@ def test_set_github_profile_round_trip(tmp_dbs): row = c.execute("SELECT github_id, github_avatar_url FROM users WHERE username='alice'").fetchone() assert row["github_id"] == 42 assert row["github_avatar_url"] == "https://example.com/a.png" + + +def test_get_user_streak_missing_returns_none(tmp_dbs): + assert dbmod.get_user_streak("nobody") is None + + +def test_put_and_get_user_streak_roundtrip(tmp_dbs): + dbmod.put_user_streak("alice", { + "current_streak": 12, + "current_start": "2026-04-08", + "last_active_date": "2026-04-19", + "max_streak": 42, + "max_start": "2025-11-01", + "max_end": "2025-12-12", + }) + row = dbmod.get_user_streak("alice") + assert row["current_streak"] == 12 + assert row["current_start"] == "2026-04-08" + assert row["last_active_date"] == "2026-04-19" + assert row["max_streak"] == 42 + assert row["max_start"] == "2025-11-01" + assert row["max_end"] == "2025-12-12" + assert row["updated_at"] # set on write + + +def test_put_user_streak_upserts(tmp_dbs): + dbmod.put_user_streak("alice", { + "current_streak": 1, "current_start": "2026-04-19", + "last_active_date": "2026-04-19", + "max_streak": 1, "max_start": "2026-04-19", "max_end": "2026-04-19", + }) + dbmod.put_user_streak("alice", { + "current_streak": 2, "current_start": "2026-04-18", + "last_active_date": "2026-04-19", + "max_streak": 5, "max_start": "2024-01-01", "max_end": "2024-01-05", + }) + row = dbmod.get_user_streak("alice") + assert row["current_streak"] == 2 + assert row["max_streak"] == 5 + assert row["max_start"] == "2024-01-01"