feat(scheduler): 新增多种报表推送功能并重构调度器代码

- 新增考勤汇总、风险进度、工作日报、工作周报等报表推送功能
- 重构调度器中的报表推送逻辑,提取通用的 deliver_report 函数
- 添加新的定时任务配置项用于各种报表推送
- 实现飞书推送配置检查函数 _feishu_delivery_configured
- 将重复的报表推送代码抽取为可复用的函数模式
```
This commit is contained in:
2026-07-13 09:47:29 +08:00
parent f8020cab56
commit 267b01b9f4
12 changed files with 722 additions and 191 deletions

View File

@@ -1,3 +1,4 @@
from collections.abc import Callable
from datetime import date
from socket import gethostname
from typing import Any
@@ -35,6 +36,7 @@ def create_scheduler(app: FastAPI | None = None) -> Any:
from app.core.database import SessionLocal
from app.core.background.task_queue import (
enqueue_attendance_summary_push,
enqueue_daily_brief_push,
enqueue_event_dispatch,
enqueue_legacy_project_sync,
@@ -42,6 +44,9 @@ def create_scheduler(app: FastAPI | None = None) -> Any:
enqueue_lifecycle_report,
enqueue_market_report,
enqueue_project_weekly_push,
enqueue_risk_progress_push,
enqueue_work_daily_push,
enqueue_work_weekly_push,
)
from app.modules.observability.service import ObservabilityService
from app.modules.reports.services import ReportService
@@ -49,59 +54,82 @@ def create_scheduler(app: FastAPI | None = None) -> Any:
settings = get_settings()
scheduler = BackgroundScheduler(timezone="Asia/Shanghai")
def run_daily_brief() -> None:
def deliver_report(
state_key: str,
build_report: Callable[[Any], dict[str, Any]],
enqueue_push: Callable[..., dict[str, Any]],
) -> None:
db = SessionLocal()
try:
report = ReportService(db).daily_brief()
_set_state(app, "last_daily_brief", report)
if (
settings.feishu_app_id
and settings.feishu_app_secret
and settings.feishu_default_chat_id
):
if settings.task_queue_enabled:
dispatch = enqueue_daily_brief_push(
receive_id=settings.feishu_default_chat_id,
receive_id_type=FeishuReceiveIdType.CHAT_ID,
actor=ActorValue.SCHEDULER,
)
_set_state(app, "last_daily_brief_dispatch", dispatch)
return
ReportService(db).push_report(
report,
service = ReportService(db)
report = build_report(service)
_set_state(app, state_key, report)
if not _feishu_delivery_configured(settings):
return
if settings.task_queue_enabled:
dispatch = enqueue_push(
receive_id=settings.feishu_default_chat_id,
receive_id_type=FeishuReceiveIdType.CHAT_ID,
actor=ActorValue.SCHEDULER,
)
_set_state(app, f"{state_key}_dispatch", dispatch)
return
service.push_report(
report,
receive_id=settings.feishu_default_chat_id,
receive_id_type=FeishuReceiveIdType.CHAT_ID,
actor=ActorValue.SCHEDULER,
)
finally:
db.close()
def run_daily_brief() -> None:
deliver_report(
"last_daily_brief",
lambda service: service.daily_brief(),
enqueue_daily_brief_push,
)
def run_project_weekly() -> None:
db = SessionLocal()
try:
report = ReportService(db).project_weekly()
_set_state(app, "last_project_weekly", report)
if (
settings.feishu_app_id
and settings.feishu_app_secret
and settings.feishu_default_chat_id
):
if settings.task_queue_enabled:
dispatch = enqueue_project_weekly_push(
receive_id=settings.feishu_default_chat_id,
receive_id_type=FeishuReceiveIdType.CHAT_ID,
actor=ActorValue.SCHEDULER,
)
_set_state(app, "last_project_weekly_dispatch", dispatch)
return
ReportService(db).push_report(
report,
receive_id=settings.feishu_default_chat_id,
receive_id_type=FeishuReceiveIdType.CHAT_ID,
actor=ActorValue.SCHEDULER,
)
finally:
db.close()
deliver_report(
"last_project_weekly",
lambda service: service.project_weekly(),
enqueue_project_weekly_push,
)
def run_attendance_summary() -> None:
deliver_report(
"last_attendance_summary",
lambda service: service.attendance_summary(),
enqueue_attendance_summary_push,
)
def run_risk_progress() -> None:
deliver_report(
"last_risk_progress",
lambda service: service.risk_progress(),
enqueue_risk_progress_push,
)
def run_work_daily() -> None:
deliver_report(
"last_work_daily",
lambda service: service.work_daily_report(
reporter=ActorValue.SCHEDULER,
actor=ActorValue.SCHEDULER,
),
enqueue_work_daily_push,
)
def run_work_weekly() -> None:
deliver_report(
"last_work_weekly",
lambda service: service.work_weekly_report(
reporter=ActorValue.SCHEDULER,
actor=ActorValue.SCHEDULER,
),
enqueue_work_weekly_push,
)
def run_legacy_project_sync() -> None:
dispatch = enqueue_legacy_project_sync(actor=ActorValue.SCHEDULER)
@@ -196,6 +224,39 @@ def create_scheduler(app: FastAPI | None = None) -> Any:
id="project_weekly_push",
replace_existing=True,
)
scheduler.add_job(
run_attendance_summary,
trigger="cron",
hour=settings.attendance_summary_cron_hour,
minute=settings.attendance_summary_cron_minute,
id="attendance_summary_push",
replace_existing=True,
)
scheduler.add_job(
run_risk_progress,
trigger="cron",
hour=settings.risk_progress_cron_hour,
minute=settings.risk_progress_cron_minute,
id="risk_progress_push",
replace_existing=True,
)
scheduler.add_job(
run_work_daily,
trigger="cron",
hour=settings.work_daily_cron_hour,
minute=settings.work_daily_cron_minute,
id="work_daily_push",
replace_existing=True,
)
scheduler.add_job(
run_work_weekly,
trigger="cron",
day_of_week=settings.work_weekly_report_day_of_week,
hour=settings.work_weekly_cron_hour,
minute=settings.work_weekly_cron_minute,
id="work_weekly_push",
replace_existing=True,
)
if settings.event_dispatch_enabled:
scheduler.add_job(
run_event_dispatch,
@@ -271,3 +332,11 @@ def create_scheduler(app: FastAPI | None = None) -> Any:
def _set_state(app: FastAPI | None, key: str, value: Any) -> None:
if app is not None:
setattr(app.state, key, value)
def _feishu_delivery_configured(settings: Any) -> bool:
return bool(
settings.feishu_app_id
and settings.feishu_app_secret
and settings.feishu_default_chat_id
)