mirror of
https://github.com/rickbarrette/simple-aircraft-manager-rails.git
synced 2026-11-17 23:50:36 -05:00
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:
1 parent
38df19c331
commit
4d3dc7150a
21 files changed
+820
-75
No files matched your search
@@ -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
|
||||
Reference in new issue
Block a user