refactor(data): 收口关系表运行时数据源
Some checks failed
Deploy Production / Build, push, deploy, verify (push) Has been cancelled
Some checks failed
Deploy Production / Build, push, deploy, verify (push) Has been cancelled
- 移除已迁移业务 AppData 运行时 fallback,改走领域 API 和关系表快读 - 补齐需求产品负责人、版本计划任务 JSON 和成员 username 回填迁移 - 统一治理字典入口,并补充 AI provider、数据源契约和领域服务测试 Co-Authored-By: Codex GPT-5 <codex@openai.com>
This commit is contained in:
@@ -1,11 +1,6 @@
|
||||
'use client';
|
||||
import { create } from 'zustand';
|
||||
import { loadServerData, saveServerData, SERVER_DATA_CACHE_MS } from '@/lib/server-data';
|
||||
import { api } from '@/lib/api';
|
||||
import { useMemberStore } from './useMemberStore';
|
||||
import {
|
||||
mergeDailySnapshotCacheForSave,
|
||||
mergeInsightCacheForSave,
|
||||
upsertDailySnapshot,
|
||||
upsertInsight,
|
||||
type XiaobaoRiskInsightCacheItem,
|
||||
@@ -26,24 +21,7 @@ interface XiaobaoRiskState {
|
||||
finishInsightUpdate: (key: string) => void;
|
||||
}
|
||||
|
||||
async function loadSnapshots(): Promise<XiaobaoRiskSnapshot[] | null> {
|
||||
try {
|
||||
const rows = await loadServerData<XiaobaoRiskSnapshot[]>('xiaobao-risk-snapshots');
|
||||
return Array.isArray(rows) ? rows : [];
|
||||
} catch {}
|
||||
return null;
|
||||
}
|
||||
|
||||
async function loadInsights(): Promise<XiaobaoRiskInsightCacheItem[] | null> {
|
||||
try {
|
||||
const rows = await loadServerData<XiaobaoRiskInsightCacheItem[]>('xiaobao-risk-insights');
|
||||
return Array.isArray(rows) ? rows : [];
|
||||
} catch {}
|
||||
return null;
|
||||
}
|
||||
|
||||
let snapshotSaveQueue: Promise<void> = Promise.resolve();
|
||||
let insightSaveQueue: Promise<void> = Promise.resolve();
|
||||
const RISK_DATA_CACHE_MS = 30_000;
|
||||
let lastRiskDataFetchAt = 0;
|
||||
|
||||
export const useXiaobaoRiskStore = create<XiaobaoRiskState>((set, get) => ({
|
||||
@@ -55,54 +33,22 @@ export const useXiaobaoRiskStore = create<XiaobaoRiskState>((set, get) => ({
|
||||
error: undefined,
|
||||
|
||||
fetchRiskData: async () => {
|
||||
if (get().riskDataLoaded && Date.now() - lastRiskDataFetchAt < SERVER_DATA_CACHE_MS) return;
|
||||
set({ riskDataLoaded: false });
|
||||
const [snapshots, insights] = await Promise.all([loadSnapshots(), loadInsights()]);
|
||||
if (get().riskDataLoaded && Date.now() - lastRiskDataFetchAt < SERVER_DATA_CACHE_MS) return;
|
||||
if (get().riskDataLoaded && Date.now() - lastRiskDataFetchAt < RISK_DATA_CACHE_MS) return;
|
||||
lastRiskDataFetchAt = Date.now();
|
||||
set({
|
||||
...(snapshots ? { snapshots } : {}),
|
||||
...(insights ? { insights } : {}),
|
||||
riskDataLoaded: snapshots !== null && insights !== null,
|
||||
error: snapshots === null || insights === null ? '小宝预警缓存加载失败' : undefined,
|
||||
riskDataLoaded: true,
|
||||
error: undefined,
|
||||
});
|
||||
},
|
||||
|
||||
saveSnapshot: async (item) => {
|
||||
const optimistic = upsertDailySnapshot(get().snapshots, item);
|
||||
set({ snapshots: optimistic, error: undefined });
|
||||
const task = snapshotSaveQueue.then(async () => {
|
||||
const remote = await loadServerData<XiaobaoRiskSnapshot[]>('xiaobao-risk-snapshots', { force: true });
|
||||
const snapshots = mergeDailySnapshotCacheForSave(get().snapshots, Array.isArray(remote) ? remote : [], item);
|
||||
set({ snapshots, error: undefined });
|
||||
await saveServerData('xiaobao-risk-snapshots', snapshots);
|
||||
void notifyRiskManagers(item).catch(() => {});
|
||||
});
|
||||
snapshotSaveQueue = task.catch(() => undefined);
|
||||
try {
|
||||
await task;
|
||||
} catch (error) {
|
||||
set({ error: '小宝预警快照保存失败' });
|
||||
throw error;
|
||||
}
|
||||
},
|
||||
|
||||
saveInsight: async (item) => {
|
||||
const optimistic = upsertInsight(get().insights, item);
|
||||
set({ insights: optimistic, error: undefined });
|
||||
const task = insightSaveQueue.then(async () => {
|
||||
const remote = await loadServerData<XiaobaoRiskInsightCacheItem[]>('xiaobao-risk-insights', { force: true });
|
||||
const insights = mergeInsightCacheForSave(get().insights, Array.isArray(remote) ? remote : [], item);
|
||||
set({ insights, error: undefined });
|
||||
await saveServerData('xiaobao-risk-insights', insights);
|
||||
});
|
||||
insightSaveQueue = task.catch(() => undefined);
|
||||
try {
|
||||
await task;
|
||||
} catch (error) {
|
||||
set({ error: '小宝预警解读保存失败' });
|
||||
throw error;
|
||||
}
|
||||
},
|
||||
|
||||
beginInsightUpdate: (key) => {
|
||||
@@ -122,32 +68,3 @@ export const useXiaobaoRiskStore = create<XiaobaoRiskState>((set, get) => ({
|
||||
set({ pendingInsightKeys: get().pendingInsightKeys.filter((item) => item !== key) });
|
||||
},
|
||||
}));
|
||||
|
||||
async function notifyRiskManagers(item: XiaobaoRiskSnapshot) {
|
||||
if (!['at_risk', 'likely_delayed', 'blocked'].includes(item.riskLevel)) return;
|
||||
const memberStore = useMemberStore.getState();
|
||||
if (!memberStore.loaded) {
|
||||
await memberStore.fetchMembers().catch(() => undefined);
|
||||
}
|
||||
const state = useMemberStore.getState();
|
||||
const roleMap = new Map(state.roles.map((role) => [role.id, role]));
|
||||
const recipients = state.members.filter((member) => {
|
||||
const permissions = roleMap.get(member.roleId)?.permissions ?? [];
|
||||
return permissions.includes('*') || permissions.includes('xiaobao.warning:manage');
|
||||
});
|
||||
await Promise.all(recipients.map((member) => api.post('/notifications', {
|
||||
recipientId: member.id,
|
||||
actorId: 'xiaobao',
|
||||
type: 'risk_alert',
|
||||
title: '小宝预警更新',
|
||||
body: `版本 ${item.versionId} 当前风险 ${item.riskLevel},风险分 ${item.riskScore}`,
|
||||
resourceType: 'version',
|
||||
resourceId: item.versionId,
|
||||
versionId: item.versionId,
|
||||
metadata: {
|
||||
riskLevel: item.riskLevel,
|
||||
riskScore: item.riskScore,
|
||||
riskSignature: `${item.versionId}:${item.date}:${item.riskScore}:${item.riskLevel}`,
|
||||
},
|
||||
}).catch(() => undefined)));
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user