diff --git a/tasks/flightradar24/ingest/mart/build_mart.py b/tasks/flightradar24/ingest/mart/build_mart.py index 8b12326..8461421 100644 --- a/tasks/flightradar24/ingest/mart/build_mart.py +++ b/tasks/flightradar24/ingest/mart/build_mart.py @@ -153,7 +153,7 @@ def find_fa_track(conn, flight_number: str, flight_date: date, # Exact match on ident_iata cur.execute( """ - SELECT id, aircraft_type, origin_iata, destination_iata + SELECT id, aircraft_type, origin_icao, destination_icao FROM fr24_ext.flight_tracks_fa WHERE ident_iata = %s AND flight_date = %s ORDER BY fetched_at DESC @@ -166,16 +166,19 @@ def find_fa_track(conn, flight_number: str, flight_date: date, return (rows[0][0], rows[0][1]) if origin_iata and destination_iata: for row in rows: - if row[2] == origin_iata and row[3] == destination_iata: + orig_iata = ICAO_TO_IATA.get(row[2]) + dest_iata = ICAO_TO_IATA.get(row[3]) + if orig_iata == origin_iata and dest_iata == destination_iata: return (row[0], row[1]) return (rows[0][0], rows[0][1]) # Try by numeric ident + route cur.execute( """ - SELECT id, aircraft_type, origin_iata, destination_iata + SELECT id, aircraft_type, origin_icao, destination_icao FROM fr24_ext.flight_tracks_fa - WHERE ident_iata ~ (%s || '$') AND flight_date = %s + WHERE regexp_replace(ident_iata, '[^0-9]', '', 'g') = %s + AND flight_date = %s ORDER BY fetched_at DESC """, (fnum, flight_date), @@ -186,7 +189,9 @@ def find_fa_track(conn, flight_number: str, flight_date: date, if origin_iata and destination_iata: for row in rows: - if row[2] == origin_iata and row[3] == destination_iata: + orig_iata = ICAO_TO_IATA.get(row[2]) + dest_iata = ICAO_TO_IATA.get(row[3]) + if orig_iata == origin_iata and dest_iata == destination_iata: return (row[0], row[1]) return (rows[0][0], rows[0][1])