File size: 10,645 Bytes
ec8acdf
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
/**
 * Live data teasers for the root welcome landing page.
 *
 * Strategy: render the committed fallback (src/generated/teasers.json)
 * immediately, then upgrade each card in place when its live fetch succeeds.
 * Anonymous access uses the same free `wm-session` HttpOnly cookie the
 * dashboard mints on boot (POST /api/wm-session, ref src/services/wm-session.ts
 * in the main package). A card only earns its LIVE badge when the fetch
 * succeeded AND the response doesn't flag itself degraded/stale/rate-limited.
 *
 * The response interfaces below are hand-copied subsets of the dashboard's
 * generated proto-JSON clients (src/generated/client/worldmonitor/...) —
 * pro-test is an isolated package and must not import them directly.
 */

import fallbackJson from '../generated/teasers.json';

export interface TeaserHeadline {
  title: string;
  source: string;
  publishedAt: number;
}

export interface TeaserCiiScore {
  region: string;
  combinedScore: number;
  trend: string;
}

export interface TeaserChokepoint {
  name: string;
  status: string;
  disruptionScore: number;
}

export interface TeaserQuote {
  symbol: string;
  display: string;
  price: number;
  change: number;
  sparkline: number[];
}

export interface TeaserState {
  headlines: { items: TeaserHeadline[]; live: boolean };
  cii: { items: TeaserCiiScore[]; live: boolean };
  chokepoints: { items: TeaserChokepoint[]; total: number; disrupted: number; live: boolean };
  quotes: { items: TeaserQuote[]; live: boolean };
}

const FETCH_TIMEOUT_MS = 5000;

interface FallbackShape {
  headlines: TeaserHeadline[];
  cii: TeaserCiiScore[];
  chokepoints: TeaserChokepoint[];
  chokepointTotal: number;
  quotes: TeaserQuote[];
}

const fallback = fallbackJson as unknown as FallbackShape;

const isDisrupted = (c: { status: string }) => c.status !== 'green';

export function getFallbackTeasers(): TeaserState {
  return {
    headlines: { items: fallback.headlines, live: false },
    cii: { items: fallback.cii, live: false },
    chokepoints: {
      items: fallback.chokepoints,
      total: fallback.chokepointTotal,
      disrupted: fallback.chokepoints.filter(isDisrupted).length,
      live: false,
    },
    quotes: { items: fallback.quotes, live: false },
  };
}

let sessionMinted = false;

async function mintSession(): Promise<void> {
  try {
    const resp = await fetch('/api/wm-session', {
      method: 'POST',
      headers: { 'Content-Type': 'application/json' },
      credentials: 'include',
      signal: AbortSignal.timeout(FETCH_TIMEOUT_MS),
    });
    // fetch() resolves on 4xx/5xx too (e.g. CORS-blocked preview domains) —
    // only an OK response means the cookie actually exists, otherwise the
    // 401-retry path in fetchJson would re-mint and re-fire every request.
    if (resp.ok) sessionMinted = true;
  } catch { /* fall back to static teasers */ }
}

async function fetchJson<T>(path: string): Promise<T | null> {
  const doFetch = () => fetch(path, {
    credentials: 'include',
    headers: { Accept: 'application/json' },
    signal: AbortSignal.timeout(FETCH_TIMEOUT_MS),
  });
  try {
    let resp = await doFetch();
    if (resp.status === 401 && sessionMinted) {
      // Cookie may have expired between mint and call — re-mint once.
      sessionMinted = false;
      await mintSession();
      if (!sessionMinted) return null;
      resp = await doFetch();
    }
    if (!resp.ok) return null;
    return await resp.json() as T;
  } catch {
    return null;
  }
}

interface RiskScoresResponse {
  ciiScores?: Array<{ region?: string; combinedScore?: number; trend?: string }>;
  degraded?: boolean;
  stale?: boolean;
}

async function fetchCii(): Promise<{ items: TeaserCiiScore[]; live: boolean } | null> {
  const resp = await fetchJson<RiskScoresResponse>('/api/intelligence/v1/get-risk-scores?region=');
  if (!resp || !Array.isArray(resp.ciiScores)) return null;
  const items = resp.ciiScores
    .filter(s => typeof s.region === 'string' && typeof s.combinedScore === 'number')
    .sort((a, b) => (b.combinedScore ?? 0) - (a.combinedScore ?? 0))
    .slice(0, 5)
    .map(s => ({ region: s.region as string, combinedScore: s.combinedScore as number, trend: s.trend ?? '' }));
  if (!items.length) return null;
  return { items, live: !resp.degraded && !resp.stale };
}

interface ChokepointStatusResponse {
  chokepoints?: Array<{ name?: string; status?: string; disruptionScore?: number }>;
  upstreamUnavailable?: boolean;
}

async function fetchChokepoints(): Promise<{ items: TeaserChokepoint[]; total: number; disrupted: number; live: boolean } | null> {
  const resp = await fetchJson<ChokepointStatusResponse>('/api/supply-chain/v1/get-chokepoint-status');
  if (!resp || !Array.isArray(resp.chokepoints)) return null;
  const all = resp.chokepoints
    .filter(c => typeof c.name === 'string' && typeof c.status === 'string')
    .map(c => ({ name: c.name as string, status: c.status as string, disruptionScore: c.disruptionScore ?? 0 }));
  if (!all.length) return null;
  const items = [...all].sort((a, b) => b.disruptionScore - a.disruptionScore).slice(0, 5);
  // "N of M disrupted" must count across ALL chokepoints, not the top-5 slice.
  return { items, total: all.length, disrupted: all.filter(isDisrupted).length, live: !resp.upstreamUnavailable };
}

interface MarketQuotesResponse {
  quotes?: Array<{ symbol?: string; display?: string; price?: number; change?: number; sparkline?: number[] }>;
  rateLimited?: boolean;
}

interface CommodityQuotesResponse {
  quotes?: Array<{ symbol?: string; display?: string; price?: number; change?: number; sparkline?: number[] }>;
}

interface CryptoQuotesResponse {
  quotes?: Array<{ symbol?: string; name?: string; price?: number; change?: number; sparkline?: number[] }>;
}

const MARKET_QUOTE_SYMBOLS = ['^GSPC', '^IXIC', '^VIX'];
const COMMODITY_QUOTE_SYMBOLS = ['CL=F', 'BZ=F', 'GC=F', 'HG=F', 'NG=F', 'EURUSD=X', 'USDJPY=X'];
const CRYPTO_QUOTE_IDS = ['bitcoin', 'ethereum'];
const QUOTE_SYMBOLS = ['^GSPC', '^IXIC', '^VIX', 'BTC', 'ETH', 'CL=F', 'BZ=F', 'GC=F', 'HG=F', 'NG=F', 'EURUSD=X', 'USDJPY=X'];

const QUOTE_LABELS: Record<string, string> = {
  '^GSPC': 'S&P 500',
  '^IXIC': 'Nasdaq',
  '^VIX': 'VIX',
  BTC: 'Bitcoin',
  ETH: 'Ethereum',
  'CL=F': 'WTI crude',
  'BZ=F': 'Brent',
  'GC=F': 'Gold',
  'HG=F': 'Copper',
  'NG=F': 'Nat gas',
  'EURUSD=X': 'EUR/USD',
  'USDJPY=X': 'USD/JPY',
};

function normalizeQuote(q: { symbol?: string; display?: string; name?: string; price?: number; change?: number; sparkline?: number[] }): TeaserQuote | null {
  if (typeof q.symbol !== 'string' || typeof q.price !== 'number' || q.price <= 0) return null;
  return {
    symbol: q.symbol,
    display: QUOTE_LABELS[q.symbol] ?? q.display ?? q.name ?? q.symbol,
    price: q.price,
    change: q.change ?? 0,
    sparkline: Array.isArray(q.sparkline) ? q.sparkline : [],
  };
}

function orderAndBackfillQuotes(liveItems: TeaserQuote[]): TeaserQuote[] {
  const bySymbol = new Map<string, TeaserQuote>();
  for (const q of fallback.quotes) bySymbol.set(q.symbol, q);
  for (const q of liveItems) bySymbol.set(q.symbol, q);
  return QUOTE_SYMBOLS
    .map(symbol => bySymbol.get(symbol))
    .filter((q): q is TeaserQuote => Boolean(q));
}

function hasCompleteLiveQuoteSet(liveItems: TeaserQuote[]): boolean {
  const liveSymbols = new Set(liveItems.map(q => q.symbol));
  return QUOTE_SYMBOLS.every(symbol => liveSymbols.has(symbol));
}

async function fetchQuotes(): Promise<{ items: TeaserQuote[]; live: boolean } | null> {
  const marketQs = MARKET_QUOTE_SYMBOLS.map(s => `symbols=${encodeURIComponent(s)}`).join('&');
  const commodityQs = COMMODITY_QUOTE_SYMBOLS.map(s => `symbols=${encodeURIComponent(s)}`).join('&');
  const cryptoQs = CRYPTO_QUOTE_IDS.map(id => `ids=${encodeURIComponent(id)}`).join('&');
  const [market, commodities, crypto] = await Promise.all([
    fetchJson<MarketQuotesResponse>(`/api/market/v1/list-market-quotes?${marketQs}`),
    fetchJson<CommodityQuotesResponse>(`/api/market/v1/list-commodity-quotes?${commodityQs}`),
    fetchJson<CryptoQuotesResponse>(`/api/market/v1/list-crypto-quotes?${cryptoQs}`),
  ]);
  const liveItems = [
    ...(market?.quotes ?? []),
    ...(commodities?.quotes ?? []),
    ...(crypto?.quotes ?? []),
  ].map(normalizeQuote).filter((q): q is TeaserQuote => Boolean(q));
  const items = orderAndBackfillQuotes(liveItems);
  if (!items.length) return null;
  const live =
    market !== null &&
    commodities !== null &&
    crypto !== null &&
    !market.rateLimited &&
    hasCompleteLiveQuoteSet(liveItems);
  return { items, live };
}

interface FeedDigestResponse {
  categories?: Record<string, { items?: Array<{ title?: string; source?: string; publishedAt?: number; importanceScore?: number }> }>;
  generatedAt?: string;
}

const DIGEST_FRESH_MS = 30 * 60 * 1000;

async function fetchHeadlines(): Promise<{ items: TeaserHeadline[]; live: boolean } | null> {
  const resp = await fetchJson<FeedDigestResponse>('/api/news/v1/list-feed-digest?variant=full&lang=en');
  if (!resp || !resp.categories) return null;
  const all = Object.values(resp.categories)
    .flatMap(c => c?.items ?? [])
    .filter(i => typeof i.title === 'string' && i.title.length > 0);
  if (!all.length) return null;
  const items = all
    .sort((a, b) => (b.importanceScore ?? 0) - (a.importanceScore ?? 0))
    .slice(0, 4)
    .map(i => ({ title: i.title as string, source: i.source ?? '', publishedAt: i.publishedAt ?? 0 }));
  // The digest response carries no degraded/stale booleans — its freshness
  // signal is generatedAt. Only claim LIVE when the digest is recent; an
  // unparseable/missing timestamp keeps the badge (matches the other
  // fetchers, which only demote on explicit signals).
  const generated = resp.generatedAt ? Date.parse(resp.generatedAt) : Number.NaN;
  const live = Number.isNaN(generated) || Date.now() - generated < DIGEST_FRESH_MS;
  return { items, live };
}

/**
 * Fetch all four teasers, merging successes over the committed fallback.
 * Never throws; cards whose fetch failed keep their fallback values with
 * live=false so the UI shows SAMPLE instead of LIVE.
 */
export async function fetchLiveTeasers(): Promise<TeaserState> {
  const state = getFallbackTeasers();
  await mintSession();
  const [headlines, cii, chokepoints, quotes] = await Promise.all([
    fetchHeadlines(),
    fetchCii(),
    fetchChokepoints(),
    fetchQuotes(),
  ]);
  if (headlines) state.headlines = headlines;
  if (cii) state.cii = cii;
  if (chokepoints) state.chokepoints = chokepoints;
  if (quotes) state.quotes = quotes;
  return state;
}