Refactor ProgressModal and API logging
- Removed AI request logging from `autoGenerateIdeas`, `generateSingleIdea`, `autoGenerateContent`, and `autoGenerateImages` functions to streamline API calls. - Updated `useProgressModal` to enhance step logging by directly processing request and response steps, improving clarity and performance. - Cleaned up deprecated logging imports in `TablePageTemplate` to reflect the removal of frontend debug logging.
This commit is contained in:
@@ -596,165 +596,33 @@ export async function autoClusterKeywords(keywordIds: number[], sectorId?: numbe
|
||||
}
|
||||
|
||||
export async function autoGenerateIdeas(clusterIds: number[]): Promise<{ success: boolean; task_id?: string; ideas_created?: number; message?: string; error?: string }> {
|
||||
const startTime = Date.now();
|
||||
const { useAIRequestLogsStore } = await import('../store/aiRequestLogsStore').catch(() => ({ useAIRequestLogsStore: null }));
|
||||
const addLog = useAIRequestLogsStore?.getState().addLog;
|
||||
|
||||
const endpoint = `/v1/planner/clusters/auto_generate_ideas/`;
|
||||
const requestBody = { ids: clusterIds };
|
||||
|
||||
addLog?.({
|
||||
function: 'autoGenerateIdeas',
|
||||
endpoint,
|
||||
request: {
|
||||
method: 'POST',
|
||||
body: requestBody,
|
||||
},
|
||||
status: 'pending',
|
||||
});
|
||||
|
||||
try {
|
||||
const response = await fetchAPI(endpoint, {
|
||||
method: 'POST',
|
||||
body: JSON.stringify(requestBody),
|
||||
});
|
||||
|
||||
const duration = Date.now() - startTime;
|
||||
addLog({
|
||||
function: 'autoGenerateIdeas',
|
||||
endpoint,
|
||||
request: {
|
||||
method: 'POST',
|
||||
body: requestBody,
|
||||
},
|
||||
response: {
|
||||
status: 200,
|
||||
data: response,
|
||||
},
|
||||
status: 'success',
|
||||
duration,
|
||||
});
|
||||
|
||||
return response;
|
||||
} catch (error: any) {
|
||||
const duration = Date.now() - startTime;
|
||||
|
||||
// Parse error message to extract error type
|
||||
let errorType = 'UNKNOWN_ERROR';
|
||||
let errorMessage = error.message || 'Unknown error';
|
||||
|
||||
if (errorMessage.includes('OperationalError')) {
|
||||
errorType = 'DATABASE_ERROR';
|
||||
errorMessage = errorMessage.replace(/API Error \(\d+\): /, '').replace(/ - .*OperationalError.*/, ' - Database operation failed');
|
||||
} else if (errorMessage.includes('ValidationError')) {
|
||||
errorType = 'VALIDATION_ERROR';
|
||||
} else if (errorMessage.includes('PermissionDenied')) {
|
||||
errorType = 'PERMISSION_ERROR';
|
||||
} else if (errorMessage.match(/API Error \(\d+\): ([^-]+)/)) {
|
||||
const match = errorMessage.match(/API Error \(\d+\): ([^-]+)/);
|
||||
if (match) {
|
||||
errorType = match[1].trim();
|
||||
errorMessage = errorMessage.replace(/API Error \(\d+\): [^-]+ - /, '');
|
||||
}
|
||||
}
|
||||
|
||||
addLog?.({
|
||||
function: 'autoGenerateIdeas',
|
||||
endpoint,
|
||||
request: {
|
||||
method: 'POST',
|
||||
body: requestBody,
|
||||
},
|
||||
response: {
|
||||
status: 500,
|
||||
error: errorMessage,
|
||||
errorType,
|
||||
},
|
||||
status: 'error',
|
||||
duration,
|
||||
});
|
||||
throw error;
|
||||
}
|
||||
}
|
||||
|
||||
export async function generateSingleIdea(ideaId: string | number, clusterId: number): Promise<{ success: boolean; task_id?: string; idea_created?: number; message?: string; error?: string }> {
|
||||
const startTime = Date.now();
|
||||
const { useAIRequestLogsStore } = await import('../store/aiRequestLogsStore').catch(() => ({ useAIRequestLogsStore: null }));
|
||||
const addLog = useAIRequestLogsStore?.getState().addLog;
|
||||
|
||||
const endpoint = `/v1/planner/ideas/${ideaId}/generate_idea/`;
|
||||
const requestBody = { cluster_id: clusterId };
|
||||
|
||||
addLog?.({
|
||||
function: 'generateSingleIdea',
|
||||
endpoint,
|
||||
request: {
|
||||
method: 'POST',
|
||||
body: requestBody,
|
||||
},
|
||||
status: 'pending',
|
||||
});
|
||||
|
||||
try {
|
||||
const response = await fetchAPI(endpoint, {
|
||||
method: 'POST',
|
||||
body: JSON.stringify(requestBody),
|
||||
});
|
||||
|
||||
const duration = Date.now() - startTime;
|
||||
addLog?.({
|
||||
function: 'generateSingleIdea',
|
||||
endpoint,
|
||||
request: {
|
||||
method: 'POST',
|
||||
body: requestBody,
|
||||
},
|
||||
response: {
|
||||
status: 200,
|
||||
data: response,
|
||||
},
|
||||
status: 'success',
|
||||
duration,
|
||||
});
|
||||
|
||||
return response;
|
||||
} catch (error: any) {
|
||||
const duration = Date.now() - startTime;
|
||||
|
||||
// Parse error message to extract error type
|
||||
let errorType = 'UNKNOWN_ERROR';
|
||||
let errorMessage = error.message || 'Unknown error';
|
||||
|
||||
if (errorMessage.includes('OperationalError')) {
|
||||
errorType = 'DATABASE_ERROR';
|
||||
errorMessage = errorMessage.replace(/API Error \(\d+\): /, '').replace(/ - .*OperationalError.*/, ' - Database operation failed');
|
||||
} else if (errorMessage.includes('ValidationError')) {
|
||||
errorType = 'VALIDATION_ERROR';
|
||||
} else if (errorMessage.includes('PermissionDenied')) {
|
||||
errorType = 'PERMISSION_ERROR';
|
||||
} else if (errorMessage.match(/API Error \(\d+\): ([^-]+)/)) {
|
||||
const match = errorMessage.match(/API Error \(\d+\): ([^-]+)/);
|
||||
if (match) {
|
||||
errorType = match[1].trim();
|
||||
errorMessage = errorMessage.replace(/API Error \(\d+\): [^-]+ - /, '');
|
||||
}
|
||||
}
|
||||
|
||||
addLog?.({
|
||||
function: 'generateSingleIdea',
|
||||
endpoint,
|
||||
request: {
|
||||
method: 'POST',
|
||||
body: requestBody,
|
||||
},
|
||||
response: {
|
||||
status: 500,
|
||||
error: errorMessage,
|
||||
errorType,
|
||||
},
|
||||
status: 'error',
|
||||
duration,
|
||||
});
|
||||
throw error;
|
||||
}
|
||||
}
|
||||
@@ -1051,161 +919,33 @@ export async function bulkUpdateTasksStatus(ids: number[], status: string): Prom
|
||||
}
|
||||
|
||||
export async function autoGenerateContent(ids: number[]): Promise<{ success: boolean; task_id?: string; tasks_updated?: number; message?: string; error?: string }> {
|
||||
const startTime = Date.now();
|
||||
const { useAIRequestLogsStore } = await import('../store/aiRequestLogsStore').catch(() => ({ useAIRequestLogsStore: null }));
|
||||
const addLog = useAIRequestLogsStore?.getState().addLog;
|
||||
|
||||
const endpoint = `/v1/writer/tasks/auto_generate_content/`;
|
||||
const requestBody = { ids };
|
||||
|
||||
addLog?.({
|
||||
function: 'autoGenerateContent',
|
||||
endpoint,
|
||||
request: {
|
||||
method: 'POST',
|
||||
body: requestBody,
|
||||
},
|
||||
status: 'pending',
|
||||
});
|
||||
|
||||
try {
|
||||
const response = await fetchAPI(endpoint, {
|
||||
method: 'POST',
|
||||
body: JSON.stringify(requestBody),
|
||||
});
|
||||
|
||||
const duration = Date.now() - startTime;
|
||||
addLog({
|
||||
function: 'autoGenerateContent',
|
||||
endpoint,
|
||||
request: {
|
||||
method: 'POST',
|
||||
body: requestBody,
|
||||
},
|
||||
response: {
|
||||
status: 200,
|
||||
data: response,
|
||||
},
|
||||
status: 'success',
|
||||
duration,
|
||||
});
|
||||
|
||||
return response;
|
||||
} catch (error: any) {
|
||||
const duration = Date.now() - startTime;
|
||||
|
||||
// Parse error message to extract error type
|
||||
let errorType = 'UNKNOWN_ERROR';
|
||||
let errorMessage = error.message || 'Unknown error';
|
||||
|
||||
if (errorMessage.includes('OperationalError')) {
|
||||
errorType = 'DATABASE_ERROR';
|
||||
errorMessage = errorMessage.replace(/API Error \(\d+\): /, '').replace(/ - .*OperationalError.*/, ' - Database operation failed');
|
||||
} else if (errorMessage.includes('ValidationError')) {
|
||||
errorType = 'VALIDATION_ERROR';
|
||||
} else if (errorMessage.match(/API Error \(\d+\): ([^-]+)/)) {
|
||||
const match = errorMessage.match(/API Error \(\d+\): ([^-]+)/);
|
||||
if (match) {
|
||||
errorType = match[1].trim();
|
||||
errorMessage = errorMessage.replace(/API Error \(\d+\): [^-]+ - /, '');
|
||||
}
|
||||
}
|
||||
|
||||
addLog?.({
|
||||
function: 'autoGenerateContent',
|
||||
endpoint,
|
||||
request: {
|
||||
method: 'POST',
|
||||
body: requestBody,
|
||||
},
|
||||
response: {
|
||||
status: 500,
|
||||
error: errorMessage,
|
||||
errorType,
|
||||
},
|
||||
status: 'error',
|
||||
duration,
|
||||
});
|
||||
throw error;
|
||||
}
|
||||
}
|
||||
|
||||
export async function autoGenerateImages(taskIds: number[]): Promise<{ success: boolean; task_id?: string; images_created?: number; message?: string; error?: string }> {
|
||||
const startTime = Date.now();
|
||||
const { useAIRequestLogsStore } = await import('../store/aiRequestLogsStore').catch(() => ({ useAIRequestLogsStore: null }));
|
||||
const addLog = useAIRequestLogsStore?.getState().addLog;
|
||||
|
||||
const endpoint = `/v1/writer/tasks/auto_generate_images/`;
|
||||
const requestBody = { task_ids: taskIds };
|
||||
|
||||
addLog?.({
|
||||
function: 'autoGenerateImages',
|
||||
endpoint,
|
||||
request: {
|
||||
method: 'POST',
|
||||
body: requestBody,
|
||||
},
|
||||
status: 'pending',
|
||||
});
|
||||
|
||||
try {
|
||||
const response = await fetchAPI(endpoint, {
|
||||
method: 'POST',
|
||||
body: JSON.stringify(requestBody),
|
||||
});
|
||||
|
||||
const duration = Date.now() - startTime;
|
||||
addLog({
|
||||
function: 'autoGenerateImages',
|
||||
endpoint,
|
||||
request: {
|
||||
method: 'POST',
|
||||
body: requestBody,
|
||||
},
|
||||
response: {
|
||||
status: 200,
|
||||
data: response,
|
||||
},
|
||||
status: 'success',
|
||||
duration,
|
||||
});
|
||||
|
||||
return response;
|
||||
} catch (error: any) {
|
||||
const duration = Date.now() - startTime;
|
||||
|
||||
// Parse error message to extract error type
|
||||
let errorType = 'UNKNOWN_ERROR';
|
||||
let errorMessage = error.message || 'Unknown error';
|
||||
|
||||
if (errorMessage.includes('OperationalError')) {
|
||||
errorType = 'DATABASE_ERROR';
|
||||
errorMessage = errorMessage.replace(/API Error \(\d+\): /, '').replace(/ - .*OperationalError.*/, ' - Database operation failed');
|
||||
} else if (errorMessage.includes('ValidationError')) {
|
||||
errorType = 'VALIDATION_ERROR';
|
||||
} else if (errorMessage.match(/API Error \(\d+\): ([^-]+)/)) {
|
||||
const match = errorMessage.match(/API Error \(\d+\): ([^-]+)/);
|
||||
if (match) {
|
||||
errorType = match[1].trim();
|
||||
errorMessage = errorMessage.replace(/API Error \(\d+\): [^-]+ - /, '');
|
||||
}
|
||||
}
|
||||
|
||||
addLog?.({
|
||||
function: 'autoGenerateImages',
|
||||
endpoint,
|
||||
request: {
|
||||
method: 'POST',
|
||||
body: requestBody,
|
||||
},
|
||||
response: {
|
||||
status: 500,
|
||||
error: errorMessage,
|
||||
errorType,
|
||||
},
|
||||
status: 'error',
|
||||
duration,
|
||||
});
|
||||
throw error;
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user