diff --git a/README.md b/README.md index 14b9422..a867fe7 100644 --- a/README.md +++ b/README.md @@ -460,6 +460,7 @@ The API also exposes authenticated management endpoints used by the plugin and s - `POST /api/jellyfin/libraries/selection` — apply the authoritative library selection and remove deselected libraries from SQLite/Meilisearch. - `POST /api/jellyfin/index` — apply the authoritative selection and start one background indexing job for selected library ids. - `GET /api/jellyfin/index/status` — report live library/item/frame/sync progress. +- `GET /api/jellyfin/index/report` — page through the persisted last-run details for failed, removed or indexed items. - `POST /api/caption` — start or resume caption generation for selected library ids. - `GET /api/caption/status` — report routing/caption/model/frame/sync progress. - `POST /api/caption/pause` — cooperatively pause routing/captioning at the next safe boundary. @@ -523,7 +524,8 @@ The plugin's library selection is authoritative. Saving the plugin settings post Library removal is targeted. **Apply selection only** (or saving the plugin settings) deletes deselected libraries from SQLite and deletes only matching `jellyfin_library_id` documents from Meilisearch; it does not clear/re-feed the complete Meilisearch index. **Index selected libraries** remains the action for scanning media. If that scan finds all retained libraries unchanged, Scene Search now skips the final full Meilisearch rebuild as well. -Use **Apply library selection / index** to apply the same authoritative set and start a backend indexing job. An empty selection is allowed only after an explicit confirmation and removes all Jellyfin-backed Scene Search data. The plugin shows an overall percentage/progress bar across all selected libraries as well as the current library, item and frame progress, and polls until the job completes. When ingestion finishes, the backend automatically rebuilds Meilisearch, so newly indexed vectors are searchable without a separate `sync-meili` command. +Use **Apply library selection / index** to apply the same authoritative set and start a backend indexing job. An empty selection is allowed only after an explicit confirmation and removes all Jellyfin-backed Scene Search data. The plugin shows an overall percentage/progress bar across all selected libraries as well as the current library, item and frame progress, and polls until the job completes. When ingestion finishes, the backend automatically rebuilds Meilisearch, so newly indexed vectors are searchable without a separate `sync-meili` command. The completed summary also exposes drill-down buttons for **Failed**, **Removed** and **Indexed** items. Those details are written to `data/last-index-report.json`, are paged on demand instead of being included in the 2-second status polling payload, and survive a dashboard refresh or container restart. Unchanged items are counted but intentionally not persisted individually because large libraries can contain tens of thousands of them. + The same page also has **Start / resume captioning** and **Pause**. Pause finishes the currently active routing batch or Ollama frame request, then waits before starting the next unit; Resume continues the same in-memory job without rebuilding its queues. Every successful caption is committed immediately, so a process/container interruption can still be recovered by pressing Start / resume and continuing from uncaptained frames. **Force recaption existing frames** deliberately regenerates captions, but reuses saved routing decisions while the routing policy is still current. diff --git a/jellyfin-plugin/Jellyfin.Plugin.SceneSearch/Api/SceneSearchController.cs b/jellyfin-plugin/Jellyfin.Plugin.SceneSearch/Api/SceneSearchController.cs index 89cf1f0..ab4c14f 100644 --- a/jellyfin-plugin/Jellyfin.Plugin.SceneSearch/Api/SceneSearchController.cs +++ b/jellyfin-plugin/Jellyfin.Plugin.SceneSearch/Api/SceneSearchController.cs @@ -58,6 +58,24 @@ public sealed class SceneSearchController : ControllerBase : Ok(result); } + [HttpGet("IndexReport")] + [ProducesResponseType(StatusCodes.Status200OK)] + public async Task> IndexReport( + [FromQuery] string? category = null, + [FromQuery] int offset = 0, + [FromQuery] int limit = 200) + { + var result = await _client.GetIndexReportAsync( + category, + offset, + limit, + HttpContext.RequestAborted).ConfigureAwait(false); + + return result is null + ? StatusCode(StatusCodes.Status503ServiceUnavailable, "Scene Search API unavailable.") + : Ok(result); + } + [HttpPost("Caption")] [ProducesResponseType(StatusCodes.Status202Accepted)] diff --git a/jellyfin-plugin/Jellyfin.Plugin.SceneSearch/Configuration/configPage.html b/jellyfin-plugin/Jellyfin.Plugin.SceneSearch/Configuration/configPage.html index 3af5afc..9c4e4e8 100644 --- a/jellyfin-plugin/Jellyfin.Plugin.SceneSearch/Configuration/configPage.html +++ b/jellyfin-plugin/Jellyfin.Plugin.SceneSearch/Configuration/configPage.html @@ -70,7 +70,17 @@ -
+
+
+ + + +
+
+
+

Captioning

@@ -115,6 +125,9 @@ var lastIndexRunning = false; var lastCaptionRunning = false; var lastCaptionStatus = null; + var reportCategory = null; + var reportOffset = 0; + var reportTotal = 0; function selectedLibraryIds() { return Array.prototype.slice.call( @@ -200,6 +213,79 @@ } } + function refreshIndexReportSummary() { + return ApiClient.getJSON( + ApiClient.getUrl('Plugins/SceneSearch/IndexReport') + '?limit=1' + ).then(function (report) { + var summary = (report && report.summary) || {}; + page.querySelector('#reportFailed span').textContent = + 'Failed (' + (summary.failed || 0) + ')'; + page.querySelector('#reportRemoved span').textContent = + 'Removed (' + (summary.pruned || 0) + ')'; + page.querySelector('#reportIndexed span').textContent = + 'Indexed (' + (summary.indexed || 0) + ')'; + if (!report || !report.available) { + page.querySelector('#indexReportMeta').textContent = + 'No saved index report yet.'; + } + }).catch(function () { + // Status remains usable even if no report is available. + }); + } + + function loadIndexReport(category, append) { + append = !!append; + var offset = append ? reportOffset : 0; + var url = ApiClient.getUrl('Plugins/SceneSearch/IndexReport') + + '?category=' + encodeURIComponent(category) + + '&offset=' + offset + '&limit=200'; + + return ApiClient.getJSON(url).then(function (report) { + reportCategory = category; + var items = (report && report.items) || []; + reportOffset = offset + items.length; + reportTotal = Number((report && report.total) || 0); + + var container = page.querySelector('#indexReportList'); + if (!append) { + container.innerHTML = ''; + } + + items.forEach(function (item) { + var row = document.createElement('div'); + row.style.cssText = + 'padding:.65em 0;border-bottom:1px solid rgba(255,255,255,.12)'; + + var title = document.createElement('strong'); + title.textContent = item.title || item.item_id || '(unnamed)'; + row.appendChild(title); + + var meta = document.createElement('div'); + meta.className = 'fieldDescription'; + meta.textContent = + (item.library || item.library_id || '') + + (item.item_id ? ' · ' + item.item_id : ''); + row.appendChild(meta); + + var reason = document.createElement('div'); + reason.textContent = item.reason || ''; + row.appendChild(reason); + container.appendChild(row); + }); + + page.querySelector('#indexReportMeta').textContent = + reportTotal + ' ' + category + ' item' + + (reportTotal === 1 ? '' : 's') + + (report.finished_at ? ' · last run ' + report.finished_at : ''); + + page.querySelector('#indexReportMore').style.display = + reportOffset < reportTotal ? '' : 'none'; + }).catch(function () { + page.querySelector('#indexReportMeta').textContent = + 'Could not load the saved index report.'; + }); + } + function refreshStatus() { return ApiClient.getJSON(ApiClient.getUrl('Plugins/SceneSearch/IndexStatus')) .then(function (status) { @@ -208,6 +294,7 @@ if (lastIndexRunning && !running) { loadLibraries(); + refreshIndexReportSummary(); } lastIndexRunning = running; }) @@ -346,7 +433,12 @@ page.querySelector('#maxResults').value = config.MaxResults || 20; page.querySelector('#timeoutSeconds').value = config.TimeoutSeconds || 60; - return Promise.all([loadLibraries(), refreshStatus(), refreshCaptionStatus()]); + return Promise.all([ + loadLibraries(), + refreshStatus(), + refreshCaptionStatus(), + refreshIndexReportSummary() + ]); }).then(function () { if (!statusTimer) { statusTimer = setInterval(refreshStatus, 2000); @@ -370,6 +462,21 @@ } }); + page.querySelector('#reportFailed').addEventListener('click', function () { + loadIndexReport('failed', false); + }); + page.querySelector('#reportRemoved').addEventListener('click', function () { + loadIndexReport('removed', false); + }); + page.querySelector('#reportIndexed').addEventListener('click', function () { + loadIndexReport('indexed', false); + }); + page.querySelector('#indexReportMore').addEventListener('click', function () { + if (reportCategory) { + loadIndexReport(reportCategory, true); + } + }); + page.querySelector('#applyLibraries').addEventListener('click', function () { var ids = selectedLibraryIds(); if (!ids.length && !window.confirm( @@ -403,6 +510,7 @@ ' and ' + removedMedia + ' media items. No full search-index rebuild.' : 'Selection applied: nothing to remove.'; loadLibraries(); + refreshIndexReportSummary(); }).catch(function () { page.querySelector('#indexStatus').textContent = 'Could not apply library selection. Check Jellyfin logs and Scene Search API status.'; diff --git a/jellyfin-plugin/Jellyfin.Plugin.SceneSearch/SceneSearchAdminModels.cs b/jellyfin-plugin/Jellyfin.Plugin.SceneSearch/SceneSearchAdminModels.cs index 9533168..33c876e 100644 --- a/jellyfin-plugin/Jellyfin.Plugin.SceneSearch/SceneSearchAdminModels.cs +++ b/jellyfin-plugin/Jellyfin.Plugin.SceneSearch/SceneSearchAdminModels.cs @@ -99,6 +99,91 @@ public sealed class SceneSearchIndexStatus } +public sealed class SceneSearchIndexReportResponse +{ + [JsonPropertyName("available")] + public bool Available { get; set; } + + [JsonPropertyName("kind")] + public string? Kind { get; set; } + + [JsonPropertyName("status")] + public string? Status { get; set; } + + [JsonPropertyName("started_at")] + public string? StartedAt { get; set; } + + [JsonPropertyName("finished_at")] + public string? FinishedAt { get; set; } + + [JsonPropertyName("error")] + public string? Error { get; set; } + + [JsonPropertyName("summary")] + public SceneSearchIndexReportSummary Summary { get; set; } = new(); + + [JsonPropertyName("category")] + public string? Category { get; set; } + + [JsonPropertyName("offset")] + public int Offset { get; set; } + + [JsonPropertyName("limit")] + public int Limit { get; set; } + + [JsonPropertyName("total")] + public int Total { get; set; } + + [JsonPropertyName("items")] + public List Items { get; set; } = []; +} + +public sealed class SceneSearchIndexReportSummary +{ + [JsonPropertyName("indexed")] + public int Indexed { get; set; } + + [JsonPropertyName("skipped")] + public int Skipped { get; set; } + + [JsonPropertyName("failed")] + public int Failed { get; set; } + + [JsonPropertyName("pruned")] + public int Pruned { get; set; } + + [JsonPropertyName("removed_libraries")] + public int RemovedLibraries { get; set; } +} + +public sealed class SceneSearchIndexReportItem +{ + [JsonPropertyName("status")] + public string Status { get; set; } = string.Empty; + + [JsonPropertyName("library_id")] + public string LibraryId { get; set; } = string.Empty; + + [JsonPropertyName("library")] + public string Library { get; set; } = string.Empty; + + [JsonPropertyName("item_id")] + public string ItemId { get; set; } = string.Empty; + + [JsonPropertyName("title")] + public string Title { get; set; } = string.Empty; + + [JsonPropertyName("reason")] + public string Reason { get; set; } = string.Empty; + + [JsonPropertyName("frames")] + public int? Frames { get; set; } + + [JsonPropertyName("preserved_annotations")] + public int? PreservedAnnotations { get; set; } +} + + public sealed class SceneSearchCaptionRequest { [JsonPropertyName("library_ids")] diff --git a/jellyfin-plugin/Jellyfin.Plugin.SceneSearch/SceneSearchClient.cs b/jellyfin-plugin/Jellyfin.Plugin.SceneSearch/SceneSearchClient.cs index 03e14aa..5007163 100644 --- a/jellyfin-plugin/Jellyfin.Plugin.SceneSearch/SceneSearchClient.cs +++ b/jellyfin-plugin/Jellyfin.Plugin.SceneSearch/SceneSearchClient.cs @@ -97,6 +97,30 @@ public sealed class SceneSearchClient : IDisposable content: null, cancellationToken); + public Task GetIndexReportAsync( + string? category, + int offset, + int limit, + CancellationToken cancellationToken) + { + var path = string.Concat( + "/api/jellyfin/index/report?offset=", + Math.Max(offset, 0).ToString(System.Globalization.CultureInfo.InvariantCulture), + "&limit=", + Math.Clamp(limit, 1, 1000).ToString(System.Globalization.CultureInfo.InvariantCulture)); + + if (!string.IsNullOrWhiteSpace(category)) + { + path += "&category=" + Uri.EscapeDataString(category); + } + + return SendAsync( + HttpMethod.Get, + path, + content: null, + cancellationToken); + } + public Task StartCaptionAsync( SceneSearchCaptionRequest request, CancellationToken cancellationToken) diff --git a/scene_search/api.py b/scene_search/api.py index 63671e8..44b9c88 100644 --- a/scene_search/api.py +++ b/scene_search/api.py @@ -189,6 +189,27 @@ def jellyfin_index_status() -> dict: return get_index_jobs().status() +@app.get( + "/api/jellyfin/index/report", + dependencies=[Depends(require_api_key)], +) +def jellyfin_index_report( + category: str | None = Query(default=None), + offset: int = Query(default=0, ge=0), + limit: int = Query(default=200, ge=1, le=1000), +) -> dict: + if category not in {None, "failed", "removed", "indexed"}: + raise HTTPException( + status_code=400, + detail="category must be failed, removed, indexed, or omitted", + ) + return get_index_jobs().report( + category=category, + offset=offset, + limit=limit, + ) + + @app.post( "/api/caption", dependencies=[Depends(require_api_key)], diff --git a/scene_search/index_jobs.py b/scene_search/index_jobs.py index 8bb6cf2..df8ed62 100644 --- a/scene_search/index_jobs.py +++ b/scene_search/index_jobs.py @@ -6,6 +6,7 @@ from datetime import datetime, timezone from typing import Any from .database import connect, init_db +from .index_report import load_index_report, save_index_report from .jellyfin_index import ( build_jellyfin_client, index_libraries, @@ -40,6 +41,39 @@ class IndexJobManager: with self._state_lock: return deepcopy(self._state) + def report( + self, + *, + category: str | None = None, + offset: int = 0, + limit: int = 200, + ) -> dict[str, Any]: + report = load_index_report() + items = list(report.get("items") or []) + if category: + items = [ + item + for item in items + if str(item.get("status") or "") == category + ] + + total = len(items) + page = items[offset:offset + limit] + return { + "available": bool(report.get("available", False)), + "kind": report.get("kind"), + "status": report.get("status"), + "started_at": report.get("started_at"), + "finished_at": report.get("finished_at"), + "error": report.get("error"), + "summary": report.get("summary") or {}, + "category": category, + "offset": offset, + "limit": limit, + "total": total, + "items": page, + } + def libraries(self) -> list[dict[str, Any]]: client = build_jellyfin_client() libraries = client.media_folders() @@ -92,7 +126,9 @@ class IndexJobManager: for value in library_ids if str(value).strip() } + started_at = datetime.now(timezone.utc).isoformat() result = reconcile_selected_libraries(selected_ids) + removed_items = list(result.pop("removed_items", []) or []) removed_ids = [ str(value) for value in (result.get("removed_library_ids") or []) @@ -104,6 +140,26 @@ class IndexJobManager: + ", ".join(removed_ids) + f" ({int(result.get('removed_media') or 0)} media item(s))" ) + + finished_at = datetime.now(timezone.utc).isoformat() + save_index_report( + { + "available": True, + "kind": "library_selection", + "status": "complete", + "started_at": started_at, + "finished_at": finished_at, + "error": None, + "summary": { + "indexed": 0, + "skipped": 0, + "failed": 0, + "pruned": int(result.get("removed_media") or 0), + "removed_libraries": int(result.get("removed_libraries") or 0), + }, + "items": removed_items, + } + ) return result def start(self, library_ids: list[str], *, force: bool = False) -> dict[str, Any]: @@ -208,8 +264,11 @@ class IndexJobManager: selected_ids: set[str], force: bool, ) -> None: + report_items: list[dict[str, object]] = [] + started_at = str(self.status().get("started_at") or datetime.now(timezone.utc).isoformat()) try: removed = reconcile_selected_libraries(selected_ids) + report_items.extend(list(removed.pop("removed_items", []) or [])) removed_ids = [ str(value) for value in (removed.get("removed_library_ids") or []) @@ -229,17 +288,34 @@ class IndexJobManager: ) if not libraries: + finished_at = datetime.now(timezone.utc).isoformat() + summary = { + "library_total": 0, + "indexed": 0, + "skipped": 0, + "failed": 0, + "pruned": int(removed.get("removed_media") or 0), + "removed_libraries": int(removed.get("removed_libraries") or 0), + } self._progress( { "running": False, "stage": "complete", - "finished_at": datetime.now(timezone.utc).isoformat(), + "finished_at": finished_at, "documents_processed": 0, - "library_total": 0, - "indexed": 0, - "skipped": 0, - "failed": 0, - "pruned": int(removed.get("removed_media") or 0), + **summary, + } + ) + save_index_report( + { + "available": True, + "kind": "index", + "status": "complete", + "started_at": started_at, + "finished_at": finished_at, + "error": None, + "summary": summary, + "items": report_items, } ) return @@ -253,6 +329,7 @@ class IndexJobManager: embedder=embedder, force=force, progress=self._progress, + report_items=report_items, ) retained_index_changed = ( int(summary.get("indexed") or 0) > 0 @@ -276,22 +353,56 @@ class IndexJobManager: "skipping full Meilisearch rebuild." ) + finished_at = datetime.now(timezone.utc).isoformat() self._progress( { "running": False, "stage": "complete", - "finished_at": datetime.now(timezone.utc).isoformat(), + "finished_at": finished_at, "documents_processed": documents, "full_meili_rebuild": retained_index_changed, **summary, } ) + save_index_report( + { + "available": True, + "kind": "index", + "status": "complete", + "started_at": started_at, + "finished_at": finished_at, + "error": None, + "summary": { + **summary, + "documents_processed": documents, + "full_meili_rebuild": retained_index_changed, + }, + "items": report_items, + } + ) except Exception as exc: + finished_at = datetime.now(timezone.utc).isoformat() self._progress( { "running": False, "stage": "failed", - "finished_at": datetime.now(timezone.utc).isoformat(), + "finished_at": finished_at, "error": str(exc), } ) + save_index_report( + { + "available": True, + "kind": "index", + "status": "failed", + "started_at": started_at, + "finished_at": finished_at, + "error": str(exc), + "summary": { + "indexed": sum(1 for item in report_items if item.get("status") == "indexed"), + "failed": sum(1 for item in report_items if item.get("status") == "failed"), + "pruned": sum(1 for item in report_items if item.get("status") == "removed"), + }, + "items": report_items, + } + ) diff --git a/scene_search/index_report.py b/scene_search/index_report.py new file mode 100644 index 0000000..21921fa --- /dev/null +++ b/scene_search/index_report.py @@ -0,0 +1,53 @@ +from __future__ import annotations + +import json +from pathlib import Path +from typing import Any + +from .config import settings + + +def report_path() -> Path: + return settings.data_dir / "last-index-report.json" + + +def save_index_report(report: dict[str, Any]) -> None: + path = report_path() + path.parent.mkdir(parents=True, exist_ok=True) + tmp = path.with_suffix(path.suffix + ".tmp") + tmp.write_text( + json.dumps(report, ensure_ascii=False, indent=2), + encoding="utf-8", + ) + tmp.replace(path) + + +def load_index_report() -> dict[str, Any]: + path = report_path() + if not path.is_file(): + return { + "available": False, + "items": [], + "summary": {}, + } + + try: + data = json.loads(path.read_text(encoding="utf-8")) + except (OSError, json.JSONDecodeError): + return { + "available": False, + "items": [], + "summary": {}, + } + + if not isinstance(data, dict): + return { + "available": False, + "items": [], + "summary": {}, + } + + data.setdefault("available", True) + data.setdefault("items", []) + data.setdefault("summary", {}) + return data diff --git a/scene_search/jellyfin_index.py b/scene_search/jellyfin_index.py index 0439506..e243d3a 100644 --- a/scene_search/jellyfin_index.py +++ b/scene_search/jellyfin_index.py @@ -463,10 +463,11 @@ def reconcile_selected_libraries( removed_library_ids: list[str] = [] removed_media_ids: list[str] = [] + removed_items: list[dict[str, object]] = [] with connect(settings.db_path) as con: rows = con.execute( """ - SELECT id, jellyfin_library_id + SELECT id, title, jellyfin_item_id, jellyfin_library_id FROM media WHERE source_kind = 'jellyfin' AND jellyfin_library_id IS NOT NULL @@ -483,6 +484,17 @@ def reconcile_selected_libraries( {str(row["jellyfin_library_id"]) for row in stale} ) removed_media_ids = [str(row["id"]) for row in stale] + removed_items = [ + { + "status": "removed", + "library_id": str(row["jellyfin_library_id"] or ""), + "library": str(row["jellyfin_library_id"] or ""), + "item_id": str(row["jellyfin_item_id"] or ""), + "title": str(row["title"] or row["jellyfin_item_id"] or row["id"]), + "reason": "Library deselected from Scene Search", + } + for row in stale + ] if removed_library_ids: placeholders = ",".join("?" for _ in removed_library_ids) @@ -502,15 +514,22 @@ def reconcile_selected_libraries( "removed_library_ids": removed_library_ids, "removed_libraries": len(removed_library_ids), "removed_media": len(removed_media_ids), + "removed_items": removed_items, } -def _reconcile_library(library_id: str, current_item_ids: set[str]) -> int: +def _reconcile_library( + library_id: str, + current_item_ids: set[str], + *, + library_name: str | None = None, + report_items: list[dict[str, object]] | None = None, +) -> int: stale_rows = [] with connect(settings.db_path) as con: rows = con.execute( """ - SELECT id, jellyfin_item_id + SELECT id, title, jellyfin_item_id FROM media WHERE source_kind = 'jellyfin' AND jellyfin_library_id = ? @@ -528,6 +547,17 @@ def _reconcile_library(library_id: str, current_item_ids: set[str]) -> int: for row in stale_rows: shutil.rmtree(settings.thumbs_dir / str(row["id"]), ignore_errors=True) + if report_items is not None: + report_items.append( + { + "status": "removed", + "library_id": library_id, + "library": library_name or library_id, + "item_id": str(row["jellyfin_item_id"] or ""), + "title": str(row["title"] or row["jellyfin_item_id"] or row["id"]), + "reason": "Item no longer present in Jellyfin library", + } + ) return len(stale_rows) @@ -540,6 +570,7 @@ def _index_library( limit: int | None, force: bool, progress: ProgressCallback | None = None, + report_items: list[dict[str, object]] | None = None, ) -> tuple[SiglipEmbedder | None, int, int, int, int]: library_id = str(library["Id"]) library_name = _library_name(library) @@ -599,6 +630,17 @@ def _index_library( if existing: con.execute("DELETE FROM media WHERE id = ?", (existing["id"],)) pruned += 1 + if report_items is not None: + report_items.append( + { + "status": "removed", + "library_id": library_id, + "library": library_name, + "item_id": item_id, + "title": title, + "reason": "Jellyfin skip tag", + } + ) shutil.rmtree( settings.thumbs_dir / jellyfin_media_id(item_id), ignore_errors=True, @@ -677,11 +719,23 @@ def _index_library( else: local_path = Path(source_path_text) if not local_path.is_file(): + reason = "No Trickplay and media path is not locally accessible" print( f"[{number}/{len(summaries)}] SKIP {title}: " "no Trickplay and media path is not locally accessible" ) failed += 1 + if report_items is not None: + report_items.append( + { + "status": "failed", + "library_id": library_id, + "library": library_name, + "item_id": item_id, + "title": title, + "reason": reason, + } + ) continue print( @@ -703,8 +757,20 @@ def _index_library( trickplay_tile_height = None if selection is None and not frame_paths: + reason = "No frames were extracted" print(f"[{number}/{len(summaries)}] SKIP {title}: no frames") failed += 1 + if report_items is not None: + report_items.append( + { + "status": "failed", + "library_id": library_id, + "library": library_name, + "item_id": item_id, + "title": title, + "reason": reason, + } + ) continue if embedder is None: @@ -817,16 +883,47 @@ def _index_library( f"{preserved_note}" ) indexed += 1 + if report_items is not None: + report_items.append( + { + "status": "indexed", + "library_id": library_id, + "library": library_name, + "item_id": item_id, + "title": title, + "frames": len(frame_rows), + "preserved_annotations": preserved, + "reason": "Indexed successfully", + } + ) except Exception as exc: failed += 1 + failed_item_id = str(summary.get("Id") or "") + failed_title = str(summary.get("Name") or failed_item_id or "(unnamed)") print( f"[{number}/{len(summaries)}] FAIL " - f"{summary.get('Name') or summary.get('Id')}: {exc}" + f"{failed_title}: {exc}" ) + if report_items is not None: + report_items.append( + { + "status": "failed", + "library_id": library_id, + "library": library_name, + "item_id": failed_item_id, + "title": failed_title, + "reason": str(exc), + } + ) if limit is None: - reconciled = _reconcile_library(library_id, current_item_ids) + reconciled = _reconcile_library( + library_id, + current_item_ids, + library_name=library_name, + report_items=report_items, + ) pruned += reconciled if reconciled: print(f" pruned {reconciled} stale item(s) no longer present in Jellyfin") @@ -844,6 +941,7 @@ def index_libraries( limit: int | None = None, force: bool = False, progress: ProgressCallback | None = None, + report_items: list[dict[str, object]] | None = None, ) -> tuple[SiglipEmbedder | None, dict]: total_indexed = 0 total_skipped = 0 @@ -869,6 +967,7 @@ def index_libraries( limit=limit, force=force, progress=progress, + report_items=report_items, ) total_indexed += indexed total_skipped += skipped diff --git a/scene_search/web/index.html b/scene_search/web/index.html index 45c846f..58dbaf6 100644 --- a/scene_search/web/index.html +++ b/scene_search/web/index.html @@ -90,6 +90,14 @@
Idle
+
+ + + +
+
+
+
@@ -127,6 +135,9 @@ let lastIndexRunning = false; let lastCaptionRunning = false; let captionLastStatus = null; + let reportCategory = null; + let reportOffset = 0; + let reportTotal = 0; let thumbnailUrls = []; $('apiKey').value = localStorage.getItem('sceneSearchApiKey') || ''; @@ -222,6 +233,55 @@ } } + async function refreshIndexReportSummary() { + try { + const report = await api('/api/jellyfin/index/report?limit=1'); + const summary = report.summary || {}; + $('reportFailed').textContent = 'Failed (' + (summary.failed || 0) + ')'; + $('reportRemoved').textContent = 'Removed (' + (summary.pruned || 0) + ')'; + $('reportIndexed').textContent = 'Indexed (' + (summary.indexed || 0) + ')'; + if (!report.available) { + $('indexReportMeta').textContent = 'No saved index report yet.'; + } + } catch (_) {} + } + + async function loadIndexReport(category, append=false) { + const offset = append ? reportOffset : 0; + try { + const report = await api( + '/api/jellyfin/index/report?category=' + encodeURIComponent(category) + + '&offset=' + offset + '&limit=200' + ); + reportCategory = category; + reportOffset = offset + (report.items || []).length; + reportTotal = Number(report.total || 0); + + const container = $('indexReportList'); + if (!append) container.innerHTML = ''; + + for (const item of (report.items || [])) { + const row = document.createElement('div'); + row.style.cssText = 'padding:8px 0;border-bottom:1px solid #282d36'; + row.innerHTML = + '' + escapeHtml(item.title || item.item_id || '(unnamed)') + '' + + '
' + + escapeHtml(item.library || item.library_id || '') + + (item.item_id ? ' · ' + escapeHtml(item.item_id) : '') + + '
' + + '
' + escapeHtml(item.reason || '') + '
'; + container.appendChild(row); + } + + $('indexReportMeta').textContent = + reportTotal + ' ' + category + ' item' + (reportTotal === 1 ? '' : 's') + + (report.finished_at ? ' · last run ' + report.finished_at : ''); + $('indexReportMore').style.display = reportOffset < reportTotal ? '' : 'none'; + } catch (e) { + $('indexReportMeta').textContent = e.message; + } + } + async function refreshStatus() { try { const s = await api('/api/jellyfin/index/status'); @@ -230,6 +290,7 @@ if (lastIndexRunning && !running) { loadLibraries(); refreshHealth(); + refreshIndexReportSummary(); } lastIndexRunning = running; return s; @@ -265,6 +326,7 @@ $('indexDetail').textContent = 'No full Meilisearch rebuild was run.'; await loadLibraries(); refreshHealth(); + refreshIndexReportSummary(); } catch (e) { $('indexStatus').className = 'status error'; $('indexStatus').textContent = e.message; @@ -472,6 +534,12 @@ $('indexNow').onclick = startIndex; $('captionNow').onclick = startCaption; $('captionPause').onclick = toggleCaptionPause; + $('reportFailed').onclick = () => loadIndexReport('failed'); + $('reportRemoved').onclick = () => loadIndexReport('removed'); + $('reportIndexed').onclick = () => loadIndexReport('indexed'); + $('indexReportMore').onclick = () => { + if (reportCategory) loadIndexReport(reportCategory, true); + }; $('searchButton').onclick = search; $('searchQuery').addEventListener('keydown', e => { if (e.key === 'Enter') search(); }); @@ -479,6 +547,7 @@ loadLibraries(); refreshStatus(); refreshCaptionStatus(); + refreshIndexReportSummary(); statusTimer = setInterval(refreshStatus, 2000); captionTimer = setInterval(refreshCaptionStatus, 2000); })(); diff --git a/tests/test_index_report.py b/tests/test_index_report.py new file mode 100644 index 0000000..2f8d9b2 --- /dev/null +++ b/tests/test_index_report.py @@ -0,0 +1,64 @@ +from __future__ import annotations + +import tempfile +import unittest +from pathlib import Path +from types import SimpleNamespace +from unittest.mock import patch + +from scene_search.index_jobs import IndexJobManager +from scene_search.index_report import load_index_report, save_index_report + + +class IndexReportTests(unittest.TestCase): + def test_report_persists_across_process_state(self) -> None: + with tempfile.TemporaryDirectory() as tmp: + fake_settings = SimpleNamespace(data_dir=Path(tmp)) + report = { + "available": True, + "kind": "index", + "status": "complete", + "summary": {"failed": 1, "pruned": 1, "indexed": 1}, + "items": [ + {"status": "failed", "title": "Bad", "reason": "No Trickplay"}, + {"status": "removed", "title": "Gone", "reason": "Removed"}, + {"status": "indexed", "title": "Good", "reason": "Indexed"}, + ], + } + + with patch("scene_search.index_report.settings", fake_settings): + save_index_report(report) + loaded = load_index_report() + + self.assertTrue(loaded["available"]) + self.assertEqual(loaded["summary"]["failed"], 1) + self.assertEqual(len(loaded["items"]), 3) + + def test_manager_report_filters_and_pages_without_polling_all_items(self) -> None: + manager = IndexJobManager(SimpleNamespace()) # type: ignore[arg-type] + stored = { + "available": True, + "kind": "index", + "status": "complete", + "started_at": "start", + "finished_at": "finish", + "summary": {"failed": 3}, + "items": [ + {"status": "failed", "title": "A"}, + {"status": "indexed", "title": "B"}, + {"status": "failed", "title": "C"}, + {"status": "failed", "title": "D"}, + ], + } + + with patch("scene_search.index_jobs.load_index_report", return_value=stored): + page = manager.report(category="failed", offset=1, limit=1) + + self.assertEqual(page["total"], 3) + self.assertEqual(page["offset"], 1) + self.assertEqual(page["limit"], 1) + self.assertEqual([item["title"] for item in page["items"]], ["C"]) + + +if __name__ == "__main__": + unittest.main()