fix(小宝预警): 合并远端风险缓存
This commit is contained in:
@@ -3,6 +3,8 @@ import test from 'node:test';
|
||||
import type { XiaobaoRiskSnapshot } from './xiaobao-risk-trend';
|
||||
import {
|
||||
findCachedInsight,
|
||||
mergeDailySnapshotCacheForSave,
|
||||
mergeInsightCacheForSave,
|
||||
upsertDailySnapshot,
|
||||
upsertInsight,
|
||||
type XiaobaoRiskInsightCacheItem,
|
||||
@@ -68,3 +70,34 @@ test('upsertInsight replaces existing version signature pair', () => {
|
||||
|
||||
assert.deepEqual(result, [replacement, otherSignature, otherVersion]);
|
||||
});
|
||||
|
||||
test('mergeDailySnapshotCacheForSave preserves remote rows and local-only rows', () => {
|
||||
const remoteOnly: XiaobaoRiskSnapshot = {
|
||||
versionId: 'remote-version',
|
||||
date: '2026-06-29',
|
||||
riskScore: 30,
|
||||
riskLevel: 'attention',
|
||||
openBugCount: 0,
|
||||
failedTestCount: 0,
|
||||
blockedCount: 0,
|
||||
silentRiskCount: 0,
|
||||
confidence: 90,
|
||||
createdAt: '2026-06-29T08:00:00.000Z',
|
||||
};
|
||||
const localOnly: XiaobaoRiskSnapshot = { ...remoteOnly, versionId: 'local-version', riskScore: 50 };
|
||||
const item: XiaobaoRiskSnapshot = { ...remoteOnly, versionId: 'current-version', riskScore: 70 };
|
||||
|
||||
const result = mergeDailySnapshotCacheForSave([localOnly], [remoteOnly], item);
|
||||
|
||||
assert.deepEqual(result, [item, remoteOnly, localOnly]);
|
||||
});
|
||||
|
||||
test('mergeInsightCacheForSave preserves remote rows and local-only rows', () => {
|
||||
const remoteOnly: XiaobaoRiskInsightCacheItem = { ...insightA, versionId: 'remote-version' };
|
||||
const localOnly: XiaobaoRiskInsightCacheItem = { ...insightA, versionId: 'local-version' };
|
||||
const item: XiaobaoRiskInsightCacheItem = { ...insightA, versionId: 'current-version' };
|
||||
|
||||
const result = mergeInsightCacheForSave([localOnly], [remoteOnly], item);
|
||||
|
||||
assert.deepEqual(result, [item, remoteOnly, localOnly]);
|
||||
});
|
||||
|
||||
@@ -45,3 +45,23 @@ export function upsertDailySnapshot(
|
||||
...rows.filter((row) => row.versionId !== item.versionId || row.date !== item.date),
|
||||
];
|
||||
}
|
||||
|
||||
export function mergeDailySnapshotCacheForSave(
|
||||
localRows: XiaobaoRiskSnapshot[],
|
||||
remoteRows: XiaobaoRiskSnapshot[],
|
||||
item: XiaobaoRiskSnapshot,
|
||||
): XiaobaoRiskSnapshot[] {
|
||||
const remoteKeys = new Set(remoteRows.map((row) => `${row.versionId}::${row.date}`));
|
||||
const localOnly = localRows.filter((row) => !remoteKeys.has(`${row.versionId}::${row.date}`));
|
||||
return upsertDailySnapshot([...remoteRows, ...localOnly], item);
|
||||
}
|
||||
|
||||
export function mergeInsightCacheForSave(
|
||||
localRows: XiaobaoRiskInsightCacheItem[],
|
||||
remoteRows: XiaobaoRiskInsightCacheItem[],
|
||||
item: XiaobaoRiskInsightCacheItem,
|
||||
): XiaobaoRiskInsightCacheItem[] {
|
||||
const remoteKeys = new Set(remoteRows.map((row) => `${row.versionId}::${row.riskSignature}`));
|
||||
const localOnly = localRows.filter((row) => !remoteKeys.has(`${row.versionId}::${row.riskSignature}`));
|
||||
return upsertInsight([...remoteRows, ...localOnly], item);
|
||||
}
|
||||
|
||||
@@ -2,6 +2,8 @@
|
||||
import { create } from 'zustand';
|
||||
import { loadServerData, saveServerData } from '@/lib/server-data';
|
||||
import {
|
||||
mergeDailySnapshotCacheForSave,
|
||||
mergeInsightCacheForSave,
|
||||
upsertDailySnapshot,
|
||||
upsertInsight,
|
||||
type XiaobaoRiskInsightCacheItem,
|
||||
@@ -11,45 +13,67 @@ import type { XiaobaoRiskSnapshot } from '@/lib/xiaobao-risk-trend';
|
||||
interface XiaobaoRiskState {
|
||||
snapshots: XiaobaoRiskSnapshot[];
|
||||
insights: XiaobaoRiskInsightCacheItem[];
|
||||
error?: string;
|
||||
fetchRiskData: () => Promise<void>;
|
||||
saveSnapshot: (item: XiaobaoRiskSnapshot) => Promise<void>;
|
||||
saveInsight: (item: XiaobaoRiskInsightCacheItem) => Promise<void>;
|
||||
}
|
||||
|
||||
async function loadSnapshots(): Promise<XiaobaoRiskSnapshot[]> {
|
||||
async function loadSnapshots(): Promise<XiaobaoRiskSnapshot[] | null> {
|
||||
try {
|
||||
const rows = await loadServerData<XiaobaoRiskSnapshot[]>('xiaobao-risk-snapshots');
|
||||
return Array.isArray(rows) ? rows : [];
|
||||
} catch {}
|
||||
return [];
|
||||
return null;
|
||||
}
|
||||
|
||||
async function loadInsights(): Promise<XiaobaoRiskInsightCacheItem[]> {
|
||||
async function loadInsights(): Promise<XiaobaoRiskInsightCacheItem[] | null> {
|
||||
try {
|
||||
const rows = await loadServerData<XiaobaoRiskInsightCacheItem[]>('xiaobao-risk-insights');
|
||||
return Array.isArray(rows) ? rows : [];
|
||||
} catch {}
|
||||
return [];
|
||||
return null;
|
||||
}
|
||||
|
||||
export const useXiaobaoRiskStore = create<XiaobaoRiskState>((set, get) => ({
|
||||
snapshots: [],
|
||||
insights: [],
|
||||
error: undefined,
|
||||
|
||||
fetchRiskData: async () => {
|
||||
const [snapshots, insights] = await Promise.all([loadSnapshots(), loadInsights()]);
|
||||
set({ snapshots, insights });
|
||||
set({
|
||||
...(snapshots ? { snapshots } : {}),
|
||||
...(insights ? { insights } : {}),
|
||||
error: snapshots === null || insights === null ? '小宝预警缓存加载失败' : undefined,
|
||||
});
|
||||
},
|
||||
|
||||
saveSnapshot: async (item) => {
|
||||
const snapshots = upsertDailySnapshot(get().snapshots, item);
|
||||
set({ snapshots });
|
||||
const optimistic = upsertDailySnapshot(get().snapshots, item);
|
||||
set({ snapshots: optimistic, error: undefined });
|
||||
try {
|
||||
const remote = await loadServerData<XiaobaoRiskSnapshot[]>('xiaobao-risk-snapshots');
|
||||
const snapshots = mergeDailySnapshotCacheForSave(optimistic, Array.isArray(remote) ? remote : [], item);
|
||||
set({ snapshots, error: undefined });
|
||||
await saveServerData('xiaobao-risk-snapshots', snapshots);
|
||||
} catch (error) {
|
||||
set({ error: '小宝预警快照保存失败' });
|
||||
throw error;
|
||||
}
|
||||
},
|
||||
|
||||
saveInsight: async (item) => {
|
||||
const insights = upsertInsight(get().insights, item);
|
||||
set({ insights });
|
||||
const optimistic = upsertInsight(get().insights, item);
|
||||
set({ insights: optimistic, error: undefined });
|
||||
try {
|
||||
const remote = await loadServerData<XiaobaoRiskInsightCacheItem[]>('xiaobao-risk-insights');
|
||||
const insights = mergeInsightCacheForSave(optimistic, Array.isArray(remote) ? remote : [], item);
|
||||
set({ insights, error: undefined });
|
||||
await saveServerData('xiaobao-risk-insights', insights);
|
||||
} catch (error) {
|
||||
set({ error: '小宝预警解读保存失败' });
|
||||
throw error;
|
||||
}
|
||||
},
|
||||
}));
|
||||
|
||||
Reference in New Issue
Block a user