```
feat: 添加AI记忆模块和事件调度系统 - 新增AI记忆模块,支持本地记忆召回和自动写入功能 - 实现事件调度系统,支持批量处理待定事件和重试机制 - 集成心跳监控机制,跟踪API、调度器和工作节点状态 - 扩展仪表板数据统计,包含AI记忆条目和心跳概要 - 添加企业运营分析报告功能,提供财务、采购等多维度分析 - 更新配置设置,增加事件调度和AI记忆相关参数 - 优化任务队列,添加事件分发任务类型 - 扩展审计日志,记录AI记忆操作和事件调度行为 - 实现领域事件模型,支持事件持久化和状态管理 - 添加观察性服务,监控系统组件健康状况 ```
This commit is contained in:
@@ -25,6 +25,7 @@ from app.modules.business.models import (
|
||||
AttendanceRecord,
|
||||
Expense,
|
||||
FundAccount,
|
||||
PerformanceMetric,
|
||||
Procurement,
|
||||
Project,
|
||||
ReportPushRun,
|
||||
@@ -55,13 +56,14 @@ from app.modules.reports.constants import (
|
||||
LifecycleResponseKey,
|
||||
LifecycleSection,
|
||||
MetricKey,
|
||||
ReportResponseKey,
|
||||
ReportPushStatus,
|
||||
ReportErrorDetail,
|
||||
ReportPushStatus,
|
||||
ReportResponseKey,
|
||||
ReportStatus,
|
||||
ReportText,
|
||||
ReportTitle,
|
||||
ReportType,
|
||||
EnterpriseAnalyticsKey,
|
||||
WorkReportMetricKey,
|
||||
)
|
||||
from app.modules.risk.constants import RiskSummaryKey, risk_level_for_score
|
||||
@@ -387,6 +389,169 @@ class ReportService:
|
||||
report[LifecycleResponseKey.AI_ANALYSIS] = self._lifecycle_ai_analysis(report, actor)
|
||||
return report
|
||||
|
||||
def enterprise_analytics(
|
||||
self,
|
||||
project_code: str | None = None,
|
||||
owner: str | None = None,
|
||||
period_start: date | None = None,
|
||||
period_end: date | None = None,
|
||||
actor: str = ActorValue.API,
|
||||
) -> dict[str, Any]:
|
||||
"""Build V3 read-only finance, procurement, performance, and operations analytics."""
|
||||
|
||||
code = _next_code("ANALYTICS")
|
||||
lifecycle = self.project_lifecycle_report(
|
||||
project_code=project_code,
|
||||
owner=owner,
|
||||
period_start=period_start,
|
||||
period_end=period_end,
|
||||
include_ai=False,
|
||||
actor=actor,
|
||||
)
|
||||
metrics = lifecycle[LifecycleResponseKey.METRICS]
|
||||
projects = metrics[LifecycleSection.PROJECTS]
|
||||
procurements = metrics[LifecycleSection.PROCUREMENTS]
|
||||
expenses = metrics[LifecycleSection.EXPENSES]
|
||||
funds = metrics[LifecycleSection.FUNDS]
|
||||
tasks = metrics[LifecycleSection.TASKS]
|
||||
risks = metrics[LifecycleSection.RISKS]
|
||||
health = metrics[LifecycleSection.HEALTH]
|
||||
finance = {
|
||||
MetricKey.BUDGET_TOTAL: projects[MetricKey.BUDGET_TOTAL],
|
||||
MetricKey.ACTUAL_TOTAL: projects[MetricKey.ACTUAL_TOTAL],
|
||||
MetricKey.BUDGET_USAGE_RATE: projects[MetricKey.BUDGET_USAGE_RATE],
|
||||
MetricKey.CURRENT_BALANCE_TOTAL: funds[MetricKey.CURRENT_BALANCE_TOTAL],
|
||||
MetricKey.NET_POSITION: funds[MetricKey.NET_POSITION],
|
||||
MetricKey.RISK_ACCOUNTS: funds[MetricKey.RISK_ACCOUNTS],
|
||||
MetricKey.PAYMENT_EXPOSURE: (
|
||||
procurements[MetricKey.ACTUAL_TOTAL] + expenses[MetricKey.AMOUNT_TOTAL]
|
||||
),
|
||||
}
|
||||
procurement = {
|
||||
MetricKey.TOTAL: procurements[MetricKey.TOTAL],
|
||||
MetricKey.PENDING_APPROVAL: procurements[MetricKey.PENDING_APPROVAL],
|
||||
MetricKey.PENDING_DELIVERY: procurements[MetricKey.PENDING_DELIVERY],
|
||||
MetricKey.UNPAID: procurements[MetricKey.UNPAID],
|
||||
MetricKey.EXPECTED_TOTAL: procurements[MetricKey.EXPECTED_TOTAL],
|
||||
MetricKey.ACTUAL_TOTAL: procurements[MetricKey.ACTUAL_TOTAL],
|
||||
MetricKey.DELIVERY_RISK: procurements[MetricKey.PENDING_DELIVERY],
|
||||
}
|
||||
performance = self._enterprise_performance_stats()
|
||||
operations = {
|
||||
MetricKey.READINESS_SCORE: health[MetricKey.SCORE],
|
||||
MetricKey.LEVEL: health[MetricKey.LEVEL],
|
||||
MetricKey.COMPLETION_RATE: tasks[MetricKey.COMPLETION_RATE],
|
||||
MetricKey.OVERDUE_TASKS: risks[MetricKey.OVERDUE_TASKS],
|
||||
MetricKey.DELAYED_PROJECTS: risks[MetricKey.DELAYED_PROJECTS],
|
||||
MetricKey.OVER_BUDGET_PROJECTS: risks[MetricKey.OVER_BUDGET_PROJECTS],
|
||||
MetricKey.OPEN_EVENTS: risks[MetricKey.OPEN_EVENTS],
|
||||
MetricKey.HIGH_EVENTS: risks[MetricKey.HIGH_EVENTS],
|
||||
}
|
||||
recommendations = lifecycle[LifecycleResponseKey.RECOMMENDATIONS]
|
||||
lines = self._enterprise_analytics_lines(
|
||||
lifecycle[LifecycleResponseKey.FILTERS],
|
||||
finance,
|
||||
procurement,
|
||||
performance,
|
||||
operations,
|
||||
recommendations,
|
||||
)
|
||||
report = _json_safe(
|
||||
{
|
||||
EnterpriseAnalyticsKey.CODE: code,
|
||||
EnterpriseAnalyticsKey.TITLE: ReportTitle.ENTERPRISE_ANALYTICS,
|
||||
EnterpriseAnalyticsKey.FILTERS: lifecycle[LifecycleResponseKey.FILTERS],
|
||||
EnterpriseAnalyticsKey.FINANCE: finance,
|
||||
EnterpriseAnalyticsKey.PROCUREMENT: procurement,
|
||||
EnterpriseAnalyticsKey.PERFORMANCE: performance,
|
||||
EnterpriseAnalyticsKey.OPERATIONS: operations,
|
||||
EnterpriseAnalyticsKey.RECOMMENDATIONS: recommendations,
|
||||
EnterpriseAnalyticsKey.LINES: lines,
|
||||
EnterpriseAnalyticsKey.CONTENT: "\n".join(lines),
|
||||
}
|
||||
)
|
||||
AuditService(self.db).log(
|
||||
AuditLogCreate(
|
||||
actor=actor,
|
||||
source=AuditSource.REPORTS,
|
||||
action=AuditAction.ENTERPRISE_ANALYTICS,
|
||||
target_type=AuditTargetType.ENTERPRISE_ANALYTICS,
|
||||
target_id=code,
|
||||
response_payload=report,
|
||||
)
|
||||
)
|
||||
EventService(self.db).emit(
|
||||
event_type=EventType.ENTERPRISE_ANALYTICS_GENERATED,
|
||||
source=EventSource.ANALYTICS,
|
||||
aggregate_type=EventAggregateType.ENTERPRISE_ANALYTICS,
|
||||
aggregate_id=code,
|
||||
actor=actor,
|
||||
payload={
|
||||
EventPayloadKey.CODE: code,
|
||||
EventPayloadKey.STATUS: ReportStatus.GENERATED,
|
||||
},
|
||||
idempotency_key=f"enterprise-analytics:{code}",
|
||||
dispatch=True,
|
||||
)
|
||||
return report
|
||||
|
||||
def _enterprise_performance_stats(self) -> dict[str, Any]:
|
||||
total = self._count(PerformanceMetric)
|
||||
confirmed = self._count(PerformanceMetric, PerformanceMetric.confirmed_score.is_not(None))
|
||||
return {
|
||||
MetricKey.TOTAL: total,
|
||||
MetricKey.CONFIRMED: confirmed,
|
||||
MetricKey.CONFIRMED_RATE: _rate(confirmed, total),
|
||||
MetricKey.AVERAGE_AUTO_SCORE: self._avg(PerformanceMetric.auto_score),
|
||||
MetricKey.AVERAGE_CONFIRMED_SCORE: self._avg(PerformanceMetric.confirmed_score),
|
||||
MetricKey.WEIGHT_TOTAL: self._sum(PerformanceMetric.weight),
|
||||
MetricKey.BY_STATUS: self._group_counts(PerformanceMetric, PerformanceMetric.status),
|
||||
}
|
||||
|
||||
def _enterprise_analytics_lines(
|
||||
self,
|
||||
filters: dict[str, Any],
|
||||
finance: dict[str, Any],
|
||||
procurement: dict[str, Any],
|
||||
performance: dict[str, Any],
|
||||
operations: dict[str, Any],
|
||||
recommendations: list[str],
|
||||
) -> list[str]:
|
||||
scope = (
|
||||
"、".join(f"{key}={value}" for key, value in filters.items() if value)
|
||||
or ReportText.DEFAULT_SCOPE
|
||||
)
|
||||
lines = [
|
||||
f"- 范围:{scope}",
|
||||
(
|
||||
f"- 财务:预算 {_money(finance[MetricKey.BUDGET_TOTAL])},"
|
||||
f"实际 {_money(finance[MetricKey.ACTUAL_TOTAL])},"
|
||||
f"净头寸 {_money(finance[MetricKey.NET_POSITION])},"
|
||||
f"支付暴露 {_money(finance[MetricKey.PAYMENT_EXPOSURE])}"
|
||||
),
|
||||
(
|
||||
f"- 采购:总数 {procurement[MetricKey.TOTAL]},"
|
||||
f"待审批 {procurement[MetricKey.PENDING_APPROVAL]},"
|
||||
f"待交付 {procurement[MetricKey.PENDING_DELIVERY]},"
|
||||
f"未付款 {procurement[MetricKey.UNPAID]}"
|
||||
),
|
||||
(
|
||||
f"- 绩效:指标 {performance[MetricKey.TOTAL]},"
|
||||
f"已确认 {performance[MetricKey.CONFIRMED]},"
|
||||
f"确认率 {performance[MetricKey.CONFIRMED_RATE]}%,"
|
||||
f"平均自动分 {performance[MetricKey.AVERAGE_AUTO_SCORE]}"
|
||||
),
|
||||
(
|
||||
f"- 运营:准备度 {operations[MetricKey.READINESS_SCORE]},"
|
||||
f"任务完成率 {operations[MetricKey.COMPLETION_RATE]}%,"
|
||||
f"逾期任务 {operations[MetricKey.OVERDUE_TASKS]},"
|
||||
f"打开风险 {operations[MetricKey.OPEN_EVENTS]}"
|
||||
),
|
||||
ReportText.ACTION_HEADER,
|
||||
]
|
||||
lines.extend(f" - {item}" for item in recommendations)
|
||||
return lines
|
||||
|
||||
def _lifecycle_filters(
|
||||
self,
|
||||
project_code: str | None,
|
||||
@@ -985,6 +1150,19 @@ class ReportService:
|
||||
response_payload=record_data,
|
||||
)
|
||||
)
|
||||
EventService(self.db).emit(
|
||||
event_type=EventType.REPORT_GENERATED,
|
||||
source=EventSource.REPORTS,
|
||||
aggregate_type=EventAggregateType.WORK_REPORT,
|
||||
aggregate_id=record.code,
|
||||
actor=actor,
|
||||
payload={
|
||||
EventPayloadKey.CODE: record.code,
|
||||
EventPayloadKey.STATUS: record.status,
|
||||
},
|
||||
idempotency_key=f"report-generated:{record.code}",
|
||||
dispatch=True,
|
||||
)
|
||||
|
||||
return {ReportResponseKey.REPORT: report, ReportResponseKey.DATA: record_data}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user