Elffuss BraidHub

kikocis/osiris public

master · 3.1 KiBRawPermalink
1import { NextResponse } from 'next/server';
2
3/**
4 * OSIRIS — Global Stats API
5 * Lightweight aggregation endpoint.
6 * Fetches metrics from all local APIs and returns ONLY the counts.
7 *
8 * ARCHITECTURE NOTE (10k+ Concurrent Users):
9 * This endpoint ensures the Next.js server serves ~100 bytes instead of 10MB+
10 * of raw GeoJSON mapping data when 10,000 users boot the dashboard simultaneously.
11 * The underlying API routes utilize their own 45-60s TTL caching, meaning the
12 * heavy external APIs (adsb.lol, USGS) are only hit once per minute, while this
13 * lightweight stats route safely serves 10k concurrent users instantly.
14 */
15
16export async function GET(req: Request) {
17 try {
18 const origin = new URL(req.url).origin;
19
20 // Fetch all internal APIs in parallel (they have their own Cache-Control TTLs)
21 const [flightsRes, satsRes, cctvRes, weatherRes, infraRes, gdeltRes] = await Promise.allSettled([
22 fetch(`${origin}/api/flights`, { next: { revalidate: 45 } }),
23 fetch(`${origin}/api/satellites`, { next: { revalidate: 3600 } }),
24 fetch(`${origin}/api/cctv`, { next: { revalidate: 3600 } }),
25 fetch(`${origin}/api/weather`, { next: { revalidate: 300 } }),
26 fetch(`${origin}/api/infrastructure`, { next: { revalidate: 86400 } }),
27 fetch(`${origin}/api/gdelt`, { next: { revalidate: 300 } })
28 ]);
29
30 let flights = 0;
31 let sats = 0;
32 let cctv = 0;
33 let weather = 0;
34 let nuclear = 0;
35 let incidents = 0;
36
37 // Safely parse counts
38 if (flightsRes.status === 'fulfilled' && flightsRes.value.ok) {
39 const data = await flightsRes.value.json();
40 flights = (data.commercial_flights?.length || 0) +
41 (data.private_flights?.length || 0) +
42 (data.private_jets?.length || 0) +
43 (data.military_flights?.length || 0);
44 }
45
46 if (satsRes.status === 'fulfilled' && satsRes.value.ok) {
47 const data = await satsRes.value.json();
48 sats = data.satellites?.length || 0;
49 }
50
51 if (cctvRes.status === 'fulfilled' && cctvRes.value.ok) {
52 const data = await cctvRes.value.json();
53 cctv = data.cameras?.length || 0;
54 }
55
56 if (weatherRes.status === 'fulfilled' && weatherRes.value.ok) {
57 const data = await weatherRes.value.json();
58 weather = data.weather_events?.length || 0;
59 }
60
61 if (infraRes.status === 'fulfilled' && infraRes.value.ok) {
62 const data = await infraRes.value.json();
63 nuclear = data.infrastructure?.length || 0;
64 }
65
66 if (gdeltRes.status === 'fulfilled' && gdeltRes.value.ok) {
67 const data = await gdeltRes.value.json();
68 incidents = data.gdelt?.length || 0;
69 }
70
71 return NextResponse.json({
72 stats: {
73 flights,
74 sats,
75 cctv,
76 weather,
77 nuclear,
78 incidents
79 },
80 timestamp: new Date().toISOString()
81 }, {
82 headers: {
83 'Cache-Control': 'public, s-maxage=30, stale-while-revalidate=60',
84 }
85 });
86
87 } catch (error) {
88 console.error('Stats aggregation failed:', error);
89 return NextResponse.json({ error: 'Failed to compute stats' }, { status: 500 });
90 }
91}