feat: Implement async AI analysis for email threads
- 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.
This commit is contained in:
@@ -0,0 +1,79 @@
|
||||
# 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 `datetime` to 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 dates
|
||||
- `account_email`: Account email to identify which emails to check for latest date
|
||||
- Updated logic to:
|
||||
1. Check database for latest email date if db_session and account_email are provided
|
||||
2. Use latest date (minus 1 minute buffer) as start date for IMAP search
|
||||
3. Fall back to `days_back` parameter 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_back` parameter
|
||||
- Adds 1-minute buffer to avoid missing emails with same timestamp
|
||||
- Reduces unnecessary email fetching and improves efficiency
|
||||
|
||||
## Usage Examples
|
||||
|
||||
### Basic Usage (Backwards Compatible)
|
||||
```python
|
||||
client = ZohoClient()
|
||||
emails = client.fetch_folder_emails(folder="INBOX", days_back=7)
|
||||
```
|
||||
|
||||
### With Database Integration (Recommended)
|
||||
```python
|
||||
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
|
||||
|
||||
1. **Efficiency**: Only fetches new emails since last sync
|
||||
2. **Reliability**: Ensures no emails are missed between syncs
|
||||
3. **Backwards Compatibility**: Still works without database parameters
|
||||
4. **Flexibility**: Falls back gracefully when database is empty
|
||||
5. **Performance**: Reduces IMAP server load and processing time
|
||||
|
||||
## Testing
|
||||
|
||||
Use `test_fetch_with_db.py` to test the new functionality:
|
||||
```bash
|
||||
python test_fetch_with_db.py
|
||||
```
|
||||
|
||||
The test script demonstrates:
|
||||
- Checking latest email date in database
|
||||
- Fetching emails with database integration
|
||||
- Displaying results
|
||||
Reference in New Issue
Block a user