POTE/scripts/send_daily_report.py
ilia 0d8d85adc1 Add complete automation, reporting, and CI/CD system
Features Added:
==============

📧 EMAIL REPORTING SYSTEM:
- EmailReporter: Send reports via SMTP (Gmail, SendGrid, custom)
- ReportGenerator: Generate daily/weekly summaries with HTML/text formatting
- Configurable via .env (SMTP_HOST, SMTP_PORT, etc.)
- Scripts: send_daily_report.py, send_weekly_report.py

🤖 AUTOMATED RUNS:
- automated_daily_run.sh: Full daily ETL pipeline + reporting
- automated_weekly_run.sh: Weekly pattern analysis + reports
- setup_cron.sh: Interactive cron job setup (5-minute setup)
- Logs saved to ~/logs/ with automatic cleanup

🔍 HEALTH CHECKS:
- health_check.py: System health monitoring
- Checks: DB connection, data freshness, counts, recent alerts
- JSON output for programmatic use
- Exit codes for monitoring integration

🚀 CI/CD PIPELINE:
- .github/workflows/ci.yml: Full CI/CD pipeline
- GitHub Actions / Gitea Actions compatible
- Jobs: lint & test, security scan, dependency scan, Docker build
- PostgreSQL service for integration tests
- 93 tests passing in CI

📚 COMPREHENSIVE DOCUMENTATION:
- AUTOMATION_QUICKSTART.md: 5-minute email setup guide
- docs/12_automation_and_reporting.md: Full automation guide
- Updated README.md with automation links
- Deployment → Production workflow guide

🛠️ IMPROVEMENTS:
- All shell scripts made executable
- Environment variable examples in .env.example
- Report logs saved with timestamps
- 30-day log retention with auto-cleanup
- Health checks can be scheduled via cron

WHAT THIS ENABLES:
==================
After deployment, users can:
1. Set up automated daily/weekly email reports (5 min)
2. Receive HTML+text emails with:
   - New trades, market alerts, suspicious timing
   - Weekly patterns, rankings, repeat offenders
3. Monitor system health automatically
4. Run full CI/CD pipeline on every commit
5. Deploy with confidence (tests + security scans)

USAGE:
======
# One-time setup (on deployed server)
./scripts/setup_cron.sh

# Or manually send reports
python scripts/send_daily_report.py --to user@example.com
python scripts/send_weekly_report.py --to user@example.com

# Check system health
python scripts/health_check.py

See AUTOMATION_QUICKSTART.md for full instructions.

93 tests passing | Full CI/CD | Email reports ready
2025-12-15 15:34:31 -05:00

120 lines
3.4 KiB
Python

#!/usr/bin/env python3
"""
Send Daily Report via Email
Generates and emails the daily POTE summary report.
Usage:
python scripts/send_daily_report.py --to user@example.com
python scripts/send_daily_report.py --to user1@example.com,user2@example.com --test-smtp
"""
import argparse
import logging
import sys
from datetime import date
from pathlib import Path
# Add project root to path
sys.path.insert(0, str(Path(__file__).parent.parent))
from pote.db import get_session
from pote.reporting.email_reporter import EmailReporter
from pote.reporting.report_generator import ReportGenerator
logging.basicConfig(
level=logging.INFO, format="%(asctime)s - %(name)s - %(levelname)s - %(message)s"
)
logger = logging.getLogger(__name__)
def main():
parser = argparse.ArgumentParser(description="Send daily POTE report via email")
parser.add_argument(
"--to", required=True, help="Recipient email addresses (comma-separated)"
)
parser.add_argument(
"--date",
help="Report date (YYYY-MM-DD), defaults to today",
default=None,
)
parser.add_argument(
"--test-smtp",
action="store_true",
help="Test SMTP connection before sending",
)
parser.add_argument(
"--save-to-file",
help="Also save report to this file path",
default=None,
)
args = parser.parse_args()
# Parse recipients
to_emails = [email.strip() for email in args.to.split(",")]
# Parse date if provided
report_date = None
if args.date:
try:
report_date = date.fromisoformat(args.date)
except ValueError:
logger.error(f"Invalid date format: {args.date}. Use YYYY-MM-DD")
sys.exit(1)
# Initialize email reporter
email_reporter = EmailReporter()
# Test SMTP connection if requested
if args.test_smtp:
logger.info("Testing SMTP connection...")
if not email_reporter.test_connection():
logger.error("SMTP connection test failed. Check your SMTP settings in .env")
logger.info(
"Required settings: SMTP_HOST, SMTP_PORT, SMTP_USER, SMTP_PASSWORD, FROM_EMAIL"
)
sys.exit(1)
logger.info("SMTP connection test successful!")
# Generate report
logger.info(f"Generating daily report for {report_date or date.today()}...")
with get_session() as session:
generator = ReportGenerator(session)
report_data = generator.generate_daily_summary(report_date)
# Format as text and HTML
text_body = generator.format_as_text(report_data, "daily")
html_body = generator.format_as_html(report_data, "daily")
# Save to file if requested
if args.save_to_file:
with open(args.save_to_file, "w") as f:
f.write(text_body)
logger.info(f"Report saved to {args.save_to_file}")
# Send email
subject = f"POTE Daily Report - {report_data['date']}"
logger.info(f"Sending report to {', '.join(to_emails)}...")
success = email_reporter.send_report(
to_emails=to_emails,
subject=subject,
body_text=text_body,
body_html=html_body,
)
if success:
logger.info("Report sent successfully!")
# Print summary to stdout
print("\n" + text_body + "\n")
sys.exit(0)
else:
logger.error("Failed to send report. Check logs for details.")
sys.exit(1)
if __name__ == "__main__":
main()