fix(小宝预警): 合并远端风险缓存
This commit is contained in:
@@ -3,6 +3,8 @@ import test from 'node:test';
|
|||||||
import type { XiaobaoRiskSnapshot } from './xiaobao-risk-trend';
|
import type { XiaobaoRiskSnapshot } from './xiaobao-risk-trend';
|
||||||
import {
|
import {
|
||||||
findCachedInsight,
|
findCachedInsight,
|
||||||
|
mergeDailySnapshotCacheForSave,
|
||||||
|
mergeInsightCacheForSave,
|
||||||
upsertDailySnapshot,
|
upsertDailySnapshot,
|
||||||
upsertInsight,
|
upsertInsight,
|
||||||
type XiaobaoRiskInsightCacheItem,
|
type XiaobaoRiskInsightCacheItem,
|
||||||
@@ -68,3 +70,34 @@ test('upsertInsight replaces existing version signature pair', () => {
|
|||||||
|
|
||||||
assert.deepEqual(result, [replacement, otherSignature, otherVersion]);
|
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),
|
...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 { create } from 'zustand';
|
||||||
import { loadServerData, saveServerData } from '@/lib/server-data';
|
import { loadServerData, saveServerData } from '@/lib/server-data';
|
||||||
import {
|
import {
|
||||||
|
mergeDailySnapshotCacheForSave,
|
||||||
|
mergeInsightCacheForSave,
|
||||||
upsertDailySnapshot,
|
upsertDailySnapshot,
|
||||||
upsertInsight,
|
upsertInsight,
|
||||||
type XiaobaoRiskInsightCacheItem,
|
type XiaobaoRiskInsightCacheItem,
|
||||||
@@ -11,45 +13,67 @@ import type { XiaobaoRiskSnapshot } from '@/lib/xiaobao-risk-trend';
|
|||||||
interface XiaobaoRiskState {
|
interface XiaobaoRiskState {
|
||||||
snapshots: XiaobaoRiskSnapshot[];
|
snapshots: XiaobaoRiskSnapshot[];
|
||||||
insights: XiaobaoRiskInsightCacheItem[];
|
insights: XiaobaoRiskInsightCacheItem[];
|
||||||
|
error?: string;
|
||||||
fetchRiskData: () => Promise<void>;
|
fetchRiskData: () => Promise<void>;
|
||||||
saveSnapshot: (item: XiaobaoRiskSnapshot) => Promise<void>;
|
saveSnapshot: (item: XiaobaoRiskSnapshot) => Promise<void>;
|
||||||
saveInsight: (item: XiaobaoRiskInsightCacheItem) => Promise<void>;
|
saveInsight: (item: XiaobaoRiskInsightCacheItem) => Promise<void>;
|
||||||
}
|
}
|
||||||
|
|
||||||
async function loadSnapshots(): Promise<XiaobaoRiskSnapshot[]> {
|
async function loadSnapshots(): Promise<XiaobaoRiskSnapshot[] | null> {
|
||||||
try {
|
try {
|
||||||
const rows = await loadServerData<XiaobaoRiskSnapshot[]>('xiaobao-risk-snapshots');
|
const rows = await loadServerData<XiaobaoRiskSnapshot[]>('xiaobao-risk-snapshots');
|
||||||
return Array.isArray(rows) ? rows : [];
|
return Array.isArray(rows) ? rows : [];
|
||||||
} catch {}
|
} catch {}
|
||||||
return [];
|
return null;
|
||||||
}
|
}
|
||||||
|
|
||||||
async function loadInsights(): Promise<XiaobaoRiskInsightCacheItem[]> {
|
async function loadInsights(): Promise<XiaobaoRiskInsightCacheItem[] | null> {
|
||||||
try {
|
try {
|
||||||
const rows = await loadServerData<XiaobaoRiskInsightCacheItem[]>('xiaobao-risk-insights');
|
const rows = await loadServerData<XiaobaoRiskInsightCacheItem[]>('xiaobao-risk-insights');
|
||||||
return Array.isArray(rows) ? rows : [];
|
return Array.isArray(rows) ? rows : [];
|
||||||
} catch {}
|
} catch {}
|
||||||
return [];
|
return null;
|
||||||
}
|
}
|
||||||
|
|
||||||
export const useXiaobaoRiskStore = create<XiaobaoRiskState>((set, get) => ({
|
export const useXiaobaoRiskStore = create<XiaobaoRiskState>((set, get) => ({
|
||||||
snapshots: [],
|
snapshots: [],
|
||||||
insights: [],
|
insights: [],
|
||||||
|
error: undefined,
|
||||||
|
|
||||||
fetchRiskData: async () => {
|
fetchRiskData: async () => {
|
||||||
const [snapshots, insights] = await Promise.all([loadSnapshots(), loadInsights()]);
|
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) => {
|
saveSnapshot: async (item) => {
|
||||||
const snapshots = upsertDailySnapshot(get().snapshots, item);
|
const optimistic = upsertDailySnapshot(get().snapshots, item);
|
||||||
set({ snapshots });
|
set({ snapshots: optimistic, error: undefined });
|
||||||
await saveServerData('xiaobao-risk-snapshots', snapshots);
|
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) => {
|
saveInsight: async (item) => {
|
||||||
const insights = upsertInsight(get().insights, item);
|
const optimistic = upsertInsight(get().insights, item);
|
||||||
set({ insights });
|
set({ insights: optimistic, error: undefined });
|
||||||
await saveServerData('xiaobao-risk-insights', insights);
|
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