Add delete backtests by filters

This commit is contained in:
2025-10-15 00:28:25 +07:00
parent 48c2d20d70
commit b3f3bccd72
8 changed files with 326 additions and 1 deletions

View File

@@ -194,6 +194,10 @@ const BacktestTable: React.FC<BacktestTableProps> = ({list, isFetching, onSortCh
const [durationMinDays, setDurationMinDays] = useState<number | null>(null)
const [durationMaxDays, setDurationMaxDays] = useState<number | null>(null)
// Delete confirmation state
const [showDeleteConfirm, setShowDeleteConfirm] = useState(false)
const [isDeleting, setIsDeleting] = useState(false)
const applyFilters = () => {
if (!onFiltersChange) return
onFiltersChange({
@@ -211,6 +215,62 @@ const BacktestTable: React.FC<BacktestTableProps> = ({list, isFetching, onSortCh
setIsFilterOpen(false)
}
const deleteFilteredBacktests = async () => {
if (!filters) return
setIsDeleting(true)
try {
const backtestClient = new BacktestClient({}, apiUrl)
const response = await backtestClient.backtest_DeleteBacktestsByFilters(
filters.scoreMin || undefined,
filters.scoreMax || undefined,
filters.winrateMin || undefined,
filters.winrateMax || undefined,
filters.maxDrawdownMax || undefined,
filters.tickers?.join(',') || undefined,
filters.indicators?.join(',') || undefined,
filters.durationMinDays || undefined,
filters.durationMaxDays || undefined,
filters.nameContains || undefined
)
// Parse the response to get the deleted count
const responseText = await response.data.text()
const result = JSON.parse(responseText)
const successToast = new Toast(`Successfully deleted ${result.deletedCount} backtests`, false)
// Refresh the data by calling the callback
if (onBacktestDeleted) {
onBacktestDeleted()
}
setShowDeleteConfirm(false)
} catch (error) {
console.error('Error deleting filtered backtests:', error)
const errorToast = new Toast('Failed to delete backtests', false)
} finally {
setIsDeleting(false)
}
}
const hasActiveFilters = () => {
if (!filters) return false
return !!(
filters.nameContains ||
filters.scoreMin !== null ||
filters.scoreMax !== null ||
filters.winrateMin !== null ||
filters.winrateMax !== null ||
filters.maxDrawdownMax !== null ||
filters.tickers?.length ||
filters.indicators?.length ||
filters.durationMinDays !== null ||
filters.durationMaxDays !== null
)
}
const clearDuration = () => {
setDurationMinDays(null)
setDurationMaxDays(null)
@@ -569,8 +629,17 @@ const BacktestTable: React.FC<BacktestTableProps> = ({list, isFetching, onSortCh
) : (
<>
{/* Filters toggle button */}
<div className="flex w-full justify-end">
<div className="flex w-full justify-end gap-2">
<button className="btn btn-sm btn-outline" onClick={() => setIsFilterOpen(true)}>Filters</button>
{hasActiveFilters() && (
<button
className="btn btn-sm btn-error"
onClick={() => setShowDeleteConfirm(true)}
disabled={isDeleting}
>
{isDeleting ? 'Deleting...' : 'Delete Filtered'}
</button>
)}
</div>
<ServerSortableTable
@@ -717,6 +786,34 @@ const BacktestTable: React.FC<BacktestTableProps> = ({list, isFetching, onSortCh
onClose={handleCloseConfigDisplayModal}
backtest={selectedBacktestForConfigView}
/>
{/* Delete Confirmation Modal */}
{showDeleteConfirm && (
<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 all backtests matching the current filters? This action cannot be undone.
</p>
<div className="flex gap-3 justify-end">
<button
className="btn btn-ghost"
onClick={() => setShowDeleteConfirm(false)}
disabled={isDeleting}
>
Cancel
</button>
<button
className="btn btn-error"
onClick={deleteFilteredBacktests}
disabled={isDeleting}
>
{isDeleting ? 'Deleting...' : 'Delete'}
</button>
</div>
</div>
</div>
)}
</>
)}
</>

View File

@@ -577,6 +577,66 @@ export class BacktestClient extends AuthorizedApiBase {
return Promise.resolve<FileResponse>(null as any);
}
backtest_DeleteBacktestsByFilters(scoreMin: number | null | undefined, scoreMax: number | null | undefined, winrateMin: number | null | undefined, winrateMax: number | null | undefined, maxDrawdownMax: number | null | undefined, tickers: string | null | undefined, indicators: string | null | undefined, durationMinDays: number | null | undefined, durationMaxDays: number | null | undefined, name: string | null | undefined): Promise<FileResponse> {
let url_ = this.baseUrl + "/Backtest/ByFilters?";
if (scoreMin !== undefined && scoreMin !== null)
url_ += "scoreMin=" + encodeURIComponent("" + scoreMin) + "&";
if (scoreMax !== undefined && scoreMax !== null)
url_ += "scoreMax=" + encodeURIComponent("" + scoreMax) + "&";
if (winrateMin !== undefined && winrateMin !== null)
url_ += "winrateMin=" + encodeURIComponent("" + winrateMin) + "&";
if (winrateMax !== undefined && winrateMax !== null)
url_ += "winrateMax=" + encodeURIComponent("" + winrateMax) + "&";
if (maxDrawdownMax !== undefined && maxDrawdownMax !== null)
url_ += "maxDrawdownMax=" + encodeURIComponent("" + maxDrawdownMax) + "&";
if (tickers !== undefined && tickers !== null)
url_ += "tickers=" + encodeURIComponent("" + tickers) + "&";
if (indicators !== undefined && indicators !== null)
url_ += "indicators=" + encodeURIComponent("" + indicators) + "&";
if (durationMinDays !== undefined && durationMinDays !== null)
url_ += "durationMinDays=" + encodeURIComponent("" + durationMinDays) + "&";
if (durationMaxDays !== undefined && durationMaxDays !== null)
url_ += "durationMaxDays=" + encodeURIComponent("" + durationMaxDays) + "&";
if (name !== undefined && name !== null)
url_ += "name=" + encodeURIComponent("" + name) + "&";
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.processBacktest_DeleteBacktestsByFilters(_response);
});
}
protected processBacktest_DeleteBacktestsByFilters(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);
}
backtest_GetBacktestsByRequestId(requestId: string): Promise<Backtest[]> {
let url_ = this.baseUrl + "/Backtest/ByRequestId/{requestId}";
if (requestId === undefined || requestId === null)
@@ -4310,6 +4370,7 @@ export enum BacktestSortableColumn {
Fees = "Fees",
SharpeRatio = "SharpeRatio",
Ticker = "Ticker",
Name = "Name",
}
export interface LightBacktest {

View File

@@ -545,6 +545,7 @@ export enum BacktestSortableColumn {
Fees = "Fees",
SharpeRatio = "SharpeRatio",
Ticker = "Ticker",
Name = "Name",
}
export interface LightBacktest {