Merge branch 'feature/admin-accuracy-reports'

# Conflicts:
#	app/scheduler/tasks.py
This commit is contained in:
dasemu committed 2026-03-17 17:56:53 +01:00
commit 39e08fbe6f
2 files changed
+314

No files matched your search

+56
View File
@@ -231,6 +231,9 @@ def run_extended_task() -> None:
# Send daily prediction notification to subscribers
_send_daily_notifications(store)
# Send accuracy report to admins
_send_admin_accuracy_report(store)
# ---------------------------------------------------------------------------
# Training task (Sunday 21h)
@@ -363,3 +366,56 @@ def _scrape_daily_actuals(store: SQLiteStore, scraper: StationScraper) -> None:
if records:
store.save_historical_actual(records)
def _send_admin_accuracy_report(store: SQLiteStore) -> None:
"""Send daily accuracy report to admin Telegram users."""
admin_ids_str = os.environ.get("ADMIN_TELEGRAM_IDS", "")
if not admin_ids_str.strip():
logger.info("No ADMIN_TELEGRAM_IDS configured, skipping accuracy report.")
return
token = os.environ.get("TELEGRAM_BOT_TOKEN")
if not token:
logger.warning("No TELEGRAM_BOT_TOKEN set, skipping admin report.")
return
admin_ids = [int(x.strip()) for x in admin_ids_str.split(",") if x.strip()]
if not admin_ids:
return
logger.info("Computing accuracy report for %d admin(s).", len(admin_ids))
try:
from predictor.accuracy import (
compute_accumulated_accuracy,
compute_daily_accuracy,
format_accuracy_report,
)
daily = compute_daily_accuracy(date.today(), store=store)
accumulated = compute_accumulated_accuracy(30, store=store)
message = format_accuracy_report(daily, accumulated)
except Exception as exc:
logger.error("Failed to compute accuracy report: %s", exc)
return
import asyncio
from telegram import Bot
from telegram.constants import ParseMode
async def _send() -> None:
bot = Bot(token=token)
for admin_id in admin_ids:
try:
await bot.send_message(
chat_id=admin_id, text=message, parse_mode=ParseMode.HTML
)
logger.info("Sent accuracy report to admin %d.", admin_id)
except Exception as exc:
logger.error(
"Failed to send accuracy report to admin %d: %s", admin_id, exc
)
asyncio.run(_send())