mirror of
https://github.com/rickbarrette/simple-aircraft-manager-rails.git
synced 2026-11-17 15:50:36 -05:00
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>
97 lines
3.4 KiB
Ruby
97 lines
3.4 KiB
Ruby
# 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
|