161 lines
5.9 KiB
TypeScript
161 lines
5.9 KiB
TypeScript
import { getAuthHeaders } from '../utils/auth';
|
|
|
|
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;
|
|
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;
|
|
error_message?: string | null;
|
|
excel_file_name?: string | null;
|
|
excel_file_size?: number | null;
|
|
mail_sent: boolean;
|
|
mail_sent_at?: string | null;
|
|
mail_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>;
|
|
}
|
|
|
|
async function getJson<T>(url: string): Promise<T> {
|
|
const res = await fetch(url, { headers: getAuthHeaders() });
|
|
if (!res.ok) throw new Error((await res.json().catch(() => ({})))?.error || 'Request failed');
|
|
return (await res.json()) as T;
|
|
}
|
|
|
|
export const listAdminSchedules = () =>
|
|
getJson<ScheduleTask[]>('/api/admin/schedules');
|
|
|
|
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>) =>
|
|
fetch(`/api/admin/schedule-config/${taskId}`, {
|
|
method: 'PUT',
|
|
headers: { 'Content-Type': 'application/json', ...getAuthHeaders() },
|
|
body: JSON.stringify(payload),
|
|
}).then(r => {
|
|
if (!r.ok) throw new Error('Update config failed');
|
|
return r.json();
|
|
});
|
|
|
|
export const testAdminScheduleSql = (taskId: number, payload: { fetch_where_conditions?: string; fetch_order_by?: string; limit?: number }) =>
|
|
fetch(`/api/admin/schedule-config/${taskId}/test-sql`, {
|
|
method: 'POST',
|
|
headers: { 'Content-Type': 'application/json', ...getAuthHeaders() },
|
|
body: JSON.stringify(payload),
|
|
}).then(r => {
|
|
if (!r.ok) return r.json().then(e => { throw new Error(e.error || 'Test failed'); });
|
|
return r.json();
|
|
});
|
|
|
|
export interface TestScheduleRunResult {
|
|
run_no: string;
|
|
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;
|
|
}
|
|
|
|
export const testAdminScheduleRun = (taskId: number, payload: { fetch_where_conditions?: string; fetch_order_by?: string; limit?: number }) =>
|
|
fetch(`/api/admin/schedule-config/${taskId}/test-run`, {
|
|
method: 'POST',
|
|
headers: { 'Content-Type': 'application/json', ...getAuthHeaders() },
|
|
body: JSON.stringify(payload),
|
|
}).then(r => {
|
|
if (!r.ok) return r.json().then(e => { throw new Error(e.error || 'Test run failed'); });
|
|
return r.json() as Promise<TestScheduleRunResult>;
|
|
});
|
|
|
|
export const createAdminSchedule = (payload: Partial<ScheduleTask> & { config?: Record<string, any> }) =>
|
|
fetch('/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> }) =>
|
|
fetch(`/api/admin/schedules/${id}`, {
|
|
method: 'PATCH',
|
|
headers: { 'Content-Type': 'application/json', ...getAuthHeaders() },
|
|
body: JSON.stringify(payload),
|
|
});
|
|
|
|
export const deleteAdminSchedule = (id: number) =>
|
|
fetch(`/api/admin/schedules/${id}`, {
|
|
method: 'DELETE',
|
|
headers: getAuthHeaders(),
|
|
});
|
|
export const runAdminSchedule = (id: number) =>
|
|
fetch(`/api/admin/schedules/${id}/run`, { method: 'POST', headers: getAuthHeaders() });
|
|
export const toggleAdminSchedule = (id: number, enabled: boolean) =>
|
|
fetch(`/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`;
|