From 58cc60ba1947032fe8a2f90553ab2c4473ce763c Mon Sep 17 00:00:00 2001 From: grabowski Date: Fri, 3 Oct 2025 16:44:57 +0700 Subject: [PATCH] Integrate automatic alerting into continuous monitoring - Alerts now run automatically after every successful new data fetch - Works for both hourly fetches and retry mode exits - Alert check runs when fresh data is saved to database - Logs alert results (total generated and sent count) Co-Authored-By: Claude --- src/main.py | 17 ++++++++++++++++- 1 file changed, 16 insertions(+), 1 deletion(-) diff --git a/src/main.py b/src/main.py index 9473d25..78c8379 100644 --- a/src/main.py +++ b/src/main.py @@ -64,7 +64,7 @@ def run_test_cycle(): return False def run_continuous_monitoring(): - """Run continuous monitoring with adaptive scheduling""" + """Run continuous monitoring with adaptive scheduling and alerting""" logger.info("Starting continuous monitoring...") try: @@ -75,11 +75,16 @@ def run_continuous_monitoring(): db_config = Config.get_database_config() scraper = EnhancedWaterMonitorScraper(db_config) + # Initialize alerting system + from .alerting import WaterLevelAlertSystem + alerting = WaterLevelAlertSystem() + # Setup signal handlers setup_signal_handlers(scraper) logger.info(f"Monitoring started with {Config.SCRAPING_INTERVAL_HOURS}h interval") logger.info("Adaptive retry: switches to 1-minute intervals when no data available") + logger.info("Alerts: automatic check after each successful data fetch") logger.info("Press Ctrl+C to stop") # Run initial cycle @@ -109,6 +114,16 @@ def run_continuous_monitoring(): if success: last_successful_fetch = current_time + + # Run alert check after every successful new data fetch + logger.info("Running alert check...") + try: + alert_results = alerting.run_alert_check() + if alert_results.get('total_alerts', 0) > 0: + logger.info(f"Alerts: {alert_results['total_alerts']} generated, {alert_results['sent']} sent") + except Exception as e: + logger.error(f"Alert check failed: {e}") + if retry_mode: logger.info("✅ Data fetch successful - switching back to hourly schedule") retry_mode = False