feat: 2026-04-15~05-02 累积变更基线 — AI 重构 + Runtime Context + DWS 修复

涵盖(每条对应已存的审计记录):
- AI 模块拆分:apps/backend/app/ai/apps -> prompts/(8 个 APP + app2a 派生)
  audit: 2026-04-20__ai-module-complete.md
- admin-web AI 管理套件:AIDashboard / AIOperations / AIRunLogs / AITriggers / TriggerManager
  audit: 2026-04-21__admin-web-ai-management-suite.md
- App2 财务洞察 prompt v3 -> v5.1 + 小程序 AI 接入(chat / board-finance)
  audit: 2026-04-22__app2_prompt_v5_1_and_miniprogram_ai_insight.md
- App2 prewarm 全过滤器 + AI 触发器 cron reschedule
  audit: 2026-04-21__app2-finance-prewarm-all-filters.md
  migration: 20260420_ai_trigger_jobs_and_app2_prewarm.sql / 20260421_app2_prewarm_cron_reschedule.sql
- AppType 联合类型对齐 + adminAiAppTypes.test.ts
  audit: 2026-04-30__admin_web_ai_app_type_alignment.md
- DashScope tokens_used 提取修复
  audit: 2026-04-30__backend_dashscope_tokens_used_extraction.md
- App3 线索完整详情 prompt
  audit: 2026-05-01__backend_app3_full_detail_prompt.md
- Runtime Context 沙箱(5-1~5-2 主线):
  - 后端 schema/service + admin_runtime_context / xcx_runtime_clock 两个 router
  - admin-web RuntimeContext.tsx + miniprogram runtime-clock.ts
  - migration: 20260501__runtime_context_sandbox.sql
  - tools/db/verify_admin_web_sandbox.py + verify_sandbox_end_to_end.py
  - database/changes: 7 份 sandbox_* 验证报告
- 飞球 DWS 修复:finance_area_daily 区域汇总 + task_engine 调整
  + RLS 视图业务日上界(migration 20260502 + scripts/ops/gen_rls_business_date_migration.py)

合规:
- .gitignore 启用 tmp/ 排除
- 不入仓:apps/etl/connectors/feiqiu/.env(API_TOKEN secret,本地修改保留)

待验证清单:
- docs/audit/changes/2026-05-04__cumulative_baseline_pending_verification.md
  每个主题的功能完整性 / 上线验证几乎都未收口,按优先级 P0~P3 逐一处理
This commit is contained in:
Neo
2026-05-04 02:30:19 +08:00
parent 2010034840
commit caf179a5da
130 changed files with 14543 additions and 2717 deletions

View File

@@ -8,12 +8,27 @@
* - 第四行:告警列表
*/
import React, { useEffect, useState, useCallback } from "react";
import React, { useEffect, useRef, useState, useCallback } from "react";
import {
Card, Row, Col, Statistic, Table, Tag, Badge, Progress,
Select, Button, message, Typography, Space,
Select, Button, message, Typography, Space, DatePicker,
} from "antd";
import { ReloadOutlined } from "@ant-design/icons";
import { ReloadOutlined, WifiOutlined } from "@ant-design/icons";
import type { Dayjs } from "dayjs";
const { RangePicker } = DatePicker;
const RANGE_OPTIONS = [
{ label: "今日", value: 1 },
{ label: "近 3 天", value: 3 },
{ label: "近 7 天", value: 7 },
{ label: "近 10 天", value: 10 },
{ label: "指定日期", value: 0 }, // 0 = 启用 RangePicker
];
const RANGE_LABEL: Record<number, string> = {
1: "今日", 3: "近 3 天", 7: "近 7 天", 10: "近 10 天",
};
import type { ColumnsType } from "antd/es/table";
import {
getDashboard,
@@ -85,51 +100,119 @@ const AIDashboard: React.FC = () => {
const [data, setData] = useState<DashboardResponse | null>(null);
const [loading, setLoading] = useState(false);
const [siteId, setSiteId] = useState<number | undefined>(undefined);
const [rangeDays, setRangeDays] = useState<number>(1); // 0=自定义日期 / 1/3/7/10
const [customRange, setCustomRange] = useState<[Dayjs, Dayjs] | null>(null);
const [wsStatus, setWsStatus] = useState<"connecting" | "connected" | "disconnected">("disconnected");
const [realtimeAlerts, setRealtimeAlerts] = useState<AlertItem[]>([]);
const wsRef = useRef<WebSocket | null>(null);
const load = useCallback(async () => {
setLoading(true);
try {
const res = await getDashboard(siteId);
const query: { site_id?: number; range_days?: number; date_from?: string; date_to?: string } = {};
if (siteId != null) query.site_id = siteId;
if (rangeDays === 0 && customRange) {
query.date_from = customRange[0].format("YYYY-MM-DD");
query.date_to = customRange[1].format("YYYY-MM-DD");
} else if (rangeDays > 0) {
query.range_days = rangeDays;
}
const res = await getDashboard(query);
setData(res);
} catch {
message.error("加载 Dashboard 失败");
} finally {
setLoading(false);
}
}, [siteId]);
}, [siteId, rangeDays, customRange]);
useEffect(() => { load(); }, [load]);
const statLabel = rangeDays === 0
? (customRange ? `${customRange[0].format("MM-DD")} ~ ${customRange[1].format("MM-DD")}` : "指定日期")
: (RANGE_LABEL[rangeDays] || "今日");
// WebSocket 实时告警订阅
useEffect(() => {
const wsKey = siteId ?? -1;
const proto = window.location.protocol === "https:" ? "wss" : "ws";
const url = `${proto}://${window.location.host}/ws/ai-alerts/${wsKey}`;
setWsStatus("connecting");
const ws = new WebSocket(url);
wsRef.current = ws;
ws.onopen = () => setWsStatus("connected");
ws.onclose = () => setWsStatus("disconnected");
ws.onerror = () => setWsStatus("disconnected");
ws.onmessage = (evt) => {
try {
const msg = JSON.parse(evt.data as string) as {
type: string;
payload: AlertItem;
};
if (msg.type === "alert_created" && msg.payload) {
setRealtimeAlerts((prev) => [msg.payload, ...prev].slice(0, 20));
message.warning(`[实时] ${msg.payload.app_type} ${msg.payload.status}`);
}
} catch {
// 忽略非 JSON 消息
}
};
return () => {
ws.close();
wsRef.current = null;
setWsStatus("disconnected");
};
}, [siteId]);
return (
<div>
{/* 顶部:门店筛选 + 刷新 */}
<Row justify="space-between" align="middle" style={{ marginBottom: 16 }}>
<Space>
<Space wrap>
<Title level={4} style={{ margin: 0 }}>AI </Title>
<Select
allowClear placeholder="门店筛选" style={{ width: 200 }}
allowClear placeholder="门店筛选" style={{ width: 180 }}
value={siteId} onChange={(v) => setSiteId(v)}
options={[{ label: "默认门店", value: 2790685415443269 }]}
/>
<Select
value={rangeDays} onChange={setRangeDays} style={{ width: 140 }}
options={RANGE_OPTIONS}
/>
{rangeDays === 0 && (
<RangePicker
value={customRange}
onChange={(v) => setCustomRange(v as [Dayjs, Dayjs] | null)}
/>
)}
</Space>
<Space>
<Badge
status={wsStatus === "connected" ? "success" : wsStatus === "connecting" ? "processing" : "default"}
text={<span style={{ fontSize: 12, color: "#888" }}><WifiOutlined /> {wsStatus === "connected" ? "已连接" : wsStatus === "connecting" ? "连接中" : "断开"}</span>}
/>
<Button icon={<ReloadOutlined />} onClick={load} loading={loading}></Button>
</Space>
<Button icon={<ReloadOutlined />} onClick={load} loading={loading}></Button>
</Row>
{/* 第一行4 个统计卡片 */}
<Row gutter={16} style={{ marginBottom: 16 }}>
<Col span={6}>
<Card><Statistic title="今日调用次数" value={data?.today_calls ?? 0} /></Card>
<Card><Statistic title={`${statLabel}调用次数`} value={data?.today_calls ?? 0} /></Card>
</Col>
<Col span={6}>
<Card>
<Statistic
title="今日成功率" suffix="%"
title={`${statLabel}成功率`} suffix="%"
value={data ? (data.today_success_rate * 100).toFixed(1) : "0.0"}
/>
</Card>
</Col>
<Col span={6}>
<Card><Statistic title="今日 Token 消耗" value={data?.today_tokens ?? 0} /></Card>
<Card><Statistic title={`${statLabel} Token 消耗`} value={data?.today_tokens ?? 0} /></Card>
</Col>
<Col span={6}>
<Card>
@@ -201,11 +284,22 @@ const AIDashboard: React.FC = () => {
</Col>
</Row>
{/* 第四行:告警列表 */}
<Card title="告警列表" size="small">
{/* 第四行:告警列表(实时 + 历史合并) */}
<Card
title="告警列表"
size="small"
extra={realtimeAlerts.length > 0 && (
<Tag color="orange">{realtimeAlerts.length} </Tag>
)}
>
<Table<AlertItem>
columns={alertColumns}
dataSource={data?.recent_alerts ?? []}
dataSource={[
...realtimeAlerts,
...(data?.recent_alerts ?? []).filter(
(a) => !realtimeAlerts.some((r) => r.id === a.id)
),
]}
rowKey="id" size="small" pagination={{ pageSize: 10 }}
loading={loading}
/>