feat(research): download original canonical run results (RP08)
This commit is contained in:
parent
8e9d0cf619
commit
e8a6b70968
8 changed files with 245 additions and 13 deletions
|
|
@ -3,6 +3,7 @@ import { Line, LineChart, ResponsiveContainer, Tooltip, XAxis, YAxis } from 'rec
|
|||
import type { CanonicalRunResult } from '@stock-bot/stock-config/canonical-research';
|
||||
import type { CanonicalMetrics } from '@stock-bot/types';
|
||||
import { actionClass } from '@/features/strategies/components/StrategyParameterForm';
|
||||
import { downloadJsonText } from '../services/downloadJsonText';
|
||||
import { DatasetDownload } from './DatasetDownload';
|
||||
|
||||
const tableClass = 'w-full text-sm text-left border-collapse';
|
||||
|
|
@ -30,6 +31,31 @@ function calendarDuration(milliseconds: string): string {
|
|||
return parts.join(', ') || '0 seconds';
|
||||
}
|
||||
const reasonText = (reason: string | undefined) => reason?.replace(/-/g, ' ') ?? 'not reported';
|
||||
|
||||
function RunResultDownload({ result }: { result: CanonicalRunResult }) {
|
||||
const [error, setError] = useState('');
|
||||
const download = () => {
|
||||
setError('');
|
||||
try {
|
||||
downloadJsonText(JSON.stringify(result), `run-${result.runId}.json`);
|
||||
} catch (cause) {
|
||||
setError(cause instanceof Error ? cause.message : 'Could not download run result');
|
||||
}
|
||||
};
|
||||
return (
|
||||
<div className="space-y-2">
|
||||
<button type="button" className={actionClass} onClick={download}>
|
||||
{error ? 'Retry run result download' : 'Download run result'}
|
||||
</button>
|
||||
{error && (
|
||||
<p role="alert" className="text-error text-sm">
|
||||
{error}
|
||||
</p>
|
||||
)}
|
||||
</div>
|
||||
);
|
||||
}
|
||||
|
||||
const metricFields: { key: keyof CanonicalMetrics; label: string; percentage?: boolean }[] = [
|
||||
{ key: 'totalReturn', label: 'Total return', percentage: true },
|
||||
{ key: 'cagr', label: 'CAGR', percentage: true },
|
||||
|
|
@ -147,6 +173,7 @@ export function CanonicalResults({
|
|||
</dd>
|
||||
</div>
|
||||
</dl>
|
||||
<RunResultDownload key={result.runId} result={result} />
|
||||
<DatasetDownload hash={request.datasetHash} />
|
||||
<details>
|
||||
<summary className="cursor-pointer">Immutable configuration and reporting policy</summary>
|
||||
|
|
|
|||
|
|
@ -1,6 +1,7 @@
|
|||
import { useEffect, useRef, useState } from 'react';
|
||||
import { actionClass } from '@/features/strategies/components/StrategyParameterForm';
|
||||
import { backtestApiV2 } from '../services/backtestApiV2';
|
||||
import { downloadJsonText } from '../services/downloadJsonText';
|
||||
|
||||
/** Changing the dataset discards pending transport state before another download is possible. */
|
||||
export function DatasetDownload({ hash }: { hash: string }) {
|
||||
|
|
@ -35,18 +36,7 @@ function Download({ hash }: { hash: string }) {
|
|||
if (!current()) {
|
||||
return;
|
||||
}
|
||||
const url = URL.createObjectURL(new Blob([text], { type: 'application/json' }));
|
||||
const anchor = document.createElement('a');
|
||||
try {
|
||||
anchor.href = url;
|
||||
anchor.download = `dataset-${hash}.json`;
|
||||
document.body.append(anchor);
|
||||
anchor.click();
|
||||
} finally {
|
||||
anchor.remove();
|
||||
// Allow the browser to consume the click before releasing the URL, even after unmount.
|
||||
setTimeout(() => URL.revokeObjectURL(url), 0);
|
||||
}
|
||||
downloadJsonText(text, `dataset-${hash}.json`);
|
||||
} catch (cause) {
|
||||
if (current()) {
|
||||
setError(cause instanceof Error ? cause.message : 'Could not download retained dataset');
|
||||
|
|
|
|||
|
|
@ -0,0 +1,15 @@
|
|||
/** Save inert JSON text and release the browser URL after it consumes the click. */
|
||||
export function downloadJsonText(text: string, filename: string): void {
|
||||
const url = URL.createObjectURL(new Blob([text], { type: 'application/json' }));
|
||||
let anchor: HTMLAnchorElement | undefined;
|
||||
try {
|
||||
anchor = document.createElement('a');
|
||||
anchor.href = url;
|
||||
anchor.download = filename;
|
||||
document.body.append(anchor);
|
||||
anchor.click();
|
||||
} finally {
|
||||
setTimeout(() => URL.revokeObjectURL(url), 0);
|
||||
anchor?.remove();
|
||||
}
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue