Add spreadsheet flight import; fix oil/fuel consumption charts

Flight import (owners, Flights tab → Import from spreadsheet):
- Upload .xlsx or .csv, preview, then confirm. Columns are matched by
  header name (Date, From, To, Tach, Hobbs, Oil, Fuel, Flight Time).
- Each flight's time is derived from the previous row's meter reading;
  rows are kept in file order so typos (tach going backwards) are
  flagged and skipped while the next good row absorbs the time.
- Unchanged tach imports as a 0.0 hr entry so fuel/oil isn't lost;
  "Full" fuel is warned about; re-imports skip existing rows.
- History only: component hours are untouched unless the owner opts in
  to advancing the aircraft's meters to the last reading.
- Upload redirects to a GET preview (Turbo ignores 200 renders after a
  form POST, which made the button appear to do nothing).

Consumption charts:
- Replace per-fill math (qty ÷ hours since last add), which spiked to
  80 gal/hr on partial fills and top-offs, with a rolling rate (total
  added ÷ hours over a trailing 10 h fuel / 25 h oil window).
- Headline shows recent (last ~50/100 hrs) and lifetime rates; straight
  chart lines instead of splines.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
rickyandClaude Opus 5.5 committed 2026-11-04 19:23:47 -05:00
1 parent 38df19c331
commit 4d3dc7150a
21 files changed
+820 -75

No files matched your search

+6
View File
@@ -65,3 +65,9 @@ end
gem "chartkick", "~> 5.2"
gem "json", "~> 2.10"
gem "roo", "~> 2.10"
gem "csv", "~> 3.3"
gem "caxlsx", "~> 4.5", group: :test
+19 -2
View File
@@ -99,10 +99,16 @@ GEM
rack-test (>= 0.6.3)
regexp_parser (>= 1.5, < 3.0)
xpath (~> 3.2)
caxlsx (4.5.0)
htmlentities (~> 4.3, >= 4.3.4)
marcel (~> 1.0)
nokogiri (~> 1.10, >= 1.10.4)
rubyzip (>= 2.4, < 4)
chartkick (5.2.1)
concurrent-ruby (1.3.8)
connection_pool (3.0.2)
crass (1.0.8)
csv (3.3.7)
date (3.6.0)
debug (1.11.1)
irb (~> 1.10)
@@ -119,6 +125,7 @@ GEM
raabro (~> 1.4)
globalid (1.4.0)
activesupport (>= 6.1)
htmlentities (4.4.2)
i18n (1.15.2)
concurrent-ruby (~> 1.0)
importmap-rails (2.2.3)
@@ -265,6 +272,9 @@ GEM
reline (0.7.0)
io-console (~> 0.5)
rexml (3.4.4)
roo (2.10.1)
nokogiri (~> 1)
rubyzip (>= 1.3.0, < 3.0.0)
rubocop (1.92.2)
json (>= 2.3)
language_server-protocol (~> 3.17.0.2)
@@ -294,7 +304,7 @@ GEM
rubocop-performance (>= 1.24)
rubocop-rails (>= 2.30)
ruby-progressbar (1.13.0)
rubyzip (3.7.0)
rubyzip (2.4.1)
securerandom (0.4.1)
selenium-webdriver (4.44.0)
base64 (~> 0.2)
@@ -392,7 +402,9 @@ DEPENDENCIES
bootsnap
brakeman
capybara
caxlsx (~> 4.5)
chartkick (~> 5.2)
csv (~> 3.3)
debug
importmap-rails
json (~> 2.10)
@@ -400,6 +412,7 @@ DEPENDENCIES
propshaft
puma (>= 5.0)
rails (~> 8.1.0)
roo (~> 2.10)
rubocop-rails-omakase
selenium-webdriver
solid_cable
@@ -439,10 +452,12 @@ CHECKSUMS
brakeman (8.1.0) sha256=cfa9e5214e4925842f4bc849a64f8a7e6c5d0425fe98eac8d7b7bc60fdc17e07
builder (3.3.0) sha256=497918d2f9dca528fdca4b88d84e4ef4387256d984b8154e9d5d3fe5a9c8835f
capybara (3.40.0) sha256=42dba720578ea1ca65fd7a41d163dd368502c191804558f6e0f71b391054aeef
caxlsx (4.5.0) sha256=e3d98d859f148df05d5462086b5079b523f29c1766b569535f1d68629ce743ff
chartkick (5.2.1) sha256=2848d7de87189f30f28d077eb0bbdebc8a1f0f6f81de1ded95008fe564369949
concurrent-ruby (1.3.8) sha256=b2f1be836e968ccc78ccfce277ea79c72a88633f22306782c16ff23fb415d1e1
connection_pool (3.0.2) sha256=33fff5ba71a12d2aa26cb72b1db8bba2a1a01823559fb01d29eb74c286e62e0a
crass (1.0.8) sha256=7a0145c7a2c59f8e7923b3a69ce819c444823a00a5127d805298fea13be24efe
csv (3.3.7) sha256=15a3f16f6628da8664d455eb634e7f22031bcf53044923caf56975109e71b9f8
date (3.6.0) sha256=18ac945c4e24ebd93534f8eab8c2fd8c5604b3e977105b3798d62a1e8d27b580
debug (1.11.1) sha256=2e0b0ac6119f2207a6f8ac7d4a73ca8eb4e440f64da0a3136c30343146e952b6
dotenv (3.2.0) sha256=e375b83121ea7ca4ce20f214740076129ab8514cd81378161f11c03853fe619d
@@ -453,6 +468,7 @@ CHECKSUMS
et-orbi (1.4.2) sha256=bb555dae668419cb24caa2a293a170e58be6d4df1e017c51f5030bdc133cd20c
fugit (1.14.0) sha256=e311a9cc85a54bb67649df45060e721282a1e2c8ebad40e41874a3a7d576f37e
globalid (1.4.0) sha256=037f12fbf1d9d7a014d501c2d5c77356fd4ddd96d7a7991d6700bba96706f427
htmlentities (4.4.2) sha256=bbafbdf69f2eca9262be4efef7e43e6a1de54c95eb600f26984f71d2fe96c5c3
i18n (1.15.2) sha256=00f9eb62412fe593b2a65a97daa75300d37abb8f7202ec748e94b6d46a9dd1b5
importmap-rails (2.2.3) sha256=7101be2a4dc97cf1558fb8f573a718404c5f6bcfe94f304bf1f39e444feeb16a
io-console (0.9.5) sha256=83dd9bbf4c4680416629245b39cd4212eb5f4d9a4ea91804870c1f6b846414d5
@@ -511,13 +527,14 @@ CHECKSUMS
regexp_parser (2.13.1) sha256=5aedb6b7c35688f51e86eef17a15374dfd287c83f4ca644b2c5f3ec50a33b44b
reline (0.7.0) sha256=5b012d8e55dbf9d450f12bde2cf7d15ff546ae80b3f8f3b30e570d431815583d
rexml (3.4.4) sha256=19e0a2c3425dfbf2d4fc1189747bdb2f849b6c5e74180401b15734bc97b5d142
roo (2.10.1) sha256=cbb43bc955f9c110e74b721c835fb9bd3515b63af88ec709ac87fbf30f8be70e
rubocop (1.92.2) sha256=14fb31eed143a0dc6f78186c550c1715e81cd38f59d05d1165050e445c1f5382
rubocop-ast (1.50.0) sha256=b9ca88300da0803ee222ad20cdb30494c0a784eed06fdc35d254b06d662788db
rubocop-performance (1.27.0) sha256=eeeb1374d062a368ee1c787b70eb0b0cc4b184cb1f8565f424760946146d61ce
rubocop-rails (2.38.0) sha256=6e2b7c532efa95e3050371bec20b8f5b025662acead8bc70ee31f0e3e6fa2f06
rubocop-rails-omakase (1.1.0) sha256=2af73ac8ee5852de2919abbd2618af9c15c19b512c4cfc1f9a5d3b6ef009109d
ruby-progressbar (1.13.0) sha256=80fc9c47a9b640d6834e0dc7b3c94c9df37f08cb072b7761e4a71e22cff29b33
rubyzip (3.7.0) sha256=65c19294da75297a939006f3516deacc33185fbd721ff1954f7a231db6d3e121
rubyzip (2.4.1) sha256=8577c88edc1fde8935eb91064c5cb1aef9ad5494b940cf19c775ee833e075615
securerandom (0.4.1) sha256=cc5193d414a4341b6e225f0cb4446aceca8e50d5e1888743fac16987638ea0b1
selenium-webdriver (4.44.0) sha256=6f1df072529af369589c46f0e01132952aabb250cfd683c274d74dc1eb5d8477
solid_cable (3.0.12) sha256=a168a54731a455d5627af48d8441ea3b554b8c1f6e6cd6074109de493e6b0460
+1
View File
@@ -75,6 +75,7 @@ Alternatively, stop the container and copy the volume.
| Fleet dashboard | Every aircraft you can access, with airworthiness status and its top issues. |
| Airworthiness | RED/ORANGE/GREEN from AD compliance, grounding squawks, inspection recurrency, and replacement-critical components. Warnings start 10 hours or 30 days ahead. Logic: `app/services/airworthiness.rb`, `app/services/compliance.rb`. |
| Hours | Absolute tach/hobbs updates or per-flight logs. Both apply the delta to every in-service component. Downward corrections are clamped at 0. |
| Flight import | Owners can import historical flights from `.xlsx` or `.csv` (Date, From, To, Tach, Hobbs, Oil Added, Fuel Added). Each flight's time is derived from the previous row's reading. A preview flags typos such as a tach going backwards, and rows already imported are skipped. History only: component hours aren't changed unless you choose to advance the aircraft's meters. |
| Airworthiness enforcement | Optional per aircraft. It blocks flight logs and hours updates while the aircraft is RED. |
| Components | Tree of parent and child parts, TBO and replacement limits, and service/replace reset. Owners can create new component types inline. |
| Squawks | Four priorities. "Ground Aircraft" makes the aircraft RED. Supports attachments, resolve, and reopen. |
@@ -0,0 +1,87 @@
# Owner-only bulk import of historical flights: upload → preview → confirm.
# The uploaded file is kept in tmp/imports between the two steps.
class FlightImportsController < ApplicationController
include AircraftScoped
before_action :require_owner!
MAX_SIZE = 10.megabytes
DIR = Rails.root.join("tmp/imports")
# Server-generated "<uuid>.<ext>"; anything else is rejected before touching the filesystem.
TOKEN_FORMAT = /\A\h{8}-\h{4}-\h{4}-\h{4}-\h{12}\.(xlsx|csv)\z/
def new; end
# Store the upload, then redirect to the preview (Turbo requires a redirect after a form POST).
def create
file = params[:file]
return redirect_to(new_aircraft_flight_import_path(@aircraft), alert: "Choose a file to upload.") unless file.respond_to?(:path)
return redirect_to(new_aircraft_flight_import_path(@aircraft), alert: "File is too large.") if file.size > MAX_SIZE
ext = File.extname(file.original_filename).delete(".").downcase
unless ext.in?(FlightImport::EXTENSIONS)
return redirect_to new_aircraft_flight_import_path(@aircraft), alert: "Upload an .xlsx or .csv file."
end
cleanup_old_files
FileUtils.mkdir_p(DIR)
token = "#{SecureRandom.uuid}.#{ext}"
FileUtils.cp(file.path, stored_path(token))
redirect_to aircraft_flight_import_path(@aircraft, token: token, filename: file.original_filename,
starting_tach: params[:starting_tach].presence,
starting_hobbs: params[:starting_hobbs].presence)
end
# Preview.
def show
@token = params[:token].to_s
return redirect_to_upload("Upload expired. Please upload the file again.") unless stored_file?(@token)
@filename = params[:filename].to_s
@starting_tach = params[:starting_tach].presence
@starting_hobbs = params[:starting_hobbs].presence
@import = build_import(@token, @filename)
@import.rows # parse now so header errors surface here
rescue FlightImport::Error => e
redirect_to_upload(e.message)
end
def confirm
token = params[:token].to_s
return redirect_to_upload("Upload expired. Please upload the file again.") unless stored_file?(token)
path = stored_path(token)
import = build_import(token, params[:filename], starting_tach: params[:starting_tach], starting_hobbs: params[:starting_hobbs])
count = import.import!(user: current_user, update_aircraft_hours: params[:update_aircraft_hours] == "1")
File.delete(path)
redirect_to aircraft_flight_logs_path(@aircraft), notice: "Imported #{count} flights."
rescue FlightImport::Error => e
redirect_to new_aircraft_flight_import_path(@aircraft), alert: e.message
rescue AircraftGroundedError
redirect_to aircraft_flight_logs_path(@aircraft),
alert: "Flights imported, but aircraft hours weren't updated: the aircraft is grounded and enforcement is on."
end
private
def stored_file?(token)
TOKEN_FORMAT.match?(token) && File.exist?(stored_path(token))
end
def redirect_to_upload(message)
redirect_to new_aircraft_flight_import_path(@aircraft), alert: message
end
def build_import(token, filename, starting_tach: @starting_tach, starting_hobbs: @starting_hobbs)
FlightImport.new(@aircraft, path: stored_path(token), filename: filename,
starting_tach: starting_tach, starting_hobbs: starting_hobbs)
end
# Files are namespaced by aircraft so a token can't be replayed on another aircraft.
def stored_path(token)
DIR.join("#{@aircraft.id}-#{File.basename(token)}")
end
def cleanup_old_files
Dir.glob(DIR.join("*")).each { |f| File.delete(f) if File.mtime(f) < 1.day.ago }
end
end
+1 -1
View File
@@ -5,7 +5,7 @@ module AircraftHelper
[ "Overview", aircraft_path(aircraft), %w[aircraft] ],
[ "Components", aircraft_components_path(aircraft), %w[components] ],
[ "Squawks", aircraft_squawks_path(aircraft), %w[squawks] ],
[ "Flights", aircraft_flight_logs_path(aircraft), %w[flight_logs] ],
[ "Flights", aircraft_flight_logs_path(aircraft), %w[flight_logs flight_imports] ],
[ "Oil & Fuel", aircraft_consumable_records_path(aircraft), %w[consumable_records] ],
[ "Logbook", aircraft_logbook_entries_path(aircraft), %w[logbook_entries] ],
[ "Inspections", aircraft_inspections_path(aircraft), %w[inspections inspection_types inspection_records] ],
+1 -1
View File
@@ -7,7 +7,7 @@ class FlightLog < ApplicationRecord
has_one_attached :track_log
validates :date, presence: true
validates :tach_time, presence: true, numericality: { greater_than: 0 }
validates :tach_time, presence: true, numericality: { greater_than_or_equal_to: 0 }
validates :hobbs_time, numericality: { greater_than_or_equal_to: 0 }, allow_nil: true
validates_uploads :track_log
before_validation :derive_times
+52 -36
View File
@@ -1,58 +1,74 @@
# Oil/fuel consumption: per-interval datapoints between consecutive records
# (sorted by flight hours), averaged over the last 20 with IQR outliers and
# excluded records left out. Oil → hours per quart; fuel → gallons per hour.
# Oil/fuel consumption as a rolling rate: total added ÷ tach hours flown over a
# trailing window. Unlike per-fill math (qty added ÷ hours since the last add),
# this tolerates partial fills and top-offs: a 15 gal top-off 0.2 h after a
# partial fill no longer reads as 75 gal/hr.
#
# oil → hours per quart (higher is better)
# fuel → gallons per hour
#
# Records marked "exclude from averages" (e.g. oil changes) still mark a point
# in time, but their quantity isn't counted.
class ConsumptionStats
WINDOW = 20
Point = Struct.new(:record, :hours, :value, :excluded, :outlier, keyword_init: true)
CHART_WINDOW_HOURS = { "oil" => 25, "fuel" => 10 }.freeze
RECENT_WINDOW_HOURS = { "oil" => 100, "fuel" => 50 }.freeze
attr_reader :record_type, :records
def initialize(aircraft, record_type)
@record_type = record_type
@records = aircraft.consumable_records.where(record_type: record_type).order(:flight_hours, :date).to_a
@records = aircraft.consumable_records.where(record_type: record_type).order(:flight_hours, :date, :id).to_a
@hours = @records.map { _1.flight_hours.to_f }
# @prefix[i] = counted quantity of records[0...i]
@prefix = @records.each_with_object([ 0.0 ]) do |r, acc|
acc << acc.last + (r.excluded_from_averages? ? 0.0 : r.quantity_added.to_f)
end
end
def oil? = record_type == "oil"
def metric_label = oil? ? "hrs/qt" : "gal/hr"
def chart_window_hours = CHART_WINDOW_HOURS.fetch(record_type)
def recent_window_hours = RECENT_WINDOW_HOURS.fetch(record_type)
# [[tach_hours, rate]] — the rolling rate at each record.
def points
@points ||= begin
pts = @records.each_cons(2).filter_map do |prev, cur|
dh = (cur.flight_hours - prev.flight_hours).to_f
qty = cur.quantity_added.to_f
next unless qty > 0 && dh > 0
Point.new(record: cur, hours: cur.flight_hours.to_f, value: oil? ? dh / qty : qty / dh,
excluded: cur.excluded_from_averages?)
end
bounds = iqr_bounds(pts.map(&:value))
pts.each { |p| p.outlier = bounds && !(bounds[0]..bounds[1]).cover?(p.value) }
@points ||= @records.each_index.filter_map do |i|
rate = rate_ending_at(i, chart_window_hours)
[ @hours[i].round(1), rate.round(2) ] if rate
end
end
alias_method :chart_data, :points
def average
window = points.last(WINDOW)
return nil if window.empty?
included = window.reject { |p| p.excluded || p.outlier }
source = included.presence || window
(source.sum(&:value) / source.size).round(1)
# Rate over roughly the last RECENT_WINDOW_HOURS (or all history if shorter).
def recent
return nil if @records.size < 2
rate_ending_at(@records.size - 1, recent_window_hours, allow_shorter: true)&.round(1)
end
alias_method :average, :recent
def lifetime
return nil if @records.size < 2
rate_between(0, @records.size - 1)&.round(1)
end
def latest = points.last&.value&.round(1)
def chart_data
points.map { |p| [ p.hours.round(1), p.value.round(2) ] }
end
def latest = points.last&.last&.round(1)
private
def iqr_bounds(values)
return nil if values.size < 5
nums = values.sort
n = nums.size
q1 = nums[n / 4]
q3 = nums[(3 * n) / 4]
iqr = q3 - q1
return nil if iqr.zero?
[ q1 - 1.5 * iqr, q3 + 1.5 * iqr ]
# Rate over the shortest span ending at record i that covers >= window hours,
# measured from an earlier record so it starts at a known fill point.
def rate_ending_at(i, window, allow_shorter: false)
target = @hours[i] - window
j = (@hours.bsearch_index { _1 > target } || @hours.size) - 1 # last record at or before target
j = 0 if j.negative? && allow_shorter
return nil if j.negative? || j >= i
rate_between(j, i)
end
# Quantity added after record j up to and including i, over the hours between them.
def rate_between(j, i)
span = @hours[i] - @hours[j]
qty = @prefix[i + 1] - @prefix[j + 1]
return nil unless span.positive? && qty.positive?
oil? ? span / qty : qty / span
end
end
+252
View File
@@ -0,0 +1,252 @@
require "roo"
# Imports historical flights from a spreadsheet (.xlsx or .csv) where each row
# holds the meter readings at the END of a flight:
#
# Date | From | To | Tach Time | Hobbs Time | Oil Added | Fuel Added | Flight Time
#
# Flight duration is derived from the previous row's reading. Rows are processed
# in file order (not re-sorted), so a reading that goes backwards is reported as
# an error instead of silently reshuffled.
#
# Importing records history only: it does NOT add time to components (that
# would double-count). Optionally, the aircraft's meters are advanced to the
# last reading afterwards via a normal hours update.
class FlightImport
class Error < StandardError; end
EXTENSIONS = %w[xlsx csv].freeze
BLANK_VALUES = [ "", "n", "no", "none", "-", "—", "0", "0.0" ].freeze
LONG_FLIGHT_HOURS = 15
DATE_FORMATS = [ "%m/%d/%y", "%m/%d/%Y", "%Y-%m-%d", "%m-%d-%y", "%m-%d-%Y", "%d-%b-%y", "%d-%b-%Y" ].freeze
# Header aliases, matched after lowercasing and stripping non-letters.
COLUMNS = {
date: ->(h) { h == "date" || h.start_with?("date") },
from: ->(h) { h.start_with?("from", "dep", "origin") },
to: ->(h) { %w[to dest destination arr arrival arrive].include?(h) },
tach: ->(h) { h.start_with?("tach") },
hobbs: ->(h) { h.start_with?("hobbs", "hobs") },
oil: ->(h) { h.start_with?("oil") },
fuel: ->(h) { h.start_with?("fuel") },
flight_time: ->(h) { h.include?("flighttime") || h.include?("duration") }
}.freeze
Row = Struct.new(:line, :date, :from, :to, :tach_out, :tach_in, :tach_time,
:hobbs_out, :hobbs_in, :hobbs_time, :oil, :fuel, :status, :messages, :baseline,
keyword_init: true) do
def importable? = status.in?(%i[ok warning])
def warn(msg) = (messages << msg; self.status = :warning if status == :ok)
def error(msg) = (messages << msg; self.status = :error)
end
attr_reader :columns, :headers
def initialize(aircraft, path:, filename:, starting_tach: nil, starting_hobbs: nil)
@aircraft = aircraft
@path = path.to_s
@filename = filename.to_s
@starting_tach = decimal(starting_tach, 1)
@starting_hobbs = decimal(starting_hobbs, 1)
end
def rows
@rows ||= build_rows
end
def importable = rows.select(&:importable?)
def count(status) = rows.count { _1.status == status }
def total_tach_hours = importable.sum(&:tach_time)
def last_reading = importable.last
def mapped?(column) = columns.key?(column)
# Creates flight logs + oil/fuel records. Returns the number of flights imported.
def import!(user:, update_aircraft_hours: false)
flights = importable
raise Error, "Nothing to import." if flights.empty?
Aircraft.transaction do
flights.each do |r|
log = @aircraft.flight_logs.create!(
user: user, date: r.date, departure_location: r.from, destination_location: r.to,
tach_out: r.tach_out, tach_in: r.tach_in, tach_time: r.tach_time,
hobbs_out: r.hobbs_out, hobbs_in: r.hobbs_in, hobbs_time: r.hobbs_time,
oil_added: r.oil, fuel_added: r.fuel, notes: "Imported from #{@filename}"
)
{ "oil" => r.oil, "fuel" => r.fuel }.each do |type, qty|
next unless qty
@aircraft.consumable_records.create!(record_type: type, flight_log: log, date: r.date,
quantity_added: qty, flight_hours: r.tach_in,
notes: "Imported from #{@filename}")
end
end
@aircraft.log_event!("flight", "Imported #{flights.size} flights from spreadsheet", user: user,
notes: "#{@filename}: #{flights.first.date} – #{flights.last.date}, #{total_tach_hours} tach hrs")
end
advance_aircraft_hours!(user) if update_aircraft_hours
flights.size
end
# Moves the aircraft's meters up to the last imported reading (never down).
# Returns the delta applied, or nil if nothing changed.
def advance_aircraft_hours!(user)
last = last_reading
return nil unless last && last.tach_in > @aircraft.tach_time
hobbs = last.hobbs_in if last.hobbs_in && last.hobbs_in >= @aircraft.hobbs_time
HoursService.new(@aircraft, user: user).update_hours!(tach_time: last.tach_in, hobbs_time: hobbs)
end
private
def build_rows
sheet, header_index = locate_header
existing = @aircraft.flight_logs.where.not(tach_in: nil).pluck(:date, :tach_in).to_set { |d, t| [ d, t.to_d ] }
prev_tach = @starting_tach
prev_hobbs = @starting_hobbs
result = []
((header_index + 1)..sheet.last_row).each do |i|
raw = sheet.row(i)
next if raw.compact.all? { _1.to_s.strip.empty? }
row = Row.new(line: i, status: :ok, messages: [])
row.date = parse_date(cell(raw, :date))
row.from = code(cell(raw, :from))
row.to = code(cell(raw, :to))
row.tach_in = decimal(cell(raw, :tach), 1)
row.hobbs_in = decimal(cell(raw, :hobbs), 1)
row.oil = quantity(row, cell(raw, :oil), "Oil")
row.fuel = quantity(row, cell(raw, :fuel), "Fuel")
flight_time = decimal(cell(raw, :flight_time), 1)
row.error("Date missing or not recognized (#{cell(raw, :date).inspect})") unless row.date
row.error("Tach reading missing") unless row.tach_in
if row.tach_in
if prev_tach.nil?
if flight_time&.positive?
row.tach_time = flight_time
row.tach_out = row.tach_in - flight_time
row.warn("First row: duration taken from Flight Time column (enter the starting tach to be exact)")
else
row.error("Starting reading only: no previous tach to measure from. Enter the tach before the first flight to import this row")
row.baseline = true
end
elsif row.tach_in < prev_tach
row.error("Tach went backwards (#{prev_tach} → #{row.tach_in}). Likely a typo")
elsif row.tach_in == prev_tach
row.tach_out = prev_tach
row.tach_time = BigDecimal("0")
row.warn("Tach didn't change (#{row.tach_in}). Imported as 0.0 hrs; check for a typo")
else
row.tach_out = prev_tach
row.tach_time = row.tach_in - prev_tach
row.warn("Long flight: #{row.tach_time} tach hrs. Missing rows?") if row.tach_time > LONG_FLIGHT_HOURS
end
end
if row.hobbs_in && prev_hobbs && row.hobbs_in > prev_hobbs
row.hobbs_out = prev_hobbs
row.hobbs_time = row.hobbs_in - prev_hobbs
elsif flight_time&.positive?
row.hobbs_time = flight_time
end
if row.hobbs_in && prev_hobbs && row.hobbs_in < prev_hobbs
row.warn("Hobbs went backwards (#{prev_hobbs} → #{row.hobbs_in}). Meter replaced?")
end
if row.status != :error && existing.include?([ row.date, row.tach_in ])
row.status = :duplicate
row.messages = [ "Already imported" ]
end
# Bad rows don't move the baseline, so the next good row absorbs the time.
if row.status != :error || row.baseline
prev_tach = row.tach_in
prev_hobbs = row.hobbs_in if row.hobbs_in
end
result << row
end
result
end
def locate_header
book = open_spreadsheet
book.sheets.each do |name|
sheet = book.sheet(name)
next unless sheet.first_row
(sheet.first_row..[ sheet.first_row + 20, sheet.last_row ].min).each do |i|
cols = detect_columns(sheet.row(i))
if cols.key?(:date) && cols.key?(:tach)
@columns = cols
@headers = sheet.row(i)
return [ sheet, i ]
end
end
end
raise Error, "Couldn't find a header row with Date and Tach columns."
end
def open_spreadsheet
ext = File.extname(@filename).delete(".").downcase
raise Error, "Upload an .xlsx or .csv file." unless ext.in?(EXTENSIONS)
Roo::Spreadsheet.open(@path, extension: ext.to_sym)
rescue Zip::Error, ArgumentError, CSV::MalformedCSVError => e
raise Error, "Couldn't read the file (#{e.message})."
end
def detect_columns(header_row)
found = {}
header_row.each_with_index do |h, idx|
norm = h.to_s.downcase.gsub(/[^a-z]/, "")
next if norm.empty?
COLUMNS.each { |key, match| found[key] ||= idx if match.(norm) }
end
found
end
def cell(raw, key)
idx = @columns[key]
idx && raw[idx]
end
def parse_date(value)
case value
when Date, DateTime, Time then value.to_date
when nil then nil
else
str = value.to_s.strip
DATE_FORMATS.each do |fmt|
date = Date.strptime(str, fmt) rescue next
return date
end
nil
end
end
def decimal(value, scale = 2)
return nil if value.nil? || value.to_s.strip.empty?
BigDecimal(value.to_s.strip.delete(",")).round(scale)
rescue ArgumentError
nil
end
def quantity(row, value, label)
return nil if value.nil? || BLANK_VALUES.include?(value.to_s.strip.downcase)
if value.to_s.strip.casecmp?("full")
row.warn("#{label} “Full” has no quantity, so it isn't recorded")
return nil
end
qty = decimal(value)
if qty.nil? || qty.negative?
row.warn("#{label} value #{value.inspect} ignored")
return nil
end
qty
end
def code(value)
value.to_s.strip.upcase.presence
end
end
+2 -2
View File
@@ -46,8 +46,8 @@
<div class="card-header"><h2 class="card-title">Consumption</h2>
<%= link_to "Oil & fuel →", aircraft_consumable_records_path(@aircraft), class: "link text-sm" %></div>
<div class="card-body grid grid-cols-2 gap-4 text-center">
<div><div class="text-2xl font-semibold"><%= @oil.average || "—" %></div><div class="text-xs text-slate-500">Avg oil, hrs/qt</div></div>
<div><div class="text-2xl font-semibold"><%= @fuel.average || "—" %></div><div class="text-xs text-slate-500">Avg fuel burn, gal/hr</div></div>
<div><div class="text-2xl font-semibold"><%= @oil.recent || "—" %></div><div class="text-xs text-slate-500">Oil, hrs/qt (last ~100 hrs)</div></div>
<div><div class="text-2xl font-semibold"><%= @fuel.recent || "—" %></div><div class="text-xs text-slate-500">Fuel burn, gal/hr (last ~50 hrs)</div></div>
</div>
</section>
</div>
+1 -1
View File
@@ -9,7 +9,7 @@
<%= f.field :flight_hours, as: :number, step: 0.1, label: "Tach at time of record" %>
</div>
<%= f.field :notes, as: :textarea, rows: 2 %>
<%= f.field :excluded_from_averages, as: :checkbox, label: "Exclude from averages", hint: "Use for oil changes or partial fills that would skew the trend." %>
<%= f.field :excluded_from_averages, as: :checkbox, label: "Exclude from averages", hint: "Use for oil changes: the quantity added is ignored in consumption rates." %>
<div class="flex gap-2"><%= f.submit "Save" %><%= link_to "Cancel", aircraft_consumable_records_path(aircraft), class: "btn-secondary" %></div>
<% end %>
<% if record.persisted? %><div class="mt-4"><%= delete_button "Delete record", aircraft_consumable_record_path(aircraft, record) %></div><% end %>
+6 -5
View File
@@ -8,15 +8,16 @@
<%= link_to "+ Add #{stats.oil? ? 'Oil' : 'Fuel'}", new_aircraft_consumable_record_path(@aircraft, type: stats.record_type), class: "btn-secondary btn-sm" %>
</div>
<div class="card-body">
<div class="mb-3 flex gap-6 text-sm">
<div><span class="text-2xl font-semibold"><%= stats.average || "—" %></span> <span class="text-slate-500"><%= stats.metric_label %> avg (last 20)</span></div>
<div><span class="text-lg"><%= stats.latest || "—" %></span> <span class="text-slate-500">latest</span></div>
<div class="mb-3 flex flex-wrap gap-x-6 gap-y-1 text-sm">
<div><span class="text-2xl font-semibold"><%= stats.recent || "—" %></span> <span class="text-slate-500"><%= stats.metric_label %>, last ~<%= stats.recent_window_hours %> hrs</span></div>
<div><span class="text-lg"><%= stats.lifetime || "—" %></span> <span class="text-slate-500">lifetime</span></div>
</div>
<% if stats.points.size >= 2 %>
<%= line_chart stats.chart_data, xtitle: "Tach hours", ytitle: stats.metric_label, height: "220px",
<%= line_chart stats.chart_data, xtitle: "Tach hours", ytitle: stats.metric_label, height: "220px", curve: false, points: false,
library: { scales: { x: { type: "linear" } } }, colors: [stats.oil? ? "#b45309" : "#0369a1"] %>
<p class="hint">Rolling <%= stats.chart_window_hours %>-hour window: total <%= stats.oil? ? "oil" : "fuel" %> added ÷ hours flown, so partial fills average out.<%= " Higher is better." if stats.oil? %></p>
<% else %>
<%= empty_state "Add at least three records to see a trend." %>
<%= empty_state "Not enough history yet (need more than #{stats.chart_window_hours} tach hours of records)." %>
<% end %>
</div>
</section>
+40
View File
@@ -0,0 +1,40 @@
<% content_for :title, "Import Flights" %>
<%= render "shared/aircraft_header", aircraft: @aircraft %>
<div class="grid gap-5 lg:grid-cols-3">
<%= form_with url: aircraft_flight_import_path(@aircraft), multipart: true, class: "card card-body space-y-4 lg:col-span-2" do |f| %>
<h2 class="card-title">Import flights from a spreadsheet</h2>
<div>
<%= f.label :file, "Spreadsheet (.xlsx or .csv)", class: "label" %>
<%= f.file_field :file, accept: ".xlsx,.csv", required: true,
class: "block text-sm text-slate-600 file:mr-3 file:rounded-md file:border-0 file:bg-slate-100 file:px-3 file:py-1.5 file:text-sm file:font-medium hover:file:bg-slate-200" %>
</div>
<div class="grid gap-4 sm:grid-cols-2">
<div>
<%= f.label :starting_tach, "Tach before the first flight (optional)", class: "label" %>
<%= f.number_field :starting_tach, step: 0.1, class: "input" %>
<p class="hint">E.g. the tach at purchase. Without it, the first row's time comes from a Flight Time column, if you have one.</p>
</div>
<div>
<%= f.label :starting_hobbs, "Hobbs before the first flight (optional)", class: "label" %>
<%= f.number_field :starting_hobbs, step: 0.1, class: "input" %>
</div>
</div>
<%= f.submit "Upload & Preview", class: "btn-primary" %>
<p class="hint">Nothing is saved until you confirm on the next page.</p>
<% end %>
<section class="card card-body text-sm text-slate-700 space-y-2">
<h3 class="font-semibold">Expected layout</h3>
<p>One row per flight, with the meter readings <em>at the end</em> of the flight. Columns are matched by header name, in any order:</p>
<ul class="list-disc pl-5 text-xs">
<li><strong>Date</strong> (required)</li>
<li><strong>Tach</strong> / Tach Time (required)</li>
<li>From, To</li>
<li>Hobbs / Hobbs Time</li>
<li>Oil Added (qt), Fuel Added (gal); “N” or blank = none</li>
<li>Flight Time (used only for the first row)</li>
</ul>
<p class="text-xs">Each flight's time is the difference from the row above. Rows are kept in file order.</p>
<p class="text-xs">Importing records history only. It doesn't add time to components.</p>
</section>
</div>
+84
View File
@@ -0,0 +1,84 @@
<% content_for :title, "Import Preview" %>
<%= render "shared/aircraft_header", aircraft: @aircraft %>
<% rows = @import.rows %>
<% problems = rows.select { _1.status.in?(%i[error warning]) } %>
<% last = @import.last_reading %>
<section class="card mb-5">
<div class="card-header">
<h2 class="card-title">Preview: <%= @filename %></h2>
<span class="text-xs text-slate-500">Columns found: <%= @import.columns.keys.map { _1.to_s.humanize }.join(", ") %></span>
</div>
<div class="card-body grid grid-cols-2 gap-4 text-center sm:grid-cols-5">
<div><div class="text-2xl font-semibold"><%= @import.importable.size %></div><div class="text-xs text-slate-500">flights to import</div></div>
<div><div class="text-2xl font-semibold"><%= hours(@import.total_tach_hours) %></div><div class="text-xs text-slate-500">tach hours</div></div>
<div><div class="text-2xl font-semibold text-amber-700"><%= @import.count(:warning) %></div><div class="text-xs text-slate-500">warnings (imported)</div></div>
<div><div class="text-2xl font-semibold text-red-700"><%= @import.count(:error) %></div><div class="text-xs text-slate-500">errors (skipped)</div></div>
<div><div class="text-2xl font-semibold text-slate-500"><%= @import.count(:duplicate) %></div><div class="text-xs text-slate-500">already imported</div></div>
</div>
<% if @import.importable.any? %>
<%= form_with url: confirm_aircraft_flight_import_path(@aircraft), class: "space-y-3 border-t border-slate-100 p-4",
data: { turbo_confirm: "Import #{@import.importable.size} flights?" } do |f| %>
<%= hidden_field_tag :token, @token %>
<%= hidden_field_tag :filename, @filename %>
<%= hidden_field_tag :starting_tach, @starting_tach %>
<%= hidden_field_tag :starting_hobbs, @starting_hobbs %>
<% if last && last.tach_in > @aircraft.tach_time %>
<label class="flex items-start gap-2 text-sm">
<%= check_box_tag :update_aircraft_hours, "1", true, class: "mt-0.5 h-4 w-4" %>
<span>Also update <%= @aircraft.tail_number %>'s tach from <%= hours(@aircraft.tach_time) %> to <strong><%= hours(last.tach_in) %></strong>
<%= "and hobbs to #{hours(last.hobbs_in)}" if last.hobbs_in && last.hobbs_in >= @aircraft.hobbs_time %>.
<span class="block text-xs text-slate-500">Works like a normal hours update: the difference (<%= hours(last.tach_in - @aircraft.tach_time) %> hrs) is added to in-service components. Leave unchecked if component times are already current.</span></span>
</label>
<% elsif last %>
<p class="text-xs text-slate-500">The aircraft's tach (<%= hours(@aircraft.tach_time) %>) is already at or past the last reading (<%= hours(last.tach_in) %>), so its hours won't change.</p>
<% end %>
<div class="flex gap-2">
<%= f.submit "Import #{@import.importable.size} Flights", class: "btn-primary" %>
<%= link_to "Upload a different file", new_aircraft_flight_import_path(@aircraft), class: "btn-secondary" %>
</div>
<% if @import.count(:error) > 0 %><p class="hint">Rows with errors are skipped. The next good row absorbs their time, so total hours stay correct. To include them, fix the spreadsheet and upload it again. Rows already imported are skipped automatically.</p><% end %>
<% end %>
<% else %>
<p class="border-t border-slate-100 p-4 text-sm">Nothing to import. <%= link_to "Upload a different file", new_aircraft_flight_import_path(@aircraft), class: "link" %></p>
<% end %>
</section>
<% if problems.any? %>
<section class="card mb-5">
<div class="card-header"><h2 class="card-title">Rows needing attention</h2></div>
<table class="table">
<thead><tr><th>Row</th><th>Date</th><th>Route</th><th class="text-right">Tach</th><th>Issue</th></tr></thead>
<tbody>
<% problems.each do |r| %>
<tr>
<td><%= r.line %></td><td class="whitespace-nowrap"><%= r.date || "—" %></td>
<td><%= [r.from, r.to].compact.join(" → ") %></td><td class="text-right"><%= r.tach_in %></td>
<td><%= tag.span(r.status == :error ? "Skipped" : "Warning", class: r.status == :error ? "badge-red" : "badge-orange") %> <span class="text-xs"><%= r.messages.join("; ") %></span></td>
</tr>
<% end %>
</tbody>
</table>
</section>
<% end %>
<details class="card">
<summary class="card-header cursor-pointer"><span class="card-title">All <%= rows.size %> rows</span></summary>
<div class="overflow-x-auto">
<table class="table">
<thead><tr><th>Row</th><th>Date</th><th>From</th><th>To</th><th class="text-right">Tach</th><th class="text-right">Tach hrs</th><th class="text-right">Hobbs</th><th class="text-right">Hobbs hrs</th><th class="text-right">Oil</th><th class="text-right">Fuel</th><th></th></tr></thead>
<tbody>
<% rows.each do |r| %>
<tr class="<%= 'opacity-50' unless r.importable? %>">
<td><%= r.line %></td><td class="whitespace-nowrap"><%= r.date %></td><td><%= r.from %></td><td><%= r.to %></td>
<td class="text-right tabular-nums"><%= r.tach_in %></td><td class="text-right tabular-nums"><%= hours(r.tach_time) if r.tach_time %></td>
<td class="text-right tabular-nums"><%= r.hobbs_in %></td><td class="text-right tabular-nums"><%= hours(r.hobbs_time) if r.hobbs_time %></td>
<td class="text-right"><%= r.oil %></td><td class="text-right"><%= r.fuel %></td>
<td><%= { ok: "", warning: tag.span("warning", class: "badge-orange"), error: tag.span("skipped", class: "badge-red"), duplicate: tag.span("duplicate", class: "badge-gray") }[r.status] %></td>
</tr>
<% end %>
</tbody>
</table>
</div>
</details>
+1 -1
View File
@@ -1,7 +1,7 @@
<% content_for :title, "#{@aircraft.tail_number} Flights" %>
<%= render "shared/aircraft_header", aircraft: @aircraft %>
<section class="card">
<div class="card-header"><h2 class="card-title">Flight Log</h2><%= link_to "+ Log Flight", new_aircraft_flight_log_path(@aircraft), class: "btn-primary" %></div>
<div class="card-header"><h2 class="card-title">Flight Log</h2><div class="flex gap-2"><% if owner? %><%= link_to "Import from spreadsheet", new_aircraft_flight_import_path(@aircraft), class: "btn-secondary" %><% end %><%= link_to "+ Log Flight", new_aircraft_flight_log_path(@aircraft), class: "btn-primary" %></div></div>
<% if @flight_logs.any? %>
<div class="overflow-x-auto">
<table class="table">
+2 -2
View File
@@ -33,8 +33,8 @@
<section class="card">
<div class="card-header"><h2 class="card-title">Consumption</h2></div>
<div class="card-body grid grid-cols-2 gap-4 text-center">
<div><div class="text-2xl font-semibold"><%= @oil.average || "—" %></div><div class="text-xs text-slate-500">Oil hrs/qt</div></div>
<div><div class="text-2xl font-semibold"><%= @fuel.average || "—" %></div><div class="text-xs text-slate-500">Fuel gal/hr</div></div>
<div><div class="text-2xl font-semibold"><%= @oil.recent || "—" %></div><div class="text-xs text-slate-500">Oil hrs/qt (recent)</div></div>
<div><div class="text-2xl font-semibold"><%= @fuel.recent || "—" %></div><div class="text-xs text-slate-500">Fuel gal/hr (recent)</div></div>
</div>
</section>
</div>
+10
View File
@@ -0,0 +1,10 @@
{
"ignored_warnings": [
{
"warning_type": "File Access",
"fingerprint": "c1d23cfd1f641b61be5cc6d747c8658806ed9e5adccb66eaca8f2aac5ef449ee",
"file": "app/controllers/flight_imports_controller.rb",
"note": "Token is validated against TOKEN_FORMAT (server-generated uuid.ext) before use, and File.basename is applied; path is confined to tmp/imports."
}
]
}
+3
View File
@@ -20,6 +20,9 @@ Rails.application.routes.draw do
end
resources :notes, except: :show
resources :flight_logs, only: %i[index new create show destroy]
resource :flight_import, only: %i[new create show] do
post :confirm
end
resources :consumable_records, path: "consumables", except: :show
resources :logbook_entries
resources :documents
+15
View File
@@ -0,0 +1,15 @@
Date,From,To,Tach Time,Hobbs Time,Oil Added,Fuel Added,Flight Time
10/17/26,KAAA,KBBB,7277.8,141.3,N,14.3,1.6
10/17/26,KBBB,KCCC,7278.4,142.1,N,N,0.8
10/18/26,KCCC,KDDD,7278.9,143.0,N,12.1,0.9
10/18/26,KDDD,KCCC,7279.5,143.8,N,N,0.8
10/20/26,KCCC,KEEE,7280.6,145.0,N,N,1.2
10/20/26,KEEE,KCCC,7281.8,146.4,N,12.9,1.4
10/21/26,KCCC,KFFF,7282.4,147.2,N,7.3,0.8
10/21/26,KFFF,KCCC,7283.2,148.1,N,N,0.9
10/23/26,KCCC,KGGG,7283.0,149.0,1,8.7,0.9
10/23/26,KGGG,KCCC,7284.5,149.8,N,N,0.8
10/24/26,KCCC,KHHH,7287.2,152.8,N,8.1,3.0
10/24/26,KHHH,KJJJ,7289.1,154.8,N,15.45,2.0
10/24/26,KJJJ,KKKK,7290.3,156.1,N,N,1.3
,,,,,,,
1 Date From To Tach Time Hobbs Time Oil Added Fuel Added Flight Time
2 10/17/26 KAAA KBBB 7277.8 141.3 N 14.3 1.6
3 10/17/26 KBBB KCCC 7278.4 142.1 N N 0.8
4 10/18/26 KCCC KDDD 7278.9 143.0 N 12.1 0.9
5 10/18/26 KDDD KCCC 7279.5 143.8 N N 0.8
6 10/20/26 KCCC KEEE 7280.6 145.0 N N 1.2
7 10/20/26 KEEE KCCC 7281.8 146.4 N 12.9 1.4
8 10/21/26 KCCC KFFF 7282.4 147.2 N 7.3 0.8
9 10/21/26 KFFF KCCC 7283.2 148.1 N N 0.9
10 10/23/26 KCCC KGGG 7283.0 149.0 1 8.7 0.9
11 10/23/26 KGGG KCCC 7284.5 149.8 N N 0.8
12 10/24/26 KCCC KHHH 7287.2 152.8 N 8.1 3.0
13 10/24/26 KHHH KJJJ 7289.1 154.8 N 15.45 2.0
14 10/24/26 KJJJ KKKK 7290.3 156.1 N N 1.3
15
@@ -0,0 +1,57 @@
require "test_helper"
class FlightImportFlowTest < ActionDispatch::IntegrationTest
setup do
@owner = create_user
@pilot = create_user
@aircraft = create_aircraft(owner: @owner, pilot: @pilot, tach_time: 7270)
end
test "owner uploads, previews, and confirms" do
sign_in @owner
get new_aircraft_flight_import_path(@aircraft)
assert_response :success
post aircraft_flight_import_path(@aircraft), params: { file: fixture_file_upload("flights.csv", "text/csv") }
# Turbo ignores a 200 render after a form POST, so upload must redirect to the preview
assert_response :redirect
assert_match %r{/flight_import\?}, response.location
follow_redirect!
assert_response :success
assert_includes response.body, "Tach went backwards"
assert_includes response.body, "Import 12 Flights"
token = response.body[/name="token"[^>]*value="([^"]+)"/, 1]
assert token
post confirm_aircraft_flight_import_path(@aircraft), params: { token: token, filename: "flights.csv", update_aircraft_hours: "1" }
assert_redirected_to aircraft_flight_logs_path(@aircraft)
assert_equal 12, @aircraft.flight_logs.count
assert_equal BigDecimal("7290.3"), @aircraft.reload.tach_time
# token is single-use
post confirm_aircraft_flight_import_path(@aircraft), params: { token: token, filename: "flights.csv" }
assert_redirected_to new_aircraft_flight_import_path(@aircraft)
assert_equal 12, @aircraft.flight_logs.count
end
test "rejects path traversal tokens and wrong file types" do
sign_in @owner
post confirm_aircraft_flight_import_path(@aircraft), params: { token: "../../config/master.key", filename: "x.csv" }
assert_redirected_to new_aircraft_flight_import_path(@aircraft)
post aircraft_flight_import_path(@aircraft), params: { file: fixture_file_upload("flights.csv", "text/plain") .tap { |f| def f.original_filename = "evil.rb" } }
assert_redirected_to new_aircraft_flight_import_path(@aircraft)
end
test "preview with a bogus token sends you back to upload" do
sign_in @owner
get aircraft_flight_import_path(@aircraft, token: "nope.csv")
assert_redirected_to new_aircraft_flight_import_path(@aircraft)
end
test "pilots cannot import" do
sign_in @pilot
post aircraft_flight_import_path(@aircraft), params: { file: fixture_file_upload("flights.csv", "text/csv") }
assert_response :redirect
assert_equal 0, @aircraft.flight_logs.count
end
end
+41 -24
View File
@@ -1,33 +1,50 @@
require "test_helper"
class ConsumptionStatsTest < ActiveSupport::TestCase
test "oil hrs/qt average excludes IQR outliers and excluded records" do
a = create_aircraft
a.consumable_records.create!(record_type: "oil", date: Date.current, quantity_added: 1, flight_hours: 100)
hours = 100.0
# interval values (hrs/qt): 10, 11, 9, 10, 1 (outlier), 12, 10 (manually excluded)
[ [ 10, false ], [ 11, false ], [ 9, false ], [ 10, false ], [ 1, false ], [ 12, false ], [ 10, true ] ].each do |dh, excl|
hours += dh
a.consumable_records.create!(record_type: "oil", date: Date.current, quantity_added: 1, flight_hours: hours,
excluded_from_averages: excl)
end
stats = ConsumptionStats.new(a, "oil")
assert_equal 7, stats.points.size
assert_equal 10.4, stats.average # (10+11+9+10+12)/5
setup { @a = create_aircraft }
def add(type, hours, qty, excluded: false)
@a.consumable_records.create!(record_type: type, date: Date.current, flight_hours: hours,
quantity_added: qty, excluded_from_averages: excluded)
end
test "zero spread disables outlier filtering (matches original)" do
a = create_aircraft
[ 100, 110, 120, 130, 131, 141 ].each do |h|
a.consumable_records.create!(record_type: "oil", date: Date.current, quantity_added: 1, flight_hours: h)
end
assert_equal 8.2, ConsumptionStats.new(a, "oil").average
test "partial fills and top-offs don't spike fuel burn" do
add "fuel", 100, 20 # baseline fill
add "fuel", 105, 20
add "fuel", 105.2, 10 # top-off 0.2 h later: per-fill math would say 50 gal/hr
add "fuel", 110, 30
stats = ConsumptionStats.new(@a, "fuel")
assert_equal [ [ 110.0, 6.0 ] ], stats.points # 60 gal over 10 h
assert_equal 6.0, stats.recent
assert_equal 6.0, stats.lifetime
end
test "fuel gal/hr" do
a = create_aircraft
a.consumable_records.create!(record_type: "fuel", date: Date.current, quantity_added: 40, flight_hours: 100)
a.consumable_records.create!(record_type: "fuel", date: Date.current, quantity_added: 17, flight_hours: 102)
assert_equal 8.5, ConsumptionStats.new(a, "fuel").average
test "oil hrs/qt rolling window, oil changes excluded" do
add "oil", 0, 6
add "oil", 10, 1
add "oil", 20, 6, excluded: true # oil change
add "oil", 25, 1
add "oil", 30, 1
add "oil", 40, 1
stats = ConsumptionStats.new(@a, "oil")
assert_equal 10.0, stats.lifetime # 40 h / 4 qt counted
assert_equal [ 25.0, 12.5 ], stats.points.first # first 25 h window: 25 h / 2 qt (oil change not counted)
assert_equal 10.0, stats.latest # window 10→40 h: 30 h / 3 qt
end
test "recent window looks back about 50 fuel hours" do
add "fuel", 0, 10
add "fuel", 100, 1000 # old era: 10 gal/hr
add "fuel", 150, 300 # recent: 6 gal/hr
stats = ConsumptionStats.new(@a, "fuel")
assert_equal 6.0, stats.recent
assert_equal 8.7, stats.lifetime # 1300 / 150
end
test "too little history" do
add "oil", 10, 1
stats = ConsumptionStats.new(@a, "oil")
assert_nil stats.recent
assert_empty stats.points
end
end
+139
View File
@@ -0,0 +1,139 @@
require "test_helper"
require "caxlsx"
class FlightImportTest < ActiveSupport::TestCase
CSV_PATH = Rails.root.join("test/fixtures/files/flights.csv")
setup do
@user = create_user
@aircraft = create_aircraft(owner: @user, tach_time: 7270, hobbs_time: 100)
@engine = create_component(@aircraft, hours_in_service: 500, hours_since_overhaul: 500)
end
def import(path = CSV_PATH, name = "flights.csv", **opts)
FlightImport.new(@aircraft, path: path, filename: name, **opts)
end
test "derives durations from consecutive readings and flags the backwards tach" do
imp = import
rows = imp.rows
assert_equal 13, rows.size
assert_equal %i[date from to tach hobbs oil fuel flight_time], imp.columns.keys
first = rows.first
assert_equal :warning, first.status # no starting tach → Flight Time column
assert_equal BigDecimal("1.6"), first.tach_time
assert_equal BigDecimal("7276.2"), first.tach_out
assert_equal BigDecimal("14.3"), first.fuel
assert_nil first.oil
assert_equal BigDecimal("0.6"), rows[1].tach_time
assert_equal BigDecimal("0.8"), rows[1].hobbs_time
bad = rows[8] # 10/23, tach 7283.0 after 7283.2
assert_equal :error, bad.status
assert_match "backwards", bad.messages.first
# the next good row absorbs the time from the last good reading
assert_equal BigDecimal("1.3"), rows[9].tach_time
assert_equal BigDecimal("7283.2"), rows[9].tach_out
assert_equal BigDecimal("1.7"), rows[9].hobbs_time
assert_equal BigDecimal("15.45"), rows[11].fuel
assert_equal 12, imp.importable.size
assert_equal BigDecimal("14.1"), imp.total_tach_hours # 7290.3 - 7276.2
end
test "starting tach makes the first row exact" do
rows = import(starting_tach: "7276.5", starting_hobbs: "140.0").rows
assert_equal :ok, rows.first.status
assert_equal BigDecimal("1.3"), rows.first.tach_time
assert_equal BigDecimal("1.3"), rows.first.hobbs_time
end
test "import creates flights and consumables but leaves component hours alone" do
count = import.import!(user: @user)
assert_equal 12, count
assert_equal 12, @aircraft.flight_logs.count
assert_equal 0, @aircraft.consumable_records.oil.count # the only oil row was the bad one
assert_equal 6, @aircraft.consumable_records.fuel.count # 8.7 gal was on the skipped row
fuel = @aircraft.consumable_records.fuel.find_by(quantity_added: 15.45)
assert_equal BigDecimal("7289.1"), fuel.flight_hours
assert_equal 7270, @aircraft.reload.tach_time
assert_equal 500, @engine.reload.hours_in_service
assert_equal 1, @aircraft.events.where(category: "flight").count
end
test "re-importing skips duplicates" do
import.import!(user: @user)
again = import
assert_equal 12, again.count(:duplicate)
assert_equal 1, again.count(:error)
assert_raises(FlightImport::Error) { again.import!(user: @user) }
end
test "optionally advances aircraft meters to the last reading" do
import.import!(user: @user, update_aircraft_hours: true)
@aircraft.reload
assert_equal BigDecimal("7290.3"), @aircraft.tach_time
assert_equal BigDecimal("156.1"), @aircraft.hobbs_time
assert_equal BigDecimal("520.3"), @engine.reload.hours_in_service # +20.3 like a normal hours update
end
test "never moves meters backwards" do
@aircraft.update!(tach_time: 7300)
import.import!(user: @user, update_aircraft_hours: true)
assert_equal 7300, @aircraft.reload.tach_time
end
test "reads xlsx with real dates and numbers, header not on first row" do
path = Rails.root.join("tmp/test-flights-#{SecureRandom.hex(4)}.xlsx")
pkg = Axlsx::Package.new
pkg.workbook.add_worksheet(name: "Log") do |ws|
ws.add_row [ "My airplane" ]
ws.add_row [ "Date", "From", "To", "Tach Time", "Hobbs Time", "Oil Added", "Fuel Added", "Flight Time" ]
ws.add_row [ Date.new(2026, 10, 17), "KAAA", "KBBB", 7277.8, 141.3, "N", 14.3, 1.6 ]
ws.add_row [ Date.new(2026, 10, 17), "KBBB", "KCCC", 7278.4, 142.1, 1, "N", 0.8 ]
end
pkg.serialize(path.to_s)
rows = import(path, "log.xlsx").rows
assert_equal 2, rows.size
assert_equal Date.new(2026, 10, 17), rows.first.date
assert_equal BigDecimal("0.6"), rows.last.tach_time
assert_equal BigDecimal("1"), rows.last.oil
ensure
FileUtils.rm_f(path)
end
test "without a Flight Time column the first row is a baseline, not a cascade of errors" do
path = Rails.root.join("tmp/nofltime-#{SecureRandom.hex(4)}.csv")
File.write(path, <<~CSV)
Date,From,To,Tach Time,Hobbs Time,Oil Added,Fuel Added
11/8/21,53NH,KPVD,6704.7,11.3,1,N
11/11/21,KPVD,KEWB,6705.2,13.5,N,Full
11/11/21,KEWB,KPVD,6705.9,14.4,N,N
11/13/21,KPVD,KPVD,6705.9,15.8, N,10
CSV
rows = import(path, "f.csv").rows
assert_equal %i[error warning ok warning], rows.map(&:status)
assert_equal BigDecimal("0.5"), rows[1].tach_time
assert_nil rows[1].fuel # "Full" has no quantity
assert_equal BigDecimal("0"), rows[3].tach_time # unchanged tach → 0.0 hr entry, fuel kept
assert_equal BigDecimal("10"), rows[3].fuel
assert_nil rows[3].oil # " N" with leading space
imp = import(path, "f.csv")
assert_equal 3, imp.import!(user: @user)
assert_equal 1, @aircraft.consumable_records.fuel.count
ensure
FileUtils.rm_f(path)
end
test "rejects files without the required columns" do
path = Rails.root.join("tmp/bad-#{SecureRandom.hex(4)}.csv")
File.write(path, "Name,Value\na,1\n")
assert_raises(FlightImport::Error) { import(path, "bad.csv").rows }
ensure
FileUtils.rm_f(path)
end
end