feat(research): select and reuse frozen datasets in workbench
This commit is contained in:
parent
6a67ae8683
commit
b106c1ca85
29 changed files with 2295 additions and 426 deletions
|
|
@ -1,6 +1,9 @@
|
|||
import type { BacktestRunResult } from '@stock-bot/types';
|
||||
import type {
|
||||
BacktestRunDatasetSelection,
|
||||
BacktestRunResult,
|
||||
HistoricalDatasetCatalogEntry,
|
||||
} from '@stock-bot/types';
|
||||
import { API_BASE_URL, apiFetch as fetch } from '@/lib/api';
|
||||
import { browserLog } from '@/utils/browserLog';
|
||||
|
||||
export interface Backtest {
|
||||
id: string;
|
||||
|
|
@ -31,6 +34,7 @@ export interface Run {
|
|||
currentSimulationDate?: string;
|
||||
createdAt: string;
|
||||
updatedAt: string;
|
||||
dataset?: BacktestRunDatasetSelection;
|
||||
}
|
||||
|
||||
export interface CreateBacktestRequest {
|
||||
|
|
@ -56,7 +60,43 @@ export class RunResultsUnavailableError extends Error {
|
|||
}
|
||||
}
|
||||
|
||||
async function requestError(response: Response, fallback: string): Promise<Error> {
|
||||
const payload: unknown = await response.json().catch(() => null);
|
||||
const message =
|
||||
payload &&
|
||||
typeof payload === 'object' &&
|
||||
'error' in payload &&
|
||||
typeof payload.error === 'string'
|
||||
? payload.error
|
||||
: `${fallback}: ${response.status} ${response.statusText}`;
|
||||
return new Error(message);
|
||||
}
|
||||
|
||||
export const backtestApiV2 = {
|
||||
async listDatasets(
|
||||
limit = 50,
|
||||
offset = 0,
|
||||
signal?: AbortSignal
|
||||
): Promise<HistoricalDatasetCatalogEntry[]> {
|
||||
const response = await fetch(
|
||||
`${API_BASE_URL}/api/v2/datasets?limit=${limit}&offset=${offset}`,
|
||||
{ signal }
|
||||
);
|
||||
if (!response.ok) {
|
||||
throw await requestError(response, 'Failed to load frozen datasets');
|
||||
}
|
||||
return (await response.json()).datasets;
|
||||
},
|
||||
|
||||
async getDataset(hash: string, signal?: AbortSignal): Promise<HistoricalDatasetCatalogEntry> {
|
||||
const response = await fetch(`${API_BASE_URL}/api/v2/datasets/${encodeURIComponent(hash)}`, {
|
||||
signal,
|
||||
});
|
||||
if (!response.ok) {
|
||||
throw await requestError(response, 'Failed to load frozen dataset');
|
||||
}
|
||||
return (await response.json()).dataset;
|
||||
},
|
||||
// Backtest operations
|
||||
async createBacktest(request: CreateBacktestRequest): Promise<Backtest> {
|
||||
const response = await fetch(`${API_BASE_URL}/api/v2/backtests`, {
|
||||
|
|
@ -69,7 +109,7 @@ export const backtestApiV2 = {
|
|||
});
|
||||
|
||||
if (!response.ok) {
|
||||
throw new Error(`Failed to create backtest: ${response.statusText}`);
|
||||
throw await requestError(response, 'Failed to create backtest');
|
||||
}
|
||||
|
||||
return response.json();
|
||||
|
|
@ -107,7 +147,7 @@ export const backtestApiV2 = {
|
|||
});
|
||||
|
||||
if (!response.ok) {
|
||||
throw new Error(`Failed to update backtest: ${response.statusText}`);
|
||||
throw await requestError(response, 'Failed to update backtest');
|
||||
}
|
||||
|
||||
return response.json();
|
||||
|
|
@ -139,9 +179,7 @@ export const backtestApiV2 = {
|
|||
});
|
||||
|
||||
if (!response.ok) {
|
||||
const errorText = await response.text();
|
||||
browserLog.error(`Failed to create run:`, response.status, errorText);
|
||||
throw new Error(`Failed to create run: ${response.statusText}`);
|
||||
throw await requestError(response, 'Failed to create run');
|
||||
}
|
||||
|
||||
return response.json();
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue