mirror of
https://github.com/rickbarrette/simple-aircraft-manager-rails.git
synced 2026-11-17 15:50:36 -05:00
Rails 8.1 + SQLite rewrite of marbindrakon/simple-aircraft-manager, designed to self-host as a single container (Solid Queue/Cache/Cable, Active Storage on disk, Hotwire + importmap, Tailwind). - Airworthiness engine ported from health/services.py (ADs, grounding squawks, inspection recurrency, component replacement) - Hours updates and flight logs cascading to in-service components, optional airworthiness enforcement - Components, squawks, ADs, inspections, logbook, documents, major records, oil/fuel consumption stats and charts, activity log - Admin/owner/pilot RBAC and public share links (status/maintenance) - Docker Compose and Kamal deployment, env-driven production config - Service and integration tests Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
59 lines
1.8 KiB
Ruby
59 lines
1.8 KiB
Ruby
# 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.
|
|
class ConsumptionStats
|
|
WINDOW = 20
|
|
Point = Struct.new(:record, :hours, :value, :excluded, :outlier, keyword_init: true)
|
|
|
|
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
|
|
end
|
|
|
|
def oil? = record_type == "oil"
|
|
def metric_label = oil? ? "hrs/qt" : "gal/hr"
|
|
|
|
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) }
|
|
end
|
|
end
|
|
|
|
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)
|
|
end
|
|
|
|
def latest = points.last&.value&.round(1)
|
|
|
|
def chart_data
|
|
points.map { |p| [ p.hours.round(1), p.value.round(2) ] }
|
|
end
|
|
|
|
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 ]
|
|
end
|
|
end
|