Automation Part 1
This commit is contained in:
144
frontend/src/services/automationService.ts
Normal file
144
frontend/src/services/automationService.ts
Normal file
@@ -0,0 +1,144 @@
|
||||
/**
|
||||
* Automation API Service
|
||||
*/
|
||||
import { fetchAPI } from './api';
|
||||
|
||||
export interface AutomationConfig {
|
||||
is_enabled: boolean;
|
||||
frequency: 'daily' | 'weekly' | 'monthly';
|
||||
scheduled_time: string;
|
||||
stage_1_batch_size: number;
|
||||
stage_2_batch_size: number;
|
||||
stage_3_batch_size: number;
|
||||
stage_4_batch_size: number;
|
||||
stage_5_batch_size: number;
|
||||
stage_6_batch_size: number;
|
||||
last_run_at: string | null;
|
||||
next_run_at: string | null;
|
||||
}
|
||||
|
||||
export interface StageResult {
|
||||
[key: string]: any;
|
||||
}
|
||||
|
||||
export interface AutomationRun {
|
||||
run_id: string;
|
||||
status: 'running' | 'paused' | 'completed' | 'failed';
|
||||
current_stage: number;
|
||||
trigger_type: 'manual' | 'scheduled';
|
||||
started_at: string;
|
||||
total_credits_used: number;
|
||||
stage_1_result: StageResult | null;
|
||||
stage_2_result: StageResult | null;
|
||||
stage_3_result: StageResult | null;
|
||||
stage_4_result: StageResult | null;
|
||||
stage_5_result: StageResult | null;
|
||||
stage_6_result: StageResult | null;
|
||||
stage_7_result: StageResult | null;
|
||||
}
|
||||
|
||||
export interface RunHistoryItem {
|
||||
run_id: string;
|
||||
status: string;
|
||||
trigger_type: string;
|
||||
started_at: string;
|
||||
completed_at: string | null;
|
||||
total_credits_used: number;
|
||||
current_stage: number;
|
||||
}
|
||||
|
||||
function buildUrl(endpoint: string, params?: Record<string, any>): string {
|
||||
let url = `/v1/automation${endpoint}`;
|
||||
if (params) {
|
||||
const query = new URLSearchParams(
|
||||
Object.entries(params)
|
||||
.filter(([, v]) => v != null)
|
||||
.map(([k, v]) => [k, String(v)])
|
||||
);
|
||||
const queryStr = query.toString();
|
||||
if (queryStr) {
|
||||
url += `?${queryStr}`;
|
||||
}
|
||||
}
|
||||
return url;
|
||||
}
|
||||
|
||||
export const automationService = {
|
||||
/**
|
||||
* Get automation configuration for site
|
||||
*/
|
||||
getConfig: async (siteId: number): Promise<AutomationConfig> => {
|
||||
return fetchAPI(buildUrl('/config/', { site_id: siteId }));
|
||||
},
|
||||
|
||||
/**
|
||||
* Update automation configuration
|
||||
*/
|
||||
updateConfig: async (siteId: number, config: Partial<AutomationConfig>): Promise<void> => {
|
||||
await fetchAPI(buildUrl('/update_config/', { site_id: siteId }), {
|
||||
method: 'PUT',
|
||||
body: JSON.stringify(config),
|
||||
});
|
||||
},
|
||||
|
||||
/**
|
||||
* Trigger automation run now
|
||||
*/
|
||||
runNow: async (siteId: number): Promise<{ run_id: string; message: string }> => {
|
||||
return fetchAPI(buildUrl('/run_now/', { site_id: siteId }), {
|
||||
method: 'POST',
|
||||
});
|
||||
},
|
||||
|
||||
/**
|
||||
* Get current automation run status
|
||||
*/
|
||||
getCurrentRun: async (siteId: number): Promise<{ run: AutomationRun | null }> => {
|
||||
return fetchAPI(buildUrl('/current_run/', { site_id: siteId }));
|
||||
},
|
||||
|
||||
/**
|
||||
* Pause automation run
|
||||
*/
|
||||
pause: async (runId: string): Promise<void> => {
|
||||
await fetchAPI(buildUrl('/pause/', { run_id: runId }), {
|
||||
method: 'POST',
|
||||
});
|
||||
},
|
||||
|
||||
/**
|
||||
* Resume paused automation run
|
||||
*/
|
||||
resume: async (runId: string): Promise<void> => {
|
||||
await fetchAPI(buildUrl('/resume/', { run_id: runId }), {
|
||||
method: 'POST',
|
||||
});
|
||||
},
|
||||
|
||||
/**
|
||||
* Get automation run history
|
||||
*/
|
||||
getHistory: async (siteId: number): Promise<RunHistoryItem[]> => {
|
||||
const response = await fetchAPI(buildUrl('/history/', { site_id: siteId }));
|
||||
return response.runs;
|
||||
},
|
||||
|
||||
/**
|
||||
* Get automation run logs
|
||||
*/
|
||||
getLogs: async (runId: string, lines: number = 100): Promise<string> => {
|
||||
const response = await fetchAPI(buildUrl('/logs/', { run_id: runId, lines }));
|
||||
return response.log;
|
||||
},
|
||||
|
||||
/**
|
||||
* Estimate credits needed
|
||||
*/
|
||||
estimate: async (siteId: number): Promise<{
|
||||
estimated_credits: number;
|
||||
current_balance: number;
|
||||
sufficient: boolean;
|
||||
}> => {
|
||||
return fetchAPI(buildUrl('/estimate/', { site_id: siteId }));
|
||||
},
|
||||
};
|
||||
Reference in New Issue
Block a user