domovoy_bot/services/email_auditor/reindex_history.py

70 lines
2.4 KiB
Python
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

#!/usr/bin/env python3
import sqlite3
import os
import re
from pathlib import Path
from datetime import datetime
# Пути
BASE_DIR = Path(__file__).resolve().parent
DB_PATH = Path("/home/matrixhasyou/domovoy_bot/database/domovoy.db")
ALL_FOLDER = BASE_DIR / "exported_emails" / "all"
def reindex():
if not DB_PATH.exists():
print(f"❌ БД не найдена: {DB_PATH}")
return
conn = sqlite3.connect(DB_PATH)
cur = conn.cursor()
print("🧹 Очистка старой статистики перед переиндексацией...")
cur.execute("DELETE FROM email_audit")
files = list(ALL_FOLDER.glob("*.pdf"))
print(f"📂 Найдено {len(files)} файлов для индексации...")
stats = {} # domain -> {in: X, out: Y, last: date}
# Регулярка для парсинга: YYYY-MM-DD_domain_subject.pdf или domain_date_sender_subject
for f in files:
name = f.stem
# Вариант 1: YYYY-MM-DD_domain_subject
match = re.match(r'(\d{4}-\d{2}-\d{2})_([^_]+)_(.*)', name)
if match:
date_str, domain, subject = match.groups()
else:
# Вариант 2: domain_date_sender_subject
match = re.match(r'([^_]+)_(\d{4}-\d{2}-\d{2})_(.*)', name)
if match:
domain, date_str, rest = match.groups()
else:
continue
is_inbound = True # Упрощаем для индексации
if domain not in stats:
stats[domain] = {'in': 0, 'out': 0, 'last': date_str}
if is_inbound:
stats[domain]['in'] += 1
else:
stats[domain]['out'] += 1
if date_str > stats[domain]['last']:
stats[domain]['last'] = date_str
for domain, s in stats.items():
print(f" Добавляю {domain}: {s['in']} вх, {s['out']} исх")
cur.execute("""
INSERT INTO email_audit (domain, description, inbound_count, outbound_count, last_email_date, updated_at)
VALUES (?, ?, ?, ?, ?, CURRENT_TIMESTAMP)
""", (domain, f"Ведомство {domain}", s['in'], s['out'], s['last']))
conn.commit()
conn.close()
print("✅ Переиндексация завершена! Обнови страницу в панели.")
if __name__ == "__main__":
reindex()