Files
company-ai-platform/app/tasks/reports.py
JiuContinent d7db84571d ```
feat: 添加飞书用户模块和订阅功能支持

- 新增feishu_users模块用于处理飞书用户身份验证和权限管理
- 新增subscriptions模块用于处理订阅相关功能
- 新增personalization模块用于个性化服务
- 在alembic迁移配置中注册新的模型模块
- 在API路由器中添加feishu_users和subscriptions路由
- 实现事件调度服务的改进,包括错误处理和状态更新优化
- 添加飞书命令处理的权限检查机制
- 实现飞书应用票据事件处理
- 改进审计日志记录功能
```
2026-07-27 08:02:17 +08:00

154 lines
4.6 KiB
Python

from collections.abc import Callable
from typing import Any
from app.tasks.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.modules.reports.constants import ReportPushStatus
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.application.delivery import ReportDeliveryService
from app.modules.reports.services import ReportService
db = SessionLocal()
try:
service = ReportService(db)
if push_run_code:
push_run = service._get_push_run(push_run_code)
if push_run.status == ReportPushStatus.SUCCESS:
return dict(push_run.provider_response or {})
report = build_report(service, actor)
return ReportDeliveryService(db).push_report(
report,
receive_id,
receive_id_type,
actor,
push_run_code=push_run_code,
)
except Exception as exc:
if push_run_code:
db.rollback()
try:
ReportService(db).update_push_run(
push_run_code,
ReportPushStatus.FAILED,
error_message=str(exc),
)
except Exception:
db.rollback()
raise
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)