Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions api/opensky.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ function getRelayHeaders(baseHeaders = {}) {
return headers;
}

async function fetchWithTimeout(url, options, timeoutMs = 15000) {
async function fetchWithTimeout(url, options, timeoutMs = 20000) {
const controller = new AbortController();
const timeout = setTimeout(() => controller.abort(), timeoutMs);
try {
Expand Down Expand Up @@ -67,7 +67,7 @@ export default async function handler(req) {
const body = await response.text();
const headers = {
'Content-Type': response.headers.get('content-type') || 'application/json',
'Cache-Control': response.headers.get('cache-control') || 'no-cache',
'Cache-Control': 'public, s-maxage=120, stale-while-revalidate=60',
...corsHeaders,
};
const xCache = response.headers.get('x-cache');
Expand Down
2 changes: 1 addition & 1 deletion api/youtube/embed.js
Original file line number Diff line number Diff line change
Expand Up @@ -158,7 +158,7 @@ export default async function handler(request) {
status: 200,
headers: {
'content-type': 'text/html; charset=utf-8',
'cache-control': 'public, s-maxage=60, stale-while-revalidate=300',
'cache-control': 'public, s-maxage=900, stale-while-revalidate=300',
},
});
}
2 changes: 1 addition & 1 deletion server/worldmonitor/infrastructure/v1/get-cable-health.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ import { CHROME_UA } from '../../../_shared/constants';
// ========================================================================

const CACHE_KEY = 'cable-health-v1';
const CACHE_TTL = 180; // 3 minutes
const CACHE_TTL = 600; // 10 min — cable health not time-critical

// In-memory fallback: serves stale data when both Redis and NGA are down
let fallbackCache: GetCableHealthResponse | null = null;
Expand Down
2 changes: 1 addition & 1 deletion server/worldmonitor/market/v1/get-sector-summary.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ import { fetchFinnhubQuote, fetchYahooQuotesBatch } from './_shared';
import { cachedFetchJson } from '../../../_shared/redis';

const REDIS_CACHE_KEY = 'market:sectors:v1';
const REDIS_CACHE_TTL = 180; // 3 min — Finnhub rate-limited
const REDIS_CACHE_TTL = 300; // 5 min — Finnhub rate-limited

export async function getSectorSummary(
_ctx: ServerContext,
Expand Down
2 changes: 1 addition & 1 deletion server/worldmonitor/market/v1/list-commodity-quotes.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ import { fetchYahooQuotesBatch } from './_shared';
import { cachedFetchJson } from '../../../_shared/redis';

const REDIS_CACHE_KEY = 'market:commodities:v1';
const REDIS_CACHE_TTL = 180; // 3 min — commodities move slower than indices
const REDIS_CACHE_TTL = 300; // 5 min — commodities move slower than indices

function redisCacheKey(symbols: string[]): string {
return `${REDIS_CACHE_KEY}:${[...symbols].sort().join(',')}`;
Expand Down
2 changes: 1 addition & 1 deletion server/worldmonitor/market/v1/list-crypto-quotes.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ import { CRYPTO_META, fetchCoinGeckoMarkets } from './_shared';
import { cachedFetchJson } from '../../../_shared/redis';

const REDIS_CACHE_KEY = 'market:crypto:v1';
const REDIS_CACHE_TTL = 180; // 3 min — CoinGecko rate-limited
const REDIS_CACHE_TTL = 300; // 5 min — CoinGecko rate-limited

export async function listCryptoQuotes(
_ctx: ServerContext,
Expand Down
2 changes: 1 addition & 1 deletion server/worldmonitor/market/v1/list-stablecoin-markets.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ import { CHROME_UA } from '../../../_shared/constants';
import { cachedFetchJson } from '../../../_shared/redis';

const REDIS_CACHE_KEY = 'market:stablecoins:v1';
const REDIS_CACHE_TTL = 180; // 3 min — CoinGecko rate-limited
const REDIS_CACHE_TTL = 300; // 5 min — CoinGecko rate-limited

// ========================================================================
// Constants and cache
Expand Down
16 changes: 7 additions & 9 deletions server/worldmonitor/military/v1/get-aircraft-details-batch.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ export async function getAircraftDetailsBatch(
.map((id) => id.trim().toLowerCase())
.filter((id) => id.length > 0);
const uniqueSorted = Array.from(new Set(normalized)).sort();
const limitedList = uniqueSorted.slice(0, 20);
const limitedList = uniqueSorted.slice(0, 10);

// Redis shared cache — batch GET all keys in a single pipeline round-trip
const SINGLE_KEY = 'military:aircraft:v1';
Expand All @@ -52,7 +52,10 @@ export async function getAircraftDetailsBatch(
}
}

const fetches = toFetch.map(async (icao24) => {
const delay = (ms: number) => new Promise<void>((r) => setTimeout(r, ms));

for (let i = 0; i < toFetch.length; i++) {
const icao24 = toFetch[i]!;
const cacheResult = await cachedFetchJson<CachedAircraftDetails>(
`${SINGLE_KEY}:${icao24}`,
SINGLE_TTL,
Expand All @@ -74,13 +77,8 @@ export async function getAircraftDetailsBatch(
return null;
},
);
if (cacheResult?.details) return { icao24, details: cacheResult.details };
return null;
});

const fetchResults = await Promise.all(fetches);
for (const r of fetchResults) {
if (r) results[r.icao24] = r.details;
if (cacheResult?.details) results[icao24] = cacheResult.details;
if (i < toFetch.length - 1) await delay(100);
}

return {
Expand Down
58 changes: 41 additions & 17 deletions server/worldmonitor/military/v1/get-theater-posture.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ import { CHROME_UA } from '../../../_shared/constants';
const CACHE_KEY = 'theater-posture:sebuf:v1';
const STALE_CACHE_KEY = 'theater-posture:sebuf:stale:v1';
const BACKUP_CACHE_KEY = 'theater-posture:sebuf:backup:v1';
const CACHE_TTL = 300;
const CACHE_TTL = 900; // 15 minutes
const STALE_TTL = 86400;
const BACKUP_TTL = 604800;

Expand All @@ -43,31 +43,24 @@ function getRelayRequestHeaders(): Record<string, string> {
return headers;
}

async function fetchMilitaryFlightsFromOpenSky(): Promise<RawFlight[]> {
const isSidecar = (process.env.LOCAL_API_MODE || '').includes('sidecar');
const baseUrl = isSidecar
? 'https://opensky-network.org/api/states/all'
: process.env.WS_RELAY_URL ? process.env.WS_RELAY_URL + '/opensky' : null;

if (!baseUrl) return [];
// Two bounding boxes covering all 9 POSTURE_THEATERS instead of fetching every
// aircraft globally. Returns ~hundreds of relevant states instead of ~10,000+.
const THEATER_QUERY_REGIONS = [
{ name: 'WESTERN', lamin: 10, lamax: 66, lomin: 9, lomax: 66 }, // Baltic→Yemen, Baltic→Iran
{ name: 'PACIFIC', lamin: 4, lamax: 44, lomin: 104, lomax: 133 }, // SCS→Korea
];

const resp = await fetch(baseUrl, {
headers: getRelayRequestHeaders(),
signal: AbortSignal.timeout(UPSTREAM_TIMEOUT_MS),
});
if (!resp.ok) throw new Error(`OpenSky API error: ${resp.status}`);

const data = (await resp.json()) as { states?: Array<[string, string, ...unknown[]]> };
function parseOpenSkyStates(
data: { states?: Array<[string, string, ...unknown[]]> },
): RawFlight[] {
if (!data.states) return [];

const flights: RawFlight[] = [];
for (const state of data.states) {
const [icao24, callsign, , , , lon, lat, altitude, onGround, velocity, heading] = state as [
string, string, unknown, unknown, unknown, number | null, number | null, number | null, boolean, number | null, number | null,
];
if (lat == null || lon == null || onGround) continue;
if (!isMilitaryCallsign(callsign) && !isMilitaryHex(icao24)) continue;

flights.push({
id: icao24,
callsign: callsign?.trim() || '',
Expand All @@ -81,6 +74,37 @@ async function fetchMilitaryFlightsFromOpenSky(): Promise<RawFlight[]> {
return flights;
}

async function fetchMilitaryFlightsFromOpenSky(): Promise<RawFlight[]> {
const isSidecar = (process.env.LOCAL_API_MODE || '').includes('sidecar');
const baseUrl = isSidecar
? 'https://opensky-network.org/api/states/all'
: process.env.WS_RELAY_URL ? process.env.WS_RELAY_URL + '/opensky' : null;

if (!baseUrl) return [];

const seenIds = new Set<string>();
const allFlights: RawFlight[] = [];

for (const region of THEATER_QUERY_REGIONS) {
const params = `lamin=${region.lamin}&lamax=${region.lamax}&lomin=${region.lomin}&lomax=${region.lomax}`;
const resp = await fetch(`${baseUrl}?${params}`, {
headers: getRelayRequestHeaders(),
signal: AbortSignal.timeout(UPSTREAM_TIMEOUT_MS),
});
if (!resp.ok) throw new Error(`OpenSky API error: ${resp.status} for ${region.name}`);

const data = (await resp.json()) as { states?: Array<[string, string, ...unknown[]]> };
for (const flight of parseOpenSkyStates(data)) {
if (!seenIds.has(flight.id)) {
seenIds.add(flight.id);
allFlights.push(flight);
}
}
}

return allFlights;
}

async function fetchMilitaryFlightsFromWingbits(): Promise<RawFlight[] | null> {
const apiKey = process.env.WINGBITS_API_KEY;
if (!apiKey) return null;
Expand Down
2 changes: 1 addition & 1 deletion server/worldmonitor/military/v1/list-military-flights.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ import { CHROME_UA } from '../../../_shared/constants';
import { cachedFetchJson } from '../../../_shared/redis';

const REDIS_CACHE_KEY = 'military:flights:v1';
const REDIS_CACHE_TTL = 120; // 2 min — real-time ADS-B data
const REDIS_CACHE_TTL = 600; // 10 min — reduce upstream API pressure

/** Snap a coordinate to a grid step so nearby bbox values share cache entries. */
const quantize = (v: number, step: number) => Math.round(v / step) * step;
Expand Down
4 changes: 3 additions & 1 deletion src/App.ts
Original file line number Diff line number Diff line change
Expand Up @@ -508,9 +508,11 @@ export class App {
// Refresh intelligence signals for CII (geopolitical variant only)
if (SITE_VARIANT === 'full') {
this.refreshScheduler.scheduleRefresh('intelligence', () => {
const { military } = this.state.intelligenceCache;
this.state.intelligenceCache = {};
if (military) this.state.intelligenceCache.military = military;
return this.dataLoader.loadIntelligenceSignals();
}, 5 * 60 * 1000);
}, 15 * 60 * 1000);
}
}
}
2 changes: 1 addition & 1 deletion src/components/StrategicPosturePanel.ts
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ export class StrategicPosturePanel extends Panel {
this.refreshInterval = setInterval(() => {
if (!this.isPanelVisible()) return;
void this.fetchAndRender();
}, 5 * 60 * 1000);
}, 15 * 60 * 1000);
}

private isPanelVisible(): boolean {
Expand Down
11 changes: 7 additions & 4 deletions src/main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -97,7 +97,7 @@ Sentry.init({
/this\.St\.unref/,
/Invalid or unexpected token/,
/evaluating 'elemFound\.value'/,
/Cannot access '\w+' before initialization/,
/[Cc]an(?:'t|not) access (?:'\w+'|lexical declaration '\w+') before initialization/,
/^Uint8Array$/,
/createObjectStore/,
/The database connection is closing/,
Expand All @@ -108,19 +108,22 @@ Sentry.init({
/a2z\.onStatusUpdate/,
/Attempting to run\(\), but is already running/,
/this\.player\.destroy is not a function/,
/isReCreate is not defined/,
/reading 'style'.*HTMLImageElement/,
/can't access property "write", \w+ is undefined/,
],
beforeSend(event) {
const msg = event.exception?.values?.[0]?.value ?? '';
if (msg.length <= 3 && /^[a-zA-Z_$]+$/.test(msg)) return null;
const frames = event.exception?.values?.[0]?.stacktrace?.frames ?? [];
// Suppress maplibre internal null-access crashes (light, placement) only when stack is in map chunk
if (/this\.style\._layers|reading '_layers'|this\.light is null|can't access property "(id|type|setFilter)", \w+ is (null|undefined)|Cannot read properties of null \(reading '(id|type|setFilter|_layers)'\)|null is not an object \(evaluating '(E\.|this\.style)|^\w{1,2} is null$/.test(msg)) {
if (/this\.style\._layers|reading '_layers'|this\.light is null|can't access property "(id|type|setFilter)", \w+ is (null|undefined)|Cannot read properties of null \(reading '(id|type|setFilter|_layers)'\)|null is not an object \(evaluating '\w{1,3}\.(id|style)|^\w{1,2} is null$/.test(msg)) {
if (frames.some(f => /\/(map|maplibre|deck-stack)-[A-Za-z0-9-]+\.js/.test(f.filename ?? ''))) return null;
}
// Suppress any TypeError that happens entirely within maplibre or deck.gl internals
if (/^TypeError:/.test(msg) && frames.length > 0) {
const appFrames = frames.filter(f => f.in_app && !/\/sentry-[A-Za-z0-9-]+\.js/.test(f.filename ?? ''));
if (appFrames.length > 0 && appFrames.every(f => /\/(map|maplibre|deck-stack)-[A-Za-z0-9-]+\.js/.test(f.filename ?? ''))) return null;
const nonSentryFrames = frames.filter(f => !/\/sentry-[A-Za-z0-9-]+\.js/.test(f.filename ?? ''));
if (nonSentryFrames.length > 0 && nonSentryFrames.every(f => /\/(map|maplibre|deck-stack)-[A-Za-z0-9-]+\.js/.test(f.filename ?? ''))) return null;
}
// Suppress errors originating entirely from blob: URLs (browser extensions)
if (frames.length > 0 && frames.every(f => /^blob:/.test(f.filename ?? ''))) return null;
Expand Down
2 changes: 1 addition & 1 deletion src/services/cached-theater-posture.ts
Original file line number Diff line number Diff line change
Expand Up @@ -114,7 +114,7 @@ const LS_MAX_AGE_MS = 30 * 60 * 1000; // 30 min max staleness for localStorage
let cachedPosture: CachedTheaterPosture | null = null;
let fetchPromise: Promise<CachedTheaterPosture | null> | null = null;
let lastFetchTime = 0;
const REFETCH_INTERVAL_MS = 5 * 60 * 1000; // 5 minutes (matches server TTL)
const REFETCH_INTERVAL_MS = 15 * 60 * 1000; // 15 minutes - reduce upstream API pressure

function createAbortError(): DOMException {
return new DOMException('The operation was aborted.', 'AbortError');
Expand Down
2 changes: 1 addition & 1 deletion src/services/military-flights.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ const DIRECT_OPENSKY_BASE_URL = wsRelayUrl
const isLocalhostRuntime = typeof window !== 'undefined' && ['localhost', '127.0.0.1'].includes(window.location.hostname);

// Cache configuration
const CACHE_TTL = 5 * 60 * 1000; // 5 minutes - match refresh interval
const CACHE_TTL = 15 * 60 * 1000; // 15 minutes - reduce upstream API pressure
let flightCache: { data: MilitaryFlight[]; timestamp: number } | null = null;

// Track flight history for trails
Expand Down