204 lines
6.9 KiB
TypeScript
204 lines
6.9 KiB
TypeScript
import { getAuthHeaders } from '../utils/auth';
|
|
import { requestJson } from './client';
|
|
|
|
export interface ScheduleTask {
|
|
id: number;
|
|
schedule_no: string;
|
|
name: string;
|
|
schedule_type: string;
|
|
owner_user_id: number;
|
|
owner_username?: string | null;
|
|
cron_expr: string;
|
|
wait_seconds: number;
|
|
dify_agent_id?: number | null;
|
|
platform_api_path?: string | null;
|
|
data_source_type?: string;
|
|
kb_sync_enabled?: boolean;
|
|
kb_group_id?: number | null;
|
|
notify_user_ids_json: number[];
|
|
notify_enabled: boolean;
|
|
enabled: boolean;
|
|
next_run_at?: string | null;
|
|
last_run_at?: string | null;
|
|
last_status?: string | null;
|
|
created_at?: string | null;
|
|
updated_at?: string | null;
|
|
}
|
|
|
|
export interface ScheduleRun {
|
|
id: number;
|
|
run_no: string;
|
|
schedule_task_id: number;
|
|
schedule_name?: string | null;
|
|
trigger_type: string;
|
|
started_at?: string | null;
|
|
finished_at?: string | null;
|
|
duration_ms: number;
|
|
status: string;
|
|
total_count: number;
|
|
success_count: number;
|
|
failed_count: number;
|
|
|
|
api_request_json?: Record<string, any> | null;
|
|
api_response_json?: Record<string, any> | null;
|
|
response_summary_json?: Record<string, any> | null;
|
|
step_logs?: StepLog[] | null;
|
|
dify_execution_snapshot?: DifyExecutionSnapshot | null;
|
|
error_message?: string | null;
|
|
excel_file_name?: string | null;
|
|
excel_file_size?: number | null;
|
|
kb_sync_status?: string | null;
|
|
kb_group_id?: number | null;
|
|
kb_document_id?: number | null;
|
|
kb_document_version_id?: number | null;
|
|
kb_sync_job_id?: number | null;
|
|
kb_sync_error?: string | null;
|
|
wecom_notified: boolean;
|
|
wecom_notified_at?: string | null;
|
|
wecom_notify_error?: string | null;
|
|
created_at?: string | null;
|
|
}
|
|
|
|
export interface StepLog {
|
|
step: string;
|
|
status: string;
|
|
started_at?: string;
|
|
finished_at?: string;
|
|
duration_ms?: number;
|
|
row_id?: string;
|
|
detail?: Record<string, any>;
|
|
}
|
|
|
|
export interface DifyExecutionSnapshot {
|
|
dify_enabled?: boolean;
|
|
dify_agent_id?: number | null;
|
|
dify_agent_name?: string | null;
|
|
dify_app_id?: string | null;
|
|
dify_system_prompt?: string;
|
|
captured_at?: string;
|
|
}
|
|
|
|
async function getJson<T>(url: string): Promise<T> {
|
|
return requestJson<T>(url, { headers: getAuthHeaders() });
|
|
}
|
|
|
|
export const listAdminSchedules = () =>
|
|
getJson<ScheduleTask[]>('/api/admin/schedules');
|
|
|
|
export const getAdminSchedule = (taskId: number) =>
|
|
getJson<ScheduleTask>(`/api/admin/schedules/${taskId}`);
|
|
|
|
export const exportAdminScheduleUrl = (taskId: number) => `/api/admin/schedules/${taskId}/export`;
|
|
|
|
export const importAdminSchedules = async (payload: any) => {
|
|
return requestJson('/api/admin/schedules/import', {
|
|
method: 'POST',
|
|
headers: { 'Content-Type': 'application/json', ...getAuthHeaders() },
|
|
body: JSON.stringify(payload),
|
|
});
|
|
};
|
|
|
|
export const getAdminScheduleTaskConfig = (taskId: number) =>
|
|
getJson<{ data_source_type: string; config: Record<string, any> | null }>(`/api/admin/schedule-config/${taskId}`);
|
|
|
|
export const updateAdminScheduleTaskConfig = (taskId: number, payload: Record<string, any>) =>
|
|
requestJson(`/api/admin/schedule-config/${taskId}`, {
|
|
method: 'PUT',
|
|
headers: { 'Content-Type': 'application/json', ...getAuthHeaders() },
|
|
body: JSON.stringify(payload),
|
|
});
|
|
|
|
export interface TestScheduleSqlResult {
|
|
total: number;
|
|
showing: number;
|
|
limit: number;
|
|
sql: string;
|
|
columns: string[];
|
|
rows: Record<string, any>[];
|
|
}
|
|
|
|
export const testAdminScheduleSql = (taskId: number, payload: { fetch_where_conditions?: string; fetch_order_by?: string; limit?: number }) =>
|
|
requestJson<TestScheduleSqlResult>(`/api/admin/schedule-config/${taskId}/test-sql`, {
|
|
method: 'POST',
|
|
headers: { 'Content-Type': 'application/json', ...getAuthHeaders() },
|
|
body: JSON.stringify(payload),
|
|
});
|
|
|
|
export interface TestScheduleRunResult {
|
|
run_no: string;
|
|
run_id: number;
|
|
sql: string;
|
|
limit: number;
|
|
total_count: number;
|
|
success_count: number;
|
|
failed_count: number;
|
|
skipped_count: number;
|
|
details: Record<string, any>[];
|
|
errors: string[];
|
|
step_logs: StepLog[];
|
|
dify_agent_id: number | null;
|
|
dify_execution_snapshot?: DifyExecutionSnapshot | null;
|
|
kb_sync_status?: string | null;
|
|
kb_sync_error?: string | null;
|
|
kb_document_id?: number | null;
|
|
}
|
|
|
|
export const testAdminScheduleRun = (
|
|
taskId: number,
|
|
payload: {
|
|
fetch_where_conditions?: string;
|
|
fetch_order_by?: string;
|
|
limit?: number;
|
|
kb_sync_enabled?: boolean;
|
|
},
|
|
) =>
|
|
requestJson<TestScheduleRunResult>(`/api/admin/schedule-config/${taskId}/test-run`, {
|
|
method: 'POST',
|
|
headers: { 'Content-Type': 'application/json', ...getAuthHeaders() },
|
|
body: JSON.stringify(payload),
|
|
});
|
|
|
|
export const createAdminSchedule = (payload: Partial<ScheduleTask> & { config?: Record<string, any> }) =>
|
|
requestJson('/api/admin/schedules', {
|
|
method: 'POST',
|
|
headers: { 'Content-Type': 'application/json', ...getAuthHeaders() },
|
|
body: JSON.stringify(payload),
|
|
});
|
|
|
|
export const updateAdminSchedule = (id: number, payload: Partial<ScheduleTask> & { config?: Record<string, any> }) =>
|
|
requestJson(`/api/admin/schedules/${id}`, {
|
|
method: 'PATCH',
|
|
headers: { 'Content-Type': 'application/json', ...getAuthHeaders() },
|
|
body: JSON.stringify(payload),
|
|
});
|
|
|
|
export const deleteAdminSchedule = (id: number) =>
|
|
requestJson(`/api/admin/schedules/${id}`, {
|
|
method: 'DELETE',
|
|
headers: getAuthHeaders(),
|
|
});
|
|
export const runAdminSchedule = (id: number) =>
|
|
requestJson(`/api/admin/schedules/${id}/run`, { method: 'POST', headers: getAuthHeaders() });
|
|
export const toggleAdminSchedule = (id: number, enabled: boolean) =>
|
|
requestJson(`/api/admin/schedules/${id}/${enabled ? 'enable' : 'disable'}`, { method: 'POST', headers: getAuthHeaders() });
|
|
export const listAdminScheduleRuns = (taskId: number, params?: { status?: string; started_after?: string; started_before?: string }) => {
|
|
const sp = new URLSearchParams();
|
|
if (params?.status) sp.set('status', params.status);
|
|
if (params?.started_after) sp.set('started_after', params.started_after);
|
|
if (params?.started_before) sp.set('started_before', params.started_before);
|
|
const qs = sp.toString();
|
|
return getJson<ScheduleRun[]>(`/api/admin/schedules/${taskId}/runs${qs ? '?' + qs : ''}`);
|
|
};
|
|
export const adminRunExcelUrl = (runId: number) => `/api/admin/schedule-runs/${runId}/excel`;
|
|
|
|
export const listWorkspaceScheduleRuns = (params?: { status?: string; schedule_name?: string; started_after?: string; started_before?: string }) => {
|
|
const sp = new URLSearchParams();
|
|
if (params?.status) sp.set('status', params.status);
|
|
if (params?.schedule_name) sp.set('schedule_name', params.schedule_name);
|
|
if (params?.started_after) sp.set('started_after', params.started_after);
|
|
if (params?.started_before) sp.set('started_before', params.started_before);
|
|
const qs = sp.toString();
|
|
return getJson<ScheduleRun[]>(`/api/workspace/schedule-runs${qs ? '?' + qs : ''}`);
|
|
};
|
|
export const workspaceRunExcelUrl = (runId: number) => `/api/workspace/schedule-runs/${runId}/excel`;
|