2024-07-31 19:47:52 +05:30
|
|
|
import { default as axios } from "axios";
|
2024-07-31 21:10:52 +05:30
|
|
|
import { WorkflowFile } from "maxun-core";
|
2024-07-31 19:47:52 +05:30
|
|
|
import { RunSettings } from "../components/molecules/RunSettings";
|
2024-09-12 23:41:33 +05:30
|
|
|
import { ScheduleSettings } from "../components/molecules/ScheduleSettings";
|
2024-09-13 08:01:18 +05:30
|
|
|
import { CreateRunResponse, ScheduleRunResponse } from "../pages/MainPage";
|
2024-11-01 08:25:14 +05:30
|
|
|
import { apiUrl } from "../apiConfig";
|
2024-07-31 19:47:52 +05:30
|
|
|
|
2024-11-15 16:16:01 +05:30
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
2024-07-31 19:47:52 +05:30
|
|
|
export const getStoredRecordings = async (): Promise<string[] | null> => {
|
|
|
|
|
try {
|
2024-11-01 08:25:14 +05:30
|
|
|
const response = await axios.get(`${apiUrl}/storage/recordings`);
|
2024-07-31 19:47:52 +05:30
|
|
|
if (response.status === 200) {
|
|
|
|
|
return response.data;
|
|
|
|
|
} else {
|
|
|
|
|
throw new Error('Couldn\'t retrieve stored recordings');
|
|
|
|
|
}
|
2024-10-22 20:44:14 +05:30
|
|
|
} catch (error: any) {
|
2024-07-31 19:47:52 +05:30
|
|
|
console.log(error);
|
|
|
|
|
return null;
|
|
|
|
|
}
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
export const getStoredRuns = async (): Promise<string[] | null> => {
|
|
|
|
|
try {
|
2024-11-01 08:25:14 +05:30
|
|
|
const response = await axios.get(`${apiUrl}/storage/runs`);
|
2024-07-31 19:47:52 +05:30
|
|
|
if (response.status === 200) {
|
|
|
|
|
return response.data;
|
|
|
|
|
} else {
|
|
|
|
|
throw new Error('Couldn\'t retrieve stored recordings');
|
|
|
|
|
}
|
2024-10-22 20:44:14 +05:30
|
|
|
} catch (error: any) {
|
2024-07-31 19:47:52 +05:30
|
|
|
console.log(error);
|
|
|
|
|
return null;
|
|
|
|
|
}
|
|
|
|
|
};
|
|
|
|
|
|
2024-10-17 15:28:52 +05:30
|
|
|
export const getStoredRecording = async (id: string) => {
|
2024-10-17 14:36:04 +05:30
|
|
|
try {
|
2024-11-01 08:25:14 +05:30
|
|
|
const response = await axios.get(`${apiUrl}/storage/recordings/${id}`);
|
2024-10-17 14:36:04 +05:30
|
|
|
if (response.status === 200) {
|
|
|
|
|
return response.data;
|
|
|
|
|
} else {
|
|
|
|
|
throw new Error(`Couldn't retrieve stored recording ${id}`);
|
|
|
|
|
}
|
2024-10-22 20:44:14 +05:30
|
|
|
} catch (error: any) {
|
2024-10-17 14:36:04 +05:30
|
|
|
console.log(error);
|
|
|
|
|
return null;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2024-11-15 16:16:01 +05:30
|
|
|
|
|
|
|
|
export const checkRunsForRecording = async (id: string): Promise<boolean> => {
|
|
|
|
|
const apiKey = localStorage.getItem('x-api-key');
|
|
|
|
|
|
|
|
|
|
// Check if the API key exists
|
|
|
|
|
if (!apiKey) {
|
|
|
|
|
console.error('API key is missing.');
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
try {
|
|
|
|
|
const response = await axios.get(`${apiUrl}/api/robots/${id}/runs`, {
|
|
|
|
|
headers: {
|
|
|
|
|
'x-api-key': apiKey, // Pass the valid API key in the header
|
|
|
|
|
},
|
|
|
|
|
withCredentials: true,
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
const runs = response.data;
|
|
|
|
|
return runs.runs.totalCount > 0;
|
|
|
|
|
} catch (error) {
|
|
|
|
|
console.error('Error checking runs for recording:', error);
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
};
|
|
|
|
|
|
2024-10-09 23:09:42 +05:30
|
|
|
export const deleteRecordingFromStorage = async (id: string): Promise<boolean> => {
|
2024-11-15 16:16:01 +05:30
|
|
|
|
|
|
|
|
const hasRuns = await checkRunsForRecording(id);
|
|
|
|
|
|
|
|
|
|
if (hasRuns) {
|
|
|
|
|
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
|
2024-07-31 19:47:52 +05:30
|
|
|
try {
|
2024-11-01 08:25:14 +05:30
|
|
|
const response = await axios.delete(`${apiUrl}/storage/recordings/${id}`);
|
2024-07-31 19:47:52 +05:30
|
|
|
if (response.status === 200) {
|
2024-11-15 16:16:01 +05:30
|
|
|
|
|
|
|
|
return true;
|
2024-07-31 19:47:52 +05:30
|
|
|
} else {
|
2024-10-09 23:09:42 +05:30
|
|
|
throw new Error(`Couldn't delete stored recording ${id}`);
|
2024-07-31 19:47:52 +05:30
|
|
|
}
|
2024-10-22 20:44:14 +05:30
|
|
|
} catch (error: any) {
|
2024-07-31 19:47:52 +05:30
|
|
|
console.log(error);
|
2024-11-15 16:16:01 +05:30
|
|
|
|
2024-07-31 19:47:52 +05:30
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
};
|
|
|
|
|
|
2024-10-09 23:09:42 +05:30
|
|
|
export const deleteRunFromStorage = async (id: string): Promise<boolean> => {
|
2024-07-31 19:47:52 +05:30
|
|
|
try {
|
2024-11-01 08:25:14 +05:30
|
|
|
const response = await axios.delete(`${apiUrl}/storage/runs/${id}`);
|
2024-07-31 19:47:52 +05:30
|
|
|
if (response.status === 200) {
|
|
|
|
|
return response.data;
|
|
|
|
|
} else {
|
2024-10-09 23:09:42 +05:30
|
|
|
throw new Error(`Couldn't delete stored recording ${id}`);
|
2024-07-31 19:47:52 +05:30
|
|
|
}
|
2024-10-22 20:44:14 +05:30
|
|
|
} catch (error: any) {
|
2024-07-31 19:47:52 +05:30
|
|
|
console.log(error);
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
};
|
|
|
|
|
|
2024-10-10 03:48:10 +05:30
|
|
|
export const editRecordingFromStorage = async (browserId: string, id: string): Promise<WorkflowFile | null> => {
|
2024-07-31 19:47:52 +05:30
|
|
|
try {
|
2024-11-01 08:25:14 +05:30
|
|
|
const response = await axios.put(`${apiUrl}/workflow/${browserId}/${id}`);
|
2024-07-31 19:47:52 +05:30
|
|
|
if (response.status === 200) {
|
|
|
|
|
return response.data;
|
|
|
|
|
} else {
|
2024-10-10 03:48:10 +05:30
|
|
|
throw new Error(`Couldn't edit stored recording ${id}`);
|
2024-07-31 19:47:52 +05:30
|
|
|
}
|
2024-10-22 20:44:14 +05:30
|
|
|
} catch (error: any) {
|
2024-07-31 19:47:52 +05:30
|
|
|
console.log(error);
|
|
|
|
|
return null;
|
|
|
|
|
}
|
|
|
|
|
};
|
|
|
|
|
|
2024-10-09 23:09:42 +05:30
|
|
|
export const createRunForStoredRecording = async (id: string, settings: RunSettings): Promise<CreateRunResponse> => {
|
2024-07-31 19:47:52 +05:30
|
|
|
try {
|
|
|
|
|
const response = await axios.put(
|
2024-11-01 08:25:14 +05:30
|
|
|
`${apiUrl}/storage/runs/${id}`,
|
2024-11-15 16:16:01 +05:30
|
|
|
{ ...settings });
|
2024-07-31 19:47:52 +05:30
|
|
|
if (response.status === 200) {
|
|
|
|
|
return response.data;
|
|
|
|
|
} else {
|
2024-10-09 23:09:42 +05:30
|
|
|
throw new Error(`Couldn't create a run for a recording ${id}`);
|
2024-07-31 19:47:52 +05:30
|
|
|
}
|
2024-10-22 20:44:14 +05:30
|
|
|
} catch (error: any) {
|
2024-07-31 19:47:52 +05:30
|
|
|
console.log(error);
|
2024-10-22 20:44:14 +05:30
|
|
|
return { browserId: '', runId: '' };
|
2024-07-31 19:47:52 +05:30
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2024-10-10 02:24:23 +05:30
|
|
|
export const interpretStoredRecording = async (id: string): Promise<boolean> => {
|
2024-07-31 19:47:52 +05:30
|
|
|
try {
|
2024-11-01 08:25:14 +05:30
|
|
|
const response = await axios.post(`${apiUrl}/storage/runs/run/${id}`);
|
2024-07-31 19:47:52 +05:30
|
|
|
if (response.status === 200) {
|
|
|
|
|
return response.data;
|
|
|
|
|
} else {
|
2024-10-10 02:24:23 +05:30
|
|
|
throw new Error(`Couldn't run a recording ${id}`);
|
2024-07-31 19:47:52 +05:30
|
|
|
}
|
2024-10-22 20:44:14 +05:30
|
|
|
} catch (error: any) {
|
2024-07-31 19:47:52 +05:30
|
|
|
console.log(error);
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2024-10-22 20:44:14 +05:30
|
|
|
export const notifyAboutAbort = async (id: string): Promise<boolean> => {
|
2024-07-31 19:47:52 +05:30
|
|
|
try {
|
2024-11-01 08:25:14 +05:30
|
|
|
const response = await axios.post(`${apiUrl}/storage/runs/abort/${id}`);
|
2024-07-31 19:47:52 +05:30
|
|
|
if (response.status === 200) {
|
|
|
|
|
return response.data;
|
|
|
|
|
} else {
|
2024-10-10 02:24:23 +05:30
|
|
|
throw new Error(`Couldn't abort a running recording with id ${id}`);
|
2024-07-31 19:47:52 +05:30
|
|
|
}
|
2024-10-22 20:44:14 +05:30
|
|
|
} catch (error: any) {
|
2024-07-31 19:47:52 +05:30
|
|
|
console.log(error);
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2024-10-10 03:35:37 +05:30
|
|
|
export const scheduleStoredRecording = async (id: string, settings: ScheduleSettings): Promise<ScheduleRunResponse> => {
|
2024-09-12 23:41:33 +05:30
|
|
|
try {
|
|
|
|
|
const response = await axios.put(
|
2024-11-01 08:25:14 +05:30
|
|
|
`${apiUrl}/storage/schedule/${id}`,
|
2024-10-22 20:44:14 +05:30
|
|
|
{ ...settings });
|
2024-09-12 23:41:33 +05:30
|
|
|
if (response.status === 200) {
|
|
|
|
|
return response.data;
|
|
|
|
|
} else {
|
2024-10-10 03:35:37 +05:30
|
|
|
throw new Error(`Couldn't schedule recording ${id}. Please try again later.`);
|
2024-09-12 23:41:33 +05:30
|
|
|
}
|
2024-10-22 20:44:14 +05:30
|
|
|
} catch (error: any) {
|
2024-09-12 23:41:33 +05:30
|
|
|
console.log(error);
|
2024-10-22 20:44:14 +05:30
|
|
|
return { message: '', runId: '' };
|
2024-09-12 23:41:33 +05:30
|
|
|
}
|
|
|
|
|
}
|
2024-10-22 20:43:49 +05:30
|
|
|
|
2024-10-23 02:36:04 +05:30
|
|
|
export const getSchedule = async (id: string) => {
|
2024-10-22 20:43:49 +05:30
|
|
|
try {
|
2024-11-01 08:25:14 +05:30
|
|
|
const response = await axios.get(`${apiUrl}/storage/schedule/${id}`);
|
2024-10-22 20:43:49 +05:30
|
|
|
if (response.status === 200) {
|
2024-10-23 03:16:57 +05:30
|
|
|
return response.data.schedule;
|
2024-10-22 20:43:49 +05:30
|
|
|
} else {
|
|
|
|
|
throw new Error(`Couldn't retrieve schedule for recording ${id}`);
|
|
|
|
|
}
|
2024-10-22 20:44:14 +05:30
|
|
|
} catch (error: any) {
|
2024-10-22 20:43:49 +05:30
|
|
|
console.log(error);
|
|
|
|
|
return null;
|
|
|
|
|
}
|
2024-10-22 21:05:52 +05:30
|
|
|
}
|
|
|
|
|
|
|
|
|
|
export const deleteSchedule = async (id: string): Promise<boolean> => {
|
|
|
|
|
try {
|
2024-11-01 08:25:14 +05:30
|
|
|
const response = await axios.delete(`${apiUrl}/storage/schedule/${id}`);
|
2024-10-22 21:05:52 +05:30
|
|
|
if (response.status === 200) {
|
|
|
|
|
return response.data;
|
|
|
|
|
} else {
|
|
|
|
|
throw new Error(`Couldn't delete schedule for recording ${id}`);
|
|
|
|
|
}
|
|
|
|
|
} catch (error: any) {
|
|
|
|
|
console.log(error);
|
|
|
|
|
return false;
|
|
|
|
|
}
|
2024-10-22 20:43:49 +05:30
|
|
|
}
|