AirTrail sync and export, GPS tracks, airport database with autocomplete

AirTrail (https://github.com/johanohly/AirTrail):
- File export (Flights → Export for AirTrail) in AirTrail's import
  format, validated against its schema; same-day repeat legs get leg
  numbers so AirTrail's de-duplication doesn't merge them.
- Automatic sync through AirTrail's REST API (Profile → AirTrail sync):
  per-user connection with an encrypted API key and scope check; flights
  are created/updated/deleted in background jobs; a full sync first
  links flights already in AirTrail (e.g. from a file import) instead of
  duplicating them; airports are resolved and cached, with manual
  mapping for unknown codes.
- GPS tracks: GPX, KML and CSV track logs are converted to AirTrail's
  track format for both sync and export; the flight page shows a summary.
- Aircraft ICAO type (e.g. C150) for AirTrail's aircraft type.

Airport database (OurAirports, the source AirTrail uses):
- Managed like AirTrail: downloaded in the background on first start,
  updated in place from Admin → Airports (adds, changes, removes; ids
  stable), with admin-managed custom airports that updates never touch.
- Each airport carries AirTrail's code, so logged codes translate to
  AirTrail's exactly (1B9 → K1B9, 4MB → K5M3).
- From/To autocomplete on new flights (airports this aircraft has used
  rank first); From is pre-filled with the last flight's destination;
  codes are normalized to uppercase.
- Flights → Standardize airport codes: rewrite logged codes to the
  standard code, and fix codes not in the database (typos such as 0R8 →
  08R are suggested).

Also: admin pages grouped under an Admin dropdown menu.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
rickyandClaude Opus 5.5 committed 2026-11-05 19:40:20 -05:00
1 parent 0c4803d717
commit 5bf682502b
68 files changed
+2667 -10

No files matched your search

+61
View File
@@ -0,0 +1,61 @@
# Flight details shared by the AirTrail file export and the API sync, so a
# flight looks the same in AirTrail whichever way it got there (and the sync
# can recognize flights that came in through a file import).
module AirTrailFlightData
AIRPORT_FORMAT = /\A[A-Z0-9]{1,4}\z/
module_function
# The code AirTrail uses for a logged airport, or nil if AirTrail can't take
# it. Logged codes are translated through the airport database (the same
# OurAirports data AirTrail uses), e.g. 1B9 → K1B9, 4MB → K5M3.
def airport_code(code)
c = code.to_s.strip.upcase
return nil if c.empty?
translated = Airport.airtrail_code_for(c) || c
translated if translated.match?(AIRPORT_FORMAT)
end
def exportable?(flight)
airport_code(flight.departure_location) && airport_code(flight.destination_location)
end
# Seconds, from Hobbs time (or tach); nil for zero-time entries.
def duration(flight)
hours = (flight.hobbs_time || flight.tach_time).to_f
hours.positive? ? (hours * 3600).round : nil
end
def note(flight)
times = [ "Tach #{flight.tach_time} h", (flight.hobbs_time && "Hobbs #{flight.hobbs_time} h") ].compact.join(" · ")
[ times, flight.route.presence, flight.notes.presence ].compact.join("\n").truncate(1000)
end
def registration(aircraft) = aircraft.tail_number.to_s.upcase.first(10).presence
# AirTrail de-duplicates on date + airports + flight number (+ reg, times).
# Repeat legs on the same day and route get "#2", "#3"… in log order.
def leg_numbers(flights)
seen = Hash.new(0)
flights.to_h do |f|
key = [ f.aircraft_id, f.date, airport_code(f.departure_location), airport_code(f.destination_location) ]
seen[key] += 1
[ f.id, (seen[key] > 1 ? "##{seen[key]}" : nil) ]
end
end
def leg_number(flight)
same_route = flight.aircraft.flight_logs.where(date: flight.date).order(:created_at, :id).select do |f|
airport_code(f.departure_location) == airport_code(flight.departure_location) &&
airport_code(f.destination_location) == airport_code(flight.destination_location)
end
leg_numbers(same_route)[flight.id]
end
# AirTrail TrackInput from the flight's track log, or nil.
def track(flight)
TrackParser.parse_attachment(flight.track_log)&.as_air_trail_json
rescue TrackParser::Error, ActiveStorage::FileNotFoundError
nil
end
end