Stop substituting synthetic market data when providers fail
- Replace synthetic fallback in getQuote()/getPriceHistory() with null returns
- Add QuoteResult/PriceHistoryResult types with { value, stale } structure
- Implement stale-while-revalidate: return cached value with stale=true on live fetch failure
- Cache failures for 30s to avoid hammering provider
- Update CompanyAnalysis type to use PriceData<T> wrapper
- Update task-processors to track failed/stale tickers explicitly
- Update price-history-card UI to show unavailable state and stale indicator
- Add comprehensive tests for failure cases
- Add e2e tests for null data, stale data, and live data scenarios
Resolves #14
This commit is contained in:
@@ -4,13 +4,13 @@ import { format } from 'date-fns';
|
||||
import { Panel } from '@/components/ui/panel';
|
||||
import { formatCurrency } from '@/lib/format';
|
||||
import { InteractivePriceChart } from '@/components/charts/interactive-price-chart';
|
||||
import type { DataSeries, Holding } from '@/lib/types';
|
||||
import type { DataSeries, Holding, PriceData } from '@/lib/types';
|
||||
|
||||
type PriceHistoryCardProps = {
|
||||
loading: boolean;
|
||||
priceHistory: Array<{ date: string; close: number }>;
|
||||
benchmarkHistory: Array<{ date: string; close: number }>;
|
||||
quote: number;
|
||||
priceHistory: PriceData<Array<{ date: string; close: number }> | null>;
|
||||
benchmarkHistory: PriceData<Array<{ date: string; close: number }> | null>;
|
||||
quote: PriceData<number | null>;
|
||||
position: Holding | null;
|
||||
};
|
||||
|
||||
@@ -29,9 +29,14 @@ function asFiniteNumber(value: string | number | null | undefined) {
|
||||
}
|
||||
|
||||
export function PriceHistoryCard(props: PriceHistoryCardProps) {
|
||||
const firstPoint = props.priceHistory[0];
|
||||
const lastPoint = props.priceHistory[props.priceHistory.length - 1];
|
||||
const priceHistoryValue = props.priceHistory.value;
|
||||
const benchmarkHistoryValue = props.benchmarkHistory.value;
|
||||
const quoteValue = props.quote.value;
|
||||
|
||||
const firstPoint = priceHistoryValue?.[0];
|
||||
const lastPoint = priceHistoryValue?.[priceHistoryValue.length - 1];
|
||||
const inPortfolio = props.position !== null;
|
||||
const hasPriceData = priceHistoryValue !== null && priceHistoryValue.length > 0;
|
||||
|
||||
const defaultChange = firstPoint && lastPoint ? lastPoint.close - firstPoint.close : null;
|
||||
const defaultChangePct = firstPoint && firstPoint.close > 0 && defaultChange !== null
|
||||
@@ -54,10 +59,16 @@ export function PriceHistoryCard(props: PriceHistoryCardProps) {
|
||||
const statusToneClass = inPortfolio ? 'text-[#96f5bf]' : 'text-[color:var(--terminal-bright)]';
|
||||
const performanceToneClass = change !== null && change < 0 ? 'text-[#ff9f9f]' : 'text-[#96f5bf]';
|
||||
|
||||
const chartData = props.priceHistory.map(point => ({
|
||||
const chartData = priceHistoryValue?.map(point => ({
|
||||
date: point.date,
|
||||
price: point.close
|
||||
}));
|
||||
})) ?? [];
|
||||
|
||||
const benchmarkData = benchmarkHistoryValue?.map((point) => ({
|
||||
date: point.date,
|
||||
price: point.close
|
||||
})) ?? [];
|
||||
|
||||
const comparisonSeries: DataSeries[] = [
|
||||
{
|
||||
id: 'stock',
|
||||
@@ -69,22 +80,28 @@ export function PriceHistoryCard(props: PriceHistoryCardProps) {
|
||||
{
|
||||
id: 'sp500',
|
||||
label: 'S&P 500',
|
||||
data: props.benchmarkHistory.map((point) => ({
|
||||
date: point.date,
|
||||
price: point.close
|
||||
})),
|
||||
data: benchmarkData,
|
||||
color: '#8ba0b8',
|
||||
type: 'line'
|
||||
}
|
||||
];
|
||||
|
||||
const helperText = Number.isFinite(props.quote)
|
||||
? `Spot price ${formatCurrency(props.quote)}`
|
||||
const quoteAvailable = quoteValue !== null && Number.isFinite(quoteValue);
|
||||
const staleIndicator = props.quote.stale ? ' (stale)' : '';
|
||||
const helperText = quoteAvailable
|
||||
? `Spot price ${formatCurrency(quoteValue)}${staleIndicator}`
|
||||
: 'Spot price unavailable';
|
||||
|
||||
return (
|
||||
<Panel title="Price chart" subtitle="Interactive chart with historical data">
|
||||
<Panel title="Price chart" subtitle={hasPriceData ? 'Interactive chart with historical data' : 'Price data unavailable'}>
|
||||
<div className="space-y-4">
|
||||
{!hasPriceData && (
|
||||
<div className="rounded border border-[color:var(--line-weak)] bg-[color:var(--surface-dim)] px-4 py-3">
|
||||
<p className="text-sm text-[color:var(--terminal-muted)]">
|
||||
Price history data is currently unavailable. This may be due to a temporary issue with the market data provider.
|
||||
</p>
|
||||
</div>
|
||||
)}
|
||||
<div className="grid grid-cols-1 gap-4 sm:grid-cols-3">
|
||||
<div className="border-b border-[color:var(--line-weak)] px-4 py-4 sm:border-b-0 sm:border-r">
|
||||
<p className="text-xs uppercase tracking-[0.14em] text-[color:var(--terminal-muted)]">Portfolio status</p>
|
||||
@@ -108,20 +125,22 @@ export function PriceHistoryCard(props: PriceHistoryCardProps) {
|
||||
</div>
|
||||
</div>
|
||||
|
||||
<InteractivePriceChart
|
||||
data={chartData}
|
||||
dataSeries={comparisonSeries}
|
||||
defaultChartType="line"
|
||||
defaultTimeRange="1Y"
|
||||
showVolume={false}
|
||||
showToolbar={true}
|
||||
height={320}
|
||||
loading={props.loading}
|
||||
formatters={{
|
||||
price: formatCurrency,
|
||||
date: (date: string) => format(new Date(date), 'MMM dd, yyyy')
|
||||
}}
|
||||
/>
|
||||
{hasPriceData && (
|
||||
<InteractivePriceChart
|
||||
data={chartData}
|
||||
dataSeries={comparisonSeries}
|
||||
defaultChartType="line"
|
||||
defaultTimeRange="1Y"
|
||||
showVolume={false}
|
||||
showToolbar={true}
|
||||
height={320}
|
||||
loading={props.loading}
|
||||
formatters={{
|
||||
price: formatCurrency,
|
||||
date: (date: string) => format(new Date(date), 'MMM dd, yyyy')
|
||||
}}
|
||||
/>
|
||||
)}
|
||||
</div>
|
||||
</Panel>
|
||||
);
|
||||
|
||||
Reference in New Issue
Block a user