mirror of
https://github.com/ShayManor/github-readme-stats.git
synced 2026-12-02 11:53:48 +05:30
feat(streaks): persist streaks on refresh and pass to renderer
Phase 1 worker now reads the stored streak, computes an updated one via processor.compute_streaks, persists it to the DB (preserving the all-time max across settings changes), and includes it in widget_data. Phase 2 _render_widgets pre-reads the stored streak and passes it to the processor.
This commit is contained in:
+20
-3
@@ -24,16 +24,31 @@ log = logging.getLogger("generator.worker")
|
|||||||
MAX_ATTEMPTS = 3
|
MAX_ATTEMPTS = 3
|
||||||
|
|
||||||
|
|
||||||
def _compute_widget_data(payload: dict, settings: dict) -> dict:
|
def _compute_widget_data(payload: dict, settings: dict, username: str) -> dict:
|
||||||
"""Compute raw widget data dict for client-side rendering."""
|
"""Compute raw widget data dict for client-side rendering.
|
||||||
|
|
||||||
|
Also persists the user's streak state as a side effect — the longest streak
|
||||||
|
must survive settings-hash rotations, so it lives outside widget_data.
|
||||||
|
"""
|
||||||
custom_tags = settings.get("custom_tags")
|
custom_tags = settings.get("custom_tags")
|
||||||
hidden_languages = settings.get("hidden_languages")
|
hidden_languages = settings.get("hidden_languages")
|
||||||
|
stored_streak = db.get_user_streak(username)
|
||||||
|
streak = processor.compute_streaks(payload, stored_streak)
|
||||||
|
db.put_user_streak(username, {
|
||||||
|
"current_streak": streak.current,
|
||||||
|
"current_start": streak.current_start,
|
||||||
|
"last_active_date": streak.last_active_date,
|
||||||
|
"max_streak": streak.max,
|
||||||
|
"max_start": streak.max_start,
|
||||||
|
"max_end": streak.max_end,
|
||||||
|
})
|
||||||
return {
|
return {
|
||||||
"grade": asdict(processor.compute_grade(payload, custom_tags=custom_tags)),
|
"grade": asdict(processor.compute_grade(payload, custom_tags=custom_tags)),
|
||||||
"impact": [asdict(w) for w in processor.compute_impact_timeline(payload)],
|
"impact": [asdict(w) for w in processor.compute_impact_timeline(payload)],
|
||||||
"collaborators": [asdict(c) for c in processor.compute_collaborators(payload)],
|
"collaborators": [asdict(c) for c in processor.compute_collaborators(payload)],
|
||||||
"focus": [asdict(f) for f in processor.compute_focus(payload, hidden_languages=hidden_languages)],
|
"focus": [asdict(f) for f in processor.compute_focus(payload, hidden_languages=hidden_languages)],
|
||||||
"languages": [asdict(l) for l in processor.compute_languages(payload, hidden_languages=hidden_languages)],
|
"languages": [asdict(l) for l in processor.compute_languages(payload, hidden_languages=hidden_languages)],
|
||||||
|
"streaks": asdict(streak),
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
@@ -41,6 +56,7 @@ def _render_widgets(username: str, payload: dict, settings: dict) -> dict[str, s
|
|||||||
enabled = settings.get("enabled") or config.ENABLED_WIDGETS
|
enabled = settings.get("enabled") or config.ENABLED_WIDGETS
|
||||||
order = settings.get("widget_order") or config.WIDGET_ORDER
|
order = settings.get("widget_order") or config.WIDGET_ORDER
|
||||||
theme = settings.get("theme", "dark")
|
theme = settings.get("theme", "dark")
|
||||||
|
stored_streak = db.get_user_streak(username)
|
||||||
widgets = processor.generate_widgets_from_github(
|
widgets = processor.generate_widgets_from_github(
|
||||||
payload,
|
payload,
|
||||||
theme=theme,
|
theme=theme,
|
||||||
@@ -49,6 +65,7 @@ def _render_widgets(username: str, payload: dict, settings: dict) -> dict[str, s
|
|||||||
enabled=enabled,
|
enabled=enabled,
|
||||||
widget_settings=settings.get("widget_settings") or {},
|
widget_settings=settings.get("widget_settings") or {},
|
||||||
achievements=settings.get("achievements") or [],
|
achievements=settings.get("achievements") or [],
|
||||||
|
stored_streak=stored_streak,
|
||||||
)
|
)
|
||||||
ordered = [w for w in order if w in enabled and w in widgets and widgets[w]]
|
ordered = [w for w in order if w in enabled and w in widgets and widgets[w]]
|
||||||
composite = compose_widget(
|
composite = compose_widget(
|
||||||
@@ -81,7 +98,7 @@ def process_one() -> bool:
|
|||||||
db.fail_job(job["id"], "settings missing", retry=False)
|
db.fail_job(job["id"], "settings missing", retry=False)
|
||||||
return True
|
return True
|
||||||
|
|
||||||
widget_data = _compute_widget_data(payload, settings_row["settings"])
|
widget_data = _compute_widget_data(payload, settings_row["settings"], username)
|
||||||
db.put_widget_data(username, settings_row["settings_hash"], widget_data)
|
db.put_widget_data(username, settings_row["settings_hash"], widget_data)
|
||||||
db.point_current_widget(username, settings_row["settings_hash"])
|
db.point_current_widget(username, settings_row["settings_hash"])
|
||||||
db.set_last_fetcher_hash(username, result.get("payload_hash", ""))
|
db.set_last_fetcher_hash(username, result.get("payload_hash", ""))
|
||||||
|
|||||||
@@ -79,3 +79,58 @@ def test_worker_marks_failed_after_max_attempts(tmp_dbs):
|
|||||||
row = c.execute("SELECT status, attempts FROM jobs WHERE username='alice'").fetchone()
|
row = c.execute("SELECT status, attempts FROM jobs WHERE username='alice'").fetchone()
|
||||||
assert row["status"] == "failed"
|
assert row["status"] == "failed"
|
||||||
assert row["attempts"] >= 3
|
assert row["attempts"] >= 3
|
||||||
|
|
||||||
|
|
||||||
|
def test_process_one_persists_streak(tmp_dbs):
|
||||||
|
"""Phase 1 worker must compute and persist the user's streak so max survives
|
||||||
|
future settings changes."""
|
||||||
|
from datetime import date, timedelta
|
||||||
|
|
||||||
|
today = date.today()
|
||||||
|
dates = [today - timedelta(days=i) for i in range(3)]
|
||||||
|
dbmod.enroll("alice", {"theme": "dark", "enabled": ["streaks"]})
|
||||||
|
fake_payload = {
|
||||||
|
"user": {"login": "alice"}, "repos": [], "events": [],
|
||||||
|
"commits": [{"date": d.isoformat(), "count": 1} for d in dates],
|
||||||
|
"total_commits": 3, "recent_commits": 3, "total_prs": 0,
|
||||||
|
"collaborators_data": [], "avatar_b64": "",
|
||||||
|
}
|
||||||
|
with patch("src.worker.fetcher_client.get_data",
|
||||||
|
return_value={"data": fake_payload, "payload_hash": "h1"}):
|
||||||
|
worker.process_one()
|
||||||
|
|
||||||
|
row = dbmod.get_user_streak("alice")
|
||||||
|
assert row is not None
|
||||||
|
assert row["current_streak"] == 3
|
||||||
|
assert row["max_streak"] == 3
|
||||||
|
assert row["last_active_date"] == today.isoformat()
|
||||||
|
|
||||||
|
|
||||||
|
def test_process_one_merges_stored_max(tmp_dbs):
|
||||||
|
"""A stored max of 50 must survive a refresh whose window max is only 2."""
|
||||||
|
from datetime import date, timedelta
|
||||||
|
|
||||||
|
today = date.today()
|
||||||
|
dbmod.enroll("alice", {"theme": "dark", "enabled": ["streaks"]})
|
||||||
|
dbmod.put_user_streak("alice", {
|
||||||
|
"current_streak": 0, "current_start": "",
|
||||||
|
"last_active_date": "2023-01-15",
|
||||||
|
"max_streak": 50,
|
||||||
|
"max_start": "2023-01-01", "max_end": "2023-02-19",
|
||||||
|
})
|
||||||
|
|
||||||
|
dates = [today - timedelta(days=i) for i in range(2)]
|
||||||
|
fake_payload = {
|
||||||
|
"user": {"login": "alice"}, "repos": [], "events": [],
|
||||||
|
"commits": [{"date": d.isoformat(), "count": 1} for d in dates],
|
||||||
|
"total_commits": 2, "recent_commits": 2, "total_prs": 0,
|
||||||
|
"collaborators_data": [], "avatar_b64": "",
|
||||||
|
}
|
||||||
|
with patch("src.worker.fetcher_client.get_data",
|
||||||
|
return_value={"data": fake_payload, "payload_hash": "h1"}):
|
||||||
|
worker.process_one()
|
||||||
|
|
||||||
|
row = dbmod.get_user_streak("alice")
|
||||||
|
assert row["max_streak"] == 50
|
||||||
|
assert row["max_start"] == "2023-01-01"
|
||||||
|
assert row["current_streak"] == 2
|
||||||
|
|||||||
Reference in New Issue
Block a user