""" check_msg_files.py Zkontroluje, zda má každý záznam v jnjemails SQLite odpovídající .msg soubor fyzicky uložený na \\\\tower\\JNJEMAILS\\. DB: \\\\tower\\JNJEMAILS\\db\\jnjemails_*.db (nejnovější) Soubory: \\\\tower\\JNJEMAILS\\*.msg Název souboru = entry_id[-20:] + ".msg" Záznamy bez entry_id mají fallback message_id "entryid:..." — ty se přeskočí zvlášť (server je nemohl uložit standardním názvem). """ import sqlite3 import sys from pathlib import Path sys.stdout.reconfigure(encoding="utf-8") DB_DIR = Path(r"\\tower\JNJEMAILS\db") MSGS_DIR = Path(r"\\tower\JNJEMAILS") def get_latest_db() -> Path: files = sorted(DB_DIR.glob("jnjemails_*.db"), key=lambda f: f.name) if not files: raise FileNotFoundError(f"Žádný jnjemails_*.db v {DB_DIR}") return files[-1] def main(): db_path = get_latest_db() print(f"DB: {db_path.name}") conn = sqlite3.connect(db_path) conn.row_factory = sqlite3.Row rows = conn.execute( "SELECT id, message_id, subject, sender, received_at, entry_id, source FROM messages" ).fetchall() conn.close() print(f"Celkem záznamů: {len(rows)}\n") missing = [] no_entry_id = [] for row in rows: entry_id = row["entry_id"] if not entry_id: no_entry_id.append(dict(row)) continue expected_file = MSGS_DIR / (entry_id[-20:] + ".msg") if not expected_file.exists(): missing.append({**dict(row), "expected_file": expected_file.name}) msg_files = sum(1 for _ in MSGS_DIR.glob("*.msg")) print(f"Záznamy bez entry_id (nelze zkontrolovat): {len(no_entry_id)}") print(f"Záznamy s entry_id: {len(rows) - len(no_entry_id)}") print(f"Chybějící .msg soubory: {len(missing)}") print(f"\n--- POROVNÁNÍ POČTŮ ---") print(f"Záznamy v DB celkem: {len(rows)}") print(f"Soubory .msg na serveru: {msg_files}") diff = msg_files - len(rows) if diff >= 0: print(f"Rozdíl: +{diff} souborů navíc (OK — všechny záznamy mají soubor)") else: print(f"Rozdíl: {diff} — CHYBÍ {abs(diff)} souborů!") if missing: print("\n--- CHYBĚJÍCÍ SOUBORY ---") for r in missing: print(f" id={r['id']} | {r['received_at']} | {r['subject'][:60]!r}") print(f" sender={r['sender']} | source={r['source']}") print(f" entry_id={r['entry_id']}") print(f" očekávaný soubor: {r['expected_file']}") if no_entry_id: print(f"\n--- ZÁZNAMY BEZ ENTRY_ID ({len(no_entry_id)}) ---") for r in no_entry_id[:20]: print(f" id={r['id']} | {r['received_at']} | {r['subject'][:60]!r} | source={r['source']}") if len(no_entry_id) > 20: print(f" ... a dalších {len(no_entry_id) - 20}") print("\nHotovo.") if __name__ == "__main__": main()