Initial commit: Rails port of Simple Aircraft Manager

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>
This commit is contained in:
rickyandClaude Opus 5.5 committed 2026-11-04 17:59:08 -05:00
commit 38df19c331
264 files changed
+8365

No files matched your search

+11
View File
@@ -0,0 +1,11 @@
# Raised when a flight-activity write is attempted on a RED aircraft whose
# airworthiness enforcement is enabled.
class AircraftGroundedError < StandardError
attr_reader :airworthiness
def initialize(airworthiness)
@airworthiness = airworthiness
reasons = airworthiness.red_issues.map(&:title).join("; ")
super([ "Aircraft is grounded and airworthiness enforcement is enabled", reasons.presence ].compact.join(": "))
end
end
+120
View File
@@ -0,0 +1,120 @@
# Computes RED / ORANGE / GREEN airworthiness for an aircraft. Checks (in order):
# AD compliance, grounding squawks, inspection recurrency, component replacement.
class Airworthiness
HOURS_WARNING = BigDecimal("10")
DAYS_WARNING = 30
RED = "RED"
ORANGE = "ORANGE"
GREEN = "GREEN"
Issue = Struct.new(:category, :severity, :title, :description, :item, keyword_init: true)
Status = Struct.new(:status, :issues, keyword_init: true) do
def red? = status == RED
def orange? = status == ORANGE
def green? = status == GREEN
def can_fly? = green?
def red_issues = issues.select { |i| i.severity == RED }
def label = { RED => "Not Airworthy", ORANGE => "Attention Needed", GREEN => "Airworthy" }[status]
end
def initialize(aircraft, today: Date.current)
@aircraft = aircraft
@today = today
@hours = aircraft.current_hours
@issues = []
end
def call
check_ads
check_grounding_squawks
check_inspections
check_components
status =
if @issues.any? { _1.severity == RED } then RED
elsif @issues.any? { _1.severity == ORANGE } then ORANGE
else GREEN
end
Status.new(status: status, issues: @issues)
end
private
def add(category, severity, title, description, item)
@issues << Issue.new(category:, severity:, title:, description:, item:)
end
def check_ads
@aircraft.applicable_ads.where(mandatory: true, compliance_type: "standard").find_each do |ad|
compliance = ad.latest_compliance_for(@aircraft)
res = Compliance.ad_status(ad, compliance, @hours, @today)
label = ad.bulletin_label
case res.rank
when Compliance::OVERDUE
desc = compliance ? "#{ad.short_description}. Compliance overdue." : "#{ad.short_description}. No compliance record found."
add("AD", RED, "#{label} #{ad.name} - Overdue", desc, ad)
when Compliance::DUE_SOON
add("AD", ORANGE, "#{label} #{ad.name} - Due Soon", "#{ad.short_description}. Compliance due soon.", ad)
end
end
end
def check_grounding_squawks
@aircraft.squawks.active.where(priority: 0).find_each do |squawk|
add("SQUAWK", RED, "Grounding Squawk", squawk.issue_reported.to_s.truncate(100).presence || "No description", squawk)
end
end
def check_inspections
@aircraft.applicable_inspection_types.where(required: true).find_each do |type|
last = type.inspection_records.for_aircraft(@aircraft).recent_first.first
res = Compliance.inspection_status(type, last, @hours, @today)
case res.rank
when Compliance::OVERDUE
if last
add("INSPECTION", RED, "#{type.name} - Overdue", "Inspection overdue.", type)
else
add("INSPECTION", RED, "#{type.name} - Never Completed", "Required inspection has no completion record.", type)
end
when Compliance::DUE_SOON
add("INSPECTION", ORANGE, "#{type.name} - Due Soon", "Inspection due soon.", type)
end
end
end
def check_components
@aircraft.components.in_use.where(replacement_critical: true).includes(:component_type).find_each do |c|
overdue = false
approaching = false
desc = ""
if c.replacement_hours
if c.hours_since_overhaul >= c.replacement_hours
overdue = true
desc = "Replace every #{c.replacement_hours} hrs. Current: #{c.hours_since_overhaul} hrs."
elsif c.hours_since_overhaul + HOURS_WARNING >= c.replacement_hours
approaching = true
desc = "Replace in #{c.replacement_hours - c.hours_since_overhaul} hrs."
end
end
if (due = c.next_replacement_date)
if @today >= due
overdue = true
desc = "Replace every #{c.replacement_days} days. Last serviced #{c.overhaul_date}."
elsif @today + DAYS_WARNING >= due
approaching = true
desc = "Replace in #{(due - @today).to_i} days."
end
end
if overdue
add("COMPONENT", RED, "#{c} - Replacement Overdue", desc, c)
elsif approaching
add("COMPONENT", ORANGE, "#{c} - Replacement Due Soon", desc, c)
end
end
end
end
+119
View File
@@ -0,0 +1,119 @@
# Shared AD / inspection due-status math (port of ad_compliance_status /
# inspection_compliance_status from the original health/services.py).
module Compliance
COMPLIANT = 0
DUE_SOON = 1
OVERDUE = 2
LABELS = %w[compliant due_soon overdue].freeze
Result = Struct.new(:rank, :next_due_date, :next_due_hours, keyword_init: true) do
def label = LABELS[rank]
end
module_function
# Last day of the month `months` after `date` (FAA "end of month" convention).
def end_of_month_after(date, months)
(date >> months).end_of_month
end
def ad_status(ad, compliance, current_hours, today = Date.current)
return Result.new(rank: COMPLIANT) if ad.conditional?
return Result.new(rank: OVERDUE) unless compliance
return Result.new(rank: COMPLIANT) if compliance.permanent?
rank = COMPLIANT
result = Result.new
due_hours = compliance.next_due_at_time.to_d
if due_hours > 0
result.next_due_hours = due_hours
if current_hours >= due_hours
rank = OVERDUE
elsif current_hours + Airworthiness::HOURS_WARNING >= due_hours
rank = [ rank, DUE_SOON ].max
end
end
if ad.recurring? && ad.recurring_months.positive?
due = end_of_month_after(compliance.date_complied, ad.recurring_months)
result.next_due_date = due
if today > due
rank = OVERDUE
elsif today + Airworthiness::DAYS_WARNING >= due
rank = [ rank, DUE_SOON ].max
end
end
result.rank = rank
result
end
def inspection_status(type, last_record, current_hours, today = Date.current)
return Result.new(rank: OVERDUE) unless last_record
return Result.new(rank: COMPLIANT) unless type.recurring?
rank = COMPLIANT
result = Result.new
if type.recurring_months.positive? || type.recurring_days.positive?
due = last_record.date
due = end_of_month_after(due, type.recurring_months) if type.recurring_months.positive?
due += type.recurring_days.days if type.recurring_days.positive?
result.next_due_date = due
if today > due
rank = OVERDUE
elsif today + Airworthiness::DAYS_WARNING >= due
rank = [ rank, DUE_SOON ].max
end
end
interval = type.recurring_hours.to_d
if interval > 0 && (hours_at = last_record.hours_at_inspection)
since = current_hours - hours_at
result.next_due_hours = hours_at + interval
if since >= interval
rank = OVERDUE
elsif since + Airworthiness::HOURS_WARNING >= interval
rank = [ rank, DUE_SOON ].max
end
end
result.rank = rank
result
end
# Rows for the ADs tab: every applicable AD with its latest compliance and status.
AdRow = Struct.new(:ad, :latest, :status, :next_due_date, :next_due_hours, keyword_init: true)
def ad_rows(aircraft)
hours = aircraft.current_hours
aircraft.applicable_ads.ordered.map do |ad|
latest = ad.latest_compliance_for(aircraft)
status =
if ad.conditional? then latest ? "compliant" : "conditional"
elsif latest.nil? then "no_compliance"
elsif latest.permanent? then "compliant"
end
res = ad_status(ad, latest, hours) unless status
AdRow.new(ad: ad, latest: latest, status: status || res.label,
next_due_date: res&.next_due_date, next_due_hours: res&.next_due_hours)
end
end
InspectionRow = Struct.new(:type, :latest, :status, :next_due_date, :next_due_hours, keyword_init: true)
def inspection_rows(aircraft)
hours = aircraft.current_hours
aircraft.applicable_inspection_types.ordered.map do |type|
latest = type.inspection_records.for_aircraft(aircraft).recent_first.first
if latest.nil?
InspectionRow.new(type: type, latest: nil, status: "never_completed")
else
res = inspection_status(type, latest, hours)
InspectionRow.new(type: type, latest: latest, status: res.label,
next_due_date: res.next_due_date, next_due_hours: res.next_due_hours)
end
end
end
end
+58
View File
@@ -0,0 +1,58 @@
# 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
+75
View File
@@ -0,0 +1,75 @@
# Flight-activity writes: absolute hours updates and flight logs. Both cascade
# the tach delta to every IN-USE component.
class HoursService
def initialize(aircraft, user: Current.user)
@aircraft = aircraft
@user = user
end
def enforce_airworthiness!
return unless @aircraft.airworthiness_enforcement?
status = Airworthiness.new(@aircraft).call
raise AircraftGroundedError, status if status.red?
end
# Set absolute tach (and optional hobbs); component hours are clamped at 0
# so downward corrections can't go negative.
def update_hours!(tach_time:, hobbs_time: nil)
enforce_airworthiness!
tach = BigDecimal(tach_time.to_s)
delta = tach - @aircraft.tach_time
old = @aircraft.tach_time
Aircraft.transaction do
@aircraft.tach_time = tach
@aircraft.hobbs_time = BigDecimal(hobbs_time.to_s) if hobbs_time.present?
@aircraft.save!
cascade_to_components(delta, clamp: true)
@aircraft.log_event!("hours",
"Hours #{delta >= 0 ? 'updated' : 'corrected'} to #{tach}",
user: @user, notes: "Previous: #{old}, delta: #{delta >= 0 ? '+' : ''}#{delta}")
end
delta
end
# Persist a flight log and apply its side effects atomically.
def log_flight!(flight)
enforce_airworthiness!
Aircraft.transaction do
flight.aircraft = @aircraft
flight.user ||= @user
flight.save!
@aircraft.tach_time += flight.tach_time
@aircraft.hobbs_time += flight.hobbs_time if flight.hobbs_time
@aircraft.save!
cascade_to_components(flight.tach_time)
{ "oil" => [ flight.oil_added, flight.oil_level_after, flight.oil_added_type ],
"fuel" => [ flight.fuel_added, flight.fuel_level_after, flight.fuel_added_type ] }.each do |type, (qty, level, kind)|
next unless qty.to_f > 0
@aircraft.consumable_records.create!(
record_type: type, flight_log: flight, date: flight.date, quantity_added: qty,
level_after: level, consumable_type: kind, flight_hours: @aircraft.tach_time,
notes: "Auto-created from flight log")
@aircraft.log_event!(type, "#{type.capitalize} added: #{qty} #{ConsumableRecord::UNITS[type]}",
user: @user, notes: "From flight log")
end
route = flight.route_summary.presence
@aircraft.log_event!("flight", "Flight logged: #{flight.tach_time} hrs#{" (#{route})" if route}", user: @user)
end
flight
end
private
def cascade_to_components(delta, clamp: false)
@aircraft.components.in_use.find_each do |c|
his = c.hours_in_service + delta
hso = c.hours_since_overhaul + delta
his, hso = [ his, 0 ].max, [ hso, 0 ].max if clamp
c.update_columns(hours_in_service: his, hours_since_overhaul: hso, updated_at: Time.current)
end
end
end