Fix false "Plex fallback" banner during the first scan

history_coverage is only written at the end of an ingest, but
has_completion_data was reading from it. During the first scan the coverage
table is empty while watch_event already holds tens of thousands of Tautulli
rows, so the dashboard announced a fallback that had not happened and claimed
the rejection component was disabled when it was not.

The flag now comes from the events themselves — does any row carry a
percent_complete — which is true the moment Tautulli rows land and false for
Plex-only history. history_source falls back to the scan record when coverage
is absent, so it reads "tautulli" mid-scan instead of null.

Also: the banner named Plex as the source without checking, and now reports
whichever source is actually active, and stays quiet while a scan is running
since the counts are still moving.

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 14:30:09 +00:00
parent a842aeb344
commit 71cfa31f09
No known key found for this signature in database
4 changed files with 55 additions and 10 deletions

View file

@ -212,7 +212,7 @@ def sources():
return jsonify({
"plex": {"configured": c.plex_configured, "base_url": c.plex_base_url},
"tautulli": {"configured": c.tautulli_configured, "base_url": c.tautulli_base_url},
"history_source": (cov[0]["source"] if cov else None),
"history_source": queries.active_history_source(d),
"has_completion_data": queries.history_has_completion(d),
"coverage": cov,
"providers": [dict(r) for r in d.query("SELECT * FROM provider")],

View file

@ -34,11 +34,26 @@ def _score_ctx(db, cfg, has_completion_data: bool) -> scoring.ScoreContext:
def history_has_completion(db) -> bool:
"""True when the active history source records percent_complete (§4.11)."""
row = db.one(
"SELECT source FROM history_coverage ORDER BY event_count DESC LIMIT 1"
)
return bool(row and row["source"] == "tautulli")
"""True when the ingested history actually carries percent_complete (§4.11).
Read from the events themselves, not from history_coverage: that row is only
written at the END of an ingest, so during the first scan the coverage table
is empty while watch_event is already full of Tautulli rows. Deriving the
flag from the table made the UI announce a Plex fallback that had not
happened, mid-scan, on every fresh database.
"""
return bool(db.scalar(
"SELECT 1 FROM watch_event WHERE percent_complete IS NOT NULL LIMIT 1"))
def active_history_source(db) -> str | None:
"""Which source history came from — coverage if it exists, else the scan."""
row = db.one("SELECT source FROM history_coverage ORDER BY event_count DESC LIMIT 1")
if row:
return row["source"]
row = db.one("SELECT history_source FROM scan WHERE history_source IS NOT NULL "
"ORDER BY id DESC LIMIT 1")
return row["history_source"] if row else None
BASE_COLUMNS = """
@ -273,7 +288,7 @@ def overview(db, cfg) -> dict:
"episodes": s("SELECT COUNT(*) FROM episode WHERE status='present'"),
"watch_events": s("SELECT COUNT(*) FROM watch_event"),
"accounts": s("SELECT COUNT(*) FROM account"),
"history_source": cov["source"] if cov else None,
"history_source": active_history_source(db),
"history_since": cov["earliest_event_at"] if cov else None,
"history_until": cov["latest_event_at"] if cov else None,
"has_completion_data": history_has_completion(db),

View file

@ -19,6 +19,7 @@ const state = {
views: [],
lastPage: null,
hasCompletion: true,
scanning: false,
};
/* ── helpers ──────────────────────────────────────────────────────── */
@ -162,9 +163,11 @@ async function loadDashboard() {
? 'Completion data available — the rejection component is active'
: 'No completion data — running degraded, rejection component disabled';
}
if (!ov.has_completion_data && ov.watch_events > 0) {
banner('warn', 'Watch history has no completion data (Plex fallback). ' +
'The score is running degraded: the “rejection” component is disabled.');
// Only a finished scan can tell us this; mid-scan the counts are still moving.
if (!ov.has_completion_data && ov.watch_events > 0 && !state.scanning) {
banner('warn', `Watch history has no completion data (source: ` +
`${ov.history_source || 'unknown'}). The score is running degraded: ` +
`the “rejection” component is disabled.`);
}
// size by library
@ -727,6 +730,7 @@ $('#scan-now').addEventListener('click', async () => {
async function pollScan() {
const btn = $('#scan-now');
btn.disabled = true;
state.scanning = true;
const tick = async () => {
const s = await api('/scans/current');
if (s) {
@ -735,6 +739,7 @@ async function pollScan() {
} else {
btn.textContent = 'Scan now';
btn.disabled = false;
state.scanning = false;
$('#banner-area').replaceChildren();
show(state.view);
}