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

@ -154,9 +154,12 @@ def test_added_at_is_corrected_when_plex_claims_it_postdates_a_play(scanned):
def test_correction_leaves_unwatched_items_alone(scanned):
"""Nothing contradicts Plex for an item nobody ever played."""
"""Nothing contradicts Plex for an item with no evidence of ever being
viewed by Tautulli OR by Plex's own lastViewedAt."""
rows = scanned.query(
"SELECT * FROM media_item WHERE kind='movie' AND watch_count = 0 "
"AND COALESCE(first_watched_at,0) = 0 "
"AND COALESCE(provider_last_viewed_at,0) = 0 "
"AND provider_added_at IS NOT NULL LIMIT 20")
assert rows
for r in rows:
@ -188,3 +191,24 @@ def test_added_at_correction_is_idempotent(scanned, rescan):
"SELECT id, added_at, provider_added_at, added_at_source FROM media_item "
"ORDER BY id")
assert [dict(r) for r in before] == [dict(r) for r in after]
def test_correction_works_from_plex_last_viewed_alone(scanned):
"""The live failure: the wrong dates are mostly older than Tautulli's history
window, so only Plex's lastViewedAt carries the evidence. The first version
of this correction used Tautulli alone and fired zero times on 65 TB."""
row = scanned.one("SELECT * FROM media_item WHERE title = 'Plex Evidence Only'")
assert row is not None
assert not row["first_watched_at"], "fixture must have no Tautulli history"
assert row["provider_last_viewed_at"] < row["provider_added_at"]
assert row["added_at"] == row["provider_last_viewed_at"], \
"correction ignored Plex's lastViewedAt"
assert row["added_at_source"] == "first_watch"
def test_seasons_get_a_provider_added_at(scanned):
"""Without one, a season's added_at_source can never be computed."""
n = scanned.scalar(
"SELECT COUNT(*) FROM media_item WHERE kind='season' AND episode_count > 0 "
"AND provider_added_at IS NULL")
assert n == 0