Files

81 lines
2.1 KiB
TypeScript

// app/api/queue.ts
// 队列状态 API 客户端
import axios from 'axios';
import { API_BASE_URL } from '../config/api-config';
interface ApiResult<T> {
code: number;
message: string;
data: T | null;
}
/**
* 队列状态响应接口
*/
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<ApiResult<QueueStatus>>(
`${API_BASE_URL}/api/v2/system/queue/status`,
{ headers }
);
if (response.data?.data) {
return { data: response.data.data };
}
return { error: response.data?.message || '队列状态响应格式错误' };
} 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 : "获取队列状态失败" };
}
}