Elffuss BraidHub

kikocis/osiris public

master · 5.1 KiBRawPermalink
1import { NextResponse } from 'next/server';
2
3// Country Intelligence Index — composite risk from earthquakes, conflicts, instability
4// Inspired by WorldMonitor's 12-signal risk scoring
5const RISK_FACTORS: Record<string, { base: number; tags: string[] }> = {
6 UA: { base: 85, tags: ['active_conflict', 'infrastructure_damage'] },
7 RU: { base: 72, tags: ['sanctions', 'military_mobilization'] },
8 IL: { base: 78, tags: ['active_conflict', 'regional_instability'] },
9 PS: { base: 90, tags: ['active_conflict', 'humanitarian_crisis'] },
10 SY: { base: 82, tags: ['post_conflict', 'infrastructure_damage'] },
11 YE: { base: 88, tags: ['active_conflict', 'humanitarian_crisis'] },
12 MM: { base: 76, tags: ['civil_unrest', 'military_junta'] },
13 SD: { base: 84, tags: ['active_conflict', 'humanitarian_crisis'] },
14 AF: { base: 80, tags: ['post_conflict', 'governance_collapse'] },
15 KP: { base: 70, tags: ['nuclear_risk', 'isolation'] },
16 IR: { base: 68, tags: ['sanctions', 'nuclear_program', 'regional_proxy'] },
17 CN: { base: 35, tags: ['strategic_competition', 'taiwan_tensions'] },
18 TW: { base: 45, tags: ['invasion_risk', 'semiconductor_dependency'] },
19 VE: { base: 60, tags: ['economic_collapse', 'political_instability'] },
20 HT: { base: 85, tags: ['gang_violence', 'governance_collapse'] },
21 LB: { base: 65, tags: ['economic_crisis', 'political_deadlock'] },
22 PK: { base: 55, tags: ['terrorism', 'political_instability'] },
23 SO: { base: 82, tags: ['terrorism', 'state_fragility'] },
24 LY: { base: 72, tags: ['divided_government', 'militia_control'] },
25 ET: { base: 62, tags: ['ethnic_tensions', 'regional_conflicts'] },
26};
27
28// Major stock exchange status
29const EXCHANGES = [
30 { name: 'NYSE', tz: 'America/New_York', open: 9.5, close: 16, country: 'US' },
31 { name: 'NASDAQ', tz: 'America/New_York', open: 9.5, close: 16, country: 'US' },
32 { name: 'LSE', tz: 'Europe/London', open: 8, close: 16.5, country: 'GB' },
33 { name: 'TSE', tz: 'Asia/Tokyo', open: 9, close: 15, country: 'JP' },
34 { name: 'SSE', tz: 'Asia/Shanghai', open: 9.5, close: 15, country: 'CN' },
35 { name: 'HKEX', tz: 'Asia/Hong_Kong', open: 9.5, close: 16, country: 'HK' },
36 { name: 'BSE', tz: 'Asia/Kolkata', open: 9.25, close: 15.5, country: 'IN' },
37 { name: 'FRA', tz: 'Europe/Berlin', open: 8, close: 20, country: 'DE' },
38 { name: 'TSX', tz: 'America/Toronto', open: 9.5, close: 16, country: 'CA' },
39 { name: 'ASX', tz: 'Australia/Sydney', open: 10, close: 16, country: 'AU' },
40 { name: 'KRX', tz: 'Asia/Seoul', open: 9, close: 15.5, country: 'KR' },
41 { name: 'MOEX', tz: 'Europe/Moscow', open: 10, close: 18.5, country: 'RU' },
42];
43
44function isExchangeOpen(ex: typeof EXCHANGES[0]): boolean {
45 try {
46 const now = new Date();
47 const formatter = new Intl.DateTimeFormat('en-US', {
48 timeZone: ex.tz, hour: 'numeric', minute: 'numeric', hour12: false, weekday: 'short',
49 });
50 const parts = formatter.formatToParts(now);
51 const weekday = parts.find(p => p.type === 'weekday')?.value || '';
52 if (['Sat', 'Sun'].includes(weekday)) return false;
53 const hour = parseInt(parts.find(p => p.type === 'hour')?.value || '0');
54 const minute = parseInt(parts.find(p => p.type === 'minute')?.value || '0');
55 const decimal = hour + minute / 60;
56 return decimal >= ex.open && decimal < ex.close;
57 } catch { return false; }
58}
59
60export async function GET() {
61 try {
62 const exchangeStatus = EXCHANGES.map(ex => ({
63 name: ex.name, country: ex.country, open: isExchangeOpen(ex),
64 }));
65
66 // Enrich risk with live earthquake proximity
67 let quakeRisks: Record<string, number> = {};
68 try {
69 const res = await fetch('https://earthquake.usgs.gov/earthquakes/feed/v1.0/summary/4.5_day.geojson', { });
70 if (res.ok) {
71 const data = await res.json();
72 // Count significant quakes per rough region
73 for (const f of data.features || []) {
74 const place = f.properties?.place || '';
75 const mag = f.properties?.mag || 0;
76 // Extract country-ish context from place name
77 for (const [code, _] of Object.entries(RISK_FACTORS)) {
78 if (place.toLowerCase().includes(code.toLowerCase())) {
79 quakeRisks[code] = (quakeRisks[code] || 0) + mag;
80 }
81 }
82 }
83 }
84 } catch (e) { console.warn('[OSIRIS] Suppressed error:', e instanceof Error ? e.message : e); }
85
86 const countries = Object.entries(RISK_FACTORS).map(([code, data]) => ({
87 code,
88 risk_score: Math.min(100, data.base + (quakeRisks[code] || 0)),
89 risk_level: data.base >= 80 ? 'CRITICAL' : data.base >= 60 ? 'HIGH' : data.base >= 40 ? 'ELEVATED' : 'LOW',
90 tags: data.tags,
91 })).sort((a, b) => b.risk_score - a.risk_score);
92
93 return NextResponse.json({
94 countries,
95 exchanges: exchangeStatus,
96 open_exchanges: exchangeStatus.filter(e => e.open).length,
97 total_exchanges: exchangeStatus.length,
98 timestamp: new Date().toISOString(),
99 });
100 } catch (err) {
101 return NextResponse.json({ countries: [], exchanges: [], error: 'Failed' }, { status: 500 });
102 }
103}