From 3ea27caca696d7c9cd2bef4233acac74563206de Mon Sep 17 00:00:00 2001 From: bolade Date: Tue, 12 Aug 2025 10:41:47 +0100 Subject: [PATCH] feat: Add alert processing functionality and UI feedback --- src/alerts.py | 2 ++ src/app.py | 25 +++++++++++++++++-------- src/zoho_client.py | 8 ++++---- static/styles.css | 23 +++++++++++++++++++++++ templates/base.html | 3 +++ templates/threads.html | 5 +++++ 6 files changed, 54 insertions(+), 12 deletions(-) diff --git a/src/alerts.py b/src/alerts.py index c0bcbd9..444c414 100644 --- a/src/alerts.py +++ b/src/alerts.py @@ -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 diff --git a/src/app.py b/src/app.py index 29fe57a..3cb8cfe 100644 --- a/src/app.py +++ b/src/app.py @@ -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) diff --git a/src/zoho_client.py b/src/zoho_client.py index fa48fa5..e5f9bd5 100644 --- a/src/zoho_client.py +++ b/src/zoho_client.py @@ -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 diff --git a/static/styles.css b/static/styles.css index d0c9da2..d8e49da 100644 --- a/static/styles.css +++ b/static/styles.css @@ -112,3 +112,26 @@ button { box-shadow: 0 6px 18px rgba(63, 99, 183, 0.35); } button:hover { filter: brightness(1.05); } + +/* Alert styles */ +.alert { + padding: 0.75rem 1rem; + border-radius: 8px; + border: 1px solid; + font-size: 0.9rem; +} +.alert.success { + background: rgba(34, 197, 94, 0.1); + border-color: var(--success); + color: #4ade80; +} +.alert.warn { + background: rgba(245, 158, 11, 0.1); + border-color: var(--warn); + color: #fbbf24; +} +.alert.danger { + background: rgba(239, 68, 68, 0.1); + border-color: var(--danger); + color: #f87171; +} diff --git a/templates/base.html b/templates/base.html index 9b7742a..32153eb 100644 --- a/templates/base.html +++ b/templates/base.html @@ -16,6 +16,9 @@
+
+ +
diff --git a/templates/threads.html b/templates/threads.html index 0c19a6b..0be9078 100644 --- a/templates/threads.html +++ b/templates/threads.html @@ -4,6 +4,11 @@

Threads{% if account %} for {{ account }}{% endif %}

Latest updated threads. Click an ID to view details and AI analysis.

+ {% if alerts_processed is not none %} +
+ ✓ Alerts processed! {{ alerts_processed }} thread(s) were checked for alerts. +
+ {% endif %} {% if status %}
{% if status.sync_in_progress %}