Updated reporting scheme. Should work better now

This commit is contained in:
Jonas Linter
2025-10-17 19:47:15 +02:00
parent 75f32234e0
commit f30632df29
6 changed files with 700 additions and 181 deletions

View File

@@ -44,8 +44,6 @@ from .email_monitoring import ReservationStatsCollector
from .email_service import create_email_service
from .logging_config import get_logger, setup_logging
from .migrations import run_all_migrations
from .notification_adapters import EmailNotificationAdapter, PushoverNotificationAdapter
from .notification_service import NotificationService
from .pushover_service import create_pushover_service
from .rate_limit import (
BURST_RATE_LIMIT,
@@ -236,9 +234,9 @@ async def lifespan(app: FastAPI):
# Initialize pushover service
pushover_service = create_pushover_service(config)
# Setup logging from config with email and pushover monitoring
# Setup logging from config with unified notification monitoring
# Only primary worker should have the report scheduler running
email_handler, report_scheduler = setup_logging(
alert_handler, report_scheduler = setup_logging(
config, email_service, pushover_service, loop, enable_scheduler=is_primary
)
_LOGGER.info("Application startup initiated (primary_worker=%s)", is_primary)
@@ -254,7 +252,7 @@ async def lifespan(app: FastAPI):
app.state.event_dispatcher = event_dispatcher
app.state.email_service = email_service
app.state.pushover_service = pushover_service
app.state.email_handler = email_handler
app.state.alert_handler = alert_handler
app.state.report_scheduler = report_scheduler
# Register push listeners for hotels with push_endpoint
@@ -313,44 +311,6 @@ async def lifespan(app: FastAPI):
report_scheduler.set_stats_collector(stats_collector.collect_stats)
_LOGGER.info("Stats collector initialized and hooked up to report scheduler")
# Send a test daily report on startup for testing (with 24-hour lookback)
_LOGGER.info("Sending test daily report on startup (last 24 hours)")
try:
# Use lookback_hours=24 to get stats from last 24 hours
stats = await stats_collector.collect_stats(lookback_hours=24)
# Send via email (if configured)
if email_service:
success = await email_service.send_daily_report(
recipients=report_scheduler.recipients,
stats=stats,
errors=None,
)
if success:
_LOGGER.info("Test daily report sent via email successfully on startup")
else:
_LOGGER.error("Failed to send test daily report via email on startup")
# Send via Pushover (if configured)
if pushover_service:
pushover_config = config.get("pushover", {})
pushover_monitoring = pushover_config.get("monitoring", {})
pushover_daily_report = pushover_monitoring.get("daily_report", {})
priority = pushover_daily_report.get("priority", 0)
success = await pushover_service.send_daily_report(
stats=stats,
errors=None,
priority=priority,
)
if success:
_LOGGER.info("Test daily report sent via Pushover successfully on startup")
else:
_LOGGER.error("Failed to send test daily report via Pushover on startup")
except Exception:
_LOGGER.exception("Error sending test daily report on startup")
# Start daily report scheduler
report_scheduler.start()
_LOGGER.info("Daily report scheduler started")
@@ -367,10 +327,10 @@ async def lifespan(app: FastAPI):
report_scheduler.stop()
_LOGGER.info("Daily report scheduler stopped")
# Close email alert handler (flush any remaining errors)
if email_handler:
email_handler.close()
_LOGGER.info("Email alert handler closed")
# Close alert handler (flush any remaining errors)
if alert_handler:
alert_handler.close()
_LOGGER.info("Alert handler closed")
# Shutdown email service thread pool
if email_service: