Use Plex's lastViewedAt for the date correction, not just Tautulli

The correction shipped in the previous commit fired zero times on the live
library. It compared Plex's addedAt against MediaShelf's first completed
Tautulli play, but Tautulli history only starts 2025-03-08 and the wrong dates
are overwhelmingly older than that. The evidence that proved the bug in the
first place was Plex's own lastViewedAt - which the provider parsed and then
dropped on the floor.

Now stored as provider_last_viewed_at and folded into the bound:
added_at = MIN(provider_added_at, first_watched_at, provider_last_viewed_at).
Seasons also get their own provider_added_at (MIN over episodes), without which
their added_at_source could never be computed.

Fixture gains the case that actually failed: added last week, last viewed 1500
days ago, no Tautulli history at all - so only lastViewedAt carries it. Also
tightened test_correction_leaves_unwatched_items_alone, which defined
"unwatched" as watch_count=0 and so wrongly expected an item with Plex view
evidence to be left alone.

Co-Authored-By: Claude Opus 5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01GVbG48GAXfCZatcmX123Ra
This commit is contained in:
Jess Hallsworth 2026-09-10 17:49:20 +00:00
parent e819548ff2
commit edbb6f088f
No known key found for this signature in database
6 changed files with 134 additions and 36 deletions

View file

@ -83,6 +83,21 @@ def _build(seed=7):
"size": 9 * 10**9}]}],
})
# The case that actually failed on the live library: Plex says it was added
# last week, Plex ALSO says it was last watched years ago, and Tautulli has
# no record at all because its history only starts recently. Only
# lastViewedAt carries the evidence here.
movies.append({
"sectionKey": "1", "ratingKey": "1998", "guid": "plex://movie/plexonly",
"type": "movie", "title": "Plex Evidence Only", "year": 1999,
"addedAt": NOW - 7 * DAY, "updatedAt": NOW - 7 * DAY,
"lastViewedAt": NOW - 1500 * DAY, # long before Tautulli existed
"duration": 100 * 60000, "viewCount": 2,
"Media": [{"videoResolution": "1080", "Part": [
{"id": 6998, "file": "/mnt/titan4/Movies/Plex Evidence Only/x.mkv",
"size": 7 * 10**9}]}],
})
# ── shows / seasons / episodes ─────────────────────────────────────
ep_rk = 50000
for s in range(6):
@ -129,7 +144,11 @@ def _build(seed=7):
ep_rk += 1
# ── Tautulli history ───────────────────────────────────────────────
watchable = [m["ratingKey"] for m in movies] + [e["ratingKey"] for e in episodes]
# 1998/1999 are the date-correction fixtures: their history is set
# explicitly below, so keep the random plays away from them.
SPECIAL = {"1998", "1999"}
watchable = [m["ratingKey"] for m in movies if m["ratingKey"] not in SPECIAL] \
+ [e["ratingKey"] for e in episodes]
row_id = 1
for _ in range(1200):
rk = rnd.choice(watchable)