fix UI 500 + slow backup with millions of processed_mails
- fix: Starlette 1.0 changed TemplateResponse signature — request is now the first positional argument, not nested inside the context dict. All HTML routes returned 500 (unhashable dict in jinja2 template cache) after the image rebuild picked up the new starlette version. - fix: processed_mails (1.7M rows in production: 1 account × 12 rules × 141k inbox mails) made backup export hit 558 MB / 90s. Moved to opt-in checkbox in the UI alongside the logs option, default off. - yield_per for streaming the processed_mails query when included. Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
This commit is contained in:
@@ -20,7 +20,11 @@ logger = logging.getLogger(__name__)
|
||||
BACKUP_VERSION = 2
|
||||
|
||||
|
||||
def export_backup(db: Session | None = None, include_logs: bool = False) -> str:
|
||||
def export_backup(
|
||||
db: Session | None = None,
|
||||
include_logs: bool = False,
|
||||
include_processed: bool = False,
|
||||
) -> str:
|
||||
close_db = False
|
||||
if db is None:
|
||||
db = SessionLocal()
|
||||
@@ -32,6 +36,7 @@ def export_backup(db: Session | None = None, include_logs: bool = False) -> str:
|
||||
"version": BACKUP_VERSION,
|
||||
"exported_at": datetime.utcnow().isoformat(),
|
||||
"includes_logs": include_logs,
|
||||
"includes_processed": include_processed,
|
||||
"accounts": [],
|
||||
}
|
||||
|
||||
@@ -78,20 +83,22 @@ def export_backup(db: Session | None = None, include_logs: bool = False) -> str:
|
||||
"processed_mails": [],
|
||||
}
|
||||
|
||||
# Verarbeitete Mails pro Regel
|
||||
processed = (
|
||||
db.query(ProcessedMail)
|
||||
.filter(ProcessedMail.rule_id == rule.id)
|
||||
.all()
|
||||
)
|
||||
for pm in processed:
|
||||
rule_data["processed_mails"].append({
|
||||
"folder": pm.folder,
|
||||
"mail_uid": pm.mail_uid,
|
||||
"mail_subject": pm.mail_subject,
|
||||
"mail_from": pm.mail_from,
|
||||
"processed_at": pm.processed_at.isoformat() if pm.processed_at else None,
|
||||
})
|
||||
# Verarbeitete Mails pro Regel (nur wenn explizit gewünscht — kann sehr
|
||||
# groß werden: typischerweise pro Regel × pro Mail im Quell-Ordner ein Eintrag)
|
||||
if include_processed:
|
||||
processed = (
|
||||
db.query(ProcessedMail)
|
||||
.filter(ProcessedMail.rule_id == rule.id)
|
||||
.yield_per(2000)
|
||||
)
|
||||
for pm in processed:
|
||||
rule_data["processed_mails"].append({
|
||||
"folder": pm.folder,
|
||||
"mail_uid": pm.mail_uid,
|
||||
"mail_subject": pm.mail_subject,
|
||||
"mail_from": pm.mail_from,
|
||||
"processed_at": pm.processed_at.isoformat() if pm.processed_at else None,
|
||||
})
|
||||
|
||||
account_data["filter_rules"].append(rule_data)
|
||||
|
||||
|
||||
Reference in New Issue
Block a user