mirror of
https://github.com/rickbarrette/simple-aircraft-manager-rails.git
synced 2026-11-17 15: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,88 @@
|
||||
require "test_helper"
|
||||
|
||||
class AirTrailExportTest < ActiveSupport::TestCase
|
||||
setup do
|
||||
@user = create_user
|
||||
@a = create_aircraft(owner: @user, tail_number: "N12345", make: "Cessna", model: "150L")
|
||||
end
|
||||
|
||||
def fly(date, from, to, tach:, hobbs: nil, user: @user, notes: nil)
|
||||
@a.flight_logs.create!(date: date, departure_location: from, destination_location: to,
|
||||
tach_time: tach, hobbs_time: hobbs, user: user, notes: notes)
|
||||
end
|
||||
|
||||
def export(**opts) = AirTrailExport.new(@a, username: "ricky", **opts)
|
||||
|
||||
test "builds AirTrail's import format" do
|
||||
fly Date.new(2026, 10, 24), "KLHV", "44N", tach: 1.9, hobbs: 2.0, notes: "Fall colors"
|
||||
data = JSON.parse(export(aircraft_type: "c150").to_json)
|
||||
|
||||
assert_equal [ { "id" => "sam-pilot", "username" => "ricky", "displayName" => "ricky" } ], data["users"]
|
||||
f = data["flights"].first
|
||||
assert_equal "2026-10-24", f["date"]
|
||||
assert_equal "day", f["datePrecision"]
|
||||
assert_equal({ "code" => "KLHV" }, f["from"])
|
||||
assert_equal({ "code" => "44N" }, f["to"])
|
||||
assert_equal 7200, f["duration"] # Hobbs 2.0 h in seconds
|
||||
assert_nil f["departure"]
|
||||
assert_nil f["arrival"]
|
||||
assert_nil f["airline"]
|
||||
assert_equal({ "name" => "Cessna 150L", "icao" => "C150" }, f["aircraft"])
|
||||
assert_equal "N12345", f["aircraftReg"]
|
||||
assert_nil f["flightNumber"]
|
||||
assert_equal "Tach 1.9 h · Hobbs 2.0 h\nFall colors", f["note"]
|
||||
passenger = f["passengers"].first
|
||||
assert_equal({ "userId" => "sam-pilot", "guestName" => nil, "seat" => "pilot", "seatNumber" => nil,
|
||||
"seatClass" => nil, "flightReason" => nil, "customFields" => {} }, passenger)
|
||||
# every key AirTrail requires (non-optional, nullable) is present
|
||||
%w[date from to departure arrival duration airline aircraft flightNumber aircraftReg note passengers].each { assert f.key?(_1), _1 }
|
||||
end
|
||||
|
||||
test "repeat legs on the same day and route get stable leg numbers so AirTrail doesn't merge them" do
|
||||
d = Date.new(2026, 6, 14)
|
||||
fly d, "KOQU", "KBID", tach: 0.4
|
||||
fly d, "KBID", "KOQU", tach: 0.4
|
||||
fly d, "KOQU", "KBID", tach: 0.5
|
||||
fly d + 1, "KOQU", "KBID", tach: 0.5
|
||||
numbers = JSON.parse(export.to_json)["flights"].map { _1["flightNumber"] }
|
||||
assert_equal [ nil, nil, "#2", nil ], numbers
|
||||
end
|
||||
|
||||
test "falls back to tach time; zero-time entries get no duration" do
|
||||
fly Date.current, "KOQU", "KOQU", tach: 1.5
|
||||
fly Date.current, "KDXR", "KDXR", tach: 0
|
||||
durations = JSON.parse(export.to_json)["flights"].map { _1["duration"] }
|
||||
assert_equal [ 5400, nil ], durations
|
||||
end
|
||||
|
||||
test "skips flights without usable airport codes and can limit to my flights" do
|
||||
other = create_user
|
||||
fly Date.current, "KOQU", "KBID", tach: 1
|
||||
fly Date.current, "", "KBID", tach: 1
|
||||
fly Date.current, "Home strip", "KBID", tach: 1
|
||||
fly Date.current, "KBID", "KOQU", tach: 1, user: other
|
||||
assert_equal [ 2, 2 ], [ export.exportable.size, export.skipped ]
|
||||
assert_equal 1, export(only_user: @user).exportable.size
|
||||
end
|
||||
|
||||
test "includes GPS tracks and defaults the aircraft type from the aircraft" do
|
||||
@a.update!(icao_type: "C150")
|
||||
f = fly Date.current, "KOQU", "KBID", tach: 1
|
||||
f.track_log.attach(io: File.open(Rails.root.join("test/fixtures/files/tracks/flight.kml")), filename: "flight.kml")
|
||||
fly Date.current, "KBID", "KOQU", tach: 1
|
||||
flights = JSON.parse(export.to_json)["flights"]
|
||||
assert_equal "kml", flights.first.dig("track", "sourceFormat")
|
||||
assert_equal 2, flights.first.dig("track", "times").size
|
||||
assert_not flights.last.key?("track")
|
||||
assert_equal "C150", flights.first.dig("aircraft", "icao")
|
||||
end
|
||||
|
||||
test "validates username and aircraft type" do
|
||||
fly Date.current, "KOQU", "KBID", tach: 1
|
||||
assert export.valid?
|
||||
assert_not AirTrailExport.new(@a, username: "ri").valid?
|
||||
assert_not AirTrailExport.new(@a, username: "jane pilot").valid?
|
||||
assert_not export(aircraft_type: "CESSNA").valid?
|
||||
assert_not AirTrailExport.new(create_aircraft, username: "ricky").valid? # no flights
|
||||
end
|
||||
end
|
||||
@@ -0,0 +1,130 @@
|
||||
require "test_helper"
|
||||
|
||||
class AirTrailSyncerTest < ActiveSupport::TestCase
|
||||
setup do
|
||||
@fake = FakeAirTrail.new(self)
|
||||
@user = create_user
|
||||
@a = create_aircraft(owner: @user, tail_number: "N12345", icao_type: "C150")
|
||||
@connection = AirTrailConnection.create!(user: @user, base_url: FakeAirTrail::BASE, api_key: "at_secret")
|
||||
@syncer = AirTrailSyncer.new(@connection)
|
||||
end
|
||||
|
||||
def fly(date, from, to, tach: 1.0, hobbs: nil, **attrs)
|
||||
# skip callbacks' job enqueueing noise: test adapter just records them
|
||||
@a.flight_logs.create!({ date: date, departure_location: from, destination_location: to, tach_time: tach,
|
||||
hobbs_time: hobbs, user: @user }.merge(attrs))
|
||||
end
|
||||
|
||||
test "api key is stored encrypted" do
|
||||
raw = AirTrailConnection.connection.select_value("select api_key from air_trail_connections where id = #{@connection.id}")
|
||||
assert_not_includes raw, "at_secret"
|
||||
assert_equal "at_secret", @connection.reload.api_key
|
||||
end
|
||||
|
||||
test "push creates the AirTrail flight with resolved ids, pilot seat and track" do
|
||||
f = fly(Date.new(2026, 10, 24), "KOQU", "KBID", tach: 0.5, hobbs: 0.6)
|
||||
f.track_log.attach(io: File.open(Rails.root.join("test/fixtures/files/tracks/flight.gpx")), filename: "flight.gpx")
|
||||
sync = @syncer.push(f)
|
||||
|
||||
assert_equal "synced", sync.status
|
||||
body = @fake.flights.fetch(sync.remote_id)
|
||||
assert_equal [ "2026-10-24", 101, 102, 7, 2160, "N12345" ],
|
||||
body.values_at("date", "fromId", "toId", "aircraftId", "duration", "aircraftReg")
|
||||
assert_equal [ { "userId" => "u-ricky", "guestName" => nil, "seat" => "pilot", "seatNumber" => nil,
|
||||
"seatClass" => nil, "flightReason" => nil, "customFields" => {} } ], body["passengers"]
|
||||
assert_equal "gpx", body.dig("track", "sourceFormat")
|
||||
assert_equal [ -71.412028, 41.597139, 5.2 ], body.dig("track", "coordinates", 0)
|
||||
assert_equal "ricky", @connection.reload.remote_username
|
||||
end
|
||||
|
||||
test "unchanged flights aren't re-sent; changed ones are replaced; deleted-in-AirTrail ones recreated" do
|
||||
f = fly(Date.current, "KOQU", "KBID")
|
||||
first = @syncer.push(f).remote_id
|
||||
puts_before = @fake.requests.count { _1[0] == :put }
|
||||
@syncer.push(f)
|
||||
assert_equal puts_before, @fake.requests.count { _1[0] == :put }
|
||||
|
||||
f.update!(notes: "Pattern work")
|
||||
@syncer.push(f)
|
||||
assert_equal "Tach 1.0 h\nPattern work", @fake.flights[first]["note"]
|
||||
|
||||
@fake.flights.delete(first)
|
||||
f.update!(notes: "Changed again")
|
||||
assert_not_equal first, @syncer.push(f).remote_id
|
||||
end
|
||||
|
||||
test "airports are looked up once and cached" do
|
||||
fly(Date.current, "KOQU", "KBID")
|
||||
fly(Date.current + 1, "KBID", "KOQU")
|
||||
@syncer.push(FlightLog.first)
|
||||
@syncer.push(FlightLog.last)
|
||||
assert_equal 2, @fake.requests.count { _1[1] == "/api/v1/airports" }
|
||||
end
|
||||
|
||||
test "unknown airports fail the flight with a clear error, and can be mapped by hand" do
|
||||
f = fly(Date.current, "53NH", "KOQU")
|
||||
sync = @syncer.push(f)
|
||||
assert_equal "error", sync.status
|
||||
assert_match "doesn't know airport 53NH", sync.last_error
|
||||
assert_equal [ "53NH" ], @connection.unresolved_airport_codes
|
||||
|
||||
assert @syncer.map_airport("53NH", "NH53")
|
||||
assert_equal "synced", @syncer.push(f).status
|
||||
assert_equal 106, @fake.flights.values.last["fromId"]
|
||||
end
|
||||
|
||||
test "logged codes are translated to AirTrail's through the airport database" do
|
||||
AirportData.sync!(StringIO.new("ident,type,name,latitude_deg,longitude_deg,elevation_ft,iso_country,iso_region,municipality,gps_code,iata_code,local_code\nK1B9,small_airport,Mansfield Municipal Airport,42,-71,122,US,US-MA,Mansfield,1B9,,1B9\n"))
|
||||
sync = @syncer.push(fly(Date.current, "KOQU", "1B9"))
|
||||
assert_equal "synced", sync.status
|
||||
assert_includes @fake.requests, [ :get, "/api/v1/airports", nil ]
|
||||
assert_equal FakeAirTrail::AIRPORTS["K1B9"], @fake.flights[sync.remote_id]["toId"]
|
||||
end
|
||||
|
||||
test "same-day repeat legs get leg numbers" do
|
||||
d = Date.current
|
||||
a = fly(d, "KOQU", "KBID")
|
||||
b = fly(d, "KOQU", "KBID")
|
||||
@syncer.push(a)
|
||||
@syncer.push(b)
|
||||
assert_equal [ nil, "#2" ], [ a, b ].map { @fake.flights[_1.air_trail_syncs.first.remote_id]["flightNumber"] }
|
||||
end
|
||||
|
||||
test "backfill links flights already in AirTrail (from a file import) instead of duplicating" do
|
||||
d = Date.new(2026, 10, 1)
|
||||
exact = fly(d, "KOQU", "KBID", hobbs: 1.2)
|
||||
repeat = fly(d, "KOQU", "KBID", hobbs: 0.8)
|
||||
remapped = fly(d + 1, "53NH", "KPVD", hobbs: 1.3) # hand-mapped to NH53 during the file import
|
||||
brand_new = fly(d + 2, "KPVD", "KMFD")
|
||||
e1 = @fake.add_existing(date: d.iso8601, from: "KOQU", to: "KBID")
|
||||
e2 = @fake.add_existing(date: d.iso8601, from: "KOQU", to: "KBID", flight_number: "#2")
|
||||
e3 = @fake.add_existing(date: (d + 1).iso8601, from: "NH53", to: "KPVD", duration: 4680)
|
||||
@fake.add_existing(date: d.iso8601, from: "KOQU", to: "KBID", reg: "N54321") # someone else's
|
||||
|
||||
result = @syncer.backfill
|
||||
assert_equal({ linked: 3, pushed: 4, failed: 0 }, result)
|
||||
assert_equal [ e1, e2, e3 ], [ exact, repeat, remapped ].map { _1.air_trail_syncs.first.remote_id }
|
||||
assert_equal 5, @fake.flights.size # 4 existing + brand_new
|
||||
assert brand_new.air_trail_syncs.first.remote_id
|
||||
assert_equal 106, @connection.airports.find_by(code: "53NH").remote_id # learned from the linked flight
|
||||
assert @connection.reload.backfilled_at
|
||||
end
|
||||
|
||||
test "bad key marks the connection" do
|
||||
@fake.valid_key = "other"
|
||||
assert_raises(AirTrailClient::AuthError) { @syncer.push(fly(Date.current, "KOQU", "KBID")) }
|
||||
assert_equal "error", @connection.reload.status
|
||||
assert_match "Invalid API key", @connection.last_error
|
||||
end
|
||||
|
||||
test "client follows pagination and maps errors" do
|
||||
3.times { |i| @fake.add_existing(date: "2026-01-0#{i + 1}", from: "KOQU", to: "KBID") }
|
||||
client = @connection.client
|
||||
ids = []
|
||||
client.each_flight { ids << _1["id"] }
|
||||
assert_equal 3, ids.size
|
||||
err = assert_raises(AirTrailClient::ValidationError) { client.create_flight({}) }
|
||||
assert_match "fromId Required", err.message
|
||||
assert_nil client.delete_flight(999_999) # 404 on delete is fine
|
||||
end
|
||||
end
|
||||
@@ -0,0 +1,52 @@
|
||||
require "test_helper"
|
||||
|
||||
class AirportCodeStandardizerTest < ActiveSupport::TestCase
|
||||
setup do
|
||||
AirportData.sync!(StringIO.new(<<~CSV))
|
||||
ident,type,name,latitude_deg,longitude_deg,elevation_ft,iso_country,iso_region,municipality,gps_code,iata_code,local_code
|
||||
KOQU,medium_airport,Quonset State Airport,41.6,-71.4,18,US,US-RI,North Kingstown,KOQU,NCO,OQU
|
||||
K1B9,small_airport,Mansfield Municipal Airport,42,-71.2,122,US,US-MA,Mansfield,1B9,,1B9
|
||||
K5M3,small_airport,Moultonborough Airport,43.8,-71.4,571,US,US-NH,Moultonborough,,,4MB
|
||||
08R,small_airport,Richmond Airport,41.5,-71.6,90,US,US-RI,West Kingston,,,08R
|
||||
KZZZ,closed,Old Field,40,-70,0,US,US-RI,Nowhere,,,ZZZ
|
||||
CSV
|
||||
@a = create_aircraft
|
||||
other = create_aircraft
|
||||
fly = ->(ac, from, to) { ac.flight_logs.create!(date: Date.current, tach_time: 1, departure_location: from, destination_location: to) }
|
||||
fly.(@a, "KOQU", "1B9")
|
||||
fly.(@a, "1B9", "4MB")
|
||||
fly.(@a, "4MB", "0R8")
|
||||
fly.(@a, "08R", "ZZZ")
|
||||
fly.(other, "1B9", "KOQU") # another aircraft: untouched
|
||||
@s = AirportCodeStandardizer.new(@a)
|
||||
end
|
||||
|
||||
test "previews each logged code against the standard code" do
|
||||
by_code = @s.changes.index_by(&:from)
|
||||
assert_equal %w[1B9 4MB ZZZ], by_code.keys.sort # KOQU and 08R already standard
|
||||
assert_equal [ "K1B9", 2, true, nil ], by_code["1B9"].to_h.values_at(:to, :flights, :suggested, :note)
|
||||
assert_match "Different identifier", by_code["4MB"].note
|
||||
assert_equal false, by_code["ZZZ"].suggested # closed airport: not pre-selected
|
||||
assert_equal [ "0R8", 1, "08R" ], @s.unknown.first.to_h.values_at(:code, :flights).push(@s.unknown.first.suggestion&.code)
|
||||
end
|
||||
|
||||
test "applies only the chosen codes, to this aircraft only" do
|
||||
assert_equal 4, @s.apply!(%w[1B9 4MB]) # 1B9 twice, 4MB twice
|
||||
assert_equal [ %w[KOQU K1B9], %w[K1B9 K5M3], %w[K5M3 0R8], %w[08R ZZZ] ],
|
||||
@a.flight_logs.order(:id).pluck(:departure_location, :destination_location)
|
||||
assert_equal "1B9", FlightLog.where.not(aircraft: @a).first.departure_location
|
||||
assert_match "1B9→K1B9", @a.events.first.notes
|
||||
assert_equal %w[ZZZ], AirportCodeStandardizer.new(@a).changes.map(&:from)
|
||||
end
|
||||
|
||||
test "fixes unknown codes (typos) to a chosen airport's standard code" do
|
||||
assert_equal 1, @s.apply!([], fixes: { "0R8" => "08R", "KOQU" => "1B9", "XXXX" => "1B9" }) # only real unknown codes
|
||||
assert_equal %w[4MB 08R], @a.flight_logs.order(:id).third.slice(:departure_location, :destination_location).values
|
||||
assert_empty AirportCodeStandardizer.new(@a).unknown
|
||||
assert_equal "KOQU", @a.flight_logs.order(:id).first.departure_location
|
||||
end
|
||||
|
||||
test "a fix to an airport not in the database is ignored" do
|
||||
assert_equal 0, @s.apply!([], fixes: { "0R8" => "NOPE" })
|
||||
end
|
||||
end
|
||||
@@ -0,0 +1,52 @@
|
||||
require "test_helper"
|
||||
|
||||
class TrackParserTest < ActiveSupport::TestCase
|
||||
def parse(name) = TrackParser.new(file_fixture_path.join("tracks", name).read, filename: name).parse
|
||||
def file_fixture_path = Rails.root.join("test/fixtures/files")
|
||||
|
||||
test "GPX: [lon, lat, alt_m] with epoch-second times" do
|
||||
t = parse("flight.gpx")
|
||||
assert_equal "gpx", t.source_format
|
||||
assert_equal [ -71.412028, 41.597139, 5.2 ], t.coordinates.first
|
||||
assert_equal 3, t.points
|
||||
assert_equal Time.utc(2026, 10, 24, 14).to_i, t.times.first
|
||||
assert_equal 2500, t.max_altitude_ft # 762 m
|
||||
json = t.as_air_trail_json
|
||||
assert_equal %i[coordinates sourceFormat sourceName times], json.keys.sort
|
||||
assert_equal "flight.gpx", json[:sourceName]
|
||||
end
|
||||
|
||||
test "KML gx:Track and LineString" do
|
||||
t = parse("flight.kml")
|
||||
assert_equal [ [ -71.412028, 41.597139, 5.2 ], [ -71.5, 41.42, 762.0 ] ], t.coordinates
|
||||
assert_equal 2, t.times.size
|
||||
line = parse("linestring.kml")
|
||||
assert_equal 3, line.points
|
||||
assert_nil line.times
|
||||
end
|
||||
|
||||
test "CSV: altitude in feet, millisecond timestamps" do
|
||||
t = parse("flight.csv")
|
||||
assert_equal "csv", t.source_format
|
||||
assert_equal [ -71.5, 41.42, 762.0 ], t.coordinates[1] # 2500 ft
|
||||
assert_equal 1_793_023_200, t.times.first
|
||||
end
|
||||
|
||||
test "sniffs the format when the extension doesn't say" do
|
||||
gpx = Rails.root.join("test/fixtures/files/tracks/flight.gpx").read
|
||||
assert_equal "gpx", TrackParser.new(gpx, filename: "track.xml").parse.source_format
|
||||
end
|
||||
|
||||
test "downsamples huge tracks to AirTrail's limit, keeping the last point" do
|
||||
rows = (0..100_200).map { |i| "#{41 + i * 1e-6},-71,1000" }
|
||||
t = TrackParser.new("lat,lon,alt\n#{rows.join("\n")}", filename: "big.csv").parse
|
||||
assert_operator t.points, :<=, TrackParser::MAX_POINTS + 1
|
||||
assert_in_delta 41 + 100_200 * 1e-6, t.coordinates.last[1], 1e-6
|
||||
end
|
||||
|
||||
test "rejects unusable tracks" do
|
||||
assert_raises(TrackParser::Error) { TrackParser.new("hello", filename: "x.txt").parse }
|
||||
assert_raises(TrackParser::Error) { TrackParser.new("lat,lon\n41,-71\n", filename: "one.csv").parse }
|
||||
assert_raises(TrackParser::Error) { TrackParser.new("a,b\n1,2\n3,4\n", filename: "x.csv").parse }
|
||||
end
|
||||
end
|
||||
Reference in new issue
Block a user