mirror of
https://github.com/rickbarrette/simple-aircraft-manager-rails.git
synced 2026-11-17 23: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>
120 lines
4.0 KiB
Ruby
120 lines
4.0 KiB
Ruby
# 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
|