统一全系统资源加载、轻量趋势与缓存恢复
This commit is contained in:
+218
-48
@@ -1,7 +1,15 @@
|
||||
import { WORKSPACE_INVALIDATED } from './pageCache';
|
||||
import { consumeEventStream, type StreamEvent } from './eventStream';
|
||||
import { publicErrorMessage } from './publicDataDisplay';
|
||||
import { RequestTimeoutError, withRequestDeadline } from './requestDeadline';
|
||||
import {
|
||||
clearReadCache,
|
||||
configureRequestScope,
|
||||
coordinateRequest,
|
||||
getRequestDiagnostics,
|
||||
invalidateReadResources,
|
||||
resetRequestScope,
|
||||
type CoordinatedResult,
|
||||
} from './requestCoordinator';
|
||||
import type {
|
||||
AuthUser,
|
||||
FilterField,
|
||||
@@ -9,6 +17,7 @@ import type {
|
||||
Collection,
|
||||
FineDefectTopResponse,
|
||||
MetricsResponse,
|
||||
TrendResponse,
|
||||
OptionsResponse,
|
||||
QueryParams,
|
||||
RecordExportParams,
|
||||
@@ -17,6 +26,8 @@ import type {
|
||||
} from './types';
|
||||
import type { ConfigureSourceBody, DataStage, DataWorkflowOperation, DataWorkflowStatus, MasterWorkbookImportBody, MasterWorkbookPreview, SourceDeletionPreview, SourceUploadOptions, WorkbookPreview } from './NoCodeDataTypes';
|
||||
|
||||
export { configureRequestScope, getRequestDiagnostics, invalidateReadResources, resetRequestScope };
|
||||
|
||||
export class ApiError extends Error {
|
||||
status: number;
|
||||
details: unknown;
|
||||
@@ -39,8 +50,9 @@ async function fetchApi(path: string, init: RequestInit): Promise<Response> {
|
||||
}
|
||||
}
|
||||
|
||||
async function readResponse<T>(response: Response): Promise<T> {
|
||||
async function readResponse<T>(response: Response): Promise<CoordinatedResult<T>> {
|
||||
const raw = await response.text();
|
||||
const responseBytes = new TextEncoder().encode(raw).byteLength;
|
||||
let payload: unknown;
|
||||
if (raw) {
|
||||
try {
|
||||
@@ -61,43 +73,83 @@ async function readResponse<T>(response: Response): Promise<T> {
|
||||
: typeof payload === 'string' && payload.trim()
|
||||
? payload
|
||||
: `请求失败(${response.status})`;
|
||||
throw new ApiError(response.status, message, payload);
|
||||
const error = new ApiError(response.status, message, payload);
|
||||
Object.defineProperty(error, 'responseBytes', { value: responseBytes });
|
||||
throw error;
|
||||
}
|
||||
if (typeof payload === 'string') throw new ApiError(response.status, '服务返回格式异常,请稍后重试。');
|
||||
return payload as T;
|
||||
if (typeof payload === 'string') {
|
||||
const error = new ApiError(response.status, '服务返回格式异常,请稍后重试。');
|
||||
Object.defineProperty(error, 'responseBytes', { value: responseBytes });
|
||||
throw error;
|
||||
}
|
||||
return { data: payload as T, status: response.status, bytes: responseBytes };
|
||||
}
|
||||
|
||||
async function request<T>(path: string, init: RequestInit = {}, timeoutMs?: number): Promise<T> {
|
||||
export async function request<T>(path: string, init: RequestInit = {}, timeoutMs?: number): Promise<T> {
|
||||
const headers = new Headers(init.headers);
|
||||
if (init.body && !(init.body instanceof FormData) && !headers.has('Content-Type')) headers.set('Content-Type', 'application/json');
|
||||
const send = async (signal = init.signal) => {
|
||||
const response = await fetchApi(path, { ...init, signal, headers, credentials: 'same-origin', cache: 'no-store' });
|
||||
return readResponse<T>(response);
|
||||
};
|
||||
if (!timeoutMs) return send();
|
||||
try { return await withRequestDeadline(signal => send(signal), timeoutMs); }
|
||||
const method = (init.method ?? 'GET').toUpperCase();
|
||||
try {
|
||||
return await coordinateRequest({
|
||||
url: path,
|
||||
method,
|
||||
signal: init.signal ?? undefined,
|
||||
timeoutMs: timeoutMs ?? requestTimeoutFor(path),
|
||||
cache: method === 'GET' && isImmutableRead(path),
|
||||
tags: requestResourceTags(path),
|
||||
category: requestCategory(path),
|
||||
priority: requestPriority(path),
|
||||
execute: async signal => {
|
||||
const response = await fetchApi(path, { ...init, signal, headers, credentials: 'same-origin', cache: 'no-store' });
|
||||
return readResponse<T>(response);
|
||||
},
|
||||
});
|
||||
}
|
||||
catch (error) {
|
||||
if (error instanceof RequestTimeoutError) throw new ApiError(408, '连接质量数据服务超时,请点击重新连接;服务恢复后可继续使用。');
|
||||
handleAccessFailure(error);
|
||||
if (error instanceof Error && error.name === 'RequestTimeoutError') throw new ApiError(408, '连接质量数据服务超时,请点击重新连接;服务恢复后可继续使用。');
|
||||
throw error;
|
||||
}
|
||||
}
|
||||
|
||||
async function requestBlob(path: string, signal?: AbortSignal): Promise<{ blob: Blob; filename: string }> {
|
||||
const response = await fetchApi(path, { credentials: 'same-origin', cache: 'no-store', signal });
|
||||
if (!response.ok) {
|
||||
const raw = await response.text();
|
||||
let payload: unknown = raw;
|
||||
try { payload = raw ? JSON.parse(raw) as unknown : undefined; } catch { /* keep plain text */ }
|
||||
const body = payload && typeof payload === 'object' ? payload as Record<string, unknown> : undefined;
|
||||
const message = typeof body?.detail === 'string' ? body.detail : typeof body?.message === 'string' ? body.message : `下载失败(${response.status})`;
|
||||
throw new ApiError(response.status, message, payload);
|
||||
try {
|
||||
return await coordinateRequest({
|
||||
url: path,
|
||||
method: 'GET',
|
||||
signal,
|
||||
timeoutMs: 900_000,
|
||||
queue: false,
|
||||
retry: false,
|
||||
tags: requestResourceTags(path),
|
||||
category: 'export',
|
||||
priority: 'active',
|
||||
execute: async requestSignal => {
|
||||
const response = await fetchApi(path, { credentials: 'same-origin', cache: 'no-store', signal: requestSignal });
|
||||
if (!response.ok) {
|
||||
const raw = await response.text();
|
||||
let payload: unknown = raw;
|
||||
try { payload = raw ? JSON.parse(raw) as unknown : undefined; } catch { /* keep plain text */ }
|
||||
const body = payload && typeof payload === 'object' ? payload as Record<string, unknown> : undefined;
|
||||
const message = typeof body?.detail === 'string' ? body.detail : typeof body?.message === 'string' ? body.message : `下载失败(${response.status})`;
|
||||
const error = new ApiError(response.status, message, payload);
|
||||
Object.defineProperty(error, 'responseBytes', { value: new TextEncoder().encode(raw).byteLength });
|
||||
throw error;
|
||||
}
|
||||
const disposition = response.headers.get('Content-Disposition') || '';
|
||||
const encoded = disposition.match(/filename\*=UTF-8''([^;]+)/i)?.[1];
|
||||
const plain = disposition.match(/filename="?([^";]+)"?/i)?.[1];
|
||||
let filename = 'source-evidence.xlsx';
|
||||
try { filename = encoded ? decodeURIComponent(encoded) : plain || filename; } catch { filename = plain || filename; }
|
||||
const blob = await response.blob();
|
||||
return { data: { blob, filename }, status: response.status, bytes: blob.size };
|
||||
},
|
||||
});
|
||||
} catch (error) {
|
||||
handleAccessFailure(error);
|
||||
if (error instanceof Error && error.name === 'RequestTimeoutError') throw new ApiError(408, '文件导出超时,请稍后重试。');
|
||||
throw error;
|
||||
}
|
||||
const disposition = response.headers.get('Content-Disposition') || '';
|
||||
const encoded = disposition.match(/filename\*=UTF-8''([^;]+)/i)?.[1];
|
||||
const plain = disposition.match(/filename="?([^";]+)"?/i)?.[1];
|
||||
let filename = 'source-evidence.xlsx';
|
||||
try { filename = encoded ? decodeURIComponent(encoded) : plain || filename; } catch { filename = plain || filename; }
|
||||
return { blob: await response.blob(), filename };
|
||||
}
|
||||
|
||||
export type { StreamEvent } from './eventStream';
|
||||
@@ -105,16 +157,21 @@ export type { StreamEvent } from './eventStream';
|
||||
async function readEventStream(path: string, options: { signal?: AbortSignal; lastEventId?: string; onEvent: (event: StreamEvent) => void }): Promise<void> {
|
||||
const headers = new Headers({ Accept: 'text/event-stream' });
|
||||
if (options.lastEventId) headers.set('Last-Event-ID', options.lastEventId);
|
||||
const response = await fetchApi(path, { headers, credentials: 'same-origin', cache: 'no-store', signal: options.signal });
|
||||
if (!response.ok) {
|
||||
const raw = await response.text();
|
||||
let payload: unknown = raw;
|
||||
try { payload = raw ? JSON.parse(raw) as unknown : undefined; } catch { /* keep plain text */ }
|
||||
const body = payload && typeof payload === 'object' ? payload as Record<string, unknown> : undefined;
|
||||
throw new ApiError(response.status, typeof body?.detail === 'string' ? body.detail : typeof body?.message === 'string' ? body.message : `事件流读取失败(${response.status})`, payload);
|
||||
try {
|
||||
const response = await fetchApi(path, { headers, credentials: 'same-origin', cache: 'no-store', signal: options.signal });
|
||||
if (!response.ok) {
|
||||
const raw = await response.text();
|
||||
let payload: unknown = raw;
|
||||
try { payload = raw ? JSON.parse(raw) as unknown : undefined; } catch { /* keep plain text */ }
|
||||
const body = payload && typeof payload === 'object' ? payload as Record<string, unknown> : undefined;
|
||||
throw new ApiError(response.status, typeof body?.detail === 'string' ? body.detail : typeof body?.message === 'string' ? body.message : `事件流读取失败(${response.status})`, payload);
|
||||
}
|
||||
if (!response.body) throw new ApiError(response.status, '浏览器没有收到可读的分析事件流');
|
||||
await consumeEventStream(response.body, options.onEvent);
|
||||
} catch (error) {
|
||||
handleAccessFailure(error);
|
||||
throw error;
|
||||
}
|
||||
if (!response.body) throw new ApiError(response.status, '浏览器没有收到可读的分析事件流');
|
||||
await consumeEventStream(response.body, options.onEvent);
|
||||
}
|
||||
|
||||
export async function refreshCsrf(): Promise<string> {
|
||||
@@ -131,7 +188,7 @@ async function mutate<T>(path: string, body?: unknown, method = 'POST', timeoutM
|
||||
headers,
|
||||
...(body === undefined ? {} : { body: JSON.stringify(body) }),
|
||||
}, timeoutMs);
|
||||
if (!silent && !/^\/api\/(?:analysis|auth)(?:\/|$)/.test(path)) window.dispatchEvent(new Event(WORKSPACE_INVALIDATED));
|
||||
if (!silent && !/^\/api\/(?:analysis|auth)(?:\/|$)/.test(path)) invalidateAfterMutation(path);
|
||||
return result;
|
||||
}
|
||||
|
||||
@@ -142,17 +199,17 @@ async function queryPost<T>(path: string, body: unknown, signal?: AbortSignal):
|
||||
|
||||
async function mutateForm<T>(path: string, body: FormData): Promise<T> {
|
||||
const token = csrfToken ?? await refreshCsrf();
|
||||
const result = await request<T>(path, { method: 'POST', headers: { 'X-XSRF-TOKEN': token }, body });
|
||||
window.dispatchEvent(new Event(WORKSPACE_INVALIDATED));
|
||||
const result = await request<T>(path, { method: 'POST', headers: { 'X-XSRF-TOKEN': token }, body }, 900_000);
|
||||
invalidateAfterMutation(path);
|
||||
return result;
|
||||
}
|
||||
|
||||
export const api = {
|
||||
sourceColumnCatalog(releaseId?: string) { return request<{releaseId:string;sourceFields:SourceColumnField[]}>(`/api/analysis/source/catalog?${toQuery({releaseId})}`); },
|
||||
sourceColumnCatalog(releaseId?: string, signal?: AbortSignal) { return request<{releaseId:string;sourceFields:SourceColumnField[]}>(`/api/analysis/source/catalog?${toQuery({releaseId})}`, { signal }); },
|
||||
sourceColumnValues(body: Record<string,unknown>, signal?: AbortSignal) { return queryPost<{values:Array<string|null>;hasMore?:boolean;total?:number;limit?:number}>('/api/analysis/source/values',body,signal); },
|
||||
workspaceState() { return request<{ authRevision: string; dataRevision: string }>('/api/workspace/state', {}, 8000); },
|
||||
filterFields(manage = false) { return request<FilterField[]>(`/api/filter-fields?manage=${manage}`); },
|
||||
filterFieldOptions(key: string, search = '') { return request<{ values: string[]; hasMore: boolean }>(`/api/filter-fields/options?${toQuery({key, search})}`); },
|
||||
workspaceState() { return request<{ authRevision: string; dataRevision: string; currentReleaseId: string | null }>('/api/workspace/state', {}, 8000); },
|
||||
filterFields(manage = false, signal?: AbortSignal) { return request<FilterField[]>(`/api/filter-fields?manage=${manage}`, { signal }); },
|
||||
filterFieldOptions(key: string, search = '', signal?: AbortSignal) { return request<{ values: string[]; hasMore: boolean }>(`/api/filter-fields/options?${toQuery({key, search})}`, { signal }); },
|
||||
filterFieldColumns() { return request<{ black: string[]; bright: string[] }>('/api/filter-fields/columns'); },
|
||||
saveFilterFields(fields: FilterField[]) { return mutate<FilterField[]>('/api/filter-fields', fields, 'PUT'); },
|
||||
refreshCsrf,
|
||||
@@ -160,17 +217,20 @@ export const api = {
|
||||
const result = await request<{ user: AuthUser }>('/api/auth/me', {}, 8000);
|
||||
return result.user;
|
||||
},
|
||||
login(username: string, password: string) {
|
||||
async login(username: string, password: string) {
|
||||
csrfToken = undefined;
|
||||
resetRequestScope();
|
||||
return mutate<{ user: AuthUser }>('/api/auth/login', { username, password });
|
||||
},
|
||||
logout() {
|
||||
return mutate<void>('/api/auth/logout');
|
||||
async logout() {
|
||||
try { return await mutate<void>('/api/auth/logout'); }
|
||||
finally { csrfToken = undefined; resetRequestScope(); }
|
||||
},
|
||||
changePassword(currentPassword: string, newPassword: string) {
|
||||
return mutate<{ success: boolean }>('/api/auth/change-password', { currentPassword, newPassword });
|
||||
},
|
||||
options() {
|
||||
return request<OptionsResponse>('/api/options');
|
||||
options(signal?: AbortSignal) {
|
||||
return request<OptionsResponse>('/api/options', { signal });
|
||||
},
|
||||
referenceNames() {
|
||||
return request<Pick<OptionsResponse, 'departments' | 'groups' | 'products' | 'projectGroups' | 'groupDepartments'>>('/api/options/reference-names');
|
||||
@@ -178,6 +238,9 @@ export const api = {
|
||||
metrics(params: QueryParams, signal?: AbortSignal) {
|
||||
return request<MetricsResponse>(`/api/metrics?${toQuery(params)}`, { signal });
|
||||
},
|
||||
trend(params: QueryParams, signal?: AbortSignal) {
|
||||
return request<TrendResponse>(`/api/metrics/trend?${toQuery(params)}`, { signal });
|
||||
},
|
||||
fineDefectTop(params: Record<string, unknown>, signal?: AbortSignal) {
|
||||
return queryPost<FineDefectTopResponse>('/api/analysis/defects', params, signal);
|
||||
},
|
||||
@@ -690,6 +753,113 @@ function toQuery(values: object): string {
|
||||
return query.toString();
|
||||
}
|
||||
|
||||
function requestPath(path: string): string {
|
||||
const base = typeof location === 'undefined' ? 'http://localhost' : location.origin;
|
||||
return new URL(path, base).pathname;
|
||||
}
|
||||
|
||||
function requestTimeoutFor(path: string): number | undefined {
|
||||
const pathname = requestPath(path);
|
||||
return pathname === '/api/workspace/state' || pathname === '/api/auth/me' || pathname === '/api/auth/csrf' ? 8_000 : undefined;
|
||||
}
|
||||
|
||||
function isImmutableRead(path: string): boolean {
|
||||
const pathname = requestPath(path);
|
||||
if (pathname === '/api/metrics' || pathname === '/api/metrics/trend') return true;
|
||||
if (pathname === '/api/options' || pathname === '/api/options/reference-names') return true;
|
||||
if (pathname === '/api/analysis/source/catalog' || pathname === '/api/rules/schema') return true;
|
||||
if (/^\/api\/governance\/releases\/[^/]+\/(?:inspect|diff)$/.test(pathname)) return true;
|
||||
if (pathname === '/api/governance/coverage') {
|
||||
const url = new URL(path, typeof location === 'undefined' ? 'http://localhost' : location.origin);
|
||||
return Boolean(url.searchParams.get('releaseId'));
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
function requestResourceTags(path: string): string[] {
|
||||
const pathname = requestPath(path);
|
||||
if (pathname.startsWith('/api/metrics')) return ['metrics', 'trend', 'query'];
|
||||
if (pathname.startsWith('/api/options')) return ['options', 'query'];
|
||||
if (pathname.startsWith('/api/filter-fields') || pathname === '/api/analysis/source/catalog') return ['options', 'catalog', 'configuration'];
|
||||
if (pathname.startsWith('/api/records')) return ['records', 'query'];
|
||||
if (pathname.startsWith('/api/analytics/targets')) return ['targets', 'query'];
|
||||
if (pathname.startsWith('/api/sources')) return ['sources', 'workflow', 'catalog'];
|
||||
if (pathname.startsWith('/api/data-workflow')) return ['workflow', 'sources', 'tasks', 'releases'];
|
||||
if (pathname.startsWith('/api/tasks') || pathname.startsWith('/api/import-runs')) return ['tasks', 'workflow'];
|
||||
if (pathname.startsWith('/api/analysis/')) return ['analysis', 'reports', 'options', 'catalog'];
|
||||
if (pathname.startsWith('/api/rules') || pathname.startsWith('/api/mappings') || pathname.startsWith('/api/governance/mapping-issues')) return ['configuration', 'metrics', 'trend', 'records', 'options', 'catalog'];
|
||||
if (pathname.startsWith('/api/governance/backups')) return ['backups', 'monitor', 'workflow'];
|
||||
if (pathname.startsWith('/api/governance/monitor')) return ['monitor', 'workflow'];
|
||||
if (pathname.startsWith('/api/governance/')) return ['configuration', 'workflow', 'sources', 'releases'];
|
||||
if (pathname.startsWith('/api/analytics/')) return ['configuration', 'targets'];
|
||||
if (pathname.startsWith('/api/users') || pathname.startsWith('/api/audit') || pathname.includes('/access-policy') || pathname.includes('/effective-access') || pathname.includes('/scope-preview') || pathname.includes('/service-identities') || pathname.includes('/service-bindings')) return ['access', 'users', 'audit', 'policy'];
|
||||
return [];
|
||||
}
|
||||
|
||||
function requestCategory(path: string): string {
|
||||
const pathname = requestPath(path);
|
||||
if (pathname === '/api/workspace/state') return 'workspace';
|
||||
if (pathname.startsWith('/api/auth/')) return 'auth';
|
||||
if (pathname.startsWith('/api/metrics')) return 'metrics';
|
||||
if (pathname.includes('/export') || pathname.endsWith('/download')) return 'export';
|
||||
if (pathname.startsWith('/api/analysis/')) return 'analysis';
|
||||
if (pathname.startsWith('/api/governance/')) return 'governance';
|
||||
if (pathname.startsWith('/api/tasks') || pathname.startsWith('/api/import-runs')) return 'tasks';
|
||||
if (pathname.startsWith('/api/sources')) return 'sources';
|
||||
if (pathname.startsWith('/api/analytics/')) return 'analytics';
|
||||
return 'other';
|
||||
}
|
||||
|
||||
function requestPriority(path: string): 'active' | 'background' {
|
||||
const pathname = requestPath(path);
|
||||
return pathname.startsWith('/api/data-workflow/operations/') || pathname.startsWith('/api/import-runs/') || pathname.startsWith('/api/tasks/')
|
||||
? 'background'
|
||||
: 'active';
|
||||
}
|
||||
|
||||
function mutationResources(path: string): string[] | null {
|
||||
const pathname = requestPath(path);
|
||||
if (pathname.startsWith('/api/auth/') || pathname.startsWith('/api/analysis/')) return [];
|
||||
if (pathname.startsWith('/api/users') || pathname.startsWith('/api/audit') || pathname.includes('/access-policy') || pathname.includes('/effective-access') || pathname.includes('/scope-preview') || pathname.includes('/service-identities') || pathname.includes('/service-bindings')) return ['*'];
|
||||
if (pathname.startsWith('/api/sources') || pathname.startsWith('/api/releases') || pathname.startsWith('/api/data-workflow') || pathname.startsWith('/api/import-runs')) {
|
||||
return ['metrics', 'trend', 'records', 'options', 'catalog', 'sources', 'workflow', 'tasks', 'releases', 'configuration'];
|
||||
}
|
||||
if (pathname.startsWith('/api/filter-fields')) return ['options', 'catalog', 'query', 'configuration'];
|
||||
if (pathname.startsWith('/api/rules') || pathname.startsWith('/api/mappings') || pathname.startsWith('/api/governance/mapping-issues') || pathname.startsWith('/api/metric-dictionary')) {
|
||||
return ['metrics', 'trend', 'records', 'options', 'catalog', 'configuration'];
|
||||
}
|
||||
if (pathname.startsWith('/api/tasks')) return ['tasks', 'workflow'];
|
||||
if (pathname.startsWith('/api/governance/backups')) return ['backups', 'monitor', 'workflow', 'configuration'];
|
||||
if (pathname.startsWith('/api/governance/monitor')) return ['monitor', 'workflow', 'configuration'];
|
||||
if (pathname.startsWith('/api/analytics/targets')) return ['targets', 'configuration'];
|
||||
if (pathname.startsWith('/api/analytics/')) return ['configuration'];
|
||||
if (pathname.startsWith('/api/governance/')) return ['configuration', 'workflow', 'sources', 'releases'];
|
||||
if (pathname.startsWith('/api/')) return null;
|
||||
return null;
|
||||
}
|
||||
|
||||
function invalidateAfterMutation(path: string): void {
|
||||
const resources = mutationResources(path);
|
||||
if (resources?.length === 0) return;
|
||||
const invalidated = resources ?? ['*'];
|
||||
invalidateReadResources(resources?.includes('*') ? undefined : resources ?? undefined);
|
||||
window.dispatchEvent(new CustomEvent(WORKSPACE_INVALIDATED, { detail: { resources: invalidated } }));
|
||||
}
|
||||
|
||||
function hasStatus(error: unknown, status: number): boolean {
|
||||
return !!error && typeof error === 'object' && (error as { status?: unknown }).status === status;
|
||||
}
|
||||
|
||||
function handleAccessFailure(error: unknown): void {
|
||||
if (hasStatus(error, 401)) {
|
||||
csrfToken = undefined;
|
||||
resetRequestScope();
|
||||
} else if (hasStatus(error, 403)) {
|
||||
clearReadCache();
|
||||
window.dispatchEvent(new Event('quality-permission-recheck'));
|
||||
}
|
||||
}
|
||||
|
||||
const MULTI_VALUE_MARKER = '~qv1~';
|
||||
|
||||
/** Encodes multiple selected values into the scalar query wire format understood by the API. */
|
||||
|
||||
Reference in New Issue
Block a user