feat(research): download validated retained datasets (RP07)
This commit is contained in:
parent
43ea2cc9b6
commit
b959779ada
14 changed files with 1076 additions and 11 deletions
|
|
@ -3,7 +3,11 @@ import { getLogger } from '@stock-bot/logger';
|
|||
import { requireServices, type IServiceContainer } from '@stock-bot/services';
|
||||
import { HistoricalDataRequestError } from '@stock-bot/types';
|
||||
import { StorageService } from '../../../orchestrator/src/services/StorageService';
|
||||
import { canonicalDatasetEntry } from '../services/canonical-research.service';
|
||||
import {
|
||||
canonicalDatasetEntry,
|
||||
CanonicalDatasetReadError,
|
||||
readCanonicalDataset,
|
||||
} from '../services/canonical-research.service';
|
||||
import { DatasetCatalogService } from '../services/dataset-catalog.service';
|
||||
import {
|
||||
DatasetPreparationError,
|
||||
|
|
@ -156,6 +160,38 @@ export function createDatasetRoutes(container: IServiceContainer) {
|
|||
return c.json({ error: 'Failed to list canonical datasets' }, 500);
|
||||
}
|
||||
});
|
||||
app.get('/api/v2/datasets/canonical/:hash/export', async c => {
|
||||
c.header('Cache-Control', 'no-store');
|
||||
c.header('X-Content-Type-Options', 'nosniff');
|
||||
try {
|
||||
c.req.raw.signal.throwIfAborted();
|
||||
const hash = c.req.param('hash');
|
||||
const dataset = await readCanonicalDataset(database, hash, c.req.raw.signal);
|
||||
c.req.raw.signal.throwIfAborted();
|
||||
return Response.json(dataset, {
|
||||
headers: {
|
||||
'Cache-Control': 'no-store',
|
||||
'X-Content-Type-Options': 'nosniff',
|
||||
'Content-Disposition': `attachment; filename="dataset-${hash}.json"`,
|
||||
},
|
||||
});
|
||||
} catch (error) {
|
||||
if (c.req.raw.signal.aborted) {
|
||||
return Response.json(
|
||||
{ error: 'Dataset export cancelled' },
|
||||
{
|
||||
status: 499,
|
||||
headers: { 'Cache-Control': 'no-store', 'X-Content-Type-Options': 'nosniff' },
|
||||
}
|
||||
);
|
||||
}
|
||||
if (error instanceof CanonicalDatasetReadError) {
|
||||
return c.json({ error: error.message }, error.status);
|
||||
}
|
||||
logger.error('Failed to export canonical dataset', error);
|
||||
return c.json({ error: 'Failed to export canonical dataset' }, 500);
|
||||
}
|
||||
});
|
||||
app.get('/api/v2/datasets/canonical/:hash', async c => {
|
||||
try {
|
||||
const hash = c.req.param('hash');
|
||||
|
|
|
|||
|
|
@ -50,6 +50,16 @@ const invalid = (message: string): never => {
|
|||
throw new HistoricalDataRequestError(message);
|
||||
};
|
||||
|
||||
/** Read rejection statuses for exports; existing callers may still catch the base request error. */
|
||||
export class CanonicalDatasetReadError extends HistoricalDataRequestError {
|
||||
constructor(
|
||||
readonly status: 400 | 404 | 409 | 422,
|
||||
message: string
|
||||
) {
|
||||
super(message);
|
||||
}
|
||||
}
|
||||
|
||||
/** The existing workbench gate, shared by retained selection and source preview. */
|
||||
export function canonicalDatasetUnavailableReason(dataset: HistoricalDatasetV2): string | null {
|
||||
return datasetUnavailableReason(dataset.manifest);
|
||||
|
|
@ -61,28 +71,31 @@ export function canonicalDatasetUnavailableReason(dataset: HistoricalDatasetV2):
|
|||
*/
|
||||
export async function readCanonicalDataset(
|
||||
database: DatasetCatalogQuery,
|
||||
hash: unknown
|
||||
hash: unknown,
|
||||
signal?: AbortSignal
|
||||
): Promise<HistoricalDatasetV2> {
|
||||
signal?.throwIfAborted();
|
||||
if (typeof hash !== 'string' || !/^[a-f0-9]{64}$/.test(hash)) {
|
||||
return invalid('Choose an explicitly retained v2 dataset');
|
||||
throw new CanonicalDatasetReadError(400, 'Choose an explicitly retained v2 dataset');
|
||||
}
|
||||
const row = (await database.query('SELECT payload FROM backtest_datasets WHERE hash=$1', [hash]))
|
||||
.rows[0];
|
||||
signal?.throwIfAborted();
|
||||
if (!row) {
|
||||
return invalid('Selected canonical dataset was not found');
|
||||
throw new CanonicalDatasetReadError(404, 'Selected canonical dataset was not found');
|
||||
}
|
||||
let dataset;
|
||||
try {
|
||||
dataset = validateDatasetV2(row.payload);
|
||||
} catch {
|
||||
return invalid('Retained v2 dataset failed content validation');
|
||||
throw new CanonicalDatasetReadError(409, 'Retained v2 dataset failed content validation');
|
||||
}
|
||||
if (dataset.manifest.hash !== hash) {
|
||||
return invalid('Retained dataset identity mismatch');
|
||||
throw new CanonicalDatasetReadError(409, 'Retained dataset identity mismatch');
|
||||
}
|
||||
const unavailable = canonicalDatasetUnavailableReason(dataset);
|
||||
if (unavailable) {
|
||||
return invalid(unavailable);
|
||||
throw new CanonicalDatasetReadError(422, unavailable);
|
||||
}
|
||||
return dataset;
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue