统一全系统资源加载、轻量趋势与缓存恢复

This commit is contained in:
2026-11-02 09:54:01 +08:00
parent 8b8f9b7728
commit b711327c8e
58 changed files with 3150 additions and 661 deletions
+33 -16
View File
@@ -1,6 +1,8 @@
import { useEffect, useState } from 'react';
import { useCallback, useEffect, useRef, useState } from 'react';
import { Alert, Button, Select, Switch, Tag } from 'antd';
import { api } from './api';
import { usePageCache, useMaintenanceRefresh } from './pageCache';
import { useDelayedBusy } from './useDelayedBusy';
const labels: Record<string, string> = { MONTH: '月报', QUARTER: '季报', YEAR: '年报' };
export function AnalysisReportsPanel({ periodType, periodKey, disabled, onOpen }: {
@@ -10,15 +12,26 @@ export function AnalysisReportsPanel({ periodType, periodKey, disabled, onOpen }
const [plans, setPlans] = useState<Record<string, unknown>[]>([]);
const [error, setError] = useState('');
const [busy, setBusy] = useState(false);
const [reportsLoaded, setReportsLoaded] = useState(false);
const [plansLoaded, setPlansLoaded] = useState(false);
const [expanded, setExpanded] = useState(false);
const { active } = usePageCache();
const read = useDelayedBusy();
const actionInFlight = useRef(false);
const [runDay, setRunDay] = useState(1);
const load = async () => {
try {
const [history, savedPlans] = await Promise.all([api.analysisReports(), api.analysisReportPlans()]);
setReports(history.items || []); setPlans(savedPlans.items || []);
} catch (value) { setError(value instanceof Error ? value.message : '报告读取失败'); }
};
useEffect(() => { void load(); }, []);
const load = useCallback(() => read.run(async () => {
const [history, savedPlans] = await Promise.allSettled([api.analysisReports(), api.analysisReportPlans()]);
const errors: string[] = [];
if (history.status === 'fulfilled') { setReports(history.value.items || []); setReportsLoaded(true); }
else errors.push(`报告列表:${history.reason instanceof Error ? history.reason.message : '读取失败'}`);
if (savedPlans.status === 'fulfilled') { setPlans(savedPlans.value.items || []); setPlansLoaded(true); }
else errors.push(`自动计划:${savedPlans.reason instanceof Error ? savedPlans.reason.message : '读取失败'}`);
setError(errors.join(';'));
}), [read.run]);
useEffect(() => { if (active && expanded && (!reportsLoaded || !plansLoaded)) void load(); }, [active, expanded, reportsLoaded, plansLoaded, load]);
useMaintenanceRefresh(() => { if (expanded) void load(); });
const generate = async () => {
if (actionInFlight.current) return; actionInFlight.current = true;
setBusy(true); setError('');
try {
const response = await api.createAnalysisReport({ periodType, periodKey });
@@ -27,9 +40,10 @@ export function AnalysisReportsPanel({ periodType, periodKey, disabled, onOpen }
if (typeof sessionId === 'string' && typeof turnId === 'string') onOpen(sessionId, turnId);
else setError('报告已登记,正在准备分析任务,请稍后刷新列表。');
} catch (value) { setError(value instanceof Error ? value.message : '报告生成失败'); }
finally { setBusy(false); }
finally { actionInFlight.current = false; setBusy(false); }
};
const toggle = async (type: string, enabled: boolean) => {
if (actionInFlight.current) return; actionInFlight.current = true;
setBusy(true); setError('');
try {
const found = plans.find(plan => plan.periodType === type || plan.reportType === type);
@@ -38,24 +52,27 @@ export function AnalysisReportsPanel({ periodType, periodKey, disabled, onOpen }
else await api.createAnalysisReportPlan(body);
await load();
} catch (value) { setError(value instanceof Error ? value.message : '报告计划保存失败'); }
finally { setBusy(false); }
finally { actionInFlight.current = false; setBusy(false); }
};
const saveDay = async () => {
if (actionInFlight.current) return; actionInFlight.current = true;
setBusy(true); setError('');
try {
await Promise.all(plans.map(plan => api.updateAnalysisReportPlan(String(plan.id), { runDay, runHour: 9 })));
await load();
} catch (value) { setError(value instanceof Error ? value.message : '执行日期保存失败'); }
finally { setBusy(false); }
finally { actionInFlight.current = false; setBusy(false); }
};
return <details className="analysis-reports"><summary>月报、季报与年报</summary>
return <details className="analysis-reports" onToggle={event => setExpanded(event.currentTarget.open)}><summary>月报、季报与年报</summary>
<p>报告使用统一指标与同一分析任务,包含期间比较、趋势和异常追查。自动计划在本机服务运行时执行。</p>
{error && <Alert type="warning" showIcon message={error} />}
{error && <Alert type="error" showIcon message="报告信息读取失败" description={reportsLoaded || plansLoaded ? `${error};成功读取的内容仍保留。` : error} action={!reportsLoaded || !plansLoaded ? <Button size="small" onClick={() => void load()}>重试</Button> : undefined} />}
{read.busy && !reportsLoaded && !plansLoaded && <div className="loading-panel">正在读取报告与自动计划…</div>}
<div className="ai-actions"><span>{periodKey} · {labels[periodType]} · 总体范围</span><Button disabled={disabled || busy} loading={busy} onClick={() => void generate()}>生成本期{labels[periodType]}</Button><Button disabled={busy} onClick={() => void load()}>刷新报告</Button></div>
<div className="analysis-report-plans"><span>自动生成上一完整周期:每个适用周期首月</span><Select aria-label="自动报告执行日" value={runDay} onChange={setRunDay} options={Array.from({ length: 28 }, (_, i) => ({ label: `${i + 1}日 09:00`, value: i + 1 }))} />
{plansLoaded && <div className="analysis-report-plans"><span>自动生成上一完整周期:每个适用周期首月</span><Select aria-label="自动报告执行日" value={runDay} onChange={setRunDay} options={Array.from({ length: 28 }, (_, i) => ({ label: `${i + 1}日 09:00`, value: i + 1 }))} />
{Object.entries(labels).map(([type, label]) => { const plan = plans.find(item => item.periodType === type || item.reportType === type); return <label key={type}>{label} <Switch aria-label={`自动${label}`} disabled={busy} checked={plan?.enabled === true} onChange={value => void toggle(type, value)} /></label>; })}
<Button size="small" disabled={busy || !plans.length} onClick={() => void saveDay()}>保存执行日期</Button>
</div>
{reports.length > 0 && <ul>{reports.slice(0, 8).map(report => <li key={String(report.id)}><span>{String(report.periodKey)} {labels[String(report.periodType)] || ''}</span> <Tag>{String(report.status)}</Tag><Button type="link" disabled={disabled || !report.sessionId || !report.turnId} onClick={() => onOpen(String(report.sessionId), String(report.turnId))}>查看分析</Button></li>)}</ul>}
</div>}
{reportsLoaded && reports.length > 0 && <ul>{reports.slice(0, 8).map(report => <li key={String(report.id)}><span>{String(report.periodKey)} {labels[String(report.periodType)] || ''}</span> <Tag>{String(report.status)}</Tag><Button type="link" disabled={disabled || !report.sessionId || !report.turnId} onClick={() => onOpen(String(report.sessionId), String(report.turnId))}>查看分析</Button></li>)}</ul>}
{reportsLoaded && !reports.length && <p className="completion-note">当前没有已生成的报告。</p>}
</details>;
}