mirror of
https://github.com/rickbarrette/simple-aircraft-manager-rails.git
synced 2026-11-17 23:50:36 -05:00
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:
1 parent
0c4803d717
commit
5bf682502b
68 files changed
+2667
-10
No files matched your search
@@ -0,0 +1,97 @@
|
||||
require "net/http"
|
||||
|
||||
# Minimal client for AirTrail's REST API (/api/v1, Bearer API key).
|
||||
class AirTrailClient
|
||||
class Error < StandardError
|
||||
attr_reader :status, :code
|
||||
|
||||
def initialize(message, status: nil, code: nil)
|
||||
super(message)
|
||||
@status = status
|
||||
@code = code
|
||||
end
|
||||
end
|
||||
class NetworkError < Error; end # retryable: timeouts, connection refused, 5xx
|
||||
class AuthError < Error; end # 401/403: bad key or missing scope
|
||||
class NotFound < Error; end
|
||||
class ValidationError < Error; end # 400/422
|
||||
|
||||
def initialize(base_url:, api_key:, timeout: 20)
|
||||
@base = URI.parse(base_url.to_s.chomp("/"))
|
||||
@api_key = api_key
|
||||
@timeout = timeout
|
||||
end
|
||||
|
||||
def me = request(:get, "/api/v1/me").fetch("data")
|
||||
|
||||
def search_airports(query) = request(:get, "/api/v1/airports", query: { query: query }).fetch("data")
|
||||
def search_aircraft(query) = request(:get, "/api/v1/aircraft", query: { query: query }).fetch("data")
|
||||
|
||||
# Yields every flight the key's user owns (follows cursor pagination).
|
||||
def each_flight
|
||||
cursor = nil
|
||||
loop do
|
||||
q = { scope: "mine", limit: 100 }
|
||||
q[:cursor] = cursor if cursor
|
||||
page = request(:get, "/api/v1/flights", query: q)
|
||||
page.fetch("data").each { yield _1 }
|
||||
cursor = page.dig("page", "nextCursor")
|
||||
break unless cursor
|
||||
end
|
||||
end
|
||||
|
||||
def create_flight(body) = request(:post, "/api/v1/flights", body: body).dig("data", "id")
|
||||
def update_flight(id, body) = request(:put, "/api/v1/flights/#{Integer(id)}", body: body)
|
||||
|
||||
def delete_flight(id)
|
||||
request(:delete, "/api/v1/flights/#{Integer(id)}")
|
||||
rescue NotFound
|
||||
nil # already gone
|
||||
end
|
||||
|
||||
private
|
||||
|
||||
def request(method, path, query: nil, body: nil)
|
||||
uri = @base.dup
|
||||
uri.path = "#{@base.path}#{path}"
|
||||
uri.query = URI.encode_www_form(query) if query
|
||||
req = Net::HTTP.const_get(method.to_s.capitalize).new(uri)
|
||||
req["Authorization"] = "Bearer #{@api_key}"
|
||||
req["Accept"] = "application/json"
|
||||
if body
|
||||
req["Content-Type"] = "application/json"
|
||||
req.body = JSON.generate(body)
|
||||
end
|
||||
|
||||
response = Net::HTTP.start(uri.host, uri.port, use_ssl: uri.scheme == "https",
|
||||
open_timeout: @timeout, read_timeout: @timeout) { _1.request(req) }
|
||||
handle(response)
|
||||
rescue Timeout::Error, SocketError, SystemCallError, OpenSSL::SSL::SSLError, EOFError, IOError => e
|
||||
raise NetworkError, "Can't reach AirTrail at #{@base.host}: #{e.message}"
|
||||
end
|
||||
|
||||
def handle(response)
|
||||
status = response.code.to_i
|
||||
json = begin
|
||||
response.body.to_s.empty? ? {} : JSON.parse(response.body)
|
||||
rescue JSON::ParserError
|
||||
{}
|
||||
end
|
||||
return json if status.between?(200, 299)
|
||||
|
||||
error = json["error"].is_a?(Hash) ? json["error"] : {}
|
||||
message = [ error["message"] || "HTTP #{status}", details(error["details"]) ].compact.join(": ")
|
||||
klass = case status
|
||||
when 401, 403 then AuthError
|
||||
when 404 then NotFound
|
||||
when 400, 409, 422 then ValidationError
|
||||
else NetworkError
|
||||
end
|
||||
raise klass.new("AirTrail: #{message}", status: status, code: error["code"])
|
||||
end
|
||||
|
||||
def details(list)
|
||||
return nil unless list.is_a?(Array) && list.any?
|
||||
list.first(3).map { |d| [ Array(d["path"]).join("."), d["message"] ].compact_blank.join(" ") }.join("; ")
|
||||
end
|
||||
end
|
||||
@@ -0,0 +1,96 @@
|
||||
# Builds an AirTrail import file (Settings → Import → AirTrail in AirTrail,
|
||||
# https://github.com/johanohly/AirTrail) from an aircraft's flight log.
|
||||
#
|
||||
# Format follows AirTrail's own backup schema (src/lib/import/airtrail.ts):
|
||||
# { flights: [...], users: [...] }, with airports referenced by code
|
||||
# (AirTrail asks the user to map codes it doesn't know) and the pilot as the
|
||||
# single passenger in the "pilot" seat. AirTrail matches the exported user to
|
||||
# a local account by username.
|
||||
#
|
||||
# AirTrail de-duplicates imports on date + airports + flight number +
|
||||
# registration + departure/arrival times. Our flights have no times, so
|
||||
# repeat legs on the same day and route get a stable leg number ("#2", "#3")
|
||||
# in the flight-number field; re-exporting and re-importing is then safe.
|
||||
class AirTrailExport
|
||||
USERNAME_FORMAT = /\A[A-Za-z0-9._-]{3,20}\z/
|
||||
TYPE_FORMAT = /\A[A-Z0-9]{1,4}\z/
|
||||
EXPORT_USER_ID = "sam-pilot".freeze
|
||||
|
||||
attr_reader :errors
|
||||
|
||||
def initialize(aircraft, username:, aircraft_type: nil, only_user: nil)
|
||||
@aircraft = aircraft
|
||||
@username = username.to_s.strip
|
||||
@aircraft_type = aircraft_type.to_s.strip.upcase.presence || aircraft.icao_type
|
||||
@only_user = only_user
|
||||
@errors = []
|
||||
end
|
||||
|
||||
def valid?
|
||||
@errors = []
|
||||
@errors << "AirTrail username must be 3–20 letters, numbers, “.”, “_” or “-”" unless @username.match?(USERNAME_FORMAT)
|
||||
@errors << "Aircraft type must be an ICAO type designator like C150 (up to 4 letters/numbers)" if @aircraft_type && !@aircraft_type.match?(TYPE_FORMAT)
|
||||
@errors << "No flights to export" if exportable.empty?
|
||||
@errors.empty?
|
||||
end
|
||||
|
||||
def flights
|
||||
@flights ||= begin
|
||||
scope = @aircraft.flight_logs.includes(track_log_attachment: :blob).order(:date, :created_at, :id)
|
||||
scope = scope.where(user: @only_user) if @only_user
|
||||
scope.to_a
|
||||
end
|
||||
end
|
||||
|
||||
# Flights need both airports, as codes AirTrail can look up.
|
||||
def exportable = @exportable ||= flights.select { AirTrailFlightData.exportable?(_1) }
|
||||
def skipped = flights.size - exportable.size
|
||||
|
||||
def filename = "#{@aircraft.tail_number.parameterize}-airtrail-#{Date.current}.json"
|
||||
|
||||
def to_json(*) = JSON.pretty_generate(as_json)
|
||||
|
||||
def as_json(*)
|
||||
{
|
||||
flights: exportable.zip(leg_numbers).map { |f, leg| flight_json(f, leg) },
|
||||
users: [ { id: EXPORT_USER_ID, username: @username, displayName: @username } ]
|
||||
}
|
||||
end
|
||||
|
||||
private
|
||||
|
||||
def airport(code) = AirTrailFlightData.airport_code(code)
|
||||
|
||||
def leg_numbers
|
||||
legs = AirTrailFlightData.leg_numbers(exportable)
|
||||
exportable.map { legs[_1.id] }
|
||||
end
|
||||
|
||||
def flight_json(f, leg)
|
||||
json = {
|
||||
date: f.date.iso8601,
|
||||
datePrecision: "day",
|
||||
from: { code: airport(f.departure_location) },
|
||||
to: { code: airport(f.destination_location) },
|
||||
departure: nil,
|
||||
arrival: nil,
|
||||
duration: AirTrailFlightData.duration(f), # seconds
|
||||
airline: nil,
|
||||
aircraft: @aircraft_type && { name: aircraft_name, icao: @aircraft_type },
|
||||
flightNumber: leg,
|
||||
aircraftReg: AirTrailFlightData.registration(@aircraft),
|
||||
note: AirTrailFlightData.note(f),
|
||||
passengers: [
|
||||
{ userId: EXPORT_USER_ID, guestName: nil, seat: "pilot", seatNumber: nil,
|
||||
seatClass: nil, flightReason: nil, customFields: {} }
|
||||
]
|
||||
}
|
||||
track = AirTrailFlightData.track(f)
|
||||
json[:track] = track if track
|
||||
json
|
||||
end
|
||||
|
||||
def aircraft_name
|
||||
[ @aircraft.make, @aircraft.model ].compact_blank.join(" ").presence || @aircraft_type
|
||||
end
|
||||
end
|
||||
@@ -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
|
||||
@@ -0,0 +1,177 @@
|
||||
# Pushes a user's flights to their AirTrail account through the REST API.
|
||||
#
|
||||
# - push(flight): create the AirTrail flight, or replace it (PUT) if it
|
||||
# changed since the last sync. Skips unchanged flights (payload digest).
|
||||
# - backfill: link flights that already exist in AirTrail (e.g. from an
|
||||
# earlier file import) instead of duplicating them, then push the rest.
|
||||
# - Airports are looked up by code once per connection and cached; codes
|
||||
# AirTrail doesn't know are recorded so the user can map them by hand.
|
||||
class AirTrailSyncer
|
||||
class UnresolvedAirport < StandardError; end
|
||||
|
||||
AIRPORT_RECHECK = 1.day
|
||||
|
||||
def initialize(connection, client: connection.client)
|
||||
@connection = connection
|
||||
@client = client
|
||||
@aircraft_ids = {}
|
||||
end
|
||||
|
||||
# Flights this connection mirrors: the ones its user logged.
|
||||
def flights = FlightLog.where(user_id: @connection.user_id)
|
||||
|
||||
def ensure_identity!
|
||||
return if @connection.remote_user_id.present?
|
||||
me = @client.me
|
||||
@connection.update!(remote_user_id: me["id"], remote_username: me["username"], status: "ok",
|
||||
last_error: nil, checked_at: Time.current)
|
||||
end
|
||||
|
||||
def push(flight, leg: :lookup)
|
||||
ensure_identity!
|
||||
sync = @connection.syncs.find_or_initialize_by(flight_log: flight)
|
||||
body = payload(flight, leg: leg == :lookup ? AirTrailFlightData.leg_number(flight) : leg)
|
||||
digest = Digest::SHA256.hexdigest(JSON.generate(body))
|
||||
return sync if sync.remote_id && sync.status == "synced" && sync.payload_digest == digest
|
||||
|
||||
if sync.remote_id
|
||||
begin
|
||||
@client.update_flight(sync.remote_id, body)
|
||||
rescue AirTrailClient::NotFound # deleted in AirTrail: recreate
|
||||
sync.remote_id = @client.create_flight(body)
|
||||
end
|
||||
else
|
||||
sync.remote_id = @client.create_flight(body)
|
||||
end
|
||||
sync.update!(status: "synced", payload_digest: digest, synced_at: Time.current, last_error: nil)
|
||||
sync
|
||||
rescue UnresolvedAirport, AirTrailClient::ValidationError => e
|
||||
sync&.update!(status: "error", last_error: e.message)
|
||||
sync
|
||||
rescue AirTrailClient::AuthError => e
|
||||
@connection.update!(status: "error", last_error: e.message, checked_at: Time.current)
|
||||
sync&.update!(status: "error", last_error: e.message)
|
||||
raise
|
||||
end
|
||||
|
||||
def delete_remote(remote_id) = @client.delete_flight(remote_id)
|
||||
|
||||
# Returns { linked:, pushed:, failed: }.
|
||||
def backfill
|
||||
ensure_identity!
|
||||
local = flights.includes(:aircraft).order(:date, :created_at, :id).to_a
|
||||
legs = AirTrailFlightData.leg_numbers(local)
|
||||
linked = link_existing(local, legs)
|
||||
pushed = 0
|
||||
failed = 0
|
||||
local.each do |f|
|
||||
next unless AirTrailFlightData.exportable?(f)
|
||||
sync = push(f, leg: legs[f.id])
|
||||
sync.status == "synced" ? pushed += 1 : failed += 1
|
||||
end
|
||||
@connection.update!(backfilled_at: Time.current, status: "ok", last_error: nil)
|
||||
{ linked: linked, pushed: pushed, failed: failed }
|
||||
end
|
||||
|
||||
def payload(flight, leg:)
|
||||
from = airport_id(flight.departure_location)
|
||||
to = airport_id(flight.destination_location)
|
||||
body = {
|
||||
date: flight.date.iso8601,
|
||||
datePrecision: "day",
|
||||
departure: nil,
|
||||
arrival: nil,
|
||||
duration: AirTrailFlightData.duration(flight),
|
||||
flightNumber: leg,
|
||||
aircraftReg: AirTrailFlightData.registration(flight.aircraft),
|
||||
note: AirTrailFlightData.note(flight),
|
||||
fromId: from,
|
||||
toId: to,
|
||||
aircraftId: aircraft_id(flight.aircraft.icao_type),
|
||||
airlineId: nil,
|
||||
passengers: [ { userId: @connection.remote_user_id, guestName: nil, seat: "pilot", seatNumber: nil,
|
||||
seatClass: nil, flightReason: nil, customFields: {} } ],
|
||||
customFields: {}
|
||||
}
|
||||
track = AirTrailFlightData.track(flight)
|
||||
body[:track] = track if track
|
||||
body
|
||||
end
|
||||
|
||||
# Look an airport code up in AirTrail (cached per connection).
|
||||
def airport_id(code)
|
||||
normalized = AirTrailFlightData.airport_code(code)
|
||||
raise UnresolvedAirport, "Airport “#{code.presence || '(blank)'}” isn't a code AirTrail can use" unless normalized
|
||||
|
||||
cached = @connection.airports.find_or_initialize_by(code: normalized)
|
||||
fresh = cached.manual? || cached.remote_id || (cached.looked_up_at && cached.looked_up_at > AIRPORT_RECHECK.ago)
|
||||
unless fresh
|
||||
results = @client.search_airports(normalized)
|
||||
match = results.find { _1["icao"].to_s.casecmp?(normalized) } || results.find { _1["iata"].to_s.casecmp?(normalized) }
|
||||
cached.update!(remote_id: match&.dig("id"), remote_icao: match&.dig("icao"), looked_up_at: Time.current)
|
||||
end
|
||||
cached.remote_id or raise UnresolvedAirport, "AirTrail doesn't know airport #{normalized}. Map it on your AirTrail settings page"
|
||||
end
|
||||
|
||||
# Map a code to an airport the user names (e.g. its ICAO/GPS code in AirTrail).
|
||||
def map_airport(code, airtrail_code)
|
||||
results = @client.search_airports(airtrail_code)
|
||||
match = results.find { _1["icao"].to_s.casecmp?(airtrail_code) || _1["iata"].to_s.casecmp?(airtrail_code) } || results.first
|
||||
return nil unless match
|
||||
@connection.airports.find_or_initialize_by(code: code)
|
||||
.update!(remote_id: match["id"], remote_icao: match["icao"], manual: true, looked_up_at: Time.current)
|
||||
match
|
||||
end
|
||||
|
||||
private
|
||||
|
||||
def aircraft_id(icao_type)
|
||||
return nil if icao_type.blank?
|
||||
@aircraft_ids.fetch(icao_type) do
|
||||
@aircraft_ids[icao_type] = @client.search_aircraft(icao_type).find { _1["icao"].to_s.casecmp?(icao_type) }&.dig("id")
|
||||
end
|
||||
end
|
||||
|
||||
# Match local flights to AirTrail flights already there (same signature
|
||||
# AirTrail de-duplicates on), falling back to date + registration + leg +
|
||||
# duration when the airports were mapped differently. Learns airport ids
|
||||
# from matched flights so hand-mapped airports carry over.
|
||||
def link_existing(local, legs)
|
||||
already = @connection.syncs.where.not(remote_id: nil).pluck(:flight_log_id).to_set
|
||||
remote = []
|
||||
@client.each_flight { remote << _1 }
|
||||
taken = @connection.syncs.where.not(remote_id: nil).pluck(:remote_id).to_set
|
||||
remote.reject! { taken.include?(_1["id"]) }
|
||||
|
||||
exact = remote.group_by { |r| [ r["date"], r.dig("from", "icao").to_s.upcase, r.dig("to", "icao").to_s.upcase, r["flightNumber"].to_s, r["aircraftReg"].to_s.upcase ] }
|
||||
loose = remote.group_by { |r| [ r["date"], r["aircraftReg"].to_s.upcase, r["flightNumber"].to_s, r["duration"] ] }
|
||||
used = Set.new
|
||||
linked = 0
|
||||
|
||||
local.each do |f|
|
||||
next if already.include?(f.id) || !AirTrailFlightData.exportable?(f)
|
||||
reg = AirTrailFlightData.registration(f.aircraft).to_s
|
||||
leg = legs[f.id].to_s
|
||||
candidates = exact[[ f.date.iso8601, AirTrailFlightData.airport_code(f.departure_location),
|
||||
AirTrailFlightData.airport_code(f.destination_location), leg, reg ]] || []
|
||||
candidates += loose[[ f.date.iso8601, reg, leg, AirTrailFlightData.duration(f) ]] || []
|
||||
match = candidates.find { !used.include?(_1["id"]) }
|
||||
next unless match
|
||||
|
||||
used << match["id"]
|
||||
learn_airport(f.departure_location, match["from"])
|
||||
learn_airport(f.destination_location, match["to"])
|
||||
@connection.syncs.find_or_initialize_by(flight_log: f).update!(remote_id: match["id"], status: "pending")
|
||||
linked += 1
|
||||
end
|
||||
linked
|
||||
end
|
||||
|
||||
def learn_airport(code, remote_airport)
|
||||
normalized = AirTrailFlightData.airport_code(code)
|
||||
return unless normalized && remote_airport.is_a?(Hash) && remote_airport["id"]
|
||||
cached = @connection.airports.find_or_initialize_by(code: normalized)
|
||||
return if cached.manual? || cached.remote_id
|
||||
cached.update!(remote_id: remote_airport["id"], remote_icao: remote_airport["icao"], looked_up_at: Time.current)
|
||||
end
|
||||
end
|
||||
@@ -0,0 +1,89 @@
|
||||
# Rewrites an aircraft's logged airport codes to the airport database's
|
||||
# standard code (AirTrail's ICAO-style code, e.g. 1B9 → K1B9).
|
||||
#
|
||||
# s = AirportCodeStandardizer.new(aircraft)
|
||||
# s.changes # => [Change(from: "1B9", to: "K1B9", airport:, flights: 12, suggested: true), ...]
|
||||
# s.unknown # => [Unknown(code: "0R8", flights: 2, suggestion: <08R Richmond Airport>)]
|
||||
# s.apply!(%w[1B9 44N], fixes: { "0R8" => "08R" }) # returns entries updated
|
||||
class AirportCodeStandardizer
|
||||
Change = Struct.new(:from, :to, :airport, :flights, :suggested, :note, keyword_init: true)
|
||||
# A code not in the database, with a likely intended airport (e.g. a typo).
|
||||
Unknown = Struct.new(:code, :flights, :suggestion, keyword_init: true)
|
||||
|
||||
def initialize(aircraft)
|
||||
@aircraft = aircraft
|
||||
end
|
||||
|
||||
def changes
|
||||
analyze[:changes]
|
||||
end
|
||||
|
||||
def unknown
|
||||
analyze[:unknown]
|
||||
end
|
||||
|
||||
# codes: logged codes to standardize; fixes: { unknown code => airport code }
|
||||
# for codes not in the database (resolved to that airport's standard code).
|
||||
def apply!(codes, fixes: {})
|
||||
chosen = changes.select { codes.include?(_1.from) }
|
||||
known_unknown = unknown.map(&:code)
|
||||
fixes.each do |from, target|
|
||||
from = from.to_s.strip.upcase
|
||||
airport = Airport.lookup(target)
|
||||
next unless airport && known_unknown.include?(from)
|
||||
chosen << Change.new(from: from, to: airport.code, airport: airport)
|
||||
end
|
||||
updated = 0
|
||||
FlightLog.transaction do
|
||||
chosen.each do |c|
|
||||
updated += @aircraft.flight_logs.where(departure_location: c.from).update_all(departure_location: c.to, updated_at: Time.current)
|
||||
updated += @aircraft.flight_logs.where(destination_location: c.from).update_all(destination_location: c.to, updated_at: Time.current)
|
||||
end
|
||||
if chosen.any?
|
||||
@aircraft.log_event!("flight", "Standardized #{chosen.size} airport codes",
|
||||
notes: chosen.map { "#{_1.from}→#{_1.to}" }.join(", "))
|
||||
end
|
||||
end
|
||||
updated
|
||||
end
|
||||
|
||||
private
|
||||
|
||||
def analyze
|
||||
@analyze ||= begin
|
||||
counts = Hash.new(0)
|
||||
@aircraft.flight_logs.pluck(:departure_location, :destination_location).flatten.each do |code|
|
||||
counts[code.to_s.strip.upcase] += 1 if code.present?
|
||||
end
|
||||
changes = []
|
||||
unknown = []
|
||||
counts.sort.each do |code, n|
|
||||
airport = Airport.lookup(code)
|
||||
if airport.nil?
|
||||
unknown << Unknown.new(code: code, flights: n, suggestion: suggest(code, counts.keys))
|
||||
elsif airport.code != code
|
||||
renamed = !airport.code.end_with?(code) # e.g. 4MB → K5M3, not just a K prefix
|
||||
note = if airport.kind == "closed" then "Closed airport. Check it's the right one"
|
||||
elsif renamed then "Different identifier, not just a prefix. Check it's the right field"
|
||||
end
|
||||
changes << Change.new(from: code, to: airport.code, airport: airport, flights: n,
|
||||
suggested: airport.kind != "closed", note: note)
|
||||
end
|
||||
end
|
||||
{ changes: changes, unknown: unknown }
|
||||
end
|
||||
end
|
||||
|
||||
# Likely intended airport for an unknown code: the same characters in a
|
||||
# different order (e.g. 0R8 → 08R), preferring codes this aircraft has used.
|
||||
def suggest(code, logged_codes)
|
||||
return nil unless code.length.between?(3, 4)
|
||||
candidates = code.chars.permutation.map(&:join).uniq - [ code ]
|
||||
familiar = candidates & logged_codes
|
||||
(familiar + (candidates - familiar)).each do |candidate|
|
||||
airport = Airport.lookup(candidate)
|
||||
return airport if airport && airport.kind != "closed"
|
||||
end
|
||||
nil
|
||||
end
|
||||
end
|
||||
@@ -0,0 +1,169 @@
|
||||
require "csv"
|
||||
|
||||
# Parses a flight's GPS track log (GPX, KML or CSV) into AirTrail's track
|
||||
# format: coordinates as [lon, lat, altitude_m] plus aligned epoch-second
|
||||
# times. Mirrors AirTrail's own parser (src/lib/track/parser.ts):
|
||||
# GPX: <trkpt>/<rtept> lat/lon attributes, <ele> in meters, <time>
|
||||
# KML: <LineString><coordinates> "lon,lat,alt" and <gx:Track> <when>/<gx:coord>
|
||||
# CSV: latitude/longitude/altitude/time columns, altitude in FEET
|
||||
class TrackParser
|
||||
class Error < StandardError; end
|
||||
|
||||
MAX_POINTS = 100_000 # AirTrail's stored-track limit
|
||||
FEET_TO_M = 0.3048
|
||||
LAT_HEADERS = %w[latitude lat].freeze
|
||||
LON_HEADERS = %w[longitude lon lng long].freeze
|
||||
ALT_HEADERS = %w[altitude alt altitude_ft altitude_feet altitudeft altitudefeet].freeze
|
||||
TIME_HEADERS = %w[timestamp time date_time datetime utc_time].freeze
|
||||
|
||||
Track = Struct.new(:coordinates, :times, :source_format, :source_name, keyword_init: true) do
|
||||
def points = coordinates.size
|
||||
def max_altitude_ft = (alts = coordinates.filter_map { _1[2] }).any? ? (alts.max / FEET_TO_M).round : nil
|
||||
|
||||
# AirTrail TrackInput
|
||||
def as_air_trail_json
|
||||
{ coordinates: coordinates, sourceFormat: source_format, sourceName: source_name.to_s.first(255).presence }
|
||||
.tap { |h| h[:times] = times if times }
|
||||
end
|
||||
end
|
||||
|
||||
def self.parse_attachment(attachment)
|
||||
return nil unless attachment&.attached?
|
||||
attachment.blob.open { |file| new(file.read, filename: attachment.filename.to_s).parse }
|
||||
end
|
||||
|
||||
def initialize(content, filename:)
|
||||
@content = content.to_s
|
||||
@filename = filename.to_s
|
||||
end
|
||||
|
||||
def parse
|
||||
coords, times =
|
||||
case format
|
||||
when "gpx" then parse_gpx
|
||||
when "kml" then parse_kml
|
||||
when "csv" then parse_csv
|
||||
else raise Error, "Unsupported track format"
|
||||
end
|
||||
raise Error, "Track has fewer than 2 points" if coords.size < 2
|
||||
times = nil unless times && times.size == coords.size
|
||||
coords, times = downsample(coords, times)
|
||||
Track.new(coordinates: coords, times: times, source_format: format, source_name: @filename)
|
||||
end
|
||||
|
||||
def format
|
||||
@format ||= case File.extname(@filename).downcase
|
||||
when ".gpx" then "gpx"
|
||||
when ".kml" then "kml"
|
||||
when ".csv" then "csv"
|
||||
else
|
||||
head = @content[0, 2000]
|
||||
if head.include?("<gpx") then "gpx"
|
||||
elsif head.include?("<kml") then "kml"
|
||||
elsif head.match?(/lat/i) && head.include?(",") then "csv"
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
private
|
||||
|
||||
def xml
|
||||
Nokogiri::XML(@content) { |config| config.nonet }.tap(&:remove_namespaces!)
|
||||
end
|
||||
|
||||
def parse_gpx
|
||||
coords = []
|
||||
times = []
|
||||
xml.xpath("//trkpt | //rtept").each do |pt|
|
||||
lat = number(pt["lat"])
|
||||
lon = number(pt["lon"])
|
||||
next unless valid?(lat, lon)
|
||||
alt = number(pt.at_xpath("ele")&.text)
|
||||
coords << coordinate(lon, lat, alt)
|
||||
times << epoch(pt.at_xpath("time")&.text)
|
||||
end
|
||||
[ coords, times.all? ? times : nil ]
|
||||
end
|
||||
|
||||
def parse_kml
|
||||
doc = xml
|
||||
track_points = doc.xpath("//Track")
|
||||
if track_points.any?
|
||||
coords = []
|
||||
times = []
|
||||
track_points.each do |track|
|
||||
whens = track.xpath("when").map(&:text)
|
||||
track.xpath("coord").each_with_index do |c, i|
|
||||
lon, lat, alt = c.text.split.map { number(_1) }
|
||||
next unless valid?(lat, lon)
|
||||
coords << coordinate(lon, lat, alt)
|
||||
times << epoch(whens[i])
|
||||
end
|
||||
end
|
||||
return [ coords, times.all? ? times : nil ]
|
||||
end
|
||||
|
||||
coords = doc.xpath("//LineString/coordinates").flat_map do |node|
|
||||
node.text.split.filter_map do |triple|
|
||||
lon, lat, alt = triple.split(",").map { number(_1) }
|
||||
coordinate(lon, lat, alt) if valid?(lat, lon)
|
||||
end
|
||||
end
|
||||
[ coords, nil ]
|
||||
end
|
||||
|
||||
def parse_csv
|
||||
rows = CSV.parse(@content.sub(/\A/, ""), headers: true)
|
||||
headers = rows.headers.compact
|
||||
find = ->(names) { headers.find { |h| names.include?(h.to_s.strip.downcase.gsub(/[^a-z_]/, "")) } }
|
||||
lat_h, lon_h, alt_h, time_h = find.(LAT_HEADERS), find.(LON_HEADERS), find.(ALT_HEADERS), find.(TIME_HEADERS)
|
||||
raise Error, "CSV track needs latitude and longitude columns" unless lat_h && lon_h
|
||||
|
||||
coords = []
|
||||
times = []
|
||||
rows.each do |row|
|
||||
lat = number(row[lat_h])
|
||||
lon = number(row[lon_h])
|
||||
next unless valid?(lat, lon)
|
||||
alt_ft = alt_h && number(row[alt_h])
|
||||
coords << coordinate(lon, lat, alt_ft && alt_ft * FEET_TO_M)
|
||||
times << (time_h && epoch(row[time_h]))
|
||||
end
|
||||
[ coords, times.all? ? times : nil ]
|
||||
rescue CSV::MalformedCSVError => e
|
||||
raise Error, "Couldn't read CSV track (#{e.message})"
|
||||
end
|
||||
|
||||
def coordinate(lon, lat, alt)
|
||||
alt ? [ lon.round(6), lat.round(6), alt.round(1) ] : [ lon.round(6), lat.round(6) ]
|
||||
end
|
||||
|
||||
def valid?(lat, lon) = lat && lon && lat.between?(-90, 90) && lon.between?(-180, 180)
|
||||
|
||||
def number(value)
|
||||
Float(value.to_s.strip)
|
||||
rescue ArgumentError, TypeError
|
||||
nil
|
||||
end
|
||||
|
||||
# ISO 8601 or epoch seconds/milliseconds → epoch seconds.
|
||||
def epoch(value)
|
||||
v = value.to_s.strip
|
||||
return nil if v.empty?
|
||||
if v.match?(/\A-?\d+(\.\d+)?\z/)
|
||||
n = v.to_f
|
||||
return (n > 10_000_000_000 ? n / 1000 : n).round
|
||||
end
|
||||
Time.iso8601(v).to_i
|
||||
rescue ArgumentError
|
||||
Time.parse(v).to_i rescue nil
|
||||
end
|
||||
|
||||
def downsample(coords, times)
|
||||
return [ coords, times ] if coords.size <= MAX_POINTS
|
||||
step = (coords.size.to_f / MAX_POINTS).ceil
|
||||
keep = (0...coords.size).step(step).to_a
|
||||
keep << coords.size - 1 unless keep.last == coords.size - 1
|
||||
[ keep.map { coords[_1] }, times && keep.map { times[_1] } ]
|
||||
end
|
||||
end
|
||||
Reference in new issue
Block a user