Improve workers for backtests

This commit is contained in:
2025-11-10 01:44:33 +07:00
parent 97f2b8229b
commit 7e52b7a734
18 changed files with 740 additions and 144 deletions

View File

@@ -2403,7 +2403,7 @@ export class JobClient extends AuthorizedApiBase {
this.baseUrl = baseUrl ?? "http://localhost:5000";
}
job_GetJobStatus(jobId: string): Promise<BacktestJobStatusResponse> {
job_GetJobStatus(jobId: string): Promise<JobStatusResponse> {
let url_ = this.baseUrl + "/Job/{jobId}";
if (jobId === undefined || jobId === null)
throw new Error("The parameter 'jobId' must be defined.");
@@ -2424,13 +2424,13 @@ export class JobClient extends AuthorizedApiBase {
});
}
protected processJob_GetJobStatus(response: Response): Promise<BacktestJobStatusResponse> {
protected processJob_GetJobStatus(response: Response): Promise<JobStatusResponse> {
const status = response.status;
let _headers: any = {}; if (response.headers && response.headers.forEach) { response.headers.forEach((v: any, k: any) => _headers[k] = v); };
if (status === 200) {
return response.text().then((_responseText) => {
let result200: any = null;
result200 = _responseText === "" ? null : JSON.parse(_responseText, this.jsonParseReviver) as BacktestJobStatusResponse;
result200 = _responseText === "" ? null : JSON.parse(_responseText, this.jsonParseReviver) as JobStatusResponse;
return result200;
});
} else if (status !== 200 && status !== 204) {
@@ -2438,7 +2438,50 @@ export class JobClient extends AuthorizedApiBase {
return throwException("An unexpected server error occurred.", status, _responseText, _headers);
});
}
return Promise.resolve<BacktestJobStatusResponse>(null as any);
return Promise.resolve<JobStatusResponse>(null as any);
}
job_DeleteJob(jobId: string): Promise<FileResponse> {
let url_ = this.baseUrl + "/Job/{jobId}";
if (jobId === undefined || jobId === null)
throw new Error("The parameter 'jobId' must be defined.");
url_ = url_.replace("{jobId}", encodeURIComponent("" + jobId));
url_ = url_.replace(/[?&]$/, "");
let options_: RequestInit = {
method: "DELETE",
headers: {
"Accept": "application/octet-stream"
}
};
return this.transformOptions(options_).then(transformedOptions_ => {
return this.http.fetch(url_, transformedOptions_);
}).then((_response: Response) => {
return this.processJob_DeleteJob(_response);
});
}
protected processJob_DeleteJob(response: Response): Promise<FileResponse> {
const status = response.status;
let _headers: any = {}; if (response.headers && response.headers.forEach) { response.headers.forEach((v: any, k: any) => _headers[k] = v); };
if (status === 200 || status === 206) {
const contentDisposition = response.headers ? response.headers.get("content-disposition") : undefined;
let fileNameMatch = contentDisposition ? /filename\*=(?:(\\?['"])(.*?)\1|(?:[^\s]+'.*?')?([^;\n]*))/g.exec(contentDisposition) : undefined;
let fileName = fileNameMatch && fileNameMatch.length > 1 ? fileNameMatch[3] || fileNameMatch[2] : undefined;
if (fileName) {
fileName = decodeURIComponent(fileName);
} else {
fileNameMatch = contentDisposition ? /filename="?([^"]*?)"?(;|$)/g.exec(contentDisposition) : undefined;
fileName = fileNameMatch && fileNameMatch.length > 1 ? fileNameMatch[1] : undefined;
}
return response.blob().then(blob => { return { fileName: fileName, data: blob, status: status, headers: _headers }; });
} else if (status !== 200 && status !== 204) {
return response.text().then((_responseText) => {
return throwException("An unexpected server error occurred.", status, _responseText, _headers);
});
}
return Promise.resolve<FileResponse>(null as any);
}
job_GetJobs(page: number | undefined, pageSize: number | undefined, sortBy: string | undefined, sortOrder: string | undefined, status: string | null | undefined, jobType: string | null | undefined, userId: number | null | undefined, workerId: string | null | undefined, bundleRequestId: string | null | undefined): Promise<PaginatedJobsResponse> {
@@ -2536,6 +2579,49 @@ export class JobClient extends AuthorizedApiBase {
}
return Promise.resolve<JobSummaryResponse>(null as any);
}
job_RetryJob(jobId: string): Promise<FileResponse> {
let url_ = this.baseUrl + "/Job/{jobId}/retry";
if (jobId === undefined || jobId === null)
throw new Error("The parameter 'jobId' must be defined.");
url_ = url_.replace("{jobId}", encodeURIComponent("" + jobId));
url_ = url_.replace(/[?&]$/, "");
let options_: RequestInit = {
method: "POST",
headers: {
"Accept": "application/octet-stream"
}
};
return this.transformOptions(options_).then(transformedOptions_ => {
return this.http.fetch(url_, transformedOptions_);
}).then((_response: Response) => {
return this.processJob_RetryJob(_response);
});
}
protected processJob_RetryJob(response: Response): Promise<FileResponse> {
const status = response.status;
let _headers: any = {}; if (response.headers && response.headers.forEach) { response.headers.forEach((v: any, k: any) => _headers[k] = v); };
if (status === 200 || status === 206) {
const contentDisposition = response.headers ? response.headers.get("content-disposition") : undefined;
let fileNameMatch = contentDisposition ? /filename\*=(?:(\\?['"])(.*?)\1|(?:[^\s]+'.*?')?([^;\n]*))/g.exec(contentDisposition) : undefined;
let fileName = fileNameMatch && fileNameMatch.length > 1 ? fileNameMatch[3] || fileNameMatch[2] : undefined;
if (fileName) {
fileName = decodeURIComponent(fileName);
} else {
fileNameMatch = contentDisposition ? /filename="?([^"]*?)"?(;|$)/g.exec(contentDisposition) : undefined;
fileName = fileNameMatch && fileNameMatch.length > 1 ? fileNameMatch[1] : undefined;
}
return response.blob().then(blob => { return { fileName: fileName, data: blob, status: status, headers: _headers }; });
} else if (status !== 200 && status !== 204) {
return response.text().then((_responseText) => {
return throwException("An unexpected server error occurred.", status, _responseText, _headers);
});
}
return Promise.resolve<FileResponse>(null as any);
}
}
export class MoneyManagementClient extends AuthorizedApiBase {
@@ -5397,7 +5483,7 @@ export interface AgentBalance {
time?: Date;
}
export interface BacktestJobStatusResponse {
export interface JobStatusResponse {
jobId?: string;
status?: string | null;
progressPercentage?: number;

View File

@@ -1208,7 +1208,7 @@ export interface AgentBalance {
time?: Date;
}
export interface BacktestJobStatusResponse {
export interface JobStatusResponse {
jobId?: string;
status?: string | null;
progressPercentage?: number;

View File

@@ -1,9 +1,9 @@
import {useState} from 'react'
import {useQuery} from '@tanstack/react-query'
import {useMutation, useQuery, useQueryClient} from '@tanstack/react-query'
import useApiUrlStore from '../../../app/store/apiStore'
import {JobClient} from '../../../generated/ManagingApi'
import {BottomMenuBar} from '../../../components/mollecules'
import {BottomMenuBar, Toast} from '../../../components/mollecules'
import JobsTable from './jobsTable'
@@ -22,6 +22,66 @@ const JobsSettings: React.FC = () => {
const [showTable, setShowTable] = useState<boolean>(false)
const jobClient = new JobClient({}, apiUrl)
const queryClient = useQueryClient()
// Retry job mutation
const retryJobMutation = useMutation({
mutationFn: async (jobId: string) => {
// The API returns FileResponse but backend actually returns JSON
const response = await jobClient.job_RetryJob(jobId)
// Parse the response as JSON
const text = await response.data.text()
return JSON.parse(text)
},
onSuccess: () => {
// Invalidate jobs queries to refresh the list
queryClient.invalidateQueries({ queryKey: ['jobs'] })
queryClient.invalidateQueries({ queryKey: ['jobSummary'] })
},
})
const handleRetryJob = async (jobId: string) => {
const toast = new Toast('Retrying job...')
try {
await retryJobMutation.mutateAsync(jobId)
toast.update('success', 'Job has been reset to Pending status')
} catch (error: any) {
const errorMessage = error?.response?.data?.error || error?.message || 'Failed to retry job'
toast.update('error', errorMessage)
}
}
// Delete job mutation
const deleteJobMutation = useMutation({
mutationFn: async (jobId: string) => {
// The API returns FileResponse but backend actually returns JSON
const response = await jobClient.job_DeleteJob(jobId)
// Parse the response as JSON if there's content
if (response.data && response.data.size > 0) {
const text = await response.data.text()
if (text) {
return JSON.parse(text)
}
}
return { message: 'Job deleted successfully', jobId }
},
onSuccess: () => {
// Invalidate jobs queries to refresh the list
queryClient.invalidateQueries({ queryKey: ['jobs'] })
queryClient.invalidateQueries({ queryKey: ['jobSummary'] })
},
})
const handleDeleteJob = async (jobId: string) => {
const toast = new Toast('Deleting job...')
try {
await deleteJobMutation.mutateAsync(jobId)
toast.update('success', 'Job has been deleted successfully')
} catch (error: any) {
const errorMessage = error?.response?.data?.error || error?.message || 'Failed to delete job'
toast.update('error', errorMessage)
}
}
// Fetch job summary statistics
const {
@@ -478,6 +538,10 @@ const JobsSettings: React.FC = () => {
sortOrder={sortOrder}
onPageChange={handlePageChange}
onSortChange={handleSortChange}
onRetryJob={handleRetryJob}
isRetrying={retryJobMutation.isPending}
onDeleteJob={handleDeleteJob}
isDeleting={deleteJobMutation.isPending}
/>
</>
)}

View File

@@ -1,4 +1,4 @@
import React, {useMemo} from 'react'
import React, {useMemo, useState} from 'react'
import {type JobListItemResponse} from '../../../generated/ManagingApi'
import {Table} from '../../../components/mollecules'
@@ -13,6 +13,10 @@ interface IJobsTable {
sortOrder: string
onPageChange: (page: number) => void
onSortChange: (sortBy: string) => void
onRetryJob?: (jobId: string) => void
isRetrying?: boolean
onDeleteJob?: (jobId: string) => void
isDeleting?: boolean
}
const JobsTable: React.FC<IJobsTable> = ({
@@ -25,8 +29,13 @@ const JobsTable: React.FC<IJobsTable> = ({
sortBy,
sortOrder,
onPageChange,
onSortChange
onSortChange,
onRetryJob,
isRetrying = false,
onDeleteJob,
isDeleting = false
}) => {
const [deleteConfirmJobId, setDeleteConfirmJobId] = useState<string | null>(null)
const getStatusBadge = (status: string | null | undefined) => {
if (!status) return <span className="badge badge-sm">-</span>
@@ -88,14 +97,6 @@ const JobsTable: React.FC<IJobsTable> = ({
}
const columns = useMemo(() => [
{
Header: () => <SortableHeader column="JobId" label="Job ID" />,
accessor: 'jobId',
width: 200,
Cell: ({ value }: any) => (
<span className="font-mono text-xs">{value || '-'}</span>
),
},
{
Header: () => <SortableHeader column="Status" label="Status" />,
accessor: 'status',
@@ -147,22 +148,6 @@ const JobsTable: React.FC<IJobsTable> = ({
<span className="font-mono text-xs">{value || '-'}</span>
),
},
{
Header: 'Bundle Request ID',
accessor: 'bundleRequestId',
width: 200,
Cell: ({ value }: any) => (
<span className="font-mono text-xs">{value || '-'}</span>
),
},
{
Header: 'Genetic Request ID',
accessor: 'geneticRequestId',
width: 200,
Cell: ({ value }: any) => (
<span className="font-mono text-xs">{value || '-'}</span>
),
},
{
Header: () => <SortableHeader column="CreatedAt" label="Created" />,
accessor: 'createdAt',
@@ -189,7 +174,69 @@ const JobsTable: React.FC<IJobsTable> = ({
<span className="text-xs text-error">{value || '-'}</span>
),
},
], [sortBy, sortOrder, onSortChange])
{
Header: 'Actions',
accessor: 'actions',
width: 180,
Cell: ({ row }: any) => {
const job = row.original
const isFailed = job.status?.toLowerCase() === 'failed'
const canRetry = isFailed && onRetryJob
const canDelete = onDeleteJob
if (!canRetry && !canDelete) {
return <span className="text-xs text-gray-400">-</span>
}
return (
<div className="flex gap-2">
{canRetry && (
<button
className="btn btn-sm btn-outline btn-primary"
onClick={() => onRetryJob(job.jobId)}
disabled={isRetrying}
title="Retry this job"
>
{isRetrying ? (
<span className="loading loading-spinner loading-xs"></span>
) : (
<svg xmlns="http://www.w3.org/2000/svg" fill="none" viewBox="0 0 24 24" strokeWidth="1.5" stroke="currentColor" className="w-4 h-4">
<path strokeLinecap="round" strokeLinejoin="round" d="M16.023 9.348h4.992v-.001M2.985 19.644v-4.992m0 0h4.992m-4.993 0l3.181 3.183a8.25 8.25 0 0013.803-3.7M4.031 9.865a8.25 8.25 0 0113.803-3.7l3.181 3.182m0-4.991v4.99" />
</svg>
)}
<span className="ml-1">Retry</span>
</button>
)}
{canDelete && (
<button
className="btn btn-sm btn-outline btn-error"
onClick={() => setDeleteConfirmJobId(job.jobId)}
disabled={isDeleting}
title="Delete this job"
>
{isDeleting ? (
<span className="loading loading-spinner loading-xs"></span>
) : (
<svg xmlns="http://www.w3.org/2000/svg" fill="none" viewBox="0 0 24 24" strokeWidth="1.5" stroke="currentColor" className="w-4 h-4">
<path strokeLinecap="round" strokeLinejoin="round" d="M14.74 9l-.346 9m-4.788 0L9.26 9m9.968-3.21c.342.052.682.107 1.022.166m-1.022-.165L18.16 19.673a2.25 2.25 0 01-2.244 2.077H8.084a2.25 2.25 0 01-2.244-2.077L4.772 5.79m14.456 0a48.108 48.108 0 00-3.478-.397m-12 .562c.34-.059.68-.114 1.022-.165m0 0a48.11 48.11 0 013.478-.397m7.5 0v-.916c0-1.18-.91-2.164-2.09-2.201a51.964 51.964 0 00-3.32 0c-1.18.037-2.09 1.022-2.09 2.201v.916m7.5 0a48.667 48.667 0 00-7.5 0" />
</svg>
)}
<span className="ml-1">Delete</span>
</button>
)}
</div>
)
},
},
{
Header: () => <SortableHeader column="JobId" label="Job ID" />,
accessor: 'jobId',
width: 200,
Cell: ({ value }: any) => (
<span className="font-mono text-xs">{value || '-'}</span>
),
},
], [sortBy, sortOrder, onSortChange, onRetryJob, isRetrying, onDeleteJob, isDeleting])
const tableData = useMemo(() => {
return jobs.map((job) => ({
@@ -200,8 +247,6 @@ const JobsTable: React.FC<IJobsTable> = ({
progressPercentage: job.progressPercentage,
userId: job.userId,
assignedWorkerId: job.assignedWorkerId,
bundleRequestId: job.bundleRequestId,
geneticRequestId: job.geneticRequestId,
createdAt: job.createdAt,
startedAt: job.startedAt,
completedAt: job.completedAt,
@@ -305,6 +350,39 @@ const JobsTable: React.FC<IJobsTable> = ({
)}
</>
)}
{/* Delete Confirmation Modal */}
{deleteConfirmJobId && (
<div className="fixed inset-0 bg-black/50 flex items-center justify-center z-50">
<div className="bg-base-100 p-6 rounded-lg shadow-xl max-w-md w-full mx-4">
<h3 className="text-lg font-semibold mb-4">Confirm Delete</h3>
<p className="text-base-content/70 mb-6">
Are you sure you want to delete this job? This action cannot be undone.
</p>
<div className="flex gap-3 justify-end">
<button
className="btn btn-ghost"
onClick={() => setDeleteConfirmJobId(null)}
disabled={isDeleting}
>
Cancel
</button>
<button
className="btn btn-error"
onClick={() => {
if (onDeleteJob) {
onDeleteJob(deleteConfirmJobId)
setDeleteConfirmJobId(null)
}
}}
disabled={isDeleting}
>
{isDeleting ? 'Deleting...' : 'Delete'}
</button>
</div>
</div>
</div>
)}
</div>
)
}