Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 9 additions & 1 deletion src/services/wrapup_facts.py
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand Down
14 changes: 13 additions & 1 deletion tests/test_wrapup_comebacks_index.py
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand All @@ -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()
Expand Down
Loading