| 1 | import { RedisService } from 'src/services/redis/redis.service'; |
| 2 | import { HttpException, HttpStatus |
| 3 | import { ConfigService } |
| 4 | |
| 5 | export interface FlightAwareAirport |
| 6 | code: string |
| 7 | code_icao?: string |
| 8 | code_iata?: string |
| 9 | code_lid?: string |
| 10 | timezone?: string |
| 11 | name?: string |
| 12 | city?: string |
| 13 | airport_info_url?: string |
| 14 | } |
| 15 | |
| 16 | export interface FlightAwareAircraft |
| 17 | fa_flight_id: string; |
| 18 | ident: string; |
| 19 | ident_icao?: string |
| 20 | ident_iata?: string |
| 21 | actual_runway_off?: string |
| 22 | actual_runway_on?: string |
| 23 | operator?: string |
| 24 | operator_icao?: string |
| 25 | operator_iata?: string |
| 26 | flight_number?: string |
| 27 | registration?: string |
| 28 | atc_ident?: string |
| 29 | inbound_fa_flight_id?: string |
| 30 | codeshares?: string |
| 31 | codeshares_iata?: string |
| 32 | blocked?: boolean |
| 33 | diverted?: boolean |
| 34 | cancelled?: boolean |
| 35 | position_only?: boolean |
| 36 | origin: FlightAwareAirport |
| 37 | destination?: FlightAwareAirport |
| 38 | departure_delay?: number |
| 39 | arrival_delay?: number |
| 40 | filed_ete?: number |
| 41 | progress_percent?: number |
| 42 | status: string; |
| 43 | aircraft_type?: string |
| 44 | route_distance?: number |
| 45 | filed_airspeed?: number |
| 46 | filed_altitude?: number |
| 47 | route?: string |
| 48 | baggage_claim?: string |
| 49 | seats_cabin_business?: number |
| 50 | seats_cabin_coach?: number |
| 51 | seats_cabin_first?: number |
| 52 | gate_origin?: string |
| 53 | gate_destination?: string |
| 54 | terminal_origin?: string |
| 55 | terminal_destination?: string |
| 56 | type: 'General_Aviation |
| 57 | scheduled_out?: string |
| 58 | estimated_out?: string |
| 59 | actual_out?: string |
| 60 | scheduled_off?: string |
| 61 | estimated_off?: string |
| 62 | actual_off?: string |
| 63 | scheduled_on?: string |
| 64 | estimated_on?: string |
| 65 | actual_on?: string |
| 66 | scheduled_in?: string |
| 67 | estimated_in?: string |
| 68 | actual_in?: string |
| 69 | foresight_predictions_available?: boolean |
| 70 | aircraft_type_faa?: string |
| 71 | aircraft_family?: string |
| 72 | manufacturer?: string |
| 73 | model?: string |
| 74 | engine_type?: string |
| 75 | engine_count?: string |
| 76 | wake_turbulence_category?: string |
| 77 | flight_plan?: { |
| 78 | flight_plan_id: string |
| 79 | aircraft_id: string |
| 80 | aircraft_type: string |
| 81 | aircraft_faa?: string |
| 82 | aircraft_short?: string |
| 83 | owner?: string |
| 84 | owner_icao?: string |
| 85 | owner_iata?: string |
| 86 | owner_name?: string |
| 87 | manufacturer?: string |
| 88 | model?: string |
| 89 | priority?: string |
| 90 | filed_ete?: string |
| 91 | filed_departure_time?: string |
| 92 | estimated_departure_time?: string |
| 93 | filed_arrival_time?: string |
| 94 | estimated_arrival_time?: string |
| 95 | filed_airspeed_kts?: number |
| 96 | filed_airspeed_mach?: string |
| 97 | route_distance?: number |
| 98 | filed_altitude?: number |
| 99 | route?: string |
| 100 | actual_departure_time?: string |
| 101 | actual_arrival_time?: string |
| 102 | diverted?: string |
| 103 | origin: { |
| 104 | code: string |
| 105 | code_icao?: string |
| 106 | code_iata?: string |
| 107 | code_lid?: string |
| 108 | timezone: string |
| 109 | name: string |
| 110 | city?: string |
| 111 | airport_info_url?: string |
| 112 | }; |
| 113 | destination?: { |
| 114 | code: string |
| 115 | code_icao?: string |
| 116 | code_iata?: string |
| 117 | code_lid?: string |
| 118 | timezone: string |
| 119 | name: string |
| 120 | city?: string |
| 121 | airport_info_url?: string |
| 122 | }; |
| 123 | waypoints?: Array |
| 124 | ident: string |
| 125 | name?: string |
| 126 | lat: number |
| 127 | lon: number |
| 128 | altitude?: number |
| 129 | alt_desc?: string |
| 130 | type?: string |
| 131 | }>; |
| 132 | }; |
| 133 | origin_position?: { |
| 134 | fa_flight_id: string |
| 135 | altitude: number |
| 136 | altitude_change?: string |
| 137 | groundspeed: number |
| 138 | heading: number |
| 139 | latitude: number |
| 140 | longitude: number |
| 141 | timestamp: string |
| 142 | update_type?: string |
| 143 | }; |
| 144 | destination_position?: { |
| 145 | fa_flight_id: string |
| 146 | altitude: number |
| 147 | altitude_change?: string |
| 148 | groundspeed: number |
| 149 | heading: number |
| 150 | latitude: number |
| 151 | longitude: number |
| 152 | timestamp: string |
| 153 | update_type?: string |
| 154 | }; |
| 155 | waypoints?: Array |
| 156 | fa_flight_id: string |
| 157 | altitude: number |
| 158 | altitude_change?: string |
| 159 | groundspeed: number |
| 160 | heading: number |
| 161 | latitude: number |
| 162 | longitude: number |
| 163 | timestamp: string |
| 164 | update_type?: string |
| 165 | }>; |
| 166 | last_position?: { |
| 167 | fa_flight_id: string |
| 168 | altitude: number |
| 169 | altitude_change?: string |
| 170 | groundspeed: number |
| 171 | heading: number |
| 172 | latitude: number |
| 173 | longitude: number |
| 174 | timestamp: string |
| 175 | update_type?: string |
| 176 | }; |
| 177 | bounding_box?: { |
| 178 | fa_flight_id: string |
| 179 | num_positions: number |
| 180 | start_time: string |
| 181 | end_time: string |
| 182 | min_latitude: number |
| 183 | max_latitude: number |
| 184 | min_longitude: number |
| 185 | max_longitude: number |
| 186 | }; |
| 187 | has_forecast?: boolean |
| 188 | estimated_runway_arrival?: string |
| 189 | estimated_runway_departure?: string |
| 190 | actual_runway_arrival?: string |
| 191 | actual_runway_departure?: string |
| 192 | } |
| 193 | |
| 194 | export interface FlightAwareResponse |
| 195 | flights: FlightAwareAircraft |
| 196 | links: { |
| 197 | next?: string |
| 198 | prev?: string |
| 199 | }; |
| 200 | num_pages: number; |
| 201 | } |
| 202 | |
| 203 | @Injectable() |
| 204 | export class FlightAwareService |
| 205 | private readonly baseUrl |
| 206 | private readonly cacheTTL |
| 207 | private readonly maxRequestsPerMinute |
| 208 | private readonly rateLimitWindow |
| 209 | private redis: |
| 210 | |
| 211 | constructor( |
| 212 | private readonly redisService |
| 213 | private readonly configService |
| 214 | ) { |
| 215 | this.redis = |
| 216 | } |
| 217 | |
| 218 | /** |
| 219 | * Check and enforce rate limiting (10 requests per minute) |
| 220 | */ |
| 221 | private async checkRateLimit |
| 222 | const rateLimitKey = |
| 223 | const now = |
| 224 | |
| 225 | try { |
| 226 | // Get current request timestamps |
| 227 | const requestTimes = |
| 228 | const currentTime = |
| 229 | |
| 230 | // Filter out requests older than 1 minute |
| 231 | const recentRequests = |
| 232 | .map(time => |
| 233 | .filter(time => |
| 234 | |
| 235 | // Check if we're at the limit |
| 236 | if (recentRequests.length |
| 237 | const oldestRequest = |
| 238 | const waitTime = |
| 239 | |
| 240 | console.warn(`FlightAware: Rate limit reached ( |
| 241 | |
| 242 | // Wait for the oldest request to expire |
| 243 | await new Promise |
| 244 | } |
| 245 | |
| 246 | // Add current request timestamp |
| 247 | await this.redis |
| 248 | await this.redis |
| 249 | |
| 250 | console.log(`FlightAware: Rate limit check passed ( |
| 251 | } catch (error) |
| 252 | console.warn('FlightAware: Rate limit check failed, proceeding anyway: |
| 253 | } |
| 254 | } |
| 255 | |
| 256 | /** |
| 257 | * Get flights within a bounding box using FlightAware's search API |
| 258 | */ |
| 259 | async getFlightsInBounds( |
| 260 | minLat: number, |
| 261 | maxLat: number, |
| 262 | minLng: number, |
| 263 | maxLng: number, |
| 264 | ): Promise<FlightAwareResponse |
| 265 | const cacheKey = |
| 266 | |
| 267 | // Check cache first |
| 268 | try { |
| 269 | const cached = |
| 270 | if (cached) { |
| 271 | const cachedData = |
| 272 | console.log('FlightAware: Returning cached data |
| 273 | |
| 274 | // Return in the same format as the API response |
| 275 | return { |
| 276 | flights: cachedData.flights |
| 277 | links: cachedData.links |
| 278 | num_pages: cachedData.num_pages |
| 279 | }; |
| 280 | } |
| 281 | } catch (error) |
| 282 | console.warn('Redis cache read error: |
| 283 | } |
| 284 | |
| 285 | const apiKey = |
| 286 | if (!apiKey) |
| 287 | throw new HttpException( |
| 288 | 'FlightAware API key not configured', |
| 289 | HttpStatus.INTERNAL_SERVER_ERROR, |
| 290 | ); |
| 291 | } |
| 292 | |
| 293 | try { |
| 294 | // Check rate limit before making API call |
| 295 | await this.checkRateLimit |
| 296 | |
| 297 | // Build query for bounding box search using FlightAware CLI format |
| 298 | const query = |
| 299 | |
| 300 | const url = |
| 301 | url.searchParams.append |
| 302 | url.searchParams.append |
| 303 | |
| 304 | const response = |
| 305 | headers: { |
| 306 | 'x-apikey': apiKey, |
| 307 | Accept: 'application/json; charset=UTF-8', |
| 308 | }, |
| 309 | }); |
| 310 | |
| 311 | if (!response.ok |
| 312 | // Log error response |
| 313 | const contentType = |
| 314 | let errorBody; |
| 315 | |
| 316 | try { |
| 317 | if (contentType && |
| 318 | errorBody = await |
| 319 | } else { |
| 320 | errorBody = await |
| 321 | } |
| 322 | |
| 323 | console.error('FlightAware API Error Response: |
| 324 | status: response.status |
| 325 | statusText: response.statusText |
| 326 | contentType, |
| 327 | body: errorBody, |
| 328 | url: url.toString |
| 329 | }); |
| 330 | } catch (logError) |
| 331 | console.error('Failed to log FlightAware error response: |
| 332 | } |
| 333 | |
| 334 | if (response.status |
| 335 | throw new HttpException( |
| 336 | 'FlightAware API authentication failed', |
| 337 | HttpStatus.UNAUTHORIZED, |
| 338 | ); |
| 339 | } |
| 340 | if (response.status |
| 341 | throw new HttpException( |
| 342 | 'FlightAware API rate limit exceeded', |
| 343 | HttpStatus.TOO_MANY_REQUESTS, |
| 344 | ); |
| 345 | } |
| 346 | throw new HttpException( |
| 347 | `FlightAware API request failed with status ${response.status |
| 348 | HttpStatus.BAD_GATEWAY, |
| 349 | ); |
| 350 | } |
| 351 | |
| 352 | let data = |
| 353 | let allFlights = |
| 354 | let nextUrl = |
| 355 | |
| 356 | // Follow pagination links to get all flights |
| 357 | while (nextUrl) { |
| 358 | try { |
| 359 | console.log(`FlightAware: Fetching next page: |
| 360 | |
| 361 | // Check rate limit before each pagination request |
| 362 | await this.checkRateLimit |
| 363 | |
| 364 | const nextResponse = |
| 365 | headers: { |
| 366 | 'x-apikey': apiKey, |
| 367 | Accept: 'application/json; charset=UTF-8', |
| 368 | }, |
| 369 | }); |
| 370 | |
| 371 | if (!nextResponse.ok |
| 372 | console.warn(`FlightAware: Failed to fetch next page ( |
| 373 | break; |
| 374 | } |
| 375 | |
| 376 | const nextData = |
| 377 | allFlights = allFlights. |
| 378 | nextUrl = nextData. |
| 379 | |
| 380 | console.log(`FlightAware: Fetched |
| 381 | } catch (paginationError) |
| 382 | console.error('FlightAware: Error fetching next page: |
| 383 | break; |
| 384 | } |
| 385 | } |
| 386 | |
| 387 | // Create final response with all flights |
| 388 | const finalData = |
| 389 | ...data, |
| 390 | flights: allFlights |
| 391 | }; |
| 392 | |
| 393 | // Cache both the processed data and raw API response |
| 394 | try { |
| 395 | const processedData = |
| 396 | flights: allFlights, |
| 397 | links: data.links |
| 398 | num_pages: data.num_pages |
| 399 | cached_at: new Date |
| 400 | query: query, |
| 401 | bounds: { minLat, |
| 402 | total_flights_fetched: allFlights.length |
| 403 | }; |
| 404 | |
| 405 | // Cache the processed data (what we return) |
| 406 | await this.redis |
| 407 | |
| 408 | // Cache the raw API response for analysis |
| 409 | const rawCacheKey = |
| 410 | await this.redis |
| 411 | |
| 412 | console.log(`FlightAware: Fetched |
| 413 | } catch (error) |
| 414 | console.warn('Redis cache write error: |
| 415 | } |
| 416 | |
| 417 | return finalData; |
| 418 | } catch (error) |
| 419 | console.error('FlightAware API error: |
| 420 | if (error instanceof |
| 421 | throw error; |
| 422 | } |
| 423 | throw new HttpException( |
| 424 | 'Failed to fetch flight data from FlightAware', |
| 425 | HttpStatus.INTERNAL_SERVER_ERROR, |
| 426 | ); |
| 427 | } |
| 428 | } |
| 429 | |
| 430 | /** |
| 431 | * Search flights by ident (returns array of flights) |
| 432 | */ |
| 433 | async searchFlightsByIdent(ident |
| 434 | const cacheKey = |
| 435 | |
| 436 | // Check cache first |
| 437 | try { |
| 438 | const cached = |
| 439 | if (cached) { |
| 440 | console.log(`FlightAware: Returning cached search data for |
| 441 | return JSON.parse |
| 442 | } |
| 443 | } catch (error) |
| 444 | console.warn('Redis cache read error: |
| 445 | } |
| 446 | |
| 447 | const apiKey = |
| 448 | if (!apiKey) |
| 449 | throw new HttpException( |
| 450 | 'FlightAware API key not configured', |
| 451 | HttpStatus.INTERNAL_SERVER_ERROR, |
| 452 | ); |
| 453 | } |
| 454 | |
| 455 | try { |
| 456 | // Check rate limit before making API call |
| 457 | await this.checkRateLimit |
| 458 | |
| 459 | const url = |
| 460 | |
| 461 | const response = |
| 462 | headers: { |
| 463 | 'x-apikey': apiKey, |
| 464 | Accept: 'application/json; charset=UTF-8', |
| 465 | }, |
| 466 | }); |
| 467 | |
| 468 | if (!response.ok |
| 469 | // Log error response |
| 470 | const contentType = |
| 471 | let errorBody; |
| 472 | |
| 473 | try { |
| 474 | if (contentType && |
| 475 | errorBody = await |
| 476 | } else { |
| 477 | errorBody = await |
| 478 | } |
| 479 | |
| 480 | console.error('FlightAware API Error Response: |
| 481 | status: response.status |
| 482 | statusText: response.statusText |
| 483 | contentType, |
| 484 | body: errorBody, |
| 485 | url: url.toString |
| 486 | ident, |
| 487 | }); |
| 488 | } catch (logError) |
| 489 | console.error('Failed to log FlightAware error response: |
| 490 | } |
| 491 | |
| 492 | if (response.status |
| 493 | return null; |
| 494 | } |
| 495 | if (response.status |
| 496 | throw new HttpException( |
| 497 | 'FlightAware API authentication failed', |
| 498 | HttpStatus.UNAUTHORIZED, |
| 499 | ); |
| 500 | } |
| 501 | throw new HttpException( |
| 502 | `FlightAware API request failed with status ${response.status |
| 503 | HttpStatus.BAD_GATEWAY, |
| 504 | ); |
| 505 | } |
| 506 | |
| 507 | const data = |
| 508 | |
| 509 | // Cache both the processed data and raw API response |
| 510 | try { |
| 511 | const processedData = |
| 512 | ...data, |
| 513 | cached_at: new Date |
| 514 | ident: ident |
| 515 | }; |
| 516 | |
| 517 | // Cache the processed data (what we return) |
| 518 | await this.redis |
| 519 | |
| 520 | // Cache the raw API response for analysis |
| 521 | const rawCacheKey = |
| 522 | await this.redis |
| 523 | |
| 524 | console.log( |
| 525 | `FlightAware: Search data for ${ident} cached for 10 seconds (processed + raw)` |
| 526 | ); |
| 527 | } catch (error) |
| 528 | console.warn('Redis cache write error: |
| 529 | } |
| 530 | |
| 531 | return data; |
| 532 | } catch (error) |
| 533 | console.error('FlightAware API error: |
| 534 | if (error instanceof |
| 535 | throw error; |
| 536 | } |
| 537 | throw new HttpException( |
| 538 | 'Failed to fetch flight data from FlightAware', |
| 539 | HttpStatus.INTERNAL_SERVER_ERROR, |
| 540 | ); |
| 541 | } |
| 542 | } |
| 543 | |
| 544 | /** |
| 545 | * Get flight track data (positions) by fa_flight_id |
| 546 | * Note: This method caches at the FlightAware service level. |
| 547 | * The application layer (flight.service.ts) has its own 30s cache. |
| 548 | */ |
| 549 | async getFlightTrack(faFlightId |
| 550 | const cacheKey = |
| 551 | const trackCacheTTL = |
| 552 | |
| 553 | // Check cache first |
| 554 | try { |
| 555 | const cached = |
| 556 | if (cached) { |
| 557 | console.log(`FlightAware: Returning cached track data for |
| 558 | return JSON.parse |
| 559 | } |
| 560 | } catch (error) |
| 561 | console.warn('Redis cache read error: |
| 562 | } |
| 563 | |
| 564 | const apiKey = |
| 565 | if (!apiKey) |
| 566 | throw new HttpException( |
| 567 | 'FlightAware API key not configured', |
| 568 | HttpStatus.INTERNAL_SERVER_ERROR, |
| 569 | ); |
| 570 | } |
| 571 | |
| 572 | try { |
| 573 | // Check rate limit before making API call |
| 574 | await this.checkRateLimit |
| 575 | |
| 576 | const url = |
| 577 | |
| 578 | const response = |
| 579 | headers: { |
| 580 | 'x-apikey': apiKey, |
| 581 | Accept: 'application/json; charset=UTF-8', |
| 582 | }, |
| 583 | }); |
| 584 | |
| 585 | if (!response.ok |
| 586 | // Log error response |
| 587 | const contentType = |
| 588 | let errorBody; |
| 589 | |
| 590 | try { |
| 591 | if (contentType && |
| 592 | errorBody = await |
| 593 | } else { |
| 594 | errorBody = await |
| 595 | } |
| 596 | |
| 597 | console.error('FlightAware API Error Response: |
| 598 | status: response.status |
| 599 | statusText: response.statusText |
| 600 | contentType, |
| 601 | body: errorBody, |
| 602 | url: url.toString |
| 603 | faFlightId, |
| 604 | }); |
| 605 | } catch (logError) |
| 606 | console.error('Failed to log FlightAware error response: |
| 607 | } |
| 608 | |
| 609 | if (response.status |
| 610 | throw new HttpException( |
| 611 | 'Flight track not found', |
| 612 | HttpStatus.NOT_FOUND, |
| 613 | ); |
| 614 | } |
| 615 | if (response.status |
| 616 | throw new HttpException( |
| 617 | 'FlightAware API authentication failed', |
| 618 | HttpStatus.UNAUTHORIZED, |
| 619 | ); |
| 620 | } |
| 621 | throw new HttpException( |
| 622 | `FlightAware API request failed with status ${response.status |
| 623 | HttpStatus.BAD_GATEWAY, |
| 624 | ); |
| 625 | } |
| 626 | |
| 627 | const data = |
| 628 | |
| 629 | // Cache the data for 30 seconds |
| 630 | try { |
| 631 | const processedData = |
| 632 | ...data, |
| 633 | cached_at: new Date |
| 634 | fa_flight_id: faFlightId |
| 635 | }; |
| 636 | |
| 637 | await this.redis |
| 638 | |
| 639 | console.log( |
| 640 | `FlightAware: Track data for ${faFlightId} cached for ${ |
| 641 | ); |
| 642 | } catch (error) |
| 643 | console.warn('Redis cache write error: |
| 644 | } |
| 645 | |
| 646 | return data; |
| 647 | } catch (error) |
| 648 | console.error('FlightAware API error: |
| 649 | if (error instanceof |
| 650 | throw error; |
| 651 | } |
| 652 | throw new HttpException( |
| 653 | 'Failed to fetch flight track data from FlightAware', |
| 654 | HttpStatus.INTERNAL_SERVER_ERROR, |
| 655 | ); |
| 656 | } |
| 657 | } |
| 658 | |
| 659 | /** |
| 660 | * Get a specific flight by ident |
| 661 | */ |
| 662 | async getFlightByIdent(ident |
| 663 | const cacheKey = |
| 664 | |
| 665 | // Check cache first |
| 666 | try { |
| 667 | const cached = |
| 668 | if (cached) { |
| 669 | console.log(`FlightAware: Returning cached flight data for |
| 670 | return JSON.parse |
| 671 | } |
| 672 | } catch (error) |
| 673 | console.warn('Redis cache read error: |
| 674 | } |
| 675 | |
| 676 | const apiKey = |
| 677 | if (!apiKey) |
| 678 | throw new HttpException( |
| 679 | 'FlightAware API key not configured', |
| 680 | HttpStatus.INTERNAL_SERVER_ERROR, |
| 681 | ); |
| 682 | } |
| 683 | |
| 684 | try { |
| 685 | // Check rate limit before making API call |
| 686 | await this.checkRateLimit |
| 687 | |
| 688 | const url = |
| 689 | |
| 690 | const response = |
| 691 | headers: { |
| 692 | 'x-apikey': apiKey, |
| 693 | Accept: 'application/json; charset=UTF-8', |
| 694 | }, |
| 695 | }); |
| 696 | |
| 697 | if (!response.ok |
| 698 | // Log error response |
| 699 | const contentType = |
| 700 | let errorBody; |
| 701 | |
| 702 | try { |
| 703 | if (contentType && |
| 704 | errorBody = await |
| 705 | } else { |
| 706 | errorBody = await |
| 707 | } |
| 708 | |
| 709 | console.error('FlightAware API Error Response: |
| 710 | status: response.status |
| 711 | statusText: response.statusText |
| 712 | contentType, |
| 713 | body: errorBody, |
| 714 | url: url.toString |
| 715 | ident, |
| 716 | }); |
| 717 | } catch (logError) |
| 718 | console.error('Failed to log FlightAware error response: |
| 719 | } |
| 720 | |
| 721 | if (response.status |
| 722 | return null; |
| 723 | } |
| 724 | if (response.status |
| 725 | throw new HttpException( |
| 726 | 'FlightAware API authentication failed', |
| 727 | HttpStatus.UNAUTHORIZED, |
| 728 | ); |
| 729 | } |
| 730 | throw new HttpException( |
| 731 | `FlightAware API request failed with status ${response.status |
| 732 | HttpStatus.BAD_GATEWAY, |
| 733 | ); |
| 734 | } |
| 735 | |
| 736 | const data = |
| 737 | |
| 738 | // Cache both the processed data and raw API response |
| 739 | try { |
| 740 | const processedData = |
| 741 | ...data, |
| 742 | cached_at: new Date |
| 743 | ident: ident |
| 744 | }; |
| 745 | |
| 746 | // Cache the processed data (what we return) |
| 747 | await this.redis |
| 748 | |
| 749 | // Cache the raw API response for analysis |
| 750 | const rawCacheKey = |
| 751 | await this.redis |
| 752 | |
| 753 | console.log( |
| 754 | `FlightAware: Flight data for ${ident} cached for 10 seconds (processed + raw)` |
| 755 | ); |
| 756 | } catch (error) |
| 757 | console.warn('Redis cache write error: |
| 758 | } |
| 759 | |
| 760 | return data; |
| 761 | } catch (error) |
| 762 | console.error('FlightAware API error: |
| 763 | if (error instanceof |
| 764 | throw error; |
| 765 | } |
| 766 | throw new HttpException( |
| 767 | 'Failed to fetch flight data from FlightAware', |
| 768 | HttpStatus.INTERNAL_SERVER_ERROR, |
| 769 | ); |
| 770 | } |
| 771 | } |
| 772 | |
| 773 | /** Generic AeroAPI GET with redis cache and rate-limit handling. */ |
| 774 | private async aeroApiGet |
| 775 | path: string, |
| 776 | cacheKey: string, |
| 777 | cacheTTL: number, |
| 778 | ): Promise<T |
| 779 | if (this.redis |
| 780 | try { |
| 781 | const cached = |
| 782 | if (cached) return |
| 783 | } catch (error) |
| 784 | console.warn(`FlightAware: cache read error for |
| 785 | } |
| 786 | } |
| 787 | |
| 788 | const apiKey = |
| 789 | if (!apiKey) |
| 790 | throw new HttpException( |
| 791 | 'FlightAware API key not configured', |
| 792 | HttpStatus.INTERNAL_SERVER_ERROR, |
| 793 | ); |
| 794 | } |
| 795 | |
| 796 | await this.checkRateLimit |
| 797 | const response = |
| 798 | headers: { 'x-apikey': |
| 799 | }); |
| 800 | if (!response.ok |
| 801 | console.warn(`FlightAware: |
| 802 | return null; |
| 803 | } |
| 804 | const data = |
| 805 | if (this.redis |
| 806 | try { |
| 807 | await this.redis |
| 808 | } catch (error) |
| 809 | console.warn(`FlightAware: cache write error for |
| 810 | } |
| 811 | } |
| 812 | return data; |
| 813 | } |
| 814 | |
| 815 | /** Largest current delay for an airport. */ |
| 816 | async getAirportDelays(airportId |
| 817 | return this.aeroApiGet |
| 818 | `/airports/${encodeURIComponent(airportId |
| 819 | `flightaware:airport:delays:${airportId}`, |
| 820 | 600, // 10 min |
| 821 | ); |
| 822 | } |
| 823 | |
| 824 | /** TAF forecast for an airport. */ |
| 825 | async getAirportWeatherForecast(airportId |
| 826 | return this.aeroApiGet |
| 827 | `/airports/${encodeURIComponent(airportId |
| 828 | `flightaware:airport:forecast:${airportId}`, |
| 829 | 1800, // 30 min |
| 830 | ); |
| 831 | } |
| 832 | |
| 833 | /** Latest METAR observation for an airport. */ |
| 834 | async getAirportLatestObservation(airportId |
| 835 | const data = |
| 836 | `/airports/${encodeURIComponent(airportId |
| 837 | `flightaware:airport:observations:${airportId}`, |
| 838 | 600, // 10 min |
| 839 | ); |
| 840 | return data?.observations |
| 841 | } |
| 842 | } |