diff --git a/web/src/NoCodeDataWorkflow.tsx b/web/src/NoCodeDataWorkflow.tsx index e957df1..dfd581e 100644 --- a/web/src/NoCodeDataWorkflow.tsx +++ b/web/src/NoCodeDataWorkflow.tsx @@ -8,7 +8,7 @@ import { api, collectionItems } from './api'; import type { AuthUser, SourceItem } from './types'; import type { DataField, DataStage, DataWorkflowOperation, DataWorkflowStatus, DeletionSource, MasterWorkbookKeyFormat, MasterWorkbookKind, MasterWorkbookPreview, SourceDeletionPreview, SourceUploadOptions, WorkbookPreview } from './NoCodeDataTypes'; import { useDataWorkflowOperation } from './useDataWorkflowOperation'; -import { detectSourceWorkbook } from './sourceWorkbookDetection'; +import { detectSourceWorkbook, sourceBatchYear, sourceFilenamePeriod } from './sourceWorkbookDetection'; import { readableSelectPopup } from './readableSelectPopup'; import { usePageCache } from './pageCache'; import { useDelayedBusy, useDelayedIndicator } from './useDelayedBusy'; @@ -39,10 +39,11 @@ function ColumnMatching({ fields, headers, value, onChange, disabled = false }: const workflowStageLabel: Record = { QUEUED: '等待处理', VALIDATING: '检查来源与配置', VERSION_CREATED: '保存更新', REBUILDING: '重新计算报表', PUBLISHING: '更新当前报表', RESTORING_SOURCE: '恢复来源并重算', SOURCE_RESTORED: '来源已恢复并更新报表', COMPLETED: '已完成', FAILED: '处理失败' }; -function WorkflowOperationFeedback({ operation, error, busy, onResume }: { operation: DataWorkflowOperation | null; error: string; busy: boolean; onResume: () => void }) { +function WorkflowOperationFeedback({ operation, error, busy, onResume, onDismiss }: { operation: DataWorkflowOperation | null; error: string; busy: boolean; onResume: () => void; onDismiss: () => void }) { if (!operation && !error) return null; const status = String(operation?.status || '').toUpperCase(); const errors = Array.isArray(operation?.errors) ? operation.errors.map(item => item.message).filter(Boolean) : []; + if (status === 'FAILED') return
{[...new Set([...errors, error].filter(Boolean))].join(';')}{operation?.restoredFromPreviousVisit &&

这是上次操作的历史结果,当前报表以已发布数据为准。详细记录可在“导入记录”中查看。

}} closable onClose={onDismiss} />
; return
{error && 重试读取进度 : undefined} />}
{workflowStageLabel[String(operation?.stage || operation?.status)] || (busy ? '正在处理' : status === 'COMPLETED' ? '报表已更新' : '操作状态')}
@@ -104,14 +105,16 @@ function SimplifiedDataWorkflow({ options, workflowStatus, pendingDeletion, onBu setBusy(true); setError(''); const failures: string[] = []; const known = new Set(drafts.map(item => `${item.file.name}:${item.file.size}:${item.file.lastModified}`)); const selected = files.filter(file => { const key = `${file.name}:${file.size}:${file.lastModified}`; if (known.has(key)) return false; known.add(key); return true; }); + const batchYear = sourceBatchYear([...drafts.map(item => item.file.name), ...selected.map(file => file.name)], new Date().getFullYear()); setReading({ done: 0, total: selected.length }); for (let index = 0; index < selected.length; index += 2) { const results = await Promise.all(selected.slice(index, index + 2).map(async file => { try { const uploaded = await api.previewSourceWorkbook(file); const { workbook: result, stage: detected } = await detectSourceWorkbook(uploaded, options.fields, api.sourceWorkbookPreview); - const nextMonth = Number(file.name.match(/(?:^|\D)(0?[1-9]|1[0-2])月/)?.[1] || new Date().getMonth() + 1); - const nextYear = Number(file.name.match(/(20\d{2})年/)?.[1] || new Date().getFullYear()); + const period = sourceFilenamePeriod(file.name); + const nextMonth = period.month || new Date().getMonth() + 1; + const nextYear = period.year || batchYear; return { id: crypto.randomUUID(), file, workbook: result, stage: detected, year: nextYear, month: nextMonth, sheetName: result.preview.sheetName, headerRow: result.preview.headerRow, fieldMap: matchFields(options.fields[detected] || [], result.preview.headers), mode: 'APPEND' as const, fileId: crypto.randomUUID(), targetId: '', scopeCode: options.scopes[0] || 'ALL' }; } catch (value) { if (!onUnauthorized(value)) failures.push(`${file.name}:${failure(value)}`); return null; } })); @@ -186,8 +189,8 @@ function SimplifiedDataWorkflow({ options, workflowStatus, pendingDeletion, onBu {missing.length > 0 && item.name).join('、')}`} />}
{mode === 'REPLACE' ? '替换后会按当前规则重算并更新对应报表。' : `${year}年${String(month).padStart(2, '0')}月 · ${stageName(stage)} · 完成后自动刷新报表`}{hasImportCapability && }
} - - + +
; } @@ -322,7 +325,7 @@ export function SourceFilesPanel({ user, sources, sourcesLoaded, loading, pendin {simplified && !showDeleted && canOperate && pendingIds.length > 0 &&
} {deletedError && void loadDeleted()}>重试} />} {metadataError && { metadataAttempted.current = true; setMetadataError(''); void loadMetadata(); }}>重试} />} - {(sourceOperation.operation || sourceOperation.error) && } + {(sourceOperation.operation || sourceOperation.error) && } {(!showDeleted && sourcesLoaded) || (showDeleted && (deletedLoaded || deletedRead.busy)) ? size="small" bordered loading={showDeleted ? deletedRead.busy : loading} dataSource={showDeleted ? deleted.map(item => item.source) : sources} rowKey="id" pagination={{ pageSize: 15, showSizeChanger: false }} scroll={{ x: 980 }} columns={[ { title: '来源文件', dataIndex: 'sourceFile', key: 'sourceFile', width: 340, ellipsis: true }, { title: '阶段', dataIndex: 'stage', key: 'stage', align: 'center', width: 90, render: value => {stageName(value)} }, @@ -711,7 +714,7 @@ export function MasterWorkbookUpload({ kind, base, onUnauthorized, onComplete, s setOpen(false)} footer={
{simplified ? '导入后自动保存配置、重算并更新报表。' : result ? '草稿经过校验并启用后,再重新计算和发布数据。' : '导入只创建新草稿,先预览、再确认。'}
{(!result || simplified && Boolean(error || workflowOperation.error)) && }
}> {error && } {workflowOperation.error && 重试读取进度 : undefined} />} - {workflowOperation.operation && } + {workflowOperation.operation && } {result ? <>
新增{txt(summary.added)}
更新{txt(summary.updated)}
停用{txt(summary.disabled)}
合计{txt(summary.total)}
diff --git a/web/src/sourceWorkbookDetection.ts b/web/src/sourceWorkbookDetection.ts index a5876d7..3366407 100644 --- a/web/src/sourceWorkbookDetection.ts +++ b/web/src/sourceWorkbookDetection.ts @@ -2,6 +2,21 @@ import type { DataField, DataStage, WorkbookPreview } from './NoCodeDataTypes'; type Candidate = { workbook: WorkbookPreview; stage: DataStage; score: number; matched: number }; +/** 兼容历史文件的“25.1”年份写法;不把“3.16~3.31”等日期范围当作年份。 */ +export function sourceFilenamePeriod(name: string): { year?: number; month?: number } { + const month = Number(name.match(/(?:^|\D)(0?[1-9]|1[0-2])月/)?.[1]) || undefined; + const full = name.match(/(?:^|\D)(20\d{2})(?:年|[-_.])/); + if (full) return { year: Number(full[1]), month }; + const short = name.match(/统计表[^\d]*([2-9]\d)[..](0?[1-9]|1[0-2])(?:\D|$)/); + return { year: short && month === Number(short[2]) ? 2000 + Number(short[1]) : undefined, month }; +} + +/** 同批文件只有一个明确年份时,将其用于没有年份的其余文件,仍可在列表逐项核对。 */ +export function sourceBatchYear(names: string[], fallback: number): number { + const years = new Set(names.map(name => sourceFilenamePeriod(name).year).filter((year): year is number => year !== undefined)); + return years.size === 1 ? [...years][0] : fallback; +} + function identify(workbook: WorkbookPreview, fields: Record): Candidate { return (['black', 'bright'] as DataStage[]).map(stage => { const required = fields[stage].filter(field => field.required); diff --git a/web/src/useDataWorkflowOperation.ts b/web/src/useDataWorkflowOperation.ts index 9cba55d..832909f 100644 --- a/web/src/useDataWorkflowOperation.ts +++ b/web/src/useDataWorkflowOperation.ts @@ -25,19 +25,24 @@ export function useDataWorkflowOperation(slot: string, onComplete?: (value: Data const terminalHandledRef = useRef(new Set()); const generationRef = useRef(0); const mountedRef = useRef(false); + const restoredIdRef = useRef(''); useEffect(() => { callbackRef.current = onComplete; }, [onComplete]); useEffect(() => { mountedRef.current = true; let pending: StoredOperation | null = null; try { pending = JSON.parse(localStorage.getItem(storageKey(slot)) || 'null') as StoredOperation | null; } catch { pending = null; } - if (pending?.operationId) setOperationId(pending.operationId); + if (pending?.operationId) { restoredIdRef.current = pending.operationId; setOperationId(pending.operationId); } return () => { mountedRef.current = false; generationRef.current++; }; }, [slot]); - const savePendingId = useCallback((value: StoredOperation | null) => { + const savePendingId = useCallback((value: StoredOperation | null, expectedId?: string) => { try { if (value) localStorage.setItem(storageKey(slot), JSON.stringify(value)); - else localStorage.removeItem(storageKey(slot)); + else { + // 另一个标签页可能已经启动新任务,旧任务结束不能清掉它的恢复入口。 + const stored = JSON.parse(localStorage.getItem(storageKey(slot)) || 'null') as StoredOperation | null; + if (!expectedId || stored?.operationId === expectedId) localStorage.removeItem(storageKey(slot)); + } } catch { /* storage may be unavailable in a restricted browser context */ } }, [slot]); @@ -48,15 +53,16 @@ export function useDataWorkflowOperation(slot: string, onComplete?: (value: Data try { const current = await api.dataWorkflowOperation(id); if (!mountedRef.current || generation !== generationRef.current) return; - setOperation(current); + setOperation(restoredIdRef.current === id ? { ...current, restoredFromPreviousVisit: true } : current); setError(''); const status = String(current.status || '').toUpperCase(); if (status === 'COMPLETED' || status === 'FAILED') { terminalHandledRef.current.add(id); generationRef.current++; setOperationId(''); + // 只有未完成任务需要跨刷新恢复;失败详情仍留在本页和服务端历史记录。 + savePendingId(null, id); if (status === 'COMPLETED') { - savePendingId(null); window.dispatchEvent(new CustomEvent(WORKSPACE_INVALIDATED, { detail: { resources: ['sources', 'workflow', 'tasks', 'releases', 'metrics', 'trend', 'records', 'options', 'catalog', 'configuration'] } })); callbackRef.current?.(current); } else setError(operationError(current)); @@ -76,6 +82,12 @@ export function useDataWorkflowOperation(slot: string, onComplete?: (value: Data if (startPromiseRef.current) return startPromiseRef.current; if (isPending(operation)) return operation; const attempt = (async () => { + const previousId = operation?.operationId || restoredIdRef.current; + if (previousId) savePendingId(null, previousId); + generationRef.current++; + restoredIdRef.current = ''; + setOperation(null); + setOperationId(''); setStarting(true); setError(''); try { @@ -109,7 +121,8 @@ export function useDataWorkflowOperation(slot: string, onComplete?: (value: Data const clear = useCallback(() => { generationRef.current++; - setOperation(null); setOperationId(''); setError(''); savePendingId(null); - }, [savePendingId]); + restoredIdRef.current = ''; + setOperation(null); setOperationId(''); setError(''); savePendingId(null, operation?.operationId); + }, [savePendingId, operation?.operationId]); return { operation, error, busy: starting || Boolean(isPending(operation)), start, resume, clear }; } diff --git a/web/verification/source_period_detection.test.mjs b/web/verification/source_period_detection.test.mjs new file mode 100644 index 0000000..c8c187f --- /dev/null +++ b/web/verification/source_period_detection.test.mjs @@ -0,0 +1,20 @@ +import test from 'node:test'; +import assert from 'node:assert/strict'; +import {readFileSync} from 'node:fs'; +import ts from 'typescript'; +const source=ts.transpileModule(readFileSync(new URL('../src/sourceWorkbookDetection.ts',import.meta.url),'utf8'),{compilerOptions:{target:ts.ScriptTarget.ES2022,module:ts.ModuleKind.ESNext}}).outputText.replace(/^import .*;\n/gm,'').replace(/export /g,''); +const {sourceFilenamePeriod:period,sourceBatchYear:year}=new Function(source+';return {sourceFilenamePeriod,sourceBatchYear};')(); +test('actual historical quality workbook names identify 2025 without confusing split-date ranges',()=>{ + assert.deepEqual(period('1月亮片质量数据统计表25.1.xlsx'),{year:2025,month:1}); + assert.deepEqual(period('2月黑片质量数据统计表25.2上.xlsx'),{year:2025,month:2}); + assert.deepEqual(period('7月亮片质量数据统计表25.7.29-7.31.xlsx'),{year:2025,month:7}); + assert.deepEqual(period('3月黑片质量数据统计表3.16~3.31.xlsx'),{year:undefined,month:3}); + assert.deepEqual(period('2025年12月黑片.xlsx'),{year:2025,month:12}); + assert.deepEqual(period('6月质量数据统计表25.5.xlsx'),{year:undefined,month:6}); +}); +test('a consistent batch year also applies to same-batch files without a year; mixed explicit years keep individual identities',()=>{ + assert.equal(year(['1月亮片质量数据统计表25.1.xlsx','12月黑片质量数据统计表12月1日-12月31日.xlsx'],2026),2025); + assert.equal(year(['2025年1月黑片.xlsx','2026年1月黑片.xlsx'],2026),2026); + assert.equal(year(['3月黑片质量数据统计表3.16~3.31.xlsx'],2026),2026); + assert.equal(period('2025年1月黑片.xlsx').year,2025); +}); diff --git a/web/verification/workflow_completion_refresh.test.mjs b/web/verification/workflow_completion_refresh.test.mjs index 8ac8b39..10bde18 100644 --- a/web/verification/workflow_completion_refresh.test.mjs +++ b/web/verification/workflow_completion_refresh.test.mjs @@ -7,15 +7,16 @@ const source = ts.transpileModule(readFileSync(new URL('../src/useDataWorkflowOp const tick = () => new Promise(resolve => setImmediate(resolve)); const deferred = () => { let resolve; const promise = new Promise(done => { resolve = done; }); return { promise, resolve }; }; -function operationHook(api) { - const events = [], callbacks = [], storage = new Map(); +function operationHook(api, restoredOperationId = '') { + const events = [], callbacks = [], storage = new Map(), state = []; + if (restoredOperationId) storage.set('quality-data-workflow:project', JSON.stringify({ operationId: restoredOperationId, startedAt: '2026-11-04T00:00:00Z' })); const window = { localStorage: storage, dispatchEvent: event => events.push(event) }; const localStorage = { getItem: key => storage.get(key) ?? null, setItem: (key, value) => storage.set(key, value), removeItem: key => storage.delete(key) }; - const hooks = { useState: value => [value, () => {}], useRef: current => ({ current }), useCallback: fn => fn, useEffect: fn => { fn(); } }; + const hooks = { useState: value => { const index = state.length; state.push(value); return [value, next => { state[index] = next; }]; }, useRef: current => ({ current }), useCallback: fn => fn, useEffect: fn => { fn(); } }; const load = new Function('api', 'window', 'localStorage', 'WORKSPACE_INVALIDATED', 'usePageCache', 'useVisiblePolling', 'hooks', `const {useState,useRef,useCallback,useEffect}=hooks;${source};return useDataWorkflowOperation;`); const useDataWorkflowOperation = load(api, window, localStorage, 'quality-workspace-invalidated', () => ({ active: true }), () => {}, hooks); const hook = useDataWorkflowOperation('project', value => callbacks.push(value)); - return { hook, events, callbacks, storage }; + return { hook, events, callbacks, storage, state }; } test('a completed workflow invalidates retained data exactly once after the server marks it complete', async () => { @@ -39,14 +40,34 @@ test('a completed workflow invalidates retained data exactly once after the serv assert.equal(events.length, 1, 'a terminal result cannot invalidate a second time'); }); -test('failed workflows never invalidate published data and keep the operation id available for recovery', async () => { +test('a cached failed operation is explicitly marked as historical and is not restored again on the next visit', async () => { + const { hook, storage, state } = operationHook({ dataWorkflowOperation: async () => ({ operationId: 'old-job', status: 'FAILED', errors: [{ message: 'old failure' }] }) }, 'old-job'); + hook.resume(); await tick(); await tick(); + assert.equal(state[0].status, 'FAILED'); + assert.equal(state[0].restoredFromPreviousVisit, true); + assert.equal(state[2], 'old failure'); + assert.equal(storage.size, 0); +}); + +test('an old terminal response cannot remove a newer task saved by another tab', async () => { + const final = deferred(); + const { hook, storage } = operationHook({ dataWorkflowOperation: async () => final.promise }); + await hook.start(async () => ({ operationId: 'old-job', status: 'QUEUED' })); + hook.resume(); await tick(); + storage.set('quality-data-workflow:project', JSON.stringify({ operationId: 'new-job' })); + final.resolve({ operationId: 'old-job', status: 'FAILED', errors: [{ message: 'old failure' }] }); + await tick(); await tick(); + assert.equal(JSON.parse(storage.get('quality-data-workflow:project')).operationId, 'new-job'); +}); + +test('failed workflows keep their visible error but remove the pending pointer so another visit does not revive an old failure', async () => { let calls = 0; const { hook, events, callbacks, storage } = operationHook({ dataWorkflowOperation: async () => { calls++; return { operationId: 'job', status: 'FAILED', errors: [{ message: 'rejected' }] }; } }); await hook.start(async () => ({ operationId: 'job', status: 'QUEUED' })); hook.resume(); await tick(); await tick(); assert.deepEqual(events, []); assert.deepEqual(callbacks, []); - assert.equal(storage.size, 1); + assert.equal(storage.size, 0, 'only unfinished operations belong in reload recovery storage'); hook.resume(); await tick(); assert.equal(calls, 1, 'a terminal failure is not polled repeatedly'); assert.deepEqual(events, []); diff --git a/web/verification/workflow_failure_feedback.test.mjs b/web/verification/workflow_failure_feedback.test.mjs new file mode 100644 index 0000000..5bf8b27 --- /dev/null +++ b/web/verification/workflow_failure_feedback.test.mjs @@ -0,0 +1,15 @@ +import test from 'node:test'; +import assert from 'node:assert/strict'; +import {readFileSync} from 'node:fs'; +import ts from 'typescript'; +import React from 'react'; +const source=readFileSync(new URL('../src/NoCodeDataWorkflow.tsx',import.meta.url),'utf8'); +const body=source.slice(source.indexOf('function WorkflowOperationFeedback('),source.indexOf('export type PendingSourceDeletion')); +const code=ts.transpileModule(body+'\nreturn WorkflowOperationFeedback;', {compilerOptions:{target:ts.ScriptTarget.ES2022,jsx:ts.JsxEmit.React}}).outputText; +const Feedback=new Function('React','Alert','Button','Progress','workflowStageLabel',code)(React,'Alert','Button','Progress',{FAILED:'处理失败'}); +function nodes(element){if(!React.isValidElement(element))return [];return [element,...React.Children.toArray(element.props.children).flatMap(nodes)];} +for(const restored of [false,true])test(restored?'historical failure is labelled and dismissible without a misleading processing progress bar':'new failure retains its concrete reason and can be dismissed',()=>{ + let dismissed=false; + const tree=Feedback({operation:{operationId:'job',status:'FAILED',stage:'FAILED',restoredFromPreviousVisit:restored,errors:[{message:'具体校验原因'}]},error:'具体校验原因',busy:false,onResume(){throw Error('terminal failure must not offer progress retry');},onDismiss(){dismissed=true;}}); + const all=nodes(tree),alerts=all.filter(n=>n.type==='Alert');assert.equal(alerts.length,1);assert.equal(alerts[0].props.message,restored?'上次操作未完成':'本次操作未完成');assert.ok(JSON.stringify(alerts[0].props.description).includes('具体校验原因'));assert.equal(all.filter(n=>n.type==='Progress').length,0);alerts[0].props.onClose();assert.equal(dismissed,true); +});