feat: Add alert processing functionality and UI feedback

This commit is contained in:
bolade
2025-08-12 10:41:47 +01:00
parent 1fd3a95093
commit 3ea27caca6
6 changed files with 54 additions and 12 deletions
+2
View File
@@ -142,6 +142,8 @@ def process_alerts(db: Session, cfg: dict) -> List[int]:
}
for m in get_thread_messages(db, t.id)[-4:]
]
print(f"These are the messages: {msgs}")
ai = analyze_thread(t.subject or "", msgs)
# Persist AI decision on thread
+17 -8
View File
@@ -49,7 +49,12 @@ app.mount("/static", StaticFiles(directory="static"), name="static")
@app.get("/", response_class=HTMLResponse)
def home(request: Request, db: Session = Depends(get_db), account: str | None = None):
def home(
request: Request,
db: Session = Depends(get_db),
account: str | None = None,
alerts_processed: int | None = None,
):
from datetime import datetime
from sqlalchemy import func
@@ -109,6 +114,7 @@ def home(request: Request, db: Session = Depends(get_db), account: str | None =
"threads": threads_with_info,
"account": account or "",
"status": _status_for_templates(),
"alerts_processed": alerts_processed,
},
)
@@ -335,7 +341,8 @@ async def config_save(request: Request):
def process(db: Session = Depends(get_db)):
cfg = load_config()
alerted = process_alerts(db, cfg)
return {"alerted_threads": alerted}
# Redirect back to main page with success indicator
return RedirectResponse(url=f"/?alerts_processed={len(alerted)}", status_code=303)
def _sync_emails_once(cfg: dict) -> int:
@@ -357,7 +364,7 @@ def _sync_emails_once(cfg: dict) -> int:
days_back = max(1, delta_days)
except Exception:
pass
max_results = 100
max_results = 5
client = ZohoClient(
email=cfg.get("zoho_email") or account_email,
app_password=cfg.get("zoho_app_password"),
@@ -411,11 +418,13 @@ def _sync_emails_background_task():
cfg = load_config() # Reload in case it was modified
cfg.update(
{
"sync_in_progress": False,
"last_sync_status": "success",
"last_sync_at": datetime.now(timezone.utc).strftime("%d/%m/%y %I:%M %p"),
"last_sync_count": count,
"last_sync_error": None,
"sync_in_progress": False,
"last_sync_status": "success",
"last_sync_at": datetime.now(timezone.utc).strftime(
"%d/%m/%y %I:%M %p"
),
"last_sync_count": count,
"last_sync_error": None,
}
)
save_config(cfg)
+4 -4
View File
@@ -156,7 +156,7 @@ class ZohoClient:
# Get email body snippet
body = self._get_email_body(email_message)
snippet = body[:200] + "..." if len(body) > 200 else body
email_data = {
"id": num.decode(),
@@ -168,11 +168,11 @@ class ZohoClient:
"messageId": message_id,
"inReplyTo": in_reply_to,
"folder": folder,
"snippet": snippet,
"snippet": body,
}
emails.append(email_data)
logging.info(f"Long body: {body}")
except Exception as e:
print(f"❌ Error processing email {num}: {e}")
continue