Fixed startup email

This commit is contained in:
Jonas Linter
2025-10-16 10:53:03 +02:00
parent 90e253b950
commit 6ad4df6990
4 changed files with 336 additions and 31 deletions

View File

@@ -299,10 +299,11 @@ 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
_LOGGER.info("Sending test daily report on startup")
# 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:
stats = await stats_collector.collect_stats()
# Use lookback_hours=24 to get stats from last 24 hours
stats = await stats_collector.collect_stats(lookback_hours=24)
success = await email_service.send_daily_report(
recipients=report_scheduler.recipients,
stats=stats,

View File

@@ -492,16 +492,27 @@ class ReservationStatsCollector:
len(self._hotel_map),
)
async def collect_stats(self) -> dict[str, Any]:
async def collect_stats(self, lookback_hours: int | None = None) -> dict[str, Any]:
"""Collect reservation statistics for the reporting period.
Args:
lookback_hours: Optional override to look back N hours from now.
If None, uses time since last report.
Returns:
Dictionary with statistics including reservations per hotel
"""
now = datetime.now()
period_start = self._last_report_time
period_end = now
if lookback_hours is not None:
# Override mode: look back N hours from now
period_start = now - timedelta(hours=lookback_hours)
period_end = now
else:
# Normal mode: since last report
period_start = self._last_report_time
period_end = now
_LOGGER.info(
"Collecting reservation stats from %s to %s",
@@ -538,8 +549,9 @@ class ReservationStatsCollector:
# Sort by reservation count descending
hotels_stats.sort(key=lambda x: x["reservations"], reverse=True)
# Update last report time
self._last_report_time = now
# Update last report time only in normal mode (not lookback mode)
if lookback_hours is None:
self._last_report_time = now
stats = {
"reporting_period": {