75a0a3fde7
- Added `get_latest_email_date()` function in `database.py` to retrieve the most recent email date for a given account and folder. - Enhanced `fetch_folder_emails()` in `zoho_client.py` to intelligently determine the start date for fetching emails based on the latest email date in the database. - Introduced `analyze_and_update_threads_async()` for asynchronous analysis of email threads, allowing concurrent processing. - Created a synchronous wrapper `analyze_and_update_threads()` for easier integration. - Updated `fetch_emails()` to support database session and account email parameters. - Added comprehensive documentation in `AI_ANALYSIS_GUIDE.md` detailing the new AI analysis functionality. - Implemented tests for the new features, including `test_fetch_with_db.py`, `test_ai_analysis.py`, and `test_single_analysis.py`. - Added error handling and logging improvements throughout the codebase.
2.6 KiB
2.6 KiB
Fetch Folder Emails Modification Summary
Changes Made
1. Database Module (database.py)
- Added
get_latest_email_date()function to retrieve the most recent email date for a given account and folder - Added import for
datetimeto support the new function
2. ZohoClient Module (zoho_client.py)
- Modified
fetch_folder_emails()to accept two new optional parameters:db_session: Database session for querying latest email datesaccount_email: Account email to identify which emails to check for latest date
- Updated logic to:
- Check database for latest email date if db_session and account_email are provided
- Use latest date (minus 1 minute buffer) as start date for IMAP search
- Fall back to
days_backparameter if no database date is available
- Updated
fetch_emails()wrapper to support the new parameters - Enhanced documentation with detailed parameter descriptions
3. App Module (app.py)
- Modified email fetching calls to pass database session and account email
- Reorganized code to create database session before fetching emails
How It Works
Before
- Always fetched emails starting from X days back
- No awareness of what emails were already in the database
- Could result in fetching duplicate emails or missing recent ones
After
- Intelligently determines start date based on database contents
- If emails exist in database: starts from the latest email date
- If no emails in database: falls back to
days_backparameter - Adds 1-minute buffer to avoid missing emails with same timestamp
- Reduces unnecessary email fetching and improves efficiency
Usage Examples
Basic Usage (Backwards Compatible)
client = ZohoClient()
emails = client.fetch_folder_emails(folder="INBOX", days_back=7)
With Database Integration (Recommended)
from database import SessionLocal
db = SessionLocal()
client = ZohoClient()
emails = client.fetch_folder_emails(
folder="INBOX",
days_back=30, # Fallback only
db_session=db,
account_email="user@example.com"
)
db.close()
Benefits
- Efficiency: Only fetches new emails since last sync
- Reliability: Ensures no emails are missed between syncs
- Backwards Compatibility: Still works without database parameters
- Flexibility: Falls back gracefully when database is empty
- Performance: Reduces IMAP server load and processing time
Testing
Use test_fetch_with_db.py to test the new functionality:
python test_fetch_with_db.py
The test script demonstrates:
- Checking latest email date in database
- Fetching emails with database integration
- Displaying results