File size: 10,555 Bytes
9d2d895
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
import type { CableAdvisory, RepairShip, UnderseaCable } from '@/types';
import { getRpcBaseUrl } from '@/services/rpc-client';
import type { NavigationalWarning } from '@/generated/client/worldmonitor/maritime/v1/service_client';
import { MaritimeServiceClient } from '@/services/generated-rpc-clients';

// UNDERSEA_CABLES (~130KB) lives in the lazy geo-map chunk; cable-activity is
// reached eagerly via data-loader, so it loads the table on demand inside the
// async fetch path rather than statically importing it onto the eager graph.
let cablesData: UnderseaCable[] = [];
let cablesDataPromise: Promise<void> | null = null;
async function ensureCablesData(): Promise<void> {
  if (cablesData.length > 0) return;
  if (!cablesDataPromise) {
    cablesDataPromise = import('@/config/geo-map')
      .then(({ UNDERSEA_CABLES }) => {
        cablesData = UNDERSEA_CABLES;
      })
      .catch((error) => {
        cablesDataPromise = null;
        throw error;
      });
  }
  try {
    await cablesDataPromise;
  } catch {
    /* keep empty → retried on the next fetch */
  }
}

const maritimeClient = new MaritimeServiceClient(getRpcBaseUrl(), { fetch: (...args) => globalThis.fetch(...args) });

interface CableActivity {
  advisories: CableAdvisory[];
  repairShips: RepairShip[];
}

interface NgaWarning {
  msgYear: number;
  msgNumber: number;
  navArea: string;
  subregion: string;
  text: string;
  status: string;
  issueDate: string;
  authority: string;
}

const CABLE_KEYWORDS = [
  'CABLE',
  'CABLESHIP',
  'CABLE SHIP',
  'CABLE LAYING',
  'CABLE OPERATIONS',
  'SUBMARINE CABLE',
  'UNDERSEA CABLE',
  'FIBER OPTIC',
  'TELECOMMUNICATIONS CABLE',
];

const CABLESHIP_PATTERNS = [
  /CABLESHIP\s+([A-Z][A-Z0-9\s\-']+)/i,
  /CABLE\s+SHIP\s+([A-Z][A-Z0-9\s\-']+)/i,
  /CS\s+([A-Z][A-Z0-9\s\-']+)/i,
  /M\/V\s+([A-Z][A-Z0-9\s\-']+)/i,
  /VESSEL\s+([A-Z][A-Z0-9\s\-']+)/i,
];

function isCableRelated(text: string): boolean {
  const upper = text.toUpperCase();
  return CABLE_KEYWORDS.some(kw => upper.includes(kw));
}

function parseCoordinates(text: string): { lat: number; lon: number }[] {
  const coords: { lat: number; lon: number }[] = [];

  // Pattern: 26-32N 056-40E or 26-32.5N 056-40.5E
  const dmsPattern = /(\d{1,3})-(\d{1,2}(?:\.\d+)?)\s*([NS])\s+(\d{1,3})-(\d{1,2}(?:\.\d+)?)\s*([EW])/gi;
  let match;

  while ((match = dmsPattern.exec(text)) !== null) {
    if (!match[1] || !match[2] || !match[3] || !match[4] || !match[5] || !match[6]) continue;

    const latDeg = parseInt(match[1], 10);
    const latMin = parseFloat(match[2]);
    const latDir = match[3].toUpperCase();
    const lonDeg = parseInt(match[4], 10);
    const lonMin = parseFloat(match[5]);
    const lonDir = match[6].toUpperCase();

    let lat = latDeg + latMin / 60;
    let lon = lonDeg + lonMin / 60;

    if (latDir === 'S') lat = -lat;
    if (lonDir === 'W') lon = -lon;

    if (lat >= -90 && lat <= 90 && lon >= -180 && lon <= 180) {
      coords.push({ lat, lon });
    }
  }

  // Pattern: 12.345N 067.890W (decimal degrees)
  const decPattern = /(\d{1,3}\.\d+)\s*([NS])\s+(\d{1,3}\.\d+)\s*([EW])/gi;
  while ((match = decPattern.exec(text)) !== null) {
    if (!match[1] || !match[2] || !match[3] || !match[4]) continue;

    let lat = parseFloat(match[1]);
    let lon = parseFloat(match[3]);

    if (match[2].toUpperCase() === 'S') lat = -lat;
    if (match[4].toUpperCase() === 'W') lon = -lon;

    if (lat >= -90 && lat <= 90 && lon >= -180 && lon <= 180) {
      coords.push({ lat, lon });
    }
  }

  return coords;
}

function extractCableshipName(text: string): string | null {
  for (const pattern of CABLESHIP_PATTERNS) {
    const match = text.match(pattern);
    if (match?.[1]) {
      const name = match[1].trim().replace(/\s+/g, ' ');
      // Skip if it's just a generic word
      if (name.length > 2 && !/^(THE|AND|FOR|WITH)$/i.test(name)) {
        return name;
      }
    }
  }
  return null;
}

function findNearestCable(lat: number, lon: number): UnderseaCable | null {
  let nearest: UnderseaCable | null = null;
  let minDist = Infinity;

  for (const cable of cablesData) {
    for (const point of cable.points) {
      const [cableLon, cableLat] = point;
      const dist = Math.sqrt((lat - cableLat) ** 2 + (lon - cableLon) ** 2);
      if (dist < minDist && dist < 5) { // Within 5 degrees
        minDist = dist;
        nearest = cable;
      }
    }
  }

  return nearest;
}

function parseIssueDate(dateStr: string): Date {
  // Format: "081653Z MAY 2024" or "101200Z JAN 2025"
  const match = dateStr.match(/(\d{2})(\d{4})Z\s+([A-Z]{3})\s+(\d{4})/i);
  if (match?.[1] && match[2] && match[3] && match[4]) {
    const day = parseInt(match[1], 10);
    const time = match[2];
    const monthStr = match[3].toUpperCase();
    const year = parseInt(match[4], 10);

    const months: Record<string, number> = {
      JAN: 0, FEB: 1, MAR: 2, APR: 3, MAY: 4, JUN: 5,
      JUL: 6, AUG: 7, SEP: 8, OCT: 9, NOV: 10, DEC: 11,
    };

    const month = months[monthStr] ?? 0;
    const hours = parseInt(time.slice(0, 2), 10);
    const minutes = parseInt(time.slice(2, 4), 10);

    return new Date(Date.UTC(year, month, day, hours, minutes));
  }
  return new Date();
}

function determineSeverity(text: string): 'fault' | 'degraded' {
  const faultKeywords = /FAULT|BREAK|CUT|DAMAGE|SEVERED|RUPTURE|OUTAGE|FAILURE/i;
  return faultKeywords.test(text) ? 'fault' : 'degraded';
}

function determineShipStatus(text: string): 'enroute' | 'on-station' {
  const onStationKeywords = /ON STATION|OPERATIONS IN PROGRESS|LAYING|REPAIRING|WORKING|COMMENCED/i;
  return onStationKeywords.test(text) ? 'on-station' : 'enroute';
}

function slugify(text: string): string {
  return text
    .toLowerCase()
    .replace(/[^a-z0-9]+/g, '-')
    .replace(/(^-|-$)+/g, '')
    .slice(0, 40);
}

function processWarnings(warnings: NgaWarning[]): CableActivity {
  const advisories: CableAdvisory[] = [];
  const repairShips: RepairShip[] = [];
  const seenIds = new Set<string>();

  const cableWarnings = warnings.filter(w => isCableRelated(w.text));

  for (const warning of cableWarnings) {
    const coords = parseCoordinates(warning.text);
    const shipName = extractCableshipName(warning.text);
    const issueDate = parseIssueDate(warning.issueDate);

    // Use first coordinate or try to match to a cable
    let lat = 0;
    let lon = 0;
    let matchedCable: UnderseaCable | null = null;

    if (coords.length > 0) {
      // Use centroid of all coordinates
      lat = coords.reduce((sum, c) => sum + c.lat, 0) / coords.length;
      lon = coords.reduce((sum, c) => sum + c.lon, 0) / coords.length;
      matchedCable = findNearestCable(lat, lon);
    }

    // If no coordinates, can't place on map
    if (lat === 0 && lon === 0) continue;

    const warningId = `nga-${warning.navArea}-${warning.msgYear}-${warning.msgNumber}`;

    // If we found a cableship name, create a repair ship entry
    if (shipName) {
      const shipId = `ship-${warningId}-${slugify(shipName)}`;
      if (!seenIds.has(shipId)) {
        seenIds.add(shipId);
        repairShips.push({
          id: shipId,
          name: shipName,
          cableId: matchedCable?.id || 'unknown',
          status: determineShipStatus(warning.text),
          lat,
          lon,
          eta: determineShipStatus(warning.text) === 'on-station' ? 'On station' : 'TBD',
          operator: warning.authority || undefined,
          note: warning.text.slice(0, 200) + (warning.text.length > 200 ? '...' : ''),
        });
      }
    }

    // Create advisory for all cable-related warnings
    const advisoryId = `advisory-${warningId}`;
    if (!seenIds.has(advisoryId)) {
      seenIds.add(advisoryId);

      const isOperation = /OPERATIONS|LAYING|REPAIR|SURVEY/i.test(warning.text);
      const title = shipName
        ? `${isOperation ? 'Cable Operations' : 'Cable Activity'}: ${shipName}`
        : `NAVAREA ${warning.navArea} Cable Warning`;

      advisories.push({
        id: advisoryId,
        cableId: matchedCable?.id || 'unknown',
        title,
        severity: determineSeverity(warning.text),
        description: warning.text.slice(0, 300) + (warning.text.length > 300 ? '...' : ''),
        reported: issueDate,
        lat,
        lon,
        impact: isOperation
          ? 'Cable operations in progress. Vessels requested to give wide berth.'
          : matchedCable
            ? `Potential impact to ${matchedCable.name} cable route.`
            : 'Navigation warning in effect for cable infrastructure.',
        repairEta: undefined,
      });
    }
  }

  return { advisories, repairShips };
}

function protoToNgaWarning(w: NavigationalWarning): NgaWarning {
  // Parse id format: "navArea-msgYear-msgNumber" (e.g., "IV-2024-42")
  const idParts = w.id.split('-');
  const navArea = idParts.length >= 3 ? idParts.slice(0, -2).join('-') : (idParts[0] || '');
  const msgYear = idParts.length >= 2 ? Number(idParts[idParts.length - 2]) || 0 : 0;
  const msgNumber = idParts.length >= 1 ? Number(idParts[idParts.length - 1]) || 0 : 0;

  // Parse area format: "navArea subregion" (e.g., "IV 21")
  const areaParts = w.area.split(' ');
  const subregion = areaParts.length > 1 ? areaParts.slice(1).join(' ') : '';

  return {
    msgYear,
    msgNumber,
    navArea,
    subregion,
    text: w.text,
    status: 'A', // All warnings from the active endpoint have status A
    issueDate: w.issuedAt ? formatNgaDate(w.issuedAt) : '',
    authority: w.authority,
  };
}

function formatNgaDate(epochMs: number): string {
  if (!epochMs) return '';
  const d = new Date(epochMs);
  const months = ['JAN', 'FEB', 'MAR', 'APR', 'MAY', 'JUN', 'JUL', 'AUG', 'SEP', 'OCT', 'NOV', 'DEC'];
  const day = String(d.getUTCDate()).padStart(2, '0');
  const hours = String(d.getUTCHours()).padStart(2, '0');
  const minutes = String(d.getUTCMinutes()).padStart(2, '0');
  const month = months[d.getUTCMonth()] || 'JAN';
  const year = d.getUTCFullYear();
  return `${day}${hours}${minutes}Z ${month} ${year}`;
}

export async function fetchCableActivity(): Promise<CableActivity> {
  try {
    await ensureCablesData();
    const response = await maritimeClient.listNavigationalWarnings({ area: '', pageSize: 0, cursor: '' });
    const warnings: NgaWarning[] = response.warnings.map(protoToNgaWarning);

    const activity = processWarnings(warnings);

    return activity;
  } catch (error) {
    console.error('[CableActivity] Failed to fetch NGA warnings:', error);
    return { advisories: [], repairShips: [] };
  }
}