feat(小宝预警): 增加趋势与解读缓存
This commit is contained in:
70
apps/web/lib/xiaobao-risk-cache.test.ts
Normal file
70
apps/web/lib/xiaobao-risk-cache.test.ts
Normal file
@@ -0,0 +1,70 @@
|
|||||||
|
import assert from 'node:assert/strict';
|
||||||
|
import test from 'node:test';
|
||||||
|
import type { XiaobaoRiskSnapshot } from './xiaobao-risk-trend';
|
||||||
|
import {
|
||||||
|
findCachedInsight,
|
||||||
|
upsertDailySnapshot,
|
||||||
|
upsertInsight,
|
||||||
|
type XiaobaoRiskInsightCacheItem,
|
||||||
|
} from './xiaobao-risk-cache';
|
||||||
|
|
||||||
|
const insightA: XiaobaoRiskInsightCacheItem = {
|
||||||
|
versionId: 'v-1',
|
||||||
|
riskSignature: 'sig-a',
|
||||||
|
insight: {
|
||||||
|
summary: '版本风险上升',
|
||||||
|
why: ['剩余工作较多'],
|
||||||
|
forecast: '可能延期 2 天',
|
||||||
|
suggestedActions: ['压缩低优先级范围'],
|
||||||
|
ownerHints: ['请项目负责人确认排期'],
|
||||||
|
generatedAt: '2026-06-29T08:00:00.000Z',
|
||||||
|
},
|
||||||
|
generatedAt: '2026-06-29T08:00:00.000Z',
|
||||||
|
};
|
||||||
|
|
||||||
|
test('findCachedInsight returns matching signature only', () => {
|
||||||
|
const rows: XiaobaoRiskInsightCacheItem[] = [
|
||||||
|
insightA,
|
||||||
|
{ ...insightA, versionId: 'v-2', riskSignature: 'sig-a' },
|
||||||
|
{ ...insightA, versionId: 'v-1', riskSignature: 'sig-b' },
|
||||||
|
];
|
||||||
|
|
||||||
|
assert.equal(findCachedInsight(rows, 'v-1', 'sig-a'), insightA);
|
||||||
|
assert.equal(findCachedInsight(rows, 'v-1', 'sig-b')?.versionId, 'v-1');
|
||||||
|
assert.equal(findCachedInsight(rows, 'v-2', 'sig-b'), undefined);
|
||||||
|
});
|
||||||
|
|
||||||
|
test('upsertDailySnapshot keeps one snapshot per version and date', () => {
|
||||||
|
const existing: XiaobaoRiskSnapshot = {
|
||||||
|
versionId: 'v-1',
|
||||||
|
date: '2026-06-29',
|
||||||
|
riskScore: 40,
|
||||||
|
riskLevel: 'attention',
|
||||||
|
openBugCount: 1,
|
||||||
|
failedTestCount: 0,
|
||||||
|
blockedCount: 0,
|
||||||
|
silentRiskCount: 0,
|
||||||
|
confidence: 80,
|
||||||
|
createdAt: '2026-06-29T08:00:00.000Z',
|
||||||
|
};
|
||||||
|
const replacement: XiaobaoRiskSnapshot = { ...existing, riskScore: 72, createdAt: '2026-06-29T09:00:00.000Z' };
|
||||||
|
const otherDay: XiaobaoRiskSnapshot = { ...existing, date: '2026-06-28', createdAt: '2026-06-28T09:00:00.000Z' };
|
||||||
|
|
||||||
|
const result = upsertDailySnapshot([existing, otherDay], replacement);
|
||||||
|
|
||||||
|
assert.deepEqual(result, [replacement, otherDay]);
|
||||||
|
});
|
||||||
|
|
||||||
|
test('upsertInsight replaces existing version signature pair', () => {
|
||||||
|
const replacement: XiaobaoRiskInsightCacheItem = {
|
||||||
|
...insightA,
|
||||||
|
insight: { ...insightA.insight, summary: '已重新生成' },
|
||||||
|
generatedAt: '2026-06-29T09:00:00.000Z',
|
||||||
|
};
|
||||||
|
const otherSignature: XiaobaoRiskInsightCacheItem = { ...insightA, riskSignature: 'sig-b' };
|
||||||
|
const otherVersion: XiaobaoRiskInsightCacheItem = { ...insightA, versionId: 'v-2' };
|
||||||
|
|
||||||
|
const result = upsertInsight([insightA, otherSignature, otherVersion], replacement);
|
||||||
|
|
||||||
|
assert.deepEqual(result, [replacement, otherSignature, otherVersion]);
|
||||||
|
});
|
||||||
47
apps/web/lib/xiaobao-risk-cache.ts
Normal file
47
apps/web/lib/xiaobao-risk-cache.ts
Normal file
@@ -0,0 +1,47 @@
|
|||||||
|
import type { XiaobaoRiskSnapshot } from './xiaobao-risk-trend';
|
||||||
|
|
||||||
|
export interface XiaobaoRiskInsight {
|
||||||
|
summary: string;
|
||||||
|
why: string[];
|
||||||
|
forecast: string;
|
||||||
|
recommendedReleaseWindow?: string;
|
||||||
|
suggestedActions: string[];
|
||||||
|
ownerHints: string[];
|
||||||
|
generatedAt: string;
|
||||||
|
}
|
||||||
|
|
||||||
|
export interface XiaobaoRiskInsightCacheItem {
|
||||||
|
versionId: string;
|
||||||
|
riskSignature: string;
|
||||||
|
insight: XiaobaoRiskInsight;
|
||||||
|
generatedAt: string;
|
||||||
|
providerInfo?: { providerId?: string; model?: string };
|
||||||
|
}
|
||||||
|
|
||||||
|
export function findCachedInsight(
|
||||||
|
rows: XiaobaoRiskInsightCacheItem[],
|
||||||
|
versionId: string,
|
||||||
|
riskSignature: string,
|
||||||
|
): XiaobaoRiskInsightCacheItem | undefined {
|
||||||
|
return rows.find((row) => row.versionId === versionId && row.riskSignature === riskSignature);
|
||||||
|
}
|
||||||
|
|
||||||
|
export function upsertInsight(
|
||||||
|
rows: XiaobaoRiskInsightCacheItem[],
|
||||||
|
item: XiaobaoRiskInsightCacheItem,
|
||||||
|
): XiaobaoRiskInsightCacheItem[] {
|
||||||
|
return [
|
||||||
|
item,
|
||||||
|
...rows.filter((row) => row.versionId !== item.versionId || row.riskSignature !== item.riskSignature),
|
||||||
|
];
|
||||||
|
}
|
||||||
|
|
||||||
|
export function upsertDailySnapshot(
|
||||||
|
rows: XiaobaoRiskSnapshot[],
|
||||||
|
item: XiaobaoRiskSnapshot,
|
||||||
|
): XiaobaoRiskSnapshot[] {
|
||||||
|
return [
|
||||||
|
item,
|
||||||
|
...rows.filter((row) => row.versionId !== item.versionId || row.date !== item.date),
|
||||||
|
];
|
||||||
|
}
|
||||||
@@ -3,6 +3,7 @@ import type { DevTask } from './dev-task';
|
|||||||
import { STATUS_PROGRESS, getEstimateHours } from './dev-task';
|
import { STATUS_PROGRESS, getEstimateHours } from './dev-task';
|
||||||
import type { TestCase } from './test-case';
|
import type { TestCase } from './test-case';
|
||||||
import { getTestCaseEstimateHours } from './test-case';
|
import { getTestCaseEstimateHours } from './test-case';
|
||||||
|
import type { XiaobaoRiskInsight } from './xiaobao-risk-cache';
|
||||||
import type { VersionDailyEvidence } from './xiaobao-risk-evidence';
|
import type { VersionDailyEvidence } from './xiaobao-risk-evidence';
|
||||||
import { summarizeRiskTrendWithCurrent, type XiaobaoRiskSnapshot } from './xiaobao-risk-trend';
|
import { summarizeRiskTrendWithCurrent, type XiaobaoRiskSnapshot } from './xiaobao-risk-trend';
|
||||||
import { WORK_HOURS, addWorkHours } from './work-hours';
|
import { WORK_HOURS, addWorkHours } from './work-hours';
|
||||||
@@ -61,6 +62,7 @@ export interface XiaobaoVersionRisk {
|
|||||||
dailyEvidence?: VersionDailyEvidence;
|
dailyEvidence?: VersionDailyEvidence;
|
||||||
signals: XiaobaoRiskSignals;
|
signals: XiaobaoRiskSignals;
|
||||||
currentSnapshot: XiaobaoRiskSnapshot;
|
currentSnapshot: XiaobaoRiskSnapshot;
|
||||||
|
aiInsight?: XiaobaoRiskInsight;
|
||||||
trend: {
|
trend: {
|
||||||
direction: 'up' | 'down' | 'flat' | 'unknown';
|
direction: 'up' | 'down' | 'flat' | 'unknown';
|
||||||
delta: number;
|
delta: number;
|
||||||
|
|||||||
55
apps/web/stores/useXiaobaoRiskStore.ts
Normal file
55
apps/web/stores/useXiaobaoRiskStore.ts
Normal file
@@ -0,0 +1,55 @@
|
|||||||
|
'use client';
|
||||||
|
import { create } from 'zustand';
|
||||||
|
import { loadServerData, saveServerData } from '@/lib/server-data';
|
||||||
|
import {
|
||||||
|
upsertDailySnapshot,
|
||||||
|
upsertInsight,
|
||||||
|
type XiaobaoRiskInsightCacheItem,
|
||||||
|
} from '@/lib/xiaobao-risk-cache';
|
||||||
|
import type { XiaobaoRiskSnapshot } from '@/lib/xiaobao-risk-trend';
|
||||||
|
|
||||||
|
interface XiaobaoRiskState {
|
||||||
|
snapshots: XiaobaoRiskSnapshot[];
|
||||||
|
insights: XiaobaoRiskInsightCacheItem[];
|
||||||
|
fetchRiskData: () => Promise<void>;
|
||||||
|
saveSnapshot: (item: XiaobaoRiskSnapshot) => Promise<void>;
|
||||||
|
saveInsight: (item: XiaobaoRiskInsightCacheItem) => Promise<void>;
|
||||||
|
}
|
||||||
|
|
||||||
|
async function loadSnapshots(): Promise<XiaobaoRiskSnapshot[]> {
|
||||||
|
try {
|
||||||
|
const rows = await loadServerData<XiaobaoRiskSnapshot[]>('xiaobao-risk-snapshots');
|
||||||
|
return Array.isArray(rows) ? rows : [];
|
||||||
|
} catch {}
|
||||||
|
return [];
|
||||||
|
}
|
||||||
|
|
||||||
|
async function loadInsights(): Promise<XiaobaoRiskInsightCacheItem[]> {
|
||||||
|
try {
|
||||||
|
const rows = await loadServerData<XiaobaoRiskInsightCacheItem[]>('xiaobao-risk-insights');
|
||||||
|
return Array.isArray(rows) ? rows : [];
|
||||||
|
} catch {}
|
||||||
|
return [];
|
||||||
|
}
|
||||||
|
|
||||||
|
export const useXiaobaoRiskStore = create<XiaobaoRiskState>((set, get) => ({
|
||||||
|
snapshots: [],
|
||||||
|
insights: [],
|
||||||
|
|
||||||
|
fetchRiskData: async () => {
|
||||||
|
const [snapshots, insights] = await Promise.all([loadSnapshots(), loadInsights()]);
|
||||||
|
set({ snapshots, insights });
|
||||||
|
},
|
||||||
|
|
||||||
|
saveSnapshot: async (item) => {
|
||||||
|
const snapshots = upsertDailySnapshot(get().snapshots, item);
|
||||||
|
set({ snapshots });
|
||||||
|
await saveServerData('xiaobao-risk-snapshots', snapshots);
|
||||||
|
},
|
||||||
|
|
||||||
|
saveInsight: async (item) => {
|
||||||
|
const insights = upsertInsight(get().insights, item);
|
||||||
|
set({ insights });
|
||||||
|
await saveServerData('xiaobao-risk-insights', insights);
|
||||||
|
},
|
||||||
|
}));
|
||||||
Reference in New Issue
Block a user