feat(research): inspect scoped stored market data (RP01)
This commit is contained in:
parent
b4ac6460ef
commit
5bfaf37d08
12 changed files with 1592 additions and 3 deletions
|
|
@ -3,6 +3,7 @@ import type { CanonicalDatasetPreparation } from '@stock-bot/stock-config/canoni
|
|||
import { canonicalStrategyJson } from '@stock-bot/stock-config/strategy';
|
||||
import { actionClass, fieldClass } from '@/features/strategies/components/StrategyParameterForm';
|
||||
import { backtestApiV2, DatasetPreparationRequestError } from '../services/backtestApiV2';
|
||||
import { StoredSourceInspector } from './StoredSourceInspector';
|
||||
|
||||
export interface DatasetPreparationState {
|
||||
busy: boolean;
|
||||
|
|
@ -268,6 +269,9 @@ export function DatasetPreparation({ disabled, begin, onStateChange, onRetained
|
|||
uses that captured snapshot without querying storage again.
|
||||
</p>
|
||||
)}
|
||||
{mode === 'stored-eod' && (
|
||||
<StoredSourceInspector disabled={disabled || Boolean(intent) || Boolean(phase)} />
|
||||
)}
|
||||
<label className="block">
|
||||
{mode === 'export' ? 'Source dataset JSON' : 'Source metadata JSON'}
|
||||
<input
|
||||
|
|
|
|||
|
|
@ -0,0 +1,364 @@
|
|||
import { useEffect, useRef, useState } from 'react';
|
||||
import type {
|
||||
StoredSourceCoverage,
|
||||
StoredSourceInspection,
|
||||
StoredSourceQuery,
|
||||
} from '@stock-bot/stock-config/stored-source';
|
||||
import { actionClass, fieldClass } from '@/features/strategies/components/StrategyParameterForm';
|
||||
import { storedSourceApi } from '../services/storedSourceApi';
|
||||
|
||||
const instant = (value: number | null) =>
|
||||
value === null ? 'Unavailable' : new Date(value).toISOString();
|
||||
const inputTime = (value: number | null) =>
|
||||
value === null ? '' : new Date(value).toISOString().slice(0, 19);
|
||||
const labels: Record<StoredSourceInspection['issues'][number]['code'], string> = {
|
||||
'identity-mismatch': 'Instrument identity mismatch',
|
||||
'invalid-timestamp': 'Invalid Unix-seconds timestamp',
|
||||
'outside-range': 'Returned outside the selected window',
|
||||
'invalid-prices': 'Invalid OHLC prices',
|
||||
'null-volume': 'Missing or null volume',
|
||||
'invalid-volume': 'Invalid volume',
|
||||
'duplicate-timestamp': 'Duplicate timestamp',
|
||||
};
|
||||
|
||||
/** Read-only diagnostics have their own request lifetime, never a dataset retention receipt. */
|
||||
export function StoredSourceInspector({ disabled }: { disabled: boolean }) {
|
||||
const [coverage, setCoverage] = useState<StoredSourceCoverage | null>(null);
|
||||
const [result, setResult] = useState<StoredSourceInspection | null>(null);
|
||||
const [draft, setDraft] = useState({
|
||||
instrumentId: '',
|
||||
interval: '5m' as StoredSourceQuery['interval'],
|
||||
start: '',
|
||||
end: '',
|
||||
});
|
||||
const [phase, setPhase] = useState<'coverage' | 'inspection' | null>(null);
|
||||
const [error, setError] = useState('');
|
||||
const current = useRef(draft);
|
||||
const generation = useRef(0),
|
||||
active = useRef(false),
|
||||
abort = useRef<AbortController | null>(null);
|
||||
useEffect(() => {
|
||||
active.current = true;
|
||||
return () => {
|
||||
active.current = false;
|
||||
generation.current++;
|
||||
abort.current?.abort();
|
||||
};
|
||||
}, []);
|
||||
const cancel = () => {
|
||||
generation.current++;
|
||||
abort.current?.abort();
|
||||
abort.current = null;
|
||||
setPhase(null);
|
||||
};
|
||||
const change = (next: typeof draft) => {
|
||||
if (disabled) {
|
||||
return;
|
||||
}
|
||||
cancel();
|
||||
if (
|
||||
next.instrumentId !== current.current.instrumentId ||
|
||||
next.interval !== current.current.interval
|
||||
) {
|
||||
setCoverage(null);
|
||||
}
|
||||
current.current = next;
|
||||
setDraft(next);
|
||||
setResult(null);
|
||||
setError('');
|
||||
};
|
||||
const run = async (kind: 'coverage' | 'inspection') => {
|
||||
if (disabled) {
|
||||
return;
|
||||
}
|
||||
const selected = { ...current.current };
|
||||
cancel();
|
||||
const revision = generation.current;
|
||||
const controller = new AbortController();
|
||||
abort.current = controller;
|
||||
setPhase(kind);
|
||||
setError('');
|
||||
if (kind === 'inspection') {
|
||||
setResult(null);
|
||||
} else {
|
||||
setCoverage(null);
|
||||
}
|
||||
try {
|
||||
if (kind === 'coverage') {
|
||||
const value = await storedSourceApi.coverage(
|
||||
{ instrumentId: selected.instrumentId, interval: selected.interval },
|
||||
controller.signal
|
||||
);
|
||||
if (active.current && generation.current === revision) {
|
||||
setCoverage(value);
|
||||
}
|
||||
} else {
|
||||
if (!selected.start || !selected.end) {
|
||||
throw new Error('Select UTC start and end times');
|
||||
}
|
||||
const value = await storedSourceApi.inspect(
|
||||
{
|
||||
instrumentId: selected.instrumentId,
|
||||
interval: selected.interval,
|
||||
start: Date.parse(`${selected.start}Z`),
|
||||
end: Date.parse(`${selected.end}Z`),
|
||||
},
|
||||
controller.signal
|
||||
);
|
||||
if (active.current && generation.current === revision) {
|
||||
setResult(value);
|
||||
}
|
||||
}
|
||||
} catch (cause) {
|
||||
if (active.current && generation.current === revision && !controller.signal.aborted) {
|
||||
setError(cause instanceof Error ? cause.message : 'Source inspection failed');
|
||||
}
|
||||
} finally {
|
||||
if (active.current && generation.current === revision) {
|
||||
abort.current = null;
|
||||
setPhase(null);
|
||||
}
|
||||
}
|
||||
};
|
||||
return (
|
||||
<section
|
||||
aria-label="Stored source inspection"
|
||||
className="space-y-3 rounded border p-3"
|
||||
onChange={event => event.stopPropagation()}
|
||||
onKeyDown={event => {
|
||||
if (event.key === 'Enter' && (event.target as HTMLElement).tagName === 'INPUT') {
|
||||
event.preventDefault();
|
||||
}
|
||||
}}
|
||||
>
|
||||
<p>
|
||||
Choose one qualified instrument and timeframe to inspect its stored coverage and field
|
||||
problems. Coverage examines only that series. First and last bars do not establish complete
|
||||
sessions. Unlocatable timestamps are counted in coverage; window diagnostics inspect rows
|
||||
returned by the time query. These diagnostics do not retain data or establish trading
|
||||
eligibility.
|
||||
</p>
|
||||
<div className="grid gap-2 sm:grid-cols-2">
|
||||
<label>
|
||||
Instrument
|
||||
<input
|
||||
className={fieldClass}
|
||||
aria-label="Inspect instrument"
|
||||
value={draft.instrumentId}
|
||||
disabled={disabled}
|
||||
onChange={event => change({ ...current.current, instrumentId: event.target.value })}
|
||||
placeholder="AAPL.US"
|
||||
/>
|
||||
</label>
|
||||
<label>
|
||||
Timeframe
|
||||
<select
|
||||
className={fieldClass}
|
||||
aria-label="Inspect timeframe"
|
||||
value={draft.interval}
|
||||
disabled={disabled}
|
||||
onChange={event =>
|
||||
change({
|
||||
...current.current,
|
||||
interval: event.target.value as StoredSourceQuery['interval'],
|
||||
})
|
||||
}
|
||||
>
|
||||
{['1m', '5m', '1h'].map(value => (
|
||||
<option key={value}>{value}</option>
|
||||
))}
|
||||
</select>
|
||||
</label>
|
||||
</div>
|
||||
<button
|
||||
type="button"
|
||||
className={actionClass}
|
||||
disabled={disabled || phase !== null || !draft.instrumentId}
|
||||
onClick={() => void run('coverage')}
|
||||
>
|
||||
Load selected coverage
|
||||
</button>
|
||||
{coverage && (
|
||||
<>
|
||||
{coverage.series.length === 0 ? (
|
||||
<p>
|
||||
No stored bars found for {coverage.query.instrumentId} {coverage.query.interval}.
|
||||
</p>
|
||||
) : (
|
||||
<div className="overflow-x-auto">
|
||||
<table className="w-full text-left">
|
||||
<caption>Stored bar coverage (UTC)</caption>
|
||||
<thead>
|
||||
<tr>
|
||||
<th>Instrument</th>
|
||||
<th>Timeframe</th>
|
||||
<th>Rows</th>
|
||||
<th>First bar</th>
|
||||
<th>Last bar</th>
|
||||
<th>Invalid timestamps</th>
|
||||
<th>Window</th>
|
||||
</tr>
|
||||
</thead>
|
||||
<tbody>
|
||||
{coverage.series.map(item => (
|
||||
<tr key={`${item.instrumentId}:${item.interval}`}>
|
||||
<td>{item.instrumentId}</td>
|
||||
<td>{item.interval}</td>
|
||||
<td>{item.rowCount}</td>
|
||||
<td>{instant(item.firstTimestamp)}</td>
|
||||
<td>{instant(item.lastTimestamp)}</td>
|
||||
<td>{item.invalidTimestampRows}</td>
|
||||
<td>
|
||||
<button
|
||||
type="button"
|
||||
className={actionClass}
|
||||
disabled={disabled}
|
||||
onClick={() =>
|
||||
change({
|
||||
instrumentId: item.instrumentId,
|
||||
interval: item.interval,
|
||||
start: inputTime(item.firstTimestamp),
|
||||
end: inputTime(
|
||||
item.lastTimestamp === null
|
||||
? null
|
||||
: Math.min(
|
||||
8_640_000_000_000_000,
|
||||
item.lastTimestamp +
|
||||
{ '1m': 60000, '5m': 300000, '1h': 3600000 }[item.interval]
|
||||
)
|
||||
),
|
||||
})
|
||||
}
|
||||
>
|
||||
Use coverage dates
|
||||
</button>
|
||||
</td>
|
||||
</tr>
|
||||
))}
|
||||
</tbody>
|
||||
</table>
|
||||
</div>
|
||||
)}
|
||||
</>
|
||||
)}
|
||||
<div className="grid gap-2 sm:grid-cols-2">
|
||||
<label>
|
||||
Start UTC (inclusive)
|
||||
<input
|
||||
type="datetime-local"
|
||||
step="1"
|
||||
className={fieldClass}
|
||||
aria-label="Inspect start UTC"
|
||||
value={draft.start}
|
||||
disabled={disabled}
|
||||
onChange={event => change({ ...current.current, start: event.target.value })}
|
||||
/>
|
||||
</label>
|
||||
<label>
|
||||
End UTC (exclusive)
|
||||
<input
|
||||
type="datetime-local"
|
||||
step="1"
|
||||
className={fieldClass}
|
||||
aria-label="Inspect end UTC"
|
||||
value={draft.end}
|
||||
disabled={disabled}
|
||||
onChange={event => change({ ...current.current, end: event.target.value })}
|
||||
/>
|
||||
</label>
|
||||
</div>
|
||||
<div className="flex gap-2">
|
||||
<button
|
||||
type="button"
|
||||
className={actionClass}
|
||||
disabled={disabled || phase !== null}
|
||||
onClick={() => void run('inspection')}
|
||||
>
|
||||
Inspect source window
|
||||
</button>
|
||||
{phase && (
|
||||
<button type="button" className={actionClass} onClick={cancel}>
|
||||
Cancel inspection
|
||||
</button>
|
||||
)}
|
||||
</div>
|
||||
{phase && (
|
||||
<p role="status">
|
||||
{phase === 'coverage' ? 'Loading stored coverage...' : 'Inspecting source window...'}
|
||||
</p>
|
||||
)}
|
||||
{error && <p role="alert">{error}</p>}
|
||||
{result && (
|
||||
<div className="space-y-2" aria-label="Source inspection result">
|
||||
<p>
|
||||
{result.query.instrumentId} · {result.query.interval} · {instant(result.query.start)} to{' '}
|
||||
{instant(result.query.end)} (exclusive)
|
||||
</p>
|
||||
{result.rowCount === 0 ? (
|
||||
<p>No stored bars in this window.</p>
|
||||
) : (
|
||||
<>
|
||||
<p>
|
||||
{result.rowCount} rows · {result.invalidRows} rows with detected problems ·{' '}
|
||||
{result.nullVolumeRows} missing/null volumes · {result.duplicateRows} extra rows at
|
||||
duplicate timestamps
|
||||
</p>
|
||||
<p>
|
||||
First bar: {instant(result.firstTimestamp)} · Last bar:{' '}
|
||||
{instant(result.lastTimestamp)}
|
||||
</p>
|
||||
{result.issues.length === 0 ? (
|
||||
<p>
|
||||
No listed field problems detected. Calendar, availability, corporate actions and
|
||||
full dataset validation are still required.
|
||||
</p>
|
||||
) : (
|
||||
<ul>
|
||||
{result.issues.map(issue => (
|
||||
<li key={issue.code}>
|
||||
<details>
|
||||
<summary>
|
||||
{labels[issue.code]}: {issue.rowCount} rows
|
||||
</summary>
|
||||
<ul>
|
||||
{issue.timestamps.map((at, index) => (
|
||||
<li key={index}>{instant(at)}</li>
|
||||
))}
|
||||
</ul>
|
||||
</details>
|
||||
</li>
|
||||
))}
|
||||
</ul>
|
||||
)}
|
||||
<details>
|
||||
<summary>Rows by UTC date</summary>
|
||||
<table>
|
||||
<thead>
|
||||
<tr>
|
||||
<th>Date</th>
|
||||
<th>Rows</th>
|
||||
<th>Rows with problems</th>
|
||||
</tr>
|
||||
</thead>
|
||||
<tbody>
|
||||
{result.days.map(day => (
|
||||
<tr key={day.date}>
|
||||
<td>{day.date}</td>
|
||||
<td>{day.rowCount}</td>
|
||||
<td>{day.invalidRows}</td>
|
||||
</tr>
|
||||
))}
|
||||
</tbody>
|
||||
</table>
|
||||
</details>
|
||||
</>
|
||||
)}
|
||||
<p>
|
||||
No rows were changed or excluded. Use reviewed source metadata with the existing preview
|
||||
below.
|
||||
</p>
|
||||
</div>
|
||||
)}
|
||||
</section>
|
||||
);
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue