dd249ccfb7
- getDocumentTypes: switch from PostgREST /document_types to GET /api/document-types - getTodayDocuments: switch from PostgREST /documents to GET /api/documents/list with userId + dateFrom params - getQueueStatus: gracefully handle 404 (endpoint not yet migrated) by returning empty queue state instead of erroring
71 lines
1.9 KiB
TypeScript
71 lines
1.9 KiB
TypeScript
// app/api/queue.ts
|
|
// 队列状态 API 客户端
|
|
|
|
import axios from 'axios';
|
|
import { API_BASE_URL } from '../config/api-config';
|
|
|
|
/**
|
|
* 队列状态响应接口
|
|
*/
|
|
export interface QueueStatus {
|
|
success: boolean;
|
|
timestamp: string;
|
|
queue: {
|
|
pending_tasks: number;
|
|
processing_tasks: number;
|
|
available_slots: number;
|
|
max_concurrent: number;
|
|
};
|
|
documents: {
|
|
waiting: number; // 排队中的文档数
|
|
processing: number; // 处理中的文档数
|
|
processing_ids: string[];
|
|
};
|
|
}
|
|
|
|
/**
|
|
* 获取队列整体状态
|
|
* @returns 队列状态信息
|
|
*/
|
|
export async function getQueueStatus(): Promise<{ data?: QueueStatus; error?: string }> {
|
|
try {
|
|
// 从 localStorage 获取 token
|
|
let token: string | null = null;
|
|
if (typeof window !== 'undefined') {
|
|
token = localStorage.getItem('access_token');
|
|
}
|
|
|
|
const headers: Record<string, string> = {
|
|
'Accept': 'application/json'
|
|
};
|
|
|
|
if (token) {
|
|
headers['Authorization'] = `Bearer ${token}`;
|
|
}
|
|
|
|
const response = await axios.get<QueueStatus>(
|
|
`${API_BASE_URL}/api/v2/system/queue/status`,
|
|
{ headers }
|
|
);
|
|
|
|
return { data: response.data };
|
|
} catch (error) {
|
|
// 队列接口暂未迁移,404 时返回空状态不报错
|
|
if (axios.isAxiosError(error) && error.response?.status === 404) {
|
|
return {
|
|
data: {
|
|
success: true,
|
|
timestamp: new Date().toISOString(),
|
|
queue: { pending_tasks: 0, processing_tasks: 0, available_slots: 0, max_concurrent: 4 },
|
|
documents: { waiting: 0, processing: 0, processing_ids: [] },
|
|
},
|
|
};
|
|
}
|
|
console.error("【队列状态】获取队列状态失败:", error);
|
|
if (axios.isAxiosError(error)) {
|
|
return { error: error.response?.data?.detail || error.message };
|
|
}
|
|
return { error: error instanceof Error ? error.message : "获取队列状态失败" };
|
|
}
|
|
}
|