from collections.abc import Callable from typing import Any from app.core.background.task_queue.constants import ( TASK_PUSH_ATTENDANCE_SUMMARY, TASK_PUSH_DAILY_BRIEF, TASK_PUSH_PROJECT_WEEKLY, TASK_PUSH_RISK_PROGRESS, TASK_PUSH_WORK_DAILY, TASK_PUSH_WORK_WEEKLY, ) from app.core.constants import ActorValue from app.core.database import SessionLocal from app.modules.feishu.constants import FeishuReceiveIdType from app.tasks.app import celery_app ReportBuilder = Callable[[Any, str], dict[str, Any]] @celery_app.task(name=TASK_PUSH_DAILY_BRIEF) def push_daily_brief( receive_id: str | None = None, receive_id_type: str = FeishuReceiveIdType.CHAT_ID, actor: str = ActorValue.SCHEDULER, push_run_code: str | None = None, ) -> dict[str, Any]: return _push_report(_build_daily_brief, receive_id, receive_id_type, actor, push_run_code) @celery_app.task(name=TASK_PUSH_PROJECT_WEEKLY) def push_project_weekly( receive_id: str | None = None, receive_id_type: str = FeishuReceiveIdType.CHAT_ID, actor: str = ActorValue.SCHEDULER, push_run_code: str | None = None, ) -> dict[str, Any]: return _push_report(_build_project_weekly, receive_id, receive_id_type, actor, push_run_code) @celery_app.task(name=TASK_PUSH_ATTENDANCE_SUMMARY) def push_attendance_summary( receive_id: str | None = None, receive_id_type: str = FeishuReceiveIdType.CHAT_ID, actor: str = ActorValue.SCHEDULER, push_run_code: str | None = None, ) -> dict[str, Any]: return _push_report( _build_attendance_summary, receive_id, receive_id_type, actor, push_run_code, ) @celery_app.task(name=TASK_PUSH_RISK_PROGRESS) def push_risk_progress( receive_id: str | None = None, receive_id_type: str = FeishuReceiveIdType.CHAT_ID, actor: str = ActorValue.SCHEDULER, push_run_code: str | None = None, ) -> dict[str, Any]: return _push_report(_build_risk_progress, receive_id, receive_id_type, actor, push_run_code) @celery_app.task(name=TASK_PUSH_WORK_DAILY) def push_work_daily( receive_id: str | None = None, receive_id_type: str = FeishuReceiveIdType.CHAT_ID, actor: str = ActorValue.SCHEDULER, push_run_code: str | None = None, ) -> dict[str, Any]: return _push_report(_build_work_daily, receive_id, receive_id_type, actor, push_run_code) @celery_app.task(name=TASK_PUSH_WORK_WEEKLY) def push_work_weekly( receive_id: str | None = None, receive_id_type: str = FeishuReceiveIdType.CHAT_ID, actor: str = ActorValue.SCHEDULER, push_run_code: str | None = None, ) -> dict[str, Any]: return _push_report(_build_work_weekly, receive_id, receive_id_type, actor, push_run_code) def _push_report( build_report: ReportBuilder, receive_id: str | None, receive_id_type: str, actor: str, push_run_code: str | None, ) -> dict[str, Any]: from app.modules.reports.services import ReportService db = SessionLocal() try: service = ReportService(db) report = build_report(service, actor) return service.push_report( report, receive_id, receive_id_type, actor, push_run_code=push_run_code, ) finally: db.close() def _build_daily_brief(service: Any, actor: str) -> dict[str, Any]: _ = actor return service.daily_brief() def _build_project_weekly(service: Any, actor: str) -> dict[str, Any]: _ = actor return service.project_weekly() def _build_attendance_summary(service: Any, actor: str) -> dict[str, Any]: _ = actor return service.attendance_summary() def _build_risk_progress(service: Any, actor: str) -> dict[str, Any]: _ = actor return service.risk_progress() def _build_work_daily(service: Any, actor: str) -> dict[str, Any]: return service.work_daily_report(reporter=actor, actor=actor) def _build_work_weekly(service: Any, actor: str) -> dict[str, Any]: return service.work_weekly_report(reporter=actor, actor=actor)