From 6bad392bb11b57dcc0909a67dc81b1b5f8015561 Mon Sep 17 00:00:00 2001 From: Claude Date: Tue, 17 Nov 2026 01:13:14 +0000 Subject: [PATCH] =?UTF-8?q?=E2=9A=A1=20Bolt:=20Wrap-Up=20comebacks=20skip?= =?UTF-8?q?=20titles=20played=20in=20the=20past=20year=20before=20the=20ma?= =?UTF-8?q?x()=20look-up?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Co-Authored-By: Claude Sonnet 5.5 Claude-Session: https://claude.ai/code/session_014GBDVACPfnJRWf1X64gcFN --- src/services/wrapup_facts.py | 10 +++++++++- tests/test_wrapup_comebacks_index.py | 14 +++++++++++++- 2 files changed, 22 insertions(+), 2 deletions(-) diff --git a/src/services/wrapup_facts.py b/src/services/wrapup_facts.py index d457c1c..64e4309 100644 --- a/src/services/wrapup_facts.py +++ b/src/services/wrapup_facts.py @@ -617,7 +617,15 @@ def _comebacks(db, W, user_id, period, types) -> list: W.viewed_at >= period.start, W.viewed_at < period.end).group_by(key).all() if k} if not inside: return [] - names = list(inside) + # A title played inside the year before the latest first play cannot have a + # gap that long; one range scan on ix_wh_user_type_viewed drops those, so + # the max() look-up below only walks the history of the few that remain + # (an artist the member plays all the time has thousands of earlier plays). + cutoff = max(inside.values()) - timedelta(days=_COMEBACK_GAP_DAYS) + recent = {k for (k,) in db.query(key).filter( + W.user_id == user_id, W.media_type.in_(types), + W.viewed_at >= cutoff, W.viewed_at < period.start).distinct()} + names = [k for k in inside if k not in recent] out = [] for i in range(0, len(names), 500): for k, last in db.query(key, func.max(W.viewed_at)).filter( diff --git a/tests/test_wrapup_comebacks_index.py b/tests/test_wrapup_comebacks_index.py index f13954a..06c4adc 100644 --- a/tests/test_wrapup_comebacks_index.py +++ b/tests/test_wrapup_comebacks_index.py @@ -30,7 +30,15 @@ def row(i, title, mt, at, series=None): row(9003, "ep 1", "show", now - timedelta(days=900), "Old Show"), # series: keyed by series row(9004, "ep 9", "show", now - timedelta(days=1), "Old Show"), row(9005, "Recent Film", "movie", now - timedelta(days=100)), # gap < a year - row(9006, "Recent Film", "movie", now - timedelta(days=1))] + row(9006, "Recent Film", "movie", now - timedelta(days=1)), + # played inside the year before: no comeback, and never worth the max() look-up + row(9007, "Regular", "movie", now - timedelta(days=40)), + row(9008, "Regular", "movie", now - timedelta(days=2)), + # just under / just over a year of silence before the week's first play + row(9009, "Edge Under", "movie", now - timedelta(days=364 + 2)), + row(9010, "Edge Under", "movie", now - timedelta(days=2)), + row(9011, "Edge Over", "movie", now - timedelta(days=366 + 2)), + row(9012, "Edge Over", "movie", now - timedelta(days=2))] db.bulk_insert_mappings(W, rows) db.commit() with conn.engine.begin() as c: @@ -49,8 +57,12 @@ def _grab(c, cur, stmt, params, *a): check("a film with no series_title comes back after 700 days", got.get("Old Film") == 698) check("a series comes back keyed by series_title", got.get("Old Show") == 899) check("a gap under a year is no comeback", "Recent Film" not in got) +check("a title played the year before is no comeback", "Regular" not in got) +check("364 days of silence is no comeback, 366 is", "Edge Under" not in got and got.get("Edge Over") == 366) stmt, params = next(s for s in statements if "max(watch_history.viewed_at)" in s[0]) +check("titles played within the year are not looked up at all", + not {"Regular", "Recent Film", "Edge Under"} & set(params)) raw = conn.engine.raw_connection() plan = " ".join(r[3] for r in raw.cursor().execute("EXPLAIN QUERY PLAN " + stmt, params)) raw.close()