From 35c67f4bd5b2902f46ae6217bd13fd20e3cd76cb Mon Sep 17 00:00:00 2001 From: Ethanfel Date: Sun, 7 Jun 2026 19:52:13 +0200 Subject: [PATCH] =?UTF-8?q?perf:=20single-pass=20get=5Ftraining=5Fstats=20?= =?UTF-8?q?(was=20O(folders=20=C3=97=20rows))?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Group clips by export folder in one scan instead of re-scanning every row for each folder; also drops the extra get_export_folders() query. Speeds up the train-dialog stats with many subcategories. Co-Authored-By: Claude Opus 4.6 --- core/db.py | 24 ++++++++++++------------ 1 file changed, 12 insertions(+), 12 deletions(-) diff --git a/core/db.py b/core/db.py index 3524018..9ad22c7 100644 --- a/core/db.py +++ b/core/db.py @@ -1087,18 +1087,18 @@ class ProcessedDB: " WHERE profile = ? AND scan_export = 0", (profile,), ).fetchall() - folders = self.get_export_folders(profile, include_scan_exports=include_scan_exports) - stats: dict[str, dict] = {} - for folder_name in folders: - videos: set[str] = set() - clips = 0 - for fn, op in rows: - grandparent = os.path.basename(os.path.dirname(os.path.dirname(op))) - if grandparent == folder_name: - videos.add(fn) - clips += 1 - stats[folder_name] = {"videos": len(videos), "clips": clips} - return {k: v for k, v in stats.items() if v["clips"] > 0} + # Single pass: group by export folder (grandparent dir), counting + # clips and distinct source videos. (Was O(folders × rows).) + videos: dict[str, set[str]] = {} + clips: dict[str, int] = {} + for fn, op in rows: + folder_name = os.path.basename(os.path.dirname(os.path.dirname(op))) + if not folder_name or folder_name.endswith("_disabled"): + continue + videos.setdefault(folder_name, set()).add(fn) + clips[folder_name] = clips.get(folder_name, 0) + 1 + return {f: {"videos": len(videos[f]), "clips": n} + for f, n in clips.items() if n > 0} # ── Scan results ─────────────────────────────────────────────